From 6967f63396d8f41753b2279e6c57ca2f4e7f9f3d Mon Sep 17 00:00:00 2001 From: pancake Date: Fri, 16 Sep 2022 15:50:21 +0200 Subject: [PATCH] Allow changing number of saved input lines ##shell Authored-by: Paul B Mahol --- libr/anal/p/anal_xtensa.c | 2 +- libr/cons/dietline.c | 46 +++++++++++++++++++++++++++++---------- libr/cons/line.c | 5 +---- libr/core/cconfig.c | 7 ++++++ libr/core/core.c | 5 ----- libr/include/r_cons.h | 5 ++++- libr/main/radare2.c | 6 +++++ libr/util/randomart.c | 8 +++---- sys/lint.sh | 1 + 9 files changed, 59 insertions(+), 26 deletions(-) diff --git a/libr/anal/p/anal_xtensa.c b/libr/anal/p/anal_xtensa.c index 4b13a6e95b..41cb1613ac 100644 --- a/libr/anal/p/anal_xtensa.c +++ b/libr/anal/p/anal_xtensa.c @@ -1989,7 +1989,7 @@ static int xtensa_op(RAnal *anal, RAnalOp *op, ut64 addr, const ut8 *buf_origina xtensa_op0_fns[(buf_original[0] & 0xf)] (anal, op, addr, buf_original); ut8 buffer[XTENSA_MAX_LENGTH] = {0}; - int len = R_MIN(op->size, XTENSA_MAX_LENGTH); + int len = R_MIN (op->size, XTENSA_MAX_LENGTH); memcpy (buffer, buf_original, len); unsigned int i; diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 862032a522..b32dcfae06 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -186,15 +186,35 @@ static void unix_word_rubout(void) { } static int inithist(void) { + if (I.history.data) { + int new_size = I.hist_size; + if (new_size > 0 && I.history.size != new_size) { + char **new_data = (char **) calloc (new_size, sizeof (char *)); + if (new_data) { + int nb_copy_lines = R_MIN (I.history.top + 1, new_size); + memcpy (new_data, I.history.data + (I.history.top + 1 - nb_copy_lines), sizeof (char *) * nb_copy_lines); + int i; + for (i = 0; i < I.history.top + 1 - nb_copy_lines; i++) { + free (I.history.data[i]); + } + free (I.history.data); + I.history.data = new_data; + I.history.size = new_size; + I.history.top = R_MIN (I.history.top, nb_copy_lines - 1); + I.history.index = R_MIN (I.history.index, nb_copy_lines - 1); + } + } + return true; + } ZERO_FILL (I.history); - if ((I.history.size + 1024) * sizeof (char *) < I.history.size) { + I.history.size = I.hist_size; + if (I.history.size <= 0) { return false; } - I.history.data = (char **) calloc ((I.history.size + 1024), sizeof (char *)); + I.history.data = (char **) calloc (I.history.size, sizeof (char *)); if (!I.history.data) { return false; } - I.history.size = R_LINE_HISTSIZE; return true; } @@ -362,8 +382,8 @@ R_API int r_line_hist_cmd_up(RLine *line) { if (line->hist_up) { return line->hist_up (line->user); } - if (!line->history.data) { - inithist (); + if (!inithist ()) { + return false; } if (line->history.index > 0 && line->history.data) { setup_hist_match (line); @@ -466,11 +486,17 @@ static int r_line_hist_down(void) { return I.cb_history_down (&I); } +R_API void r_line_hist_set_size(int size) { + I.hist_size = R_MIN (size, 65536); +} + +R_API int r_line_hist_get_size(void) { + return I.history.size; +} + R_API const char *r_line_hist_get(int n) { int i = 0; - if (!I.history.data) { - inithist (); - } + inithist (); n--; if (I.history.data) { for (i = 0; i < I.history.size && I.history.data[i]; i++) { @@ -484,9 +510,7 @@ R_API const char *r_line_hist_get(int n) { R_API int r_line_hist_list(void) { int i = 0; - if (!I.history.data) { - inithist (); - } + inithist (); if (I.history.data) { for (i = 0; i < I.history.size && I.history.data[i]; i++) { const char *pad = r_str_pad (' ', 32 - strlen (I.history.data[i])); diff --git a/libr/cons/line.c b/libr/cons/line.c index fbf5a20cd0..b4b7e675f8 100644 --- a/libr/cons/line.c +++ b/libr/cons/line.c @@ -24,9 +24,6 @@ R_API RLine *r_line_new(void) { #else I.vtmode = 2; #endif - if (!r_line_dietline_init ()) { - R_LOG_ERROR ("r_line_dietline_init has failed"); - } r_line_completion_init (&I.completion, 4096); return &I; } @@ -105,4 +102,4 @@ R_API void r_line_completion_clear(RLineCompletion *completion) { r_pvector_clear (&completion->args); } -#include "dietline.c" \ No newline at end of file +#include "dietline.c" diff --git a/libr/core/cconfig.c b/libr/core/cconfig.c index 72dc77436a..77a88d234e 100644 --- a/libr/core/cconfig.c +++ b/libr/core/cconfig.c @@ -2610,6 +2610,12 @@ static bool cb_scr_histblock(void *user, void *data) { return true; } +static bool cb_scr_histsize(void *user, void *data) { + RConfigNode *node = (RConfigNode *) data; + r_line_hist_set_size(node->i_value); + return true; +} + static bool cb_scrprompt(void *user, void *data) { RCore *core = (RCore *)user; RConfigNode *node = (RConfigNode *) data; @@ -4157,6 +4163,7 @@ R_API int r_core_config_init(RCore *core) { SETCB ("scr.hist.block", "true", &cb_scr_histblock, "use blocks for histogram"); SETCB ("scr.hist.filter", "true", &cb_scr_histfilter, "filter history for matching lines when using up/down keys"); SETBPREF ("scr.hist.save", "true", "always save history on exit"); + SETICB("scr.hist.size", R_LINE_HISTSIZE, &cb_scr_histsize, "set input lines history size"); n = NODECB ("scr.strconv", "asciiesc", &cb_scrstrconv); SETDESC (n, "convert string before display"); SETOPTIONS (n, "asciiesc", "asciidot", NULL); diff --git a/libr/core/core.c b/libr/core/core.c index e26b8ed5d3..2ce0e05144 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -3072,11 +3072,6 @@ R_API bool r_core_init(RCore *core) { core->cons->user_fgets = (void *)r_core_fgets; #endif //r_line_singleton ()->user = (void *)core; - char *histpath = r_str_home (".cache/radare2/history"); - if (histpath) { - r_line_hist_load (histpath); - free (histpath); - } } core->print->cons = core->cons; r_cons_bind (&core->print->consbind); diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 12730f1c6b..2a62a0c8b4 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -1097,7 +1097,8 @@ struct r_line_t { RLineHud *hud; RList *sdbshell_hist; RListIter *sdbshell_hist_iter; - int vtmode; + int vtmode; // R2_580 duplicated and unused from the global RCons.vtmode + int hist_size; }; /* RLine */ #ifdef R_API @@ -1121,6 +1122,8 @@ R_API bool r_line_hist_save(const char *file); R_API int r_line_hist_label(const char *label, void(*cb)(const char*)); R_API void r_line_label_show(void); R_API int r_line_hist_list(void); +R_API int r_line_hist_get_size(void); +R_API void r_line_hist_set_size(int size); R_API const char *r_line_hist_get(int n); R_API int r_line_set_hist_callback(RLine *line, RLineHistoryUpCb cb_up, RLineHistoryDownCb cb_down); diff --git a/libr/main/radare2.c b/libr/main/radare2.c index 0e39ea888d..f25d6c71fd 100644 --- a/libr/main/radare2.c +++ b/libr/main/radare2.c @@ -998,6 +998,12 @@ R_API int r_main_radare2(int argc, const char **argv) { r_config_set_b (r->config, "scr.utf8", false); } + char *histpath = r_str_home (".cache/radare2/history"); + if (histpath) { + r_line_hist_load (histpath); + free (histpath); + } + if (r_config_get_b (r->config, "zign.autoload")) { autoload_zigns (r); } diff --git a/libr/util/randomart.c b/libr/util/randomart.c index c7a44ae86f..be830787c6 100644 --- a/libr/util/randomart.c +++ b/libr/util/randomart.c @@ -72,10 +72,10 @@ R_API char *r_print_randomart(const ut8 *dgst_raw, ut32 dgst_raw_len, ut64 addr) y += (input & 0x2) ? 1 : -1; /* assure we are still in bounds */ - x = R_MAX(x, 0); - y = R_MAX(y, 0); - x = R_MIN(x, FLDSIZE_X - 1); - y = R_MIN(y, FLDSIZE_Y - 1); + x = R_MAX (x, 0); + y = R_MAX (y, 0); + x = R_MIN (x, FLDSIZE_X - 1); + y = R_MIN (y, FLDSIZE_Y - 1); /* augment the field */ if (field[x][y] < len - 2) { diff --git a/sys/lint.sh b/sys/lint.sh index 7332f5c3d7..c0a934213f 100755 --- a/sys/lint.sh +++ b/sys/lint.sh @@ -7,6 +7,7 @@ cd "$(dirname $0)"/.. # find calls without ( #(git grep -n -e '[a-z](' | grep -v static | grep -v _API | grep -v shlr | grep libr/core) && exit 1 # validated and ready to go lintings +(git grep -e 'R_MIN(' -e 'R_MAX(' libr | grep c:) && exit 1 (git grep -n 'cmp(' libr | grep -v R_API | grep -v static | grep c:) && exit 1 # (git grep -n 'len(' libr | grep -v R_API | grep -v static | grep c:) && exit 1 # (git grep -n ',"' libr | grep -v R_API | grep -v static | grep c:) && exit 1