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

Separate command history for sdb shell "ks" #10820

Merged
merged 2 commits into from Jul 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions libr/cons/dietline.c
Expand Up @@ -253,6 +253,7 @@ R_API int r_line_set_hist_callback(RLine *line, RLineHistoryUpCb up, RLineHistor
line->cb_history_down = down;
line->offset_hist_index = 0;
line->file_hist_index = 0;
line->sdbshell_hist_iter = r_list_head (line->sdbshell_hist);
return 1;
}

Expand Down Expand Up @@ -376,6 +377,7 @@ R_API void r_line_hist_free() {
}
}
R_FREE (I.history.data);
R_FREE (I.sdbshell_hist);
I.history.index = 0;
}

Expand Down
35 changes: 35 additions & 0 deletions libr/core/cmd.c
Expand Up @@ -971,6 +971,26 @@ static int callback_foreach_kv (void *user, const char *k, const char *v) {
return 1;
}

R_API int sdbshell_history_up(RLine *line) {
if (!line->sdbshell_hist_iter || !line->sdbshell_hist_iter->n) {
return false;
}
line->sdbshell_hist_iter = line->sdbshell_hist_iter->n;
strncpy (line->buffer.data, line->sdbshell_hist_iter->data, R_LINE_BUFSIZE - 1);
line->buffer.index = line->buffer.length = strlen (line->buffer.data);
return true;
}

R_API int sdbshell_history_down(RLine *line) {
if (!line->sdbshell_hist_iter || !line->sdbshell_hist_iter->p) {
return false;
}
line->sdbshell_hist_iter = line->sdbshell_hist_iter->p;
strncpy (line->buffer.data, line->sdbshell_hist_iter->data, R_LINE_BUFSIZE - 1);
line->buffer.index = line->buffer.length = strlen (line->buffer.data);
return true;
}

static int cmd_kuery(void *data, const char *input) {
char buf[1024], *out;
RCore *core = (RCore*)data;
Expand Down Expand Up @@ -1010,6 +1030,13 @@ static int cmd_kuery(void *data, const char *input) {
free (p);
}
if (!s) s = core->sdb;
RLine *line = core->cons->line;
if (!line->sdbshell_hist) {
line->sdbshell_hist = r_list_newf (free);
r_list_append (line->sdbshell_hist, r_str_new ("\0"));
}
RList *sdb_hist = line->sdbshell_hist;
r_line_set_hist_callback (line, &sdbshell_history_up, &sdbshell_history_down);
for (;;) {
r_line_set_prompt (p);
if (r_cons_fgets (buf, buflen, 0, NULL) < 1) {
Expand All @@ -1018,11 +1045,19 @@ static int cmd_kuery(void *data, const char *input) {
if (!*buf) {
break;
}

if (sdb_hist && r_list_length (sdb_hist) == 1 ||
(r_list_length (sdb_hist) > 1 && strcmp (r_list_get_n (sdb_hist, 1), buf))) {
r_list_insert (sdb_hist, 1, strdup (buf));
}
line->sdbshell_hist_iter = sdb_hist->head;

out = sdb_querys (s, NULL, 0, buf);
if (out) {
r_cons_println (out);
}
}
r_line_set_hist_callback (core->cons->line, &cmd_history_up, &cmd_history_down);
break;
case 'o':
if (r_sandbox_enable (0)) {
Expand Down
2 changes: 2 additions & 0 deletions libr/include/r_cons.h
Expand Up @@ -840,6 +840,8 @@ struct r_line_t {
int offset_hist_index;
bool file_prompt;
int file_hist_index;
RList *sdbshell_hist;
RListIter *sdbshell_hist_iter;
}; /* RLine */

#ifdef R_API
Expand Down