Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Offset history #9591

Merged
merged 12 commits into from Mar 8, 2018
31 changes: 31 additions & 0 deletions libr/cons/dietline.c
Expand Up @@ -313,6 +313,19 @@ static int r_line_hist_up() {
if (!I.history.data) {
inithist ();
}
if (I.offset_prompt) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use tabs

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ideally the solution should be to delegate the logic of the history to the caller, not embed that in here

RCore *core = I.user;
RIOUndo *undo = &core->io->undo;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rcons shouldnt have access to core or io

char line[R_LINE_BUFSIZE - 1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont do 4096 in the stack. you only need 64bytes to do an snprintf of pfmt64x

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;
}
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);
Expand All @@ -325,6 +338,24 @@ static int r_line_hist_down() {
if (I.hist_down) {
return I.hist_down (I.user);
}
if (I.offset_prompt) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we use tabs. not spaces

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]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use snprintf or r_str_newf and avoid abusing the stack with this char line[R_LINE_BUFSIZE] because to fill a PFMT64x you dont need more than 64 bytes. not 4096

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 ();
Expand Down
6 changes: 4 additions & 2 deletions libr/core/core.c
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions libr/core/visual.c
Expand Up @@ -845,13 +845,16 @@ 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;
core->cons->line->offset_index = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about doing: r_line_set_history_callback() and implement the logic of offset_prompt in here?

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);
reset_print_cur (core->print);
core->cons->line->offset_prompt = false;
}
}

Expand Down
2 changes: 2 additions & 0 deletions libr/include/r_cons.h
Expand Up @@ -796,6 +796,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 Down