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

(r2) i cmd: consistent json output when no file is loaded #23040

Merged
merged 3 commits into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions libr/core/cmd_info.inc.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ static RCoreHelpMessage help_msg_iS = {
static RCoreHelpMessage help_msg_i = {
"Usage: i", "", "Get info from opened file (see rabin2's manpage)",
"i", "[*jq]", "show info of current file (in JSON)",
"iA", "", "list archs found in current binary",
"ia", "", "show all info (imports, exports, sections..)",
"iA", "", "list archs found in current binary",
"ib", "", "reload the current buffer for setting of the bin (use once only)",
"ic", "[?]", "List classes, methods and fields (icj for json)",
"iC", "[j]", "show signature info (entitlements, ...)",
Expand Down Expand Up @@ -1136,6 +1136,12 @@ static void cmd_ic(RCore *core, const char *input, PJ *pj, bool is_array, bool v
}

static void cmd_iz(RCore *core, PJ *pj, int mode, int is_array, bool va, const char *input) {
RBinInfo *info = r_bin_get_info (core->bin);
if (!info && pj) {
astralia marked this conversation as resolved.
Show resolved Hide resolved
r_cons_print ("[]");
r_core_return_value (core, 1);
return;
}
bool rdump = false;
if (input[1] == '-') { // "iz-"
char *strpurge = core->bin->strpurge;
Expand Down Expand Up @@ -1235,6 +1241,12 @@ static void cmd_iz(RCore *core, PJ *pj, int mode, int is_array, bool va, const c

static void cmd_iS(RCore *core, const char *input, PJ **_pj, int mode, const bool va, const bool is_array) {
PJ *pj = *_pj;
RBinInfo *info = r_bin_get_info (core->bin);
if (!info && pj) {
r_cons_print ("[]");
r_core_return_value (core, 1);
return;
}
if (!input[1]) {
RBININFO ("sections", R_CORE_BIN_ACC_SECTIONS, NULL, 0);
} else if (input[1] == ',' || input[1] == ' ') {
Expand Down Expand Up @@ -1314,7 +1326,11 @@ static void cmd_it(RCore *core, PJ *pj) {
ut64 limit = r_config_get_i (core->config, "bin.hashlimit");
RBinInfo *info = r_bin_get_info (core->bin);
if (!info) {
R_LOG_ERROR ("Cannot get bin info");
if (is_json) {
r_cons_print ("{}");
} else {
R_LOG_ERROR ("Cannot get bin info");
}
r_core_return_value (core, 1);
return;
}
Expand Down Expand Up @@ -1727,6 +1743,9 @@ static int cmd_info(void *data, const char *input) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("{}");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
r_list_foreach (objs, iter, bf) {
core->bin->cur = bf;
RBININFO ("info", R_CORE_BIN_ACC_INFO, NULL, 0);
Expand All @@ -1741,6 +1760,9 @@ static int cmd_info(void *data, const char *input) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("{}");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
r_list_foreach (objs, iter, bf) {
core->bin->cur = bf;
RBININFO ("main", R_CORE_BIN_ACC_MAIN, NULL, 0);
Expand All @@ -1755,6 +1777,9 @@ static int cmd_info(void *data, const char *input) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("[]");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
r_list_foreach (objs, iter, bf) {
core->bin->cur = bf;
RBININFO ("memory", R_CORE_BIN_ACC_MEM, input + 1, 0);
Expand Down Expand Up @@ -1784,6 +1809,9 @@ static int cmd_info(void *data, const char *input) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("[]");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
r_list_foreach (bfiles, iter, bf) {
core->bin->cur = bf;
RBININFO ("versioninfo", R_CORE_BIN_ACC_VERSIONINFO, NULL, 0);
Expand Down Expand Up @@ -1848,10 +1876,10 @@ static int cmd_info(void *data, const char *input) {
subcmds++;
}
if (mode == R_MODE_JSON) {
r_cons_println ("}");
r_cons_printf ("}");
}
} else {
r_cons_println ("{}");
r_cons_printf ("{}");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
break;
case 'A': // "iA"
Expand Down Expand Up @@ -1937,7 +1965,9 @@ static int cmd_info(void *data, const char *input) {
} else if (!bin_header (core, mode)) {
/// XXX header vs fields wtf
if (!r_core_bin_info (core, R_CORE_BIN_ACC_HEADER, pj, mode, va, NULL, NULL)) {
R_LOG_ERROR ("No header fields found");
if (!pj) {
R_LOG_ERROR ("No header fields found");
}
r_core_return_value (core, 1);
}
}
Expand Down Expand Up @@ -2004,6 +2034,10 @@ static int cmd_info(void *data, const char *input) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("{}");
astralia marked this conversation as resolved.
Show resolved Hide resolved
break;
}
RList *bfiles = r_core_bin_files (core);
r_list_foreach (bfiles, iter, bf) {
core->bin->cur = bf;
Expand All @@ -2021,6 +2055,9 @@ static int cmd_info(void *data, const char *input) {
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("[]");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
r_list_foreach (objs, iter, bf) {
RBinObject *obj = bf->bo;
core->bin->cur = bf;
Expand All @@ -2031,12 +2068,15 @@ static int cmd_info(void *data, const char *input) {
r_list_free (objs);
}
break;
case 'r': // "ir"
case 'r': // "ir"
{
RList *objs = r_core_bin_files (core);
RListIter *iter;
RBinFile *bf;
RBinFile *cur = core->bin->cur;
if (!cur && pj) {
r_cons_printf ("[]");
astralia marked this conversation as resolved.
Show resolved Hide resolved
}
r_list_foreach (objs, iter, bf) {
core->bin->cur = bf;
RBININFO ("relocs", R_CORE_BIN_ACC_RELOCS, NULL, 0);
Expand Down
22 changes: 11 additions & 11 deletions test/db/json/cmd_i
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# iSj
iSj
icj
idj
iEj
iej
ihj
# iIj
iIj
iij
ij
# ilj
ilj
iLj
# iMj
# imj
iMj
imj
iRj
# irj
# iSj
irj
iSj
is.j @ 0xffffffffffffffff
isj
iSj entropy
iSj sha1
# iSSj
# itj
# iVj
# izj
iSSj
itj
iVj
izj
izzj
iZj
6 changes: 3 additions & 3 deletions test/db/json/json2
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ fj
fdj
fd.j
fsj
# iaj
# iAj
# iCj
iaj
iAj
iCj
tj
ttj
Loading