2019-06-17 00:23:58 +00:00
|
|
|
/* radare - LGPL - Copyright 2007-2019 - pancake */
|
2010-01-30 13:02:53 +00:00
|
|
|
|
2019-04-24 23:58:17 +00:00
|
|
|
#include <r_util.h>
|
2012-10-03 23:20:00 +00:00
|
|
|
#include <r_cons.h>
|
2010-01-30 13:02:53 +00:00
|
|
|
|
2022-01-21 16:03:37 +00:00
|
|
|
static R_TH_LOCAL RLine r_line_instance;
|
2010-01-30 13:02:53 +00:00
|
|
|
#define I r_line_instance
|
|
|
|
|
2019-06-17 00:23:58 +00:00
|
|
|
R_API RLine *r_line_singleton(void) {
|
2010-02-18 15:36:55 +00:00
|
|
|
return &r_line_instance;
|
|
|
|
}
|
|
|
|
|
2019-06-17 00:23:58 +00:00
|
|
|
R_API RLine *r_line_new(void) {
|
2013-08-28 01:06:10 +00:00
|
|
|
I.hist_up = NULL;
|
|
|
|
I.hist_down = NULL;
|
2010-01-30 13:02:53 +00:00
|
|
|
I.prompt = strdup ("> ");
|
2013-08-28 01:06:10 +00:00
|
|
|
I.contents = NULL;
|
2019-09-09 18:09:47 +00:00
|
|
|
I.enable_vi_mode = false;
|
2019-07-27 07:40:52 +00:00
|
|
|
I.clipboard = NULL;
|
2022-10-17 16:41:48 +00:00
|
|
|
I.kill_ring = r_list_newf (free);
|
2019-07-27 07:40:52 +00:00
|
|
|
I.kill_ring_ptr = -1;
|
2022-12-10 18:23:37 +00:00
|
|
|
#if R2__WINDOWS__
|
2020-05-03 09:31:52 +00:00
|
|
|
I.vtmode = r_cons_is_vtcompat ();
|
|
|
|
#else
|
|
|
|
I.vtmode = 2;
|
2018-08-12 18:27:05 +00:00
|
|
|
#endif
|
2019-04-24 23:58:17 +00:00
|
|
|
r_line_completion_init (&I.completion, 4096);
|
2010-01-30 13:02:53 +00:00
|
|
|
return &I;
|
|
|
|
}
|
|
|
|
|
2019-06-17 00:23:58 +00:00
|
|
|
R_API void r_line_free(void) {
|
2010-01-30 13:02:53 +00:00
|
|
|
// XXX: prompt out of the heap?
|
2015-12-08 11:55:29 +00:00
|
|
|
free ((void *)I.prompt);
|
2010-01-30 13:02:53 +00:00
|
|
|
I.prompt = NULL;
|
2021-01-02 08:37:23 +00:00
|
|
|
r_list_free (I.kill_ring);
|
2010-01-30 13:02:53 +00:00
|
|
|
r_line_hist_free ();
|
2019-04-24 23:58:17 +00:00
|
|
|
r_line_completion_fini (&I.completion);
|
2010-01-30 13:02:53 +00:00
|
|
|
}
|
2010-02-21 19:21:36 +00:00
|
|
|
|
2021-12-21 18:52:17 +00:00
|
|
|
R_API void r_line_clipboard_push(const char *str) {
|
2019-07-27 07:40:52 +00:00
|
|
|
I.kill_ring_ptr += 1;
|
|
|
|
r_list_insert (I.kill_ring, I.kill_ring_ptr, strdup (str));
|
|
|
|
}
|
|
|
|
|
2011-03-01 23:02:50 +00:00
|
|
|
// handle const or dynamic prompts?
|
2015-12-08 11:55:29 +00:00
|
|
|
R_API void r_line_set_prompt(const char *prompt) {
|
2011-11-01 03:37:13 +00:00
|
|
|
free (I.prompt);
|
|
|
|
I.prompt = strdup (prompt);
|
2020-02-23 10:55:54 +00:00
|
|
|
RCons *cons = r_cons_singleton ();
|
|
|
|
I.cb_fkey = cons->cb_fkey;
|
2011-11-01 03:37:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// handle const or dynamic prompts?
|
2019-06-17 00:23:58 +00:00
|
|
|
R_API char *r_line_get_prompt(void) {
|
2011-11-01 03:37:13 +00:00
|
|
|
return strdup (I.prompt);
|
2011-03-01 23:02:50 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 23:58:17 +00:00
|
|
|
R_API void r_line_completion_init(RLineCompletion *completion, size_t args_limit) {
|
|
|
|
completion->run = NULL;
|
2019-04-25 14:23:19 +00:00
|
|
|
completion->run_user = NULL;
|
2019-04-24 23:58:17 +00:00
|
|
|
completion->args_limit = args_limit;
|
|
|
|
r_pvector_init (&completion->args, free);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_line_completion_fini(RLineCompletion *completion) {
|
|
|
|
r_line_completion_clear (completion);
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_line_completion_push(RLineCompletion *completion, const char *str) {
|
|
|
|
r_return_if_fail (completion && str);
|
2019-08-11 06:15:28 +00:00
|
|
|
if (completion->quit) {
|
2021-10-28 17:59:10 +00:00
|
|
|
return;
|
2019-08-11 06:15:28 +00:00
|
|
|
}
|
2022-11-06 18:48:53 +00:00
|
|
|
if (r_pvector_length (&completion->args) < completion->args_limit) {
|
2019-06-17 00:23:58 +00:00
|
|
|
char *s = strdup (str);
|
|
|
|
if (s) {
|
|
|
|
r_pvector_push (&completion->args, (void *)s);
|
|
|
|
}
|
2019-08-11 06:15:28 +00:00
|
|
|
} else {
|
2021-10-28 17:59:10 +00:00
|
|
|
completion->quit = true;
|
2022-04-24 23:12:44 +00:00
|
|
|
R_LOG_WARN ("Maximum completion capacity reached, increase scr.maxtab");
|
2019-04-24 23:58:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_line_completion_set(RLineCompletion *completion, int argc, const char **argv) {
|
|
|
|
r_return_if_fail (completion && (argc >= 0));
|
|
|
|
r_line_completion_clear (completion);
|
2019-08-11 06:15:28 +00:00
|
|
|
if (argc > completion->args_limit) {
|
2022-12-09 19:07:22 +00:00
|
|
|
argc = completion->args_limit;
|
|
|
|
R_LOG_DEBUG ("Maximum completion capacity reached, increase scr.maxtab (%d %d)",
|
|
|
|
argc, completion->args_limit);
|
2019-08-11 06:15:28 +00:00
|
|
|
}
|
2019-04-24 23:58:17 +00:00
|
|
|
size_t count = R_MIN (argc, completion->args_limit);
|
|
|
|
r_pvector_reserve (&completion->args, count);
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < count; i++) {
|
|
|
|
r_line_completion_push (completion, argv[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
R_API void r_line_completion_clear(RLineCompletion *completion) {
|
|
|
|
r_return_if_fail (completion);
|
2019-08-11 06:15:28 +00:00
|
|
|
completion->quit = false;
|
2019-04-24 23:58:17 +00:00
|
|
|
r_pvector_clear (&completion->args);
|
|
|
|
}
|
|
|
|
|
2022-09-16 13:50:21 +00:00
|
|
|
#include "dietline.c"
|