From 2740e74e4904a5c3e4e8bae28308906da649e21a Mon Sep 17 00:00:00 2001 From: Rene Laemmert Date: Wed, 1 Jun 2016 12:16:00 +0200 Subject: [PATCH] added cursor to undo seek history (#5054) * added cursor to undo seek history * cleanup the mentioned parts --- libr/core/cmd_seek.c | 31 ++++++++++++++-------------- libr/core/graph.c | 12 ++++++----- libr/core/visual.c | 31 ++++++++++++++-------------- libr/include/r_io.h | 13 ++++++++---- libr/include/r_print.h | 1 + libr/io/undo.c | 47 ++++++++++++++++++++++++++---------------- libr/util/print.c | 4 ++++ 7 files changed, 82 insertions(+), 57 deletions(-) diff --git a/libr/core/cmd_seek.c b/libr/core/cmd_seek.c index 146a541058..21257696d9 100644 --- a/libr/core/cmd_seek.c +++ b/libr/core/cmd_seek.c @@ -134,7 +134,7 @@ static int cmd_seek(void *data, const char *input) { if (input[1] && input[2]) { if (core->io->debug) { off = r_debug_reg_get (core->dbg, input + 2); - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek (core, off, 1); } else { RReg *orig = core->dbg->reg; @@ -224,7 +224,7 @@ static int cmd_seek(void *data, const char *input) { break; case 1: off = cb.addr; - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek (core, off, 1); r_core_block_read (core, 0); break; @@ -238,7 +238,7 @@ static int cmd_seek(void *data, const char *input) { "sC const seek to comment matching 'const'\n"); break; case ' ': - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek (core, off * sign, 1); r_core_block_read (core, 0); break; @@ -295,32 +295,33 @@ static int cmd_seek(void *data, const char *input) { case '+': if (input[1]!='\0') { int delta = (input[1]=='+')? core->blocksize: off; - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek_delta (core, delta); } else { - off = r_io_sundo_redo (core->io); - if (off != UT64_MAX) - r_core_seek (core, off, 0); + RUndos *undo = r_io_sundo_redo (core->io); + if (undo != NULL) + r_core_seek (core, undo->off, 0); } break; case '-': if (input[1]!='\0') { int delta = (input[1]=='-') ? -core->blocksize: -off; - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek_delta (core, delta); } else { - off = r_io_sundo (core->io, core->offset); - if (off != UT64_MAX) - r_core_seek (core, off, 0); + RUndos *undo = r_io_sundo (core->io, core->offset); + if (undo) { + r_core_seek (core, undo->off, 0); + } } r_core_block_read (core, 1); break; case 'n': - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek_next (core, r_config_get (core->config, "scr.nkey")); break; case 'p': - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek_previous (core, r_config_get (core->config, "scr.nkey")); break; case 'a': @@ -337,13 +338,13 @@ static int cmd_seek(void *data, const char *input) { r_cmd_call (core->rcmd, cmd); free (cmd); } - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_seek_align (core, off, 0); break; case 'b': if (off == 0) off = core->offset; - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); r_core_anal_bb_seek (core, off); break; case 'f': // "sf" diff --git a/libr/core/graph.c b/libr/core/graph.c index ed783c4727..823e99d0cc 100644 --- a/libr/core/graph.c +++ b/libr/core/graph.c @@ -2521,7 +2521,7 @@ static void goto_asmqjmps(RAGraph *g, RCore *core) { r_agraph_set_curnode (g, addr_node); agraph_update_seek (g, addr_node, true); } else { - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, 0); r_core_seek (core, addr, 0); } } @@ -2774,16 +2774,18 @@ R_API int r_core_visual_graph(RCore *core, RAGraph *g, RAnalFunction *_fcn, int case 'u': { if (!fcn) break; - ut64 off = r_io_sundo (core->io, core->offset); - if (off != UT64_MAX) r_core_seek (core, off, 0); + RUndos *undo = r_io_sundo (core->io, core->offset); + if (undo) { + r_core_seek (core, undo->off, 0); + } else eprintf ("Can not undo\n"); break; } case 'U': { if (!fcn) break; - ut64 off = r_io_sundo_redo (core->io); - if (off != UT64_MAX) r_core_seek (core, off, 0); + RUndos *undo = r_io_sundo_redo (core->io); + if (undo != NULL) r_core_seek (core, undo->off, 0); else eprintf ("Cannot redo\n"); break; } diff --git a/libr/core/visual.c b/libr/core/visual.c index 5d3472530d..491e80f3bb 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -1067,7 +1067,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { off = r_core_get_asmqjmps (core, chbuf); if (off != UT64_MAX) { int delta = R_ABS ((st64)off-(st64)offset); - r_io_sundo_push (core->io, offset); + r_io_sundo_push (core->io, offset, r_print_get_cursor (core->print)); if (core->print->cur_enabled && delta<100) { core->print->cur = delta; } else { @@ -1102,14 +1102,15 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { if (core->print->cur_enabled) { int delta = R_ABS ((st64)op->jump-(st64)offset); if ( op->jump < core->offset || op->jump >= core->print->screen_bounds) { - r_io_sundo_push (core->io, offset); + r_io_sundo_push (core->io, offset, r_print_get_cursor (core->print)); r_core_visual_seek_animation (core, op->jump); core->print->cur = 0; } else { + r_io_sundo_push (core->io, offset, r_print_get_cursor (core->print)); core->print->cur = delta; } } else { - r_io_sundo_push (core->io, offset); + r_io_sundo_push (core->io, offset, 0); r_core_visual_seek_animation (core, op->jump); } } @@ -1407,7 +1408,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { offset = 0; r_core_seek (core, offset, 1); } else r_core_seek (core, 0, 1); - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); break; case 'G': ret = 0; @@ -1435,7 +1436,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { ret = -1; } if (ret != -1) - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); break; case 'h': if (core->print->cur_enabled) { @@ -1706,15 +1707,15 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { break; case '>': r_core_seek_align (core, core->blocksize, 1); - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); break; case '<': r_core_seek_align (core, core->blocksize, -1); r_core_seek_align (core, core->blocksize, -1); - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); break; case '.': - r_io_sundo_push (core->io, core->offset); + r_io_sundo_push (core->io, core->offset, r_print_get_cursor (core->print)); if (core->print->cur_enabled) { r_core_seek (core, core->offset+core->print->cur, 1); core->print->cur = 0; @@ -1810,10 +1811,10 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { break; case 'u': { - ut64 off = r_io_sundo (core->io, core->offset); - if (off != UT64_MAX) { - r_core_visual_seek_animation (core, off); - reset_print_cur (core->print); + RUndos *undo = r_io_sundo (core->io, core->offset); + if (undo) { + r_core_visual_seek_animation (core, undo->off); + core->print->cur = undo->cursor; } else { eprintf ("Cannot undo\n"); } @@ -1821,9 +1822,9 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { break; case 'U': { - ut64 off = r_io_sundo_redo (core->io); - if (off != UT64_MAX) { - r_core_visual_seek_animation (core, off); + RUndos *undo = r_io_sundo_redo (core->io); + if (undo) { + r_core_visual_seek_animation (core, undo->off); reset_print_cur (core->print); } } diff --git a/libr/include/r_io.h b/libr/include/r_io.h index 87e571ec49..e36863d89b 100644 --- a/libr/include/r_io.h +++ b/libr/include/r_io.h @@ -84,6 +84,11 @@ typedef struct { #define R_IO_DESC_TYPE_OPENED 1 #define R_IO_DESC_TYPE_CLOSED 0 +typedef struct r_undo_t { + ut64 off; + int cursor; +} RUndos; + /* stores write and seek changes */ #define R_IO_UNDOS 64 typedef struct r_io_undo_t { @@ -96,7 +101,7 @@ typedef struct r_io_undo_t { int idx; int undos; /* available undos */ int redos; /* available redos */ - ut64 seek[R_IO_UNDOS]; + RUndos seek[R_IO_UNDOS]; /*int fd[R_IO_UNDOS]; // XXX: Must be RIODesc* */ } RIOUndo; @@ -423,9 +428,9 @@ R_API RIOSection * r_io_section_get_first_in_paddr_range(RIO *io, ut64 addr, ut6 R_API int r_io_undo_init(RIO *io); R_API void r_io_undo_enable(RIO *io, int seek, int write); /* seek undo */ -R_API ut64 r_io_sundo(RIO *io, ut64 offset); -R_API ut64 r_io_sundo_redo(RIO *io); -R_API void r_io_sundo_push(RIO *io, ut64 off); +R_API RUndos *r_io_sundo(RIO *io, ut64 offset); +R_API RUndos *r_io_sundo_redo(RIO *io); +R_API void r_io_sundo_push(RIO *io, ut64 off, int cursor); R_API void r_io_sundo_reset(RIO *io); R_API void r_io_sundo_list(RIO *io, int mode); /* write undo */ diff --git a/libr/include/r_print.h b/libr/include/r_print.h index 5b56a743fd..bbb00d0cc0 100644 --- a/libr/include/r_print.h +++ b/libr/include/r_print.h @@ -114,6 +114,7 @@ R_API void r_print_c(RPrint *p, const ut8 *str, int len); R_API void r_print_raw(RPrint *p, ut64 addr, const ut8* buf, int len, int offlines); R_API void r_print_cursor(RPrint *p, int cur, int set); R_API void r_print_cursor_range(RPrint *p, int cur, int to, int set); +R_API int r_print_get_cursor(RPrint *p); R_API void r_print_set_cursor(RPrint *p, int curset, int ocursor, int cursor); R_API void r_print_code(RPrint *p, ut64 addr, ut8 *buf, int len, char lang); #define SEEFLAG -2 diff --git a/libr/io/undo.c b/libr/io/undo.c index 263ea05de8..e0107c1e6e 100644 --- a/libr/io/undo.c +++ b/libr/io/undo.c @@ -29,47 +29,57 @@ R_API void r_io_undo_enable(RIO *io, int s, int w) { /* undo seekz */ -R_API ut64 r_io_sundo(RIO *io, ut64 offset) { - ut64 off; +R_API RUndos *r_io_sundo(RIO *io, ut64 offset) { + RUndos *undo; if (!io->undo.s_enable || !io->undo.undos) - return UT64_MAX; + return NULL; /* No redos yet, store the current seek so we can redo to it. */ if (!io->undo.redos) { - io->undo.seek[io->undo.idx] = offset; + undo = &io->undo.seek[io->undo.idx]; + undo->off = offset; + undo->cursor = 0; } io->undo.idx = (io->undo.idx - 1 + R_IO_UNDOS) % R_IO_UNDOS; io->undo.undos--; io->undo.redos++; - off = io->undo.seek[io->undo.idx]; - io->off = r_io_section_vaddr_to_maddr_try (io, off); - return off; + undo = &io->undo.seek[io->undo.idx]; + io->off = r_io_section_vaddr_to_maddr_try (io, undo->off); + return undo; } -R_API ut64 r_io_sundo_redo(RIO *io) { - ut64 off; +R_API RUndos *r_io_sundo_redo(RIO *io) { + RUndos *undo; if (!io->undo.s_enable || !io->undo.redos) - return UT64_MAX; + return NULL; io->undo.idx = (io->undo.idx + 1) % R_IO_UNDOS; io->undo.undos++; io->undo.redos--; - off = io->undo.seek[io->undo.idx]; - io->off = r_io_section_vaddr_to_maddr_try (io, off); - return off; + undo = &io->undo.seek[io->undo.idx]; + io->off = r_io_section_vaddr_to_maddr_try (io, undo->off); + return undo; } -R_API void r_io_sundo_push(RIO *io, ut64 off) { +R_API void r_io_sundo_push(RIO *io, ut64 off, int cursor) { if (!io->undo.s_enable) return; + RUndos *undo; //the first insert - if (io->undo.idx > 0) - if (io->undo.seek[io->undo.idx - 1] == off) return; - io->undo.seek[io->undo.idx] = off; + if (io->undo.idx > 0) { + undo = &io->undo.seek[io->undo.idx - 1]; + if (undo->off == off && undo->cursor == cursor) { + return; + } + } + + undo = &io->undo.seek[io->undo.idx]; + undo->off = off; + undo->cursor = cursor; io->undo.idx = (io->undo.idx + 1) % R_IO_UNDOS; /* Only R_IO_UNDOS - 1 undos can be used because r_io_sundo_undo () must * push the current position for redo as well, which takes one entry in @@ -111,7 +121,8 @@ R_API void r_io_sundo_list(RIO *io, int mode) { } for (i = start; i < end || j == 0; i = (i + 1) % R_IO_UNDOS) { int idx = (j< undos)? undos - j - 1: j - undos - 1; - ut64 addr = io->undo.seek[i]; + RUndos *undo = &io->undo.seek[i]; + ut64 addr = undo->off; ut64 notLast = j+1