Skip to content

Commit

Permalink
Port v and V to RzShell (#4745)
Browse files Browse the repository at this point in the history
* Print log messages if inserting of description fails.

* Port V command to RzShell

* Port v (panels) commands to RzShell and drop vi.

Drops the vi command for two reasons:

- vi (open file in editor) is not at all related to the visual mode.
Hence shouldn't be in the same group.
- (Opinionated) Users can just open the file in another
terminal window. Why go the extra way over Rizin?

Replace old command names with something more descriptive.

- v [name] -> vl name
- v= -> vs

* Fix return values

* Add missing space before V command.

* Split up V and v files to allow for definitino of Vp and Vpp commands

* Restructure help for visual mode.

Add VH and VHH for key list in the visual mode.
Remove examples, but add a more detailed description about the key-sequence parameter.
  • Loading branch information
Rot127 authored Nov 28, 2024
1 parent f1b99c9 commit db97112
Show file tree
Hide file tree
Showing 17 changed files with 488 additions and 171 deletions.
65 changes: 0 additions & 65 deletions librz/core/cmd/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ static const char *help_msg_vertical_bar[] = {
NULL
};

static const char *help_msg_v[] = {
"Usage:", "v[*i]", "",
"v", "", "open visual panels",
"v", " test", "load saved layout with name test",
"v=", " test", "save current layout with name test",
"vi", " test", "open the file test in 'cfg.editor'",
NULL
};

RZ_API void rz_core_cmd_help(const RzCore *core, const char *help[]) {
rz_cons_cmd_help(help, core->print->flags & RZ_PRINT_FLAGS_COLOR);
}
Expand Down Expand Up @@ -385,60 +376,6 @@ RZ_IPI void rz_core_kuery_print(RzCore *core, const char *k) {
free(out);
}

RZ_IPI int rz_cmd_panels(void *data, const char *input) {
RzCore *core = (RzCore *)data;
RzCoreVisual *visual = core->visual;
if (core->vmode) {
return false;
}
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("core: Panel mode requires scr.interactive=true.\n");
return false;
}
char *sp = strchr(input, ' ');
switch (input[0]) {
case ' ': // "v [name]"
if (visual->panels_root->active_tab) {
rz_load_panels_layout(core, input + 1);
}
rz_config_set(core->config, "scr.layout", input + 1);
return true;
case '=': // "v= [name]"
rz_save_panels_layout(core, input + 1);
rz_config_set(core->config, "scr.layout", input + 1);
return true;
case 'i': // "vi [file]"
if (sp) {
char *r = rz_core_editor(core, sp + 1, NULL);
if (r) {
free(r);
} else {
RZ_LOG_ERROR("core: Cannot open file (%s)\n", sp + 1);
}
}
////rz_sys_cmdf ("v%s", input);
return false;
case 0:
rz_core_visual_panels_root(core, visual->panels_root);
return true;
default:
rz_core_cmd_help(core, help_msg_v);
return false;
}
}

RZ_IPI int rz_cmd_visual(void *data, const char *input) {
RzCore *core = (RzCore *)data;
if (core->http_up) {
return false;
}
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("core: Visual mode requires scr.interactive=true.\n");
return false;
}
return rz_core_visual((RzCore *)data, input);
}

RZ_IPI RzCmdStatus rz_push_escaped_handler(RzCore *core, int argc, const char **argv) {
char *input = rz_str_array_join(argv + 1, argc - 1, " ");
int len = rz_str_unescape(input);
Expand Down Expand Up @@ -5282,8 +5219,6 @@ RZ_API void rz_core_cmd_init(RzCore *core) {
} cmds[] = {
{ "/", "search kw, pattern aes", rz_cmd_search },
{ "p", "print current block", rz_cmd_print },
{ "V", "enter visual mode", rz_cmd_visual },
{ "v", "enter visual mode", rz_cmd_panels },
{ "x", "alias for px", rz_cmd_hexdump },
};

Expand Down
2 changes: 2 additions & 0 deletions librz/core/cmd/cmd_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,14 @@ static RzCmdDesc *create_cmd_desc(RzCmd *cmd, RzCmdDesc *parent, RzCmdDescType t
res->type = type;
res->name = rz_str_dup(name);
if (!res->name) {
RZ_LOG_ERROR("Failed to duplicate cmd name.\n");
goto err;
}
res->n_children = 0;
res->help = help ? help : &not_defined_help;
rz_pvector_init(&res->children, (RzPVectorFree)cmd_desc_free);
if (ht_insert && !ht_sp_insert(cmd->ht_cmds, name, res)) {
RZ_LOG_WARN("Command already in hash table. Previous command has been replaced.\n");
goto err;
}
cmd_desc_set_parent(cmd, res, parent);
Expand Down
46 changes: 46 additions & 0 deletions librz/core/cmd/cmd_interactive.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-FileCopyrightText: 2024 Rot127 <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only

#include <rz_cmd.h>
#include <rz_type.h>
#include <rz_util/rz_assert.h>
#include <rz_util/rz_log.h>
#include <rz_util/rz_panels.h>

#include "../core_private.h"

RZ_IPI RzCmdStatus rz_interactive_visual_handler(RzCore *core, int argc, const char **argv) {
if (core->http_up) {
RZ_LOG_ERROR("core->http_up=false.\n");
return RZ_CMD_STATUS_ERROR;
}
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("Visual mode requires scr.interactive=true.\n");
return RZ_CMD_STATUS_ERROR;
}
const char *v_commands = argc > 1 ? argv[1] : "";
rz_core_visual(core, v_commands);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_interactive_visual_disas_handler(RzCore *core, int argc, const char **argv) {
rz_core_visual(core, "p");
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_interactive_visual_emu_handler(RzCore *core, int argc, const char **argv) {
rz_core_visual(core, "pp");
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_interactive_visual_help_handler(RzCore *core, int argc, const char **argv) {
rz_core_cmd_help(core, rz_core_visual_get_short_help());
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_interactive_visual_help_detail_handler(RzCore *core, int argc, const char **argv) {
rz_core_cmd_help(core, rz_core_visual_get_long_help());
rz_cons_printf("%s\n", "Function Keys: (See 'e key.'), defaults to");
rz_core_cmd_help(core, rz_core_visual_get_fcn_help());
return RZ_CMD_STATUS_OK;
}
47 changes: 47 additions & 0 deletions librz/core/cmd/cmd_interactive_panel.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2024 Rot127 <[email protected]>
// SPDX-License-Identifier: LGPL-3.0-only

#include <rz_cmd.h>
#include <rz_type.h>
#include <rz_util/rz_assert.h>
#include <rz_util/rz_log.h>
#include <rz_util/rz_panels.h>

#include "../core_private.h"

RZ_IPI RzCmdStatus rz_interactive_panel_handler(RzCore *core, int argc, const char **argv) {
if (core->vmode) {
RZ_LOG_ERROR("core->vmode == false.\n");
return RZ_CMD_STATUS_ERROR;
}
if (!rz_cons_is_interactive()) {
RZ_LOG_ERROR("Panel mode requires scr.interactive=true.\n");
return RZ_CMD_STATUS_ERROR;
}

RzCoreVisual *visual = core->visual;
if (rz_core_visual_panels_root(core, visual->panels_root)) {
return RZ_CMD_STATUS_OK;
}
RZ_LOG_ERROR("rz_core_visual_panels_root() failed\n");
return RZ_CMD_STATUS_ERROR;
}

RZ_IPI RzCmdStatus rz_interactive_panel_load_handler(RzCore *core, int argc, const char **argv) {
RzCoreVisual *visual = core->visual;

if (visual && visual->panels_root && visual->panels_root->active_tab) {
rz_load_panels_layout(core, argv[1]);
}
rz_config_set(core->config, "scr.layout", argv[1]);
RZ_LOG_INFO("Set scr.layout = %s", argv[1]);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_interactive_panel_store_handler(RzCore *core, int argc, const char **argv) {
rz_save_panels_layout(core, argv[1]);

rz_return_val_if_fail(core->config, RZ_CMD_STATUS_ERROR);
rz_config_set(core->config, "scr.layout", argv[1]);
return RZ_CMD_STATUS_OK;
}
127 changes: 119 additions & 8 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ static const RzCmdDescDetail query_sdb_get_set_details[2];
static const RzCmdDescDetail cmd_print_byte_array_details[3];
static const RzCmdDescDetail pf_details[3];
static const RzCmdDescDetail print_rising_and_falling_entropy_details[2];
static const RzCmdDescDetail interactive_visual_details[2];
static const RzCmdDescDetail write_details[3];
static const RzCmdDescDetail write_bits_details[2];
static const RzCmdDescDetail wv_details[2];
Expand Down Expand Up @@ -789,6 +790,9 @@ static const RzCmdDescArg type_union_c_args[2];
static const RzCmdDescArg type_union_c_nl_args[2];
static const RzCmdDescArg type_xrefs_list_args[2];
static const RzCmdDescArg type_xrefs_function_args[2];
static const RzCmdDescArg interactive_visual_args[2];
static const RzCmdDescArg interactive_panel_load_args[2];
static const RzCmdDescArg interactive_panel_store_args[2];
static const RzCmdDescArg write_args[2];
static const RzCmdDescArg write_bits_args[2];
static const RzCmdDescArg write_unset_bits_args[2];
Expand Down Expand Up @@ -17435,12 +17439,103 @@ static const RzCmdDescHelp type_xrefs_list_all_help = {
.args = type_xrefs_list_all_args,
};

static const RzCmdDescHelp cmd_visual_help = {
.summary = "Enter visual mode",
static const RzCmdDescHelp V_help = {
.summary = "Interactive mode",
};
static const RzCmdDescDetailEntry interactive_visual_Parameters_detail_entries[] = {
{ .text = "V", .arg_str = " <key_sequence>", .comment = "The <key-sequence> argument is a string of keys to press directly after entering the visual mode. See 'VH' or 'VHH' for a full list of valid keys." },
{ 0 },
};
static const RzCmdDescDetail interactive_visual_details[] = {
{ .name = "Parameters", .entries = interactive_visual_Parameters_detail_entries },
{ 0 },
};
static const RzCmdDescArg interactive_visual_args[] = {
{
.name = "key-sequence",
.type = RZ_CMD_ARG_TYPE_STRING,
.flags = RZ_CMD_ARG_FLAG_LAST,
.optional = true,

},
{ 0 },
};
static const RzCmdDescHelp interactive_visual_help = {
.summary = "Enter interactive visual mode",
.description = "Use Rizin (mostly) without shell. Scrolling disassembly, debugging, searching or graph views. All with a few keyboard shortcuts.",
.details = interactive_visual_details,
.args = interactive_visual_args,
};

static const RzCmdDescArg interactive_visual_help_args[] = {
{ 0 },
};
static const RzCmdDescHelp interactive_visual_help_help = {
.summary = "Show most common keys shortcuts of the visual mode.",
.args = interactive_visual_help_args,
};

static const RzCmdDescArg interactive_visual_help_detail_args[] = {
{ 0 },
};
static const RzCmdDescHelp interactive_visual_help_detail_help = {
.summary = "Show all keys shortcuts of the visual mode.",
.args = interactive_visual_help_detail_args,
};

static const RzCmdDescArg interactive_visual_disas_args[] = {
{ 0 },
};
static const RzCmdDescHelp interactive_visual_disas_help = {
.summary = "Enter interactive visual mode and select next mode (alias for 'V p').",
.args = interactive_visual_disas_args,
};

static const RzCmdDescArg interactive_visual_emu_args[] = {
{ 0 },
};
static const RzCmdDescHelp interactive_visual_emu_help = {
.summary = "Enter interactive visual mode and select the mode after next (alias for 'V pp').",
.args = interactive_visual_emu_args,
};

static const RzCmdDescHelp v_help = {
.summary = "Interactive panel mode",
};
static const RzCmdDescArg interactive_panel_args[] = {
{ 0 },
};
static const RzCmdDescHelp interactive_panel_help = {
.summary = "Enter interactive panel mode",
.args = interactive_panel_args,
};

static const RzCmdDescArg interactive_panel_load_args[] = {
{
.name = "name",
.type = RZ_CMD_ARG_TYPE_STRING,
.flags = RZ_CMD_ARG_FLAG_LAST,

},
{ 0 },
};
static const RzCmdDescHelp interactive_panel_load_help = {
.summary = "Load panel layout",
.args = interactive_panel_load_args,
};

static const RzCmdDescArg interactive_panel_store_args[] = {
{
.name = "name",
.type = RZ_CMD_ARG_TYPE_STRING,
.flags = RZ_CMD_ARG_FLAG_LAST,

static const RzCmdDescHelp cmd_panels_help = {
.summary = "Enter visual panel mode",
},
{ 0 },
};
static const RzCmdDescHelp interactive_panel_store_help = {
.summary = "Store panel layout",
.args = interactive_panel_store_args,
};

static const RzCmdDescHelp w_help = {
Expand Down Expand Up @@ -22950,11 +23045,27 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *type_xrefs_list_all_cd = rz_cmd_desc_argv_new(core->rcmd, tx_cd, "txl", rz_type_xrefs_list_all_handler, &type_xrefs_list_all_help);
rz_warn_if_fail(type_xrefs_list_all_cd);

RzCmdDesc *cmd_visual_cd = rz_cmd_desc_oldinput_new(core->rcmd, root_cd, "V", rz_cmd_visual, &cmd_visual_help);
rz_warn_if_fail(cmd_visual_cd);
RzCmdDesc *V_cd = rz_cmd_desc_group_new(core->rcmd, root_cd, "V", rz_interactive_visual_handler, &interactive_visual_help, &V_help);
rz_warn_if_fail(V_cd);
RzCmdDesc *interactive_visual_help_cd = rz_cmd_desc_argv_new(core->rcmd, V_cd, "VH", rz_interactive_visual_help_handler, &interactive_visual_help_help);
rz_warn_if_fail(interactive_visual_help_cd);

RzCmdDesc *interactive_visual_help_detail_cd = rz_cmd_desc_argv_new(core->rcmd, V_cd, "VHH", rz_interactive_visual_help_detail_handler, &interactive_visual_help_detail_help);
rz_warn_if_fail(interactive_visual_help_detail_cd);

RzCmdDesc *interactive_visual_disas_cd = rz_cmd_desc_argv_new(core->rcmd, V_cd, "Vp", rz_interactive_visual_disas_handler, &interactive_visual_disas_help);
rz_warn_if_fail(interactive_visual_disas_cd);

RzCmdDesc *interactive_visual_emu_cd = rz_cmd_desc_argv_new(core->rcmd, V_cd, "Vpp", rz_interactive_visual_emu_handler, &interactive_visual_emu_help);
rz_warn_if_fail(interactive_visual_emu_cd);

RzCmdDesc *v_cd = rz_cmd_desc_group_new(core->rcmd, root_cd, "v", rz_interactive_panel_handler, &interactive_panel_help, &v_help);
rz_warn_if_fail(v_cd);
RzCmdDesc *interactive_panel_load_cd = rz_cmd_desc_argv_new(core->rcmd, v_cd, "vl", rz_interactive_panel_load_handler, &interactive_panel_load_help);
rz_warn_if_fail(interactive_panel_load_cd);

RzCmdDesc *cmd_panels_cd = rz_cmd_desc_oldinput_new(core->rcmd, root_cd, "v", rz_cmd_panels, &cmd_panels_help);
rz_warn_if_fail(cmd_panels_cd);
RzCmdDesc *interactive_panel_store_cd = rz_cmd_desc_argv_new(core->rcmd, v_cd, "vs", rz_interactive_panel_store_handler, &interactive_panel_store_help);
rz_warn_if_fail(interactive_panel_store_cd);

RzCmdDesc *w_cd = rz_cmd_desc_group_new(core->rcmd, root_cd, "w", rz_write_handler, &write_help, &w_help);
rz_warn_if_fail(w_cd);
Expand Down
16 changes: 14 additions & 2 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2371,9 +2371,21 @@ RZ_IPI RzCmdStatus rz_type_xrefs_graph_handler(RzCore *core, int argc, const cha
// "txl"
RZ_IPI RzCmdStatus rz_type_xrefs_list_all_handler(RzCore *core, int argc, const char **argv);
// "V"
RZ_IPI int rz_cmd_visual(void *data, const char *input);
RZ_IPI RzCmdStatus rz_interactive_visual_handler(RzCore *core, int argc, const char **argv);
// "VH"
RZ_IPI RzCmdStatus rz_interactive_visual_help_handler(RzCore *core, int argc, const char **argv);
// "VHH"
RZ_IPI RzCmdStatus rz_interactive_visual_help_detail_handler(RzCore *core, int argc, const char **argv);
// "Vp"
RZ_IPI RzCmdStatus rz_interactive_visual_disas_handler(RzCore *core, int argc, const char **argv);
// "Vpp"
RZ_IPI RzCmdStatus rz_interactive_visual_emu_handler(RzCore *core, int argc, const char **argv);
// "v"
RZ_IPI int rz_cmd_panels(void *data, const char *input);
RZ_IPI RzCmdStatus rz_interactive_panel_handler(RzCore *core, int argc, const char **argv);
// "vl"
RZ_IPI RzCmdStatus rz_interactive_panel_load_handler(RzCore *core, int argc, const char **argv);
// "vs"
RZ_IPI RzCmdStatus rz_interactive_panel_store_handler(RzCore *core, int argc, const char **argv);
// "w"
RZ_IPI RzCmdStatus rz_write_handler(RzCore *core, int argc, const char **argv);
// "wB"
Expand Down
10 changes: 4 additions & 6 deletions librz/core/cmd_descs/cmd_descs.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,13 +263,11 @@ commands:
summary: Types, noreturn, signatures, C parser and more
subcommands: cmd_type
- name: V
cname: cmd_visual
summary: Enter visual mode
type: RZ_CMD_DESC_TYPE_OLDINPUT
summary: Interactive mode
subcommands: cmd_interactive
- name: v
cname: cmd_panels
summary: Enter visual panel mode
type: RZ_CMD_DESC_TYPE_OLDINPUT
summary: Interactive panel mode
subcommands: cmd_interactive_panel
- name: w
summary: Write commands
subcommands: cmd_write
Expand Down
Loading

0 comments on commit db97112

Please sign in to comment.