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
|
|
|
|
2010-02-21 19:21:36 +00:00
|
|
|
static 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;
|
|
|
|
I.kill_ring = r_list_newf (NULL);
|
|
|
|
I.kill_ring_ptr = -1;
|
2019-03-15 04:55:08 +00:00
|
|
|
#if __WINDOWS__
|
2019-06-18 16:28:10 +00:00
|
|
|
I.ansicon = r_cons_is_ansicon ();
|
2018-08-12 18:27:05 +00:00
|
|
|
#endif
|
2017-04-08 22:29:44 +00:00
|
|
|
if (!r_line_dietline_init ()) {
|
2010-01-30 13:02:53 +00:00
|
|
|
eprintf ("error: r_line_dietline_init\n");
|
2017-04-08 22:29:44 +00:00
|
|
|
}
|
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;
|
|
|
|
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
|
|
|
|
2019-07-27 07:40:52 +00:00
|
|
|
R_API void r_line_clipboard_push (const char *str) {
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-17 00:23:58 +00:00
|
|
|
if (r_pvector_len (&completion->args) < completion->args_limit) {
|
|
|
|
char *s = strdup (str);
|
|
|
|
if (s) {
|
|
|
|
r_pvector_push (&completion->args, (void *)s);
|
|
|
|
}
|
2019-08-11 06:15:28 +00:00
|
|
|
} else {
|
|
|
|
completion->quit = true;
|
2019-08-12 15:54:34 +00:00
|
|
|
eprintf ("WARNING: 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) {
|
2019-08-12 15:54:34 +00:00
|
|
|
eprintf ("WARNING: Maximum completion capacity reached, increase scr.maxtab");
|
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);
|
|
|
|
}
|
|
|
|
|
2010-02-21 19:21:36 +00:00
|
|
|
#include "dietline.c"
|