From a1cb888f95bda5928b44a2fbb07af4613c4045e7 Mon Sep 17 00:00:00 2001 From: Riccardo Schirone <562321+ret2libc@users.noreply.github.com> Date: Thu, 5 Oct 2023 15:31:21 +0200 Subject: [PATCH] core/cmd: fix some leftover of ?i command (#3911) --- librz/core/agraph.c | 4 +- librz/core/cmd/cmd_math.c | 39 +++++++++++++++---- librz/core/cmd/cmd_print.c | 2 +- librz/core/core_private.h | 2 + librz/core/tui/panels.c | 6 +-- librz/core/tui/rop.c | 2 +- librz/core/tui/visual.c | 10 ++--- librz/core/tui/vmenus.c | 2 +- librz/core/tui/vmenus_graph.c | 2 +- .../debug/p/native/windows/windows_message.c | 4 +- 10 files changed, 50 insertions(+), 23 deletions(-) diff --git a/librz/core/agraph.c b/librz/core/agraph.c index 4e2da8371f6..d3acea59715 100644 --- a/librz/core/agraph.c +++ b/librz/core/agraph.c @@ -4231,7 +4231,7 @@ RZ_IPI int rz_core_visual_graph(RzCore *core, RzAGraph *g, RzAnalysisFunction *_ get_bbupdate(g, core, fcn); } break; case '\\': - nextword(core, g, rz_config_get(core->config, "scr.highlight")); + nextword(core, g, rz_cons_singleton()->highlight); break; case 'b': rz_core_visual_browse(core, ""); @@ -4706,7 +4706,7 @@ RZ_IPI int rz_core_visual_graph(RzCore *core, RzAGraph *g, RzAnalysisFunction *_ break; case '/': showcursor(core, true); - rz_core_cmd0(core, "?i highlight;e scr.highlight=`yp`"); + rz_core_prompt_highlight(core); showcursor(core, false); break; case ':': diff --git a/librz/core/cmd/cmd_math.c b/librz/core/cmd/cmd_math.c index e44d8561d59..f0a983b2937 100644 --- a/librz/core/cmd/cmd_math.c +++ b/librz/core/cmd/cmd_math.c @@ -8,6 +8,8 @@ #include "rz_util.h" #include "rz_types.h" +#define HIGHLIGHT_SZ 1024 + static const char *help_msg_greater_sign[] = { "Usage:", "[cmd]>[file]", "redirects console from 'cmd' output to 'file'", "[cmd] > [file]", "", "redirect STDOUT of 'cmd' to 'file'", @@ -575,18 +577,41 @@ RZ_IPI RzCmdStatus rz_yank_hud_file_handler(RzCore *core, int argc, const char * return RZ_CMD_STATUS_OK; } -static RzCmdStatus prompt_handler(RzCore *core, int argc, const char **argv, bool echo) { +static bool get_prompt(RzCore *core, char *prompt, char *output, size_t output_sz) { if (!rz_cons_is_interactive()) { RZ_LOG_ERROR("core: Not running in interactive mode\n"); - return RZ_CMD_STATUS_WRONG_ARGS; + return false; } - char foo[1024]; rz_cons_flush(); - // TODO: rz_cons_input() + rz_line_set_prompt(prompt); + rz_cons_fgets(output, output_sz, 0, NULL); + output[output_sz - 1] = 0; + return true; +} + +/** + * \brief Show a prompt "highlight" and highlights the string inserted by the user + * + * \param core Reference to RzCore + */ +RZ_IPI void rz_core_prompt_highlight(RzCore *core) { + char highlight_str[HIGHLIGHT_SZ]; + + if (!get_prompt(core, "highlight: ", highlight_str, sizeof(highlight_str))) { + return; + } + + rz_cons_highlight(highlight_str); +} + +static RzCmdStatus prompt_handler(RzCore *core, int argc, const char **argv, bool echo) { + char foo[1024]; + snprintf(foo, sizeof(foo) - 1, "%s: ", argv[1]); - rz_line_set_prompt(foo); - rz_cons_fgets(foo, sizeof(foo), 0, NULL); - foo[sizeof(foo) - 1] = 0; + if (!get_prompt(core, foo, foo, sizeof(foo))) { + return RZ_CMD_STATUS_ERROR; + } + rz_core_yank_set_str(core, RZ_CORE_FOREIGN_ADDR, foo); core->num->value = rz_num_math(core->num, foo); rz_cons_set_raw(0); diff --git a/librz/core/cmd/cmd_print.c b/librz/core/cmd/cmd_print.c index 7014694b072..bb5978e8717 100644 --- a/librz/core/cmd/cmd_print.c +++ b/librz/core/cmd/cmd_print.c @@ -238,7 +238,7 @@ static const char *help_detail2_pf[] = { "pf", " 5sqw string quad word", "Print an array with sqw struct along with its field names", "pf", " {integer}? (bifc)", "Print integer times the following format (bifc)", "pf", " [4]w[7]i", "Print an array of 4 words and then an array of 7 integers", - "pf", " ic...?i foo bar \"(pf xw yo foo)troll\" yo", "Print nested anonymous structures", + "pf", " ic...%i foo bar \"(pf xw yo foo)troll\" yo", "Print nested anonymous structures", "pf", " ;..x", "Print value located 6 bytes from current offset", "pf", " [10]z[3]i[10]Zb", "Print an fixed size str, widechar, and var", "pfj", " +F @ 0x14", "Print the content at given offset with flag", diff --git a/librz/core/core_private.h b/librz/core/core_private.h index 4c90a551fc1..666d83d4bad 100644 --- a/librz/core/core_private.h +++ b/librz/core/core_private.h @@ -460,4 +460,6 @@ static inline char *rz_address_str(ut64 addr) { return rz_str_newf("0x%" PFMT64x, addr); } +RZ_IPI void rz_core_prompt_highlight(RzCore *core); + #endif diff --git a/librz/core/tui/panels.c b/librz/core/tui/panels.c index 4d075feb82b..48257d1c7b5 100644 --- a/librz/core/tui/panels.c +++ b/librz/core/tui/panels.c @@ -2000,7 +2000,7 @@ static bool __handle_mouse_on_panel(RzCore *core, RzPanel *panel, int x, int y, __set_addr_by_type(core, PANEL_CMD_DISASSEMBLY, addr); } rz_flag_set(core->flags, "panel.addr", addr, 1); - rz_config_set(core->config, "scr.highlight", word); + rz_cons_highlight(word); #if 1 // TODO implement sync { @@ -6758,7 +6758,7 @@ void __panels_process(RzCore *core, RzPanels *panels) { __set_panel_addr(core, cur, core->offset); break; case 'G': { - const char *hl = rz_config_get(core->config, "scr.highlight"); + const char *hl = rz_cons_singleton()->highlight; if (hl) { ut64 addr = rz_num_math(core->num, hl); __set_panel_addr(core, cur, addr); @@ -6808,7 +6808,7 @@ void __panels_process(RzCore *core, RzPanels *panels) { cur->view->refresh = true; break; case '/': - rz_core_cmd0(core, "?i highlight;e scr.highlight=`yp`"); + rz_core_prompt_highlight(core); break; case 'z': if (panels->curnode > 0) { diff --git a/librz/core/tui/rop.c b/librz/core/tui/rop.c index 33ad7bc3f23..0781fffe365 100644 --- a/librz/core/tui/rop.c +++ b/librz/core/tui/rop.c @@ -185,7 +185,7 @@ RZ_IPI int rz_core_visual_view_rop(RzCore *core) { } } break; case '/': - rz_core_cmd0(core, "?i highlight;e scr.highlight=`yp`"); + rz_core_prompt_highlight(core); break; case 'i': { rz_line_set_prompt("insert value: "); diff --git a/librz/core/tui/visual.c b/librz/core/tui/visual.c index fe1f89ba97e..3ccabaa2d34 100644 --- a/librz/core/tui/visual.c +++ b/librz/core/tui/visual.c @@ -1281,10 +1281,10 @@ RZ_IPI int rz_core_visual_xrefs(RzCore *core, bool xref_to, bool fcnInsteadOfAdd rz_cons_newline(); } /* prepare highlight */ - char *cmd = strdup(rz_config_get(core->config, "scr.highlight")); + char *cmd = strdup(rz_cons_singleton()->highlight); char *ats = rz_str_newf("%" PFMT64x, curat); if (ats && !*cmd) { - (void)rz_config_set(core->config, "scr.highlight", ats); + rz_cons_highlight(ats); } /* print disasm */ char *d = rz_str_ansi_crop(dis, 0, 0, cols, rows - 9); @@ -1294,7 +1294,7 @@ RZ_IPI int rz_core_visual_xrefs(RzCore *core, bool xref_to, bool fcnInsteadOfAdd } /* flush and restore highlight */ rz_cons_flush(); - rz_config_set(core->config, "scr.highlight", cmd); + rz_cons_highlight(cmd); free(ats); free(cmd); free(dis); @@ -1345,7 +1345,7 @@ RZ_IPI int rz_core_visual_xrefs(RzCore *core, bool xref_to, bool fcnInsteadOfAdd } goto repeat; } else if (ch == '/') { - rz_core_cmd0(core, "?i highlight;e scr.highlight=`yp`"); + rz_core_prompt_highlight(core); goto repeat; } else if (ch == 'x' || ch == '<') { xref_to = true; @@ -2920,7 +2920,7 @@ RZ_IPI int rz_core_visual_cmd(RzCore *core, const char *arg) { visual_search(core); } else { if (visual->autoblocksize) { - rz_core_cmd0(core, "?i highlight;e scr.highlight=`yp`"); + rz_core_prompt_highlight(core); } else { rz_core_block_size(core, core->blocksize - cols); } diff --git a/librz/core/tui/vmenus.c b/librz/core/tui/vmenus.c index e1db219baea..fe4908379ba 100644 --- a/librz/core/tui/vmenus.c +++ b/librz/core/tui/vmenus.c @@ -715,7 +715,7 @@ RZ_IPI void rz_core_visual_analysis(RzCore *core, const char *input) { } continue; case '/': - rz_core_cmd0(core, "?i highlight;e scr.highlight=`yp`"); + rz_core_prompt_highlight(core); break; case 'a': switch (level) { diff --git a/librz/core/tui/vmenus_graph.c b/librz/core/tui/vmenus_graph.c index bae08ae5952..85bafda8cea 100644 --- a/librz/core/tui/vmenus_graph.c +++ b/librz/core/tui/vmenus_graph.c @@ -385,7 +385,7 @@ RZ_IPI int rz_core_visual_view_graph(RzCore *core) { if (rz_cons_fgets(cmd, sizeof(cmd), 0, NULL) < 0) { cmd[0] = '\0'; } - rz_config_set(core->config, "scr.highlight", cmd); + rz_cons_highlight(cmd); rz_cons_set_raw(1); rz_cons_show_cursor(false); rz_cons_clear(); diff --git a/librz/debug/p/native/windows/windows_message.c b/librz/debug/p/native/windows/windows_message.c index aa431928d21..79b1f52c8db 100644 --- a/librz/debug/p/native/windows/windows_message.c +++ b/librz/debug/p/native/windows/windows_message.c @@ -513,7 +513,7 @@ RZ_API bool rz_w32_add_winmsg_breakpoint(RzDebug *dbg, const char *msg_name, con } else { reg = "edx"; } - b->cond = rz_str_newf("?q `ae %s,%d,-`", reg, type); + b->cond = rz_str_newf("%= `ae %s,%d,-`", reg, type); } else { char *reg; if (!strcmp(dbg->arch, "arm")) { @@ -529,7 +529,7 @@ RZ_API bool rz_w32_add_winmsg_breakpoint(RzDebug *dbg, const char *msg_name, con reg = "ecx"; } } - b->cond = rz_str_newf("?q `ae %lu,%s,%d,+,[4],-`", type, reg, dbg->bits); + b->cond = rz_str_newf("%= `ae %lu,%s,%d,+,[4],-`", type, reg, dbg->bits); } free(name); return true;