Skip to content

Commit

Permalink
Implement pluggable dietline history. Use it in Vo (radareorg#9591)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanpencil authored and radare committed Mar 8, 2018
1 parent bd42962 commit dd94194
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 33 deletions.
83 changes: 52 additions & 31 deletions libr/cons/dietline.c
Expand Up @@ -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;
Expand Down Expand Up @@ -307,42 +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_up_cb) {
r_line_set_hist_callback (&I, &cmd_history_up, &cmd_history_down);
}
if (!I.history.data) {
inithist ();
}
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;
}
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);
}
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) {
Expand Down
6 changes: 4 additions & 2 deletions libr/core/core.c
Expand Up @@ -1231,9 +1231,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;
Expand Down
4 changes: 4 additions & 0 deletions libr/core/graph.c
Expand Up @@ -3444,13 +3444,17 @@ 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) {
if (buf[2] == '.') {
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;
}
}

Expand Down
52 changes: 52 additions & 0 deletions libr/core/visual.c
@@ -1,6 +1,7 @@
/* radare - LGPL - Copyright 2009-2018 - pancake */

#include <r_core.h>
#include <r_cons.h>

static int obs = 0;
static int blocksize = 0;
Expand Down Expand Up @@ -842,8 +843,57 @@ static void reset_print_cur(RPrint *p) {
p->ocur = -1;
}

R_API 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--;
ut64 off = undo->seek[undo->idx + line->offset_index].off;
RFlagItem *f = r_flag_get_at (core->flags, off, false);
char *command;
if (f && f->offset == off && f->offset > 0) {
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;
}

R_API 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;
}
ut64 off = undo->seek[undo->idx + line->offset_index].off;
RFlagItem *f = r_flag_get_at (core->flags, off, false);
char *command;
if (f && f->offset == off && f->offset > 0) {
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;
}

static void visual_offset(RCore *core) {
char buf[256];
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) {
Expand All @@ -852,6 +902,8 @@ 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;
}
}

Expand Down
11 changes: 11 additions & 0 deletions libr/include/r_cons.h
Expand Up @@ -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;
Expand All @@ -796,6 +801,8 @@ struct r_line_t {
int (*hist_down)(void *user);
char *contents;
bool zerosep;
bool offset_prompt;
int offset_index;
}; /* RLine */

#ifdef R_API
Expand All @@ -820,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
Expand Down
3 changes: 3 additions & 0 deletions libr/include/r_core.h
Expand Up @@ -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 */
Expand Down

0 comments on commit dd94194

Please sign in to comment.