Skip to content

Commit

Permalink
Move pj command to RzShell.
Browse files Browse the repository at this point in the history
This also removes the pj. and pj.. sub-commands.
pj. was an alias for pj @ 0 and pj.. never worked (the function returned always NULL).
  • Loading branch information
Rot127 committed Nov 21, 2024
1 parent b4570fd commit 444bc35
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 28 deletions.
39 changes: 14 additions & 25 deletions librz/core/cmd/cmd_print.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include <rz_types.h>

#include "../core_private.h"
#include "rz_util/rz_strbuf.h"
#include <rz_util/rz_strbuf.h>

#define PF_USAGE_STR "pf[.k[.f[=v]]|[v]]|[n]|[0|cnt][fmt] [a0 a1 ...]"

Expand Down Expand Up @@ -131,7 +131,6 @@ static const char *help_msg_p[] = {
"ph", "[?][=|hash] ([len])", "calculate hash for a block",
"pi", "[?][bdefrj] [num]", "print instructions",
"pI", "[?][iI][df] [len]", "print N instructions/bytes (f=func)",
"pj", "[?] [len]", "print as indented JSON",
"pm", "[?] [magic]", "print libmagic data (see pm? and /m?)",
"po", "[?] hex", "print operation applied to block (see po?)",
"pp", "[?][sz] [len]", "print patterns, see pp? for more help",
Expand All @@ -145,14 +144,6 @@ static const char *help_msg_p[] = {
NULL
};

static const char *help_msg_pj[] = {
"Usage:", "pj[..] [size]", "",
"pj", "", "print current block as indented JSON",
"pj.", "", "print as indented JSON from 0 to the current offset",
"pj..", "", "print JSON path from 0 to the current offset",
NULL
};

static const char *help_msg_px[] = {
"Usage:", "px[0afoswqWqQ][f]", " # Print heXadecimal",
"px", "", "show hexdump",
Expand Down Expand Up @@ -2507,21 +2498,6 @@ RZ_IPI int rz_cmd_print(void *data, const char *input) {
rz_core_block_read(core);
}
switch (*input) {
case 'j': // "pj"
if (input[1] == '?') {
rz_core_cmd_help(core, help_msg_pj);
} else if (input[1] == '.') {
rz_core_cmdf(core, "pj %" PFMT64u " @ 0", core->offset);
} else {
if (core->blocksize < 4 || !memcmp(core->block, "\xff\xff\xff\xff", 4)) {
RZ_LOG_ERROR("core: Cannot read\n");
} else {
char *res = rz_print_json_indent((const char *)core->block, true, " ", NULL);
rz_cons_printf("%s\n", res);
free(res);
}
}
break;
case 'x': // "px"
{
bool show_offset = rz_config_get_i(core->config, "hex.offset");
Expand Down Expand Up @@ -3498,6 +3474,19 @@ RZ_IPI RzCmdStatus rz_cmd_disassembly_function_handler(RzCore *core, int argc, c
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_print_current_block_json_handler(RzCore *core, int argc, const char **argv) {
rz_return_val_if_fail(core, RZ_CMD_STATUS_ERROR);
bool enable_color = rz_config_get_i(core->config, "escr.color") != 0;
char *res = rz_print_json_indent((const char *)core->block, enable_color, " ", NULL);
if (RZ_STR_ISEMPTY(res)) {
rz_cons_printf("Couldn't find a JSON string.\n");
} else {
rz_cons_printf("%s\n", res);
}
free(res);
return RZ_CMD_STATUS_OK;
}

RZ_IPI RzCmdStatus rz_print_function_rzil_handler(RzCore *core, int argc, const char **argv) {
RzAnalysisFunction *f = rz_analysis_first_function_in(core->analysis, core->offset);
if (!f) {
Expand Down
11 changes: 11 additions & 0 deletions librz/core/cmd_descs/cmd_descs.c
Original file line number Diff line number Diff line change
Expand Up @@ -13994,6 +13994,14 @@ static const RzCmdDescHelp print_instructions_function_help = {
.args = print_instructions_function_args,
};

static const RzCmdDescArg print_current_block_json_args[] = {
{ 0 },
};
static const RzCmdDescHelp print_current_block_json_help = {
.summary = "Print formatted json at current offset.",
.args = print_current_block_json_args,
};

static const RzCmdDescArg print_function_rzil_args[] = {
{ 0 },
};
Expand Down Expand Up @@ -21975,6 +21983,9 @@ RZ_IPI void rzshell_cmddescs_init(RzCore *core) {
RzCmdDesc *print_instructions_function_cd = rz_cmd_desc_argv_new(core->rcmd, pI_cd, "pIf", rz_print_instructions_function_handler, &print_instructions_function_help);
rz_warn_if_fail(print_instructions_function_cd);

RzCmdDesc *print_current_block_json_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_cd, "pj", rz_print_current_block_json_handler, &print_current_block_json_help);
rz_warn_if_fail(print_current_block_json_cd);

RzCmdDesc *print_function_rzil_cd = rz_cmd_desc_argv_new(core->rcmd, cmd_print_cd, "plf", rz_print_function_rzil_handler, &print_function_rzil_help);
rz_warn_if_fail(print_function_rzil_cd);

Expand Down
2 changes: 2 additions & 0 deletions librz/core/cmd_descs/cmd_descs.h
Original file line number Diff line number Diff line change
Expand Up @@ -1920,6 +1920,8 @@ RZ_IPI RzCmdStatus rz_assembly_of_hex_alias_handler(RzCore *core, int argc, cons
RZ_IPI RzCmdStatus rz_print_instructions_handler(RzCore *core, int argc, const char **argv);
// "pIf"
RZ_IPI RzCmdStatus rz_print_instructions_function_handler(RzCore *core, int argc, const char **argv);
// "pj"
RZ_IPI RzCmdStatus rz_print_current_block_json_handler(RzCore *core, int argc, const char **argv);
// "plf"
RZ_IPI RzCmdStatus rz_print_function_rzil_handler(RzCore *core, int argc, const char **argv);
// "pp0"
Expand Down
4 changes: 4 additions & 0 deletions librz/core/cmd_descs/cmd_print.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -864,6 +864,10 @@ commands:
summary: Print all instructions at the current function
cname: print_instructions_function
args: []
- name: pj
summary: Print formatted JSON at current offset.
cname: print_current_block_json
args: []
- name: plf
summary: Print the RzIL of the function
cname: print_function_rzil
Expand Down
2 changes: 1 addition & 1 deletion librz/include/rz_util/rz_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ RZ_API int rz_print_row_at_off(RzPrint *p, ut32 offset);

// WIP
RZ_API void rz_print_set_screenbounds(RzPrint *p, ut64 addr);
RZ_API char *rz_print_json_indent(const char *s, bool color, const char *tab, const char **colors);
RZ_API RZ_OWN char *rz_print_json_indent(RZ_NULLABLE const char *s, bool color, const char *tab, RZ_NULLABLE const char **palette);
RZ_API char *rz_print_json_human(const char *s);

RZ_API RZ_OWN RzStrBuf *rz_print_colorize_asm_str(RZ_BORROW RzPrint *p, const RzAsmTokenString *toks);
Expand Down
15 changes: 13 additions & 2 deletions librz/util/json_indent.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,18 @@ RZ_API char *rz_print_json_human(const char *s) {
return O;
}

RZ_API char *rz_print_json_indent(const char *s, bool color, const char *tab, const char **palette) {
/**
* \brief Formats the JSON string at \p s with indentation.
*
* \param s The JSON string to format.
* \param color Format with color?
* \param tab The string to use as indentation tab.
* \param palette The color palette to use. If NULL and \p color == true, a default color palatte is used.
*
* \return The formatted JSON string or NULL in case of failure.
*/
RZ_API RZ_OWN char *rz_print_json_indent(RZ_NULLABLE const char *s, bool color, const char *tab, RZ_NULLABLE const char **palette) {
rz_return_val_if_fail(tab, NULL);
int indent = 0;
const int indentSize = strlen(tab);
int instr = 0;
Expand All @@ -156,7 +167,7 @@ RZ_API char *rz_print_json_indent(const char *s, bool color, const char *tab, co
return NULL;
}

char *O = malloc(osz);
char *O = RZ_NEWS0(char, osz);
if (!O) {
return NULL;
}
Expand Down
45 changes: 45 additions & 0 deletions test/db/cmd/print
Original file line number Diff line number Diff line change
Expand Up @@ -266,5 +266,50 @@ prz 100 | rz-ax -S
EOF
EXPECT=<<EOF
7f454c46020101
EOF
RUN

NAME=pj - Print json
FILE==
CMDS=<<EOF
ws "{\"as\": [1, 2, 3],\"asdf\": {\"a\": 1,\"b\": 0}}"
pj
s 0x4
pj
pj @ 0
pj @ 0x80
EOF
EXPECT=<<EOF
{
"as": [
1,
2,
3
],
"asdf": {
"a": 1,
"b": 0
}
}
[
1,
2,
3
] {
"a": 1,
"b": 0
}
{
"as": [
1,
2,
3
],
"asdf": {
"a": 1,
"b": 0
}
}
Couldn't find a JSON string.
EOF
RUN

0 comments on commit 444bc35

Please sign in to comment.