From 23999edb0a9267bf917b561d5a35bff76d3a13b0 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Mon, 5 Mar 2018 17:00:20 +0100 Subject: [PATCH 01/12] Fix offset history --- libr/cons/dietline.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 337aea4795385..151b6958d5401 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -313,7 +313,14 @@ static int r_line_hist_up() { if (!I.history.data) { inithist (); } - if (I.history.index > 0) { + if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { + while (I.history.index > 0 && I.history.data[--I.history.index][0] != 's' ); + int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; + strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); + I.buffer.index = I.buffer.length = strlen (I.buffer.data); + return true; + } + else if (I.history.index > 0) { strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; @@ -338,6 +345,14 @@ static int r_line_hist_down() { I.buffer.index = I.buffer.length = 0; return false; } + if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { + while (I.history.index != I.history.top && I.history.data[I.history.index++][0] != 's' ); + I.history.index--; + int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; + strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); + I.buffer.index = I.buffer.length = strlen (I.buffer.data); + return true; + } if (I.history.data[I.history.index]) { strncpy (I.buffer.data, I.history.data[I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); From 349da7e08ea43962237296aced931dc049ff0d6d Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Mon, 5 Mar 2018 17:05:25 +0100 Subject: [PATCH 02/12] Refactor --- libr/cons/dietline.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 151b6958d5401..3b062a413285c 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -320,7 +320,7 @@ static int r_line_hist_up() { I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; } - else if (I.history.index > 0) { + if (I.history.index > 0) { strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; From cc68dc4e3ed977d7d6a794d2e1d360b40e718e17 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Mon, 5 Mar 2018 17:07:57 +0100 Subject: [PATCH 03/12] Refactor tabs --- libr/cons/dietline.c | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 3b062a413285c..c6f3ea849f0fa 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -313,14 +313,14 @@ static int r_line_hist_up() { if (!I.history.data) { inithist (); } - if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { - while (I.history.index > 0 && I.history.data[--I.history.index][0] != 's' ); - int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; - strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; - } - if (I.history.index > 0) { + if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { + while (I.history.index > 0 && I.history.data[--I.history.index][0] != 's' ); + int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; + strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); + I.buffer.index = I.buffer.length = strlen (I.buffer.data); + return true; + } + if (I.history.index > 0) { strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; @@ -345,14 +345,14 @@ static int r_line_hist_down() { I.buffer.index = I.buffer.length = 0; return false; } - if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { - while (I.history.index != I.history.top && I.history.data[I.history.index++][0] != 's' ); - I.history.index--; - int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; - strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; - } + if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { + while (I.history.index != I.history.top && I.history.data[I.history.index++][0] != 's' ); + I.history.index--; + int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; + strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); + I.buffer.index = I.buffer.length = strlen (I.buffer.data); + return true; + } if (I.history.data[I.history.index]) { strncpy (I.buffer.data, I.history.data[I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); From 32f711cbeb2cb95ce084fc4c8ce6dda78f12b60e Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Mon, 5 Mar 2018 18:17:22 +0100 Subject: [PATCH 04/12] Refactor strcmp --- libr/cons/dietline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index c6f3ea849f0fa..f7e6f89d45f9f 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -313,7 +313,7 @@ static int r_line_hist_up() { if (!I.history.data) { inithist (); } - if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { + if (!strcmp (r_line_get_prompt(), "[offset]> ")) { while (I.history.index > 0 && I.history.data[--I.history.index][0] != 's' ); int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); @@ -345,7 +345,7 @@ static int r_line_hist_down() { I.buffer.index = I.buffer.length = 0; return false; } - if (strcmp(r_line_get_prompt(), "[offset]> ") == 0) { + if (!strcmp (r_line_get_prompt(), "[offset]> ")) { while (I.history.index != I.history.top && I.history.data[I.history.index++][0] != 's' ); I.history.index--; int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; From 511f83439d15a3d10b7c6ae056939a4d756837b9 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Wed, 7 Mar 2018 15:35:26 +0100 Subject: [PATCH 05/12] boolean flag offset_prompt --- libr/cons/dietline.c | 4 ++-- libr/core/visual.c | 2 ++ libr/include/r_cons.h | 1 + 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index f7e6f89d45f9f..0408ffca12259 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -313,7 +313,7 @@ static int r_line_hist_up() { if (!I.history.data) { inithist (); } - if (!strcmp (r_line_get_prompt(), "[offset]> ")) { + if (I.offset_prompt) { while (I.history.index > 0 && I.history.data[--I.history.index][0] != 's' ); int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); @@ -345,7 +345,7 @@ static int r_line_hist_down() { I.buffer.index = I.buffer.length = 0; return false; } - if (!strcmp (r_line_get_prompt(), "[offset]> ")) { + if (I.offset_prompt) { while (I.history.index != I.history.top && I.history.data[I.history.index++][0] != 's' ); I.history.index--; int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; diff --git a/libr/core/visual.c b/libr/core/visual.c index 4e82e354d58f3..af06e77819ce7 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -845,6 +845,7 @@ static void reset_print_cur(RPrint *p) { static void visual_offset(RCore *core) { char buf[256]; r_line_set_prompt ("[offset]> "); + core->cons->line->offset_prompt = true; strcpy (buf, "s "); if (r_cons_fgets (buf + 2, sizeof (buf) - 3, 0, NULL) > 0) { if (buf[2] == '.') { @@ -852,6 +853,7 @@ static void visual_offset(RCore *core) { } r_core_cmd0 (core, buf); reset_print_cur (core->print); + core->cons->line->offset_prompt = false; } } diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index d4f849ed007d7..28b5447bf5abc 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -796,6 +796,7 @@ struct r_line_t { int (*hist_down)(void *user); char *contents; bool zerosep; + bool offset_prompt; }; /* RLine */ #ifdef R_API From 0efed08bb0fd6bd901ba9934259799a9b122bbef Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 01:14:06 +0100 Subject: [PATCH 06/12] offset autocomplete + offset history --- libr/cons/dietline.c | 39 ++++++++++++++++++++++++++++----------- libr/core/core.c | 6 ++++-- libr/core/visual.c | 1 + libr/include/r_cons.h | 1 + 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 0408ffca12259..325f316916ec5 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -16,6 +16,7 @@ #define USE_UTF8 1 #endif + static char *r_line_nullstr = ""; static const char dl_basic_word_break_characters[] = " \t\n\"\\'`@$><=;|&{("; @@ -314,9 +315,15 @@ static int r_line_hist_up() { inithist (); } if (I.offset_prompt) { - while (I.history.index > 0 && I.history.data[--I.history.index][0] != 's' ); - int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; - strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); + RCore *core = I.user; + RIOUndo *undo = &core->io->undo; + char line[R_LINE_BUFSIZE - 1]; + if (I.offset_index <= -undo->undos) { + return false; + } + I.offset_index--; + sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index]); + strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; } @@ -332,6 +339,24 @@ static int r_line_hist_down() { if (I.hist_down) { return I.hist_down (I.user); } + if (I.offset_prompt) { + RCore *core = I.user; + RIOUndo *undo = &core->io->undo; + if (I.offset_index >= undo->redos) { + return false; + } + I.offset_index++; + if (I.offset_index == undo->redos) { + I.buffer.data[0] = '\0'; + I.buffer.index = I.buffer.length = 0; + return false; + } + char line[R_LINE_BUFSIZE - 1]; + sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index]); + strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); + I.buffer.index = I.buffer.length = strlen (I.buffer.data); + return true; + } I.buffer.index = 0; if (!I.history.data) { inithist (); @@ -345,14 +370,6 @@ static int r_line_hist_down() { I.buffer.index = I.buffer.length = 0; return false; } - if (I.offset_prompt) { - while (I.history.index != I.history.top && I.history.data[I.history.index++][0] != 's' ); - I.history.index--; - int skip_whitespace = I.history.data[I.history.index][1] == ' ' ? 2 : 1; - strncpy (I.buffer.data, I.history.data[I.history.index] + skip_whitespace, R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; - } if (I.history.data[I.history.index]) { strncpy (I.buffer.data, I.history.data[I.history.index], R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); diff --git a/libr/core/core.c b/libr/core/core.c index e0e81980ec30c..7a9dccecfeaa9 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -1230,9 +1230,11 @@ static int autocomplete(RLine *line) { || !strncmp (line->buffer.data, "agfl ", 5) || !strncmp (line->buffer.data, "aecu ", 5) || !strncmp (line->buffer.data, "aesu ", 5) - || !strncmp (line->buffer.data, "aeim ", 5)) { + || !strncmp (line->buffer.data, "aeim ", 5) + || line->offset_prompt) { int n, i = 0; - int sdelta = (line->buffer.data[1] == ' ') + int sdelta = line->offset_prompt + ? 0 : (line->buffer.data[1] == ' ') ? 2 : (line->buffer.data[2] == ' ') ? 3 : (line->buffer.data[3] == ' ') ? 4 : 5; diff --git a/libr/core/visual.c b/libr/core/visual.c index af06e77819ce7..94535e5c870f0 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -846,6 +846,7 @@ static void visual_offset(RCore *core) { char buf[256]; r_line_set_prompt ("[offset]> "); core->cons->line->offset_prompt = true; + core->cons->line->offset_index = 0; strcpy (buf, "s "); if (r_cons_fgets (buf + 2, sizeof (buf) - 3, 0, NULL) > 0) { if (buf[2] == '.') { diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 28b5447bf5abc..3a05a75eb892c 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -797,6 +797,7 @@ struct r_line_t { char *contents; bool zerosep; bool offset_prompt; + int offset_index; }; /* RLine */ #ifdef R_API From 738008695df792e37798dcd05c7d2fbb551c7ff7 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 01:19:03 +0100 Subject: [PATCH 07/12] whitespace fix --- libr/cons/dietline.c | 1 - 1 file changed, 1 deletion(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index 325f316916ec5..ac6b984ee78b6 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -16,7 +16,6 @@ #define USE_UTF8 1 #endif - static char *r_line_nullstr = ""; static const char dl_basic_word_break_characters[] = " \t\n\"\\'`@$><=;|&{("; From a6757c3dc509bb05d7e76282af52975832f3875f Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 09:19:15 +0100 Subject: [PATCH 08/12] fix warning --- libr/cons/dietline.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index ac6b984ee78b6..d183a18e672b5 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -321,7 +321,7 @@ static int r_line_hist_up() { return false; } I.offset_index--; - sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index]); + sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; @@ -351,7 +351,7 @@ static int r_line_hist_down() { return false; } char line[R_LINE_BUFSIZE - 1]; - sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index]); + sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; From 11c684027a418a5c6e05ac9f2f6c197840d30544 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 11:33:24 +0100 Subject: [PATCH 09/12] fix tabs + sprintf to r_str_newf --- libr/cons/dietline.c | 55 ++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 28 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index d183a18e672b5..ad34914801bde 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -313,16 +313,16 @@ static int r_line_hist_up() { if (!I.history.data) { inithist (); } - if (I.offset_prompt) { - RCore *core = I.user; - RIOUndo *undo = &core->io->undo; - char line[R_LINE_BUFSIZE - 1]; - if (I.offset_index <= -undo->undos) { - return false; - } - I.offset_index--; - sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); - strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); + if (I.offset_prompt) { + RCore *core = I.user; + RIOUndo *undo = &core->io->undo; + /*char line[R_LINE_BUFSIZE - 1];*/ + if (I.offset_index <= -undo->undos) { + return false; + } + I.offset_index--; + char *line = r_str_newf("0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); + strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); I.buffer.index = I.buffer.length = strlen (I.buffer.data); return true; } @@ -338,24 +338,23 @@ static int r_line_hist_down() { if (I.hist_down) { return I.hist_down (I.user); } - if (I.offset_prompt) { - RCore *core = I.user; - RIOUndo *undo = &core->io->undo; - if (I.offset_index >= undo->redos) { - return false; - } - I.offset_index++; - if (I.offset_index == undo->redos) { - I.buffer.data[0] = '\0'; - I.buffer.index = I.buffer.length = 0; - return false; - } - char line[R_LINE_BUFSIZE - 1]; - sprintf(line, "0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); - strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; - } + if (I.offset_prompt) { + RCore *core = I.user; + RIOUndo *undo = &core->io->undo; + if (I.offset_index >= undo->redos) { + return false; + } + I.offset_index++; + if (I.offset_index == undo->redos) { + I.buffer.data[0] = '\0'; + I.buffer.index = I.buffer.length = 0; + return false; + } + char *line = r_str_newf("0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); + strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); + I.buffer.index = I.buffer.length = strlen (I.buffer.data); + return true; + } I.buffer.index = 0; if (!I.history.data) { inithist (); From 3e98cda4ef2e634c049003b629d660e9c6002285 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 22:41:33 +0100 Subject: [PATCH 10/12] added callbacks --- libr/cons/dietline.c | 113 +++++++++++++++++++----------------------- libr/core/visual.c | 35 ++++++++++++- libr/include/r_cons.h | 9 ++++ 3 files changed, 95 insertions(+), 62 deletions(-) diff --git a/libr/cons/dietline.c b/libr/cons/dietline.c index ad34914801bde..c1e2c7e05140c 100644 --- a/libr/cons/dietline.c +++ b/libr/cons/dietline.c @@ -278,6 +278,52 @@ static int r_line_readchar() { } #endif +R_API int r_line_set_hist_callback(RLine *line, RLineHistoryUpCb up_cb, RLineHistoryDownCb down_cb) { + line->history_up_cb = up_cb; + line->history_down_cb = down_cb; + line->offset_index = 0; + return 1; +} + +R_API int cmd_history_up(RLine *line) { + if (line->hist_up) { + return line->hist_up (line->user); + } + if (!line->history.data) { + inithist (); + } + if (line->history.index > 0) { + strncpy (line->buffer.data, line->history.data[--line->history.index], R_LINE_BUFSIZE - 1); + line->buffer.index = line->buffer.length = strlen (line->buffer.data); + return true; + } + return false; +} + +R_API int cmd_history_down(RLine *line) { + if (line->hist_down) { + return line->hist_down (line->user); + } + line->buffer.index = 0; + if (!line->history.data) { + inithist (); + } + if (line->history.index == line->history.top) { + return false; + } + line->history.index++; + if (line->history.index == line->history.top) { + line->buffer.data[0] = '\0'; + line->buffer.index = line->buffer.length = 0; + return false; + } + if (line->history.data[line->history.index]) { + strncpy (line->buffer.data, line->history.data[line->history.index], R_LINE_BUFSIZE - 1); + line->buffer.index = line->buffer.length = strlen (line->buffer.data); + } + return true; +} + R_API int r_line_hist_add(const char *line) { if (!line || !*line) { return false; @@ -307,72 +353,17 @@ R_API int r_line_hist_add(const char *line) { } static int r_line_hist_up() { - if (I.hist_up) { - return I.hist_up (I.user); - } - if (!I.history.data) { - inithist (); - } - if (I.offset_prompt) { - RCore *core = I.user; - RIOUndo *undo = &core->io->undo; - /*char line[R_LINE_BUFSIZE - 1];*/ - if (I.offset_index <= -undo->undos) { - return false; - } - I.offset_index--; - char *line = r_str_newf("0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); - strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; - } - if (I.history.index > 0) { - strncpy (I.buffer.data, I.history.data[--I.history.index], R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; + if (!I.history_up_cb) { + r_line_set_hist_callback (&I, &cmd_history_up, &cmd_history_down); } - return false; + return I.history_up_cb (&I); } static int r_line_hist_down() { - if (I.hist_down) { - return I.hist_down (I.user); + if (!I.history_down_cb) { + r_line_set_hist_callback (&I, &cmd_history_up, &cmd_history_down); } - if (I.offset_prompt) { - RCore *core = I.user; - RIOUndo *undo = &core->io->undo; - if (I.offset_index >= undo->redos) { - return false; - } - I.offset_index++; - if (I.offset_index == undo->redos) { - I.buffer.data[0] = '\0'; - I.buffer.index = I.buffer.length = 0; - return false; - } - char *line = r_str_newf("0x%"PFMT64x, undo->seek[undo->idx + I.offset_index].off); - strncpy(I.buffer.data, line, R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - return true; - } - I.buffer.index = 0; - if (!I.history.data) { - inithist (); - } - if (I.history.index == I.history.top) { - return false; - } - I.history.index++; - if (I.history.index == I.history.top) { - I.buffer.data[0] = '\0'; - I.buffer.index = I.buffer.length = 0; - return false; - } - if (I.history.data[I.history.index]) { - strncpy (I.buffer.data, I.history.data[I.history.index], R_LINE_BUFSIZE - 1); - I.buffer.index = I.buffer.length = strlen (I.buffer.data); - } - return true; + return I.history_down_cb (&I); } R_API const char *r_line_hist_get(int n) { diff --git a/libr/core/visual.c b/libr/core/visual.c index 94535e5c870f0..fe743e1b5167a 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -1,6 +1,7 @@ /* radare - LGPL - Copyright 2009-2018 - pancake */ #include +#include static int obs = 0; static int blocksize = 0; @@ -842,11 +843,42 @@ static void reset_print_cur(RPrint *p) { p->ocur = -1; } +static int offset_history_up(RLine *line) { + RCore *core = line->user; + RIOUndo *undo = &core->io->undo; + if (line->offset_index <= -undo->undos) { + return false; + } + line->offset_index--; + char *command = r_str_newf ("0x%"PFMT64x, undo->seek[undo->idx + line->offset_index].off); + strncpy (line->buffer.data, command, R_LINE_BUFSIZE - 1); + line->buffer.index = line->buffer.length = strlen (line->buffer.data); + return true; +} + +static int offset_history_down(RLine *line) { + RCore *core = line->user; + RIOUndo *undo = &core->io->undo; + if (line->offset_index >= undo->redos) { + return false; + } + line->offset_index++; + if (line->offset_index == undo->redos) { + line->buffer.data[0] = '\0'; + line->buffer.index = line->buffer.length = 0; + return false; + } + char *command = r_str_newf ("0x%"PFMT64x, undo->seek[undo->idx + line->offset_index].off); + strncpy (line->buffer.data, command, R_LINE_BUFSIZE - 1); + line->buffer.index = line->buffer.length = strlen (line->buffer.data); + return true; +} + static void visual_offset(RCore *core) { char buf[256]; r_line_set_prompt ("[offset]> "); core->cons->line->offset_prompt = true; - core->cons->line->offset_index = 0; + r_line_set_hist_callback (core->cons->line, &offset_history_up, &offset_history_down); strcpy (buf, "s "); if (r_cons_fgets (buf + 2, sizeof (buf) - 3, 0, NULL) > 0) { if (buf[2] == '.') { @@ -854,6 +886,7 @@ static void visual_offset(RCore *core) { } r_core_cmd0 (core, buf); reset_print_cur (core->print); + r_line_set_hist_callback (core->cons->line, &cmd_history_up, &cmd_history_down); core->cons->line->offset_prompt = false; } } diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 3a05a75eb892c..f33ab63b692b3 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -781,9 +781,14 @@ typedef struct r_line_comp_t { typedef char* (*RLineEditorCb)(void *core, const char *str); +typedef int (*RLineHistoryUpCb)(RLine* line); +typedef int (*RLineHistoryDownCb)(RLine* line); + struct r_line_t { RLineCompletion completion; RLineHistory history; + RLineHistoryUpCb history_up_cb; + RLineHistoryDownCb history_down_cb; RLineBuffer buffer; RLineEditorCb editor_cb; int echo; @@ -822,6 +827,10 @@ R_API void r_line_label_show(void); R_API int r_line_hist_list(void); 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); +R_API int cmd_history_up(RLine *line); +R_API int cmd_history_down(RLine *line); + #define R_CONS_INVERT(x,y) (y? (x?Color_INVERT: Color_INVERT_RESET): (x?"[":"]")) #endif From b0c593c282f54ffdef240bc71fce1e1be3749858 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 22:50:44 +0100 Subject: [PATCH 11/12] flag names instead of numeric offsets --- libr/core/visual.c | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/libr/core/visual.c b/libr/core/visual.c index fe743e1b5167a..0dafd1dc4c6b6 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -850,7 +850,17 @@ static int offset_history_up(RLine *line) { return false; } line->offset_index--; - char *command = r_str_newf ("0x%"PFMT64x, undo->seek[undo->idx + line->offset_index].off); + ut64 off = undo->seek[undo->idx + line->offset_index].off; + core->flags->space_strict = true; + RFlagItem *f = r_flag_get_at (core->flags, off, true); + core->flags->space_strict = false; + char *command; + if (f && f->offset == off) { + command = r_str_newf ("%s", f->name); + } + else { + command = r_str_newf ("0x%"PFMT64x, off); + } strncpy (line->buffer.data, command, R_LINE_BUFSIZE - 1); line->buffer.index = line->buffer.length = strlen (line->buffer.data); return true; @@ -868,7 +878,17 @@ static int offset_history_down(RLine *line) { line->buffer.index = line->buffer.length = 0; return false; } - char *command = r_str_newf ("0x%"PFMT64x, undo->seek[undo->idx + line->offset_index].off); + ut64 off = undo->seek[undo->idx + line->offset_index].off; + core->flags->space_strict = true; + RFlagItem *f = r_flag_get_at (core->flags, off, true); + core->flags->space_strict = false; + char *command; + if (f && f->offset == off) { + command = r_str_newf ("%s", f->name); + } + else { + command = r_str_newf ("0x%"PFMT64x, off); + } strncpy (line->buffer.data, command, R_LINE_BUFSIZE - 1); line->buffer.index = line->buffer.length = strlen (line->buffer.data); return true; From 03b12d9ceafc7b20b0011de42ec49ffc38d1c7f2 Mon Sep 17 00:00:00 2001 From: cyanpencil Date: Thu, 8 Mar 2018 23:25:17 +0100 Subject: [PATCH 12/12] small fix + offset history in graph view --- libr/core/graph.c | 4 ++++ libr/core/visual.c | 18 +++++++----------- libr/include/r_core.h | 3 +++ 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/libr/core/graph.c b/libr/core/graph.c index edb2db91694f1..0465720cf3ec3 100644 --- a/libr/core/graph.c +++ b/libr/core/graph.c @@ -3429,6 +3429,8 @@ static void visual_offset(RAGraph *g, RCore *core) { r_cons_get_size (&rows); r_cons_gotoxy (0, rows); r_cons_flush (); + core->cons->line->offset_prompt = true; + r_line_set_hist_callback (core->cons->line, &offset_history_up, &offset_history_down); r_line_set_prompt ("[offset]> "); strcpy (buf, "s "); if (r_cons_fgets (buf + 2, sizeof (buf) - 3, 0, NULL) > 0) { @@ -3436,6 +3438,8 @@ static void visual_offset(RAGraph *g, RCore *core) { buf[1] = '.'; } r_core_cmd0 (core, buf); + r_line_set_hist_callback (core->cons->line, &cmd_history_up, &cmd_history_down); + core->cons->line->offset_prompt = false; } } diff --git a/libr/core/visual.c b/libr/core/visual.c index 0dafd1dc4c6b6..1784ba05260db 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -843,7 +843,7 @@ static void reset_print_cur(RPrint *p) { p->ocur = -1; } -static int offset_history_up(RLine *line) { +R_API int offset_history_up(RLine *line) { RCore *core = line->user; RIOUndo *undo = &core->io->undo; if (line->offset_index <= -undo->undos) { @@ -851,11 +851,9 @@ static int offset_history_up(RLine *line) { } line->offset_index--; ut64 off = undo->seek[undo->idx + line->offset_index].off; - core->flags->space_strict = true; - RFlagItem *f = r_flag_get_at (core->flags, off, true); - core->flags->space_strict = false; + RFlagItem *f = r_flag_get_at (core->flags, off, false); char *command; - if (f && f->offset == off) { + if (f && f->offset == off && f->offset > 0) { command = r_str_newf ("%s", f->name); } else { @@ -866,7 +864,7 @@ static int offset_history_up(RLine *line) { return true; } -static int offset_history_down(RLine *line) { +R_API int offset_history_down(RLine *line) { RCore *core = line->user; RIOUndo *undo = &core->io->undo; if (line->offset_index >= undo->redos) { @@ -879,11 +877,9 @@ static int offset_history_down(RLine *line) { return false; } ut64 off = undo->seek[undo->idx + line->offset_index].off; - core->flags->space_strict = true; - RFlagItem *f = r_flag_get_at (core->flags, off, true); - core->flags->space_strict = false; + RFlagItem *f = r_flag_get_at (core->flags, off, false); char *command; - if (f && f->offset == off) { + if (f && f->offset == off && f->offset > 0) { command = r_str_newf ("%s", f->name); } else { @@ -896,9 +892,9 @@ static int offset_history_down(RLine *line) { static void visual_offset(RCore *core) { char buf[256]; - r_line_set_prompt ("[offset]> "); core->cons->line->offset_prompt = true; r_line_set_hist_callback (core->cons->line, &offset_history_up, &offset_history_down); + r_line_set_prompt ("[offset]> "); strcpy (buf, "s "); if (r_cons_fgets (buf + 2, sizeof (buf) - 3, 0, NULL) > 0) { if (buf[2] == '.') { diff --git a/libr/include/r_core.h b/libr/include/r_core.h index ec9913921fe29..0fc639abd26a5 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -612,6 +612,9 @@ R_API void r_core_syscmd_ls(const char *input); R_API void r_core_syscmd_cat(const char *file); R_API void r_core_syscmd_mkdir(const char *dir); +R_API int offset_history_up(RLine *line); +R_API int offset_history_down(RLine *line); + // TODO : move into debug or syscall++ R_API char *cmd_syscall_dostr(RCore *core, int num); /* tasks */