Skip to content

Commit

Permalink
Use RzOutputMode for rz_core_file_print() (o cmd) (#1769)
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka authored Oct 4, 2021
1 parent 5b7b094 commit e75fbc5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 34 deletions.
70 changes: 40 additions & 30 deletions librz/core/cfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -1299,7 +1299,40 @@ RZ_API RzCoreFile *rz_core_file_get_by_fd(RzCore *core, int fd) {
return NULL;
}

RZ_API int rz_core_file_list(RzCore *core, int mode) {
RZ_API bool rz_core_raw_file_print(RzCore *core) {
RzCoreFile *f;
RzIODesc *desc;
RzBinFile *bf;
RzListIter *it1, *it2, *it3;
rz_list_foreach (core->files, it1, f) {
desc = rz_io_desc_get(core->io, f->fd);
if (!desc) {
continue;
}
bool header_loaded = false;
rz_list_foreach (core->bin->binfiles, it2, bf) {
if (bf->fd == f->fd) {
header_loaded = true;
break;
}
}
if (!header_loaded) {
RzList *maps = rz_io_map_get_for_fd(core->io, f->fd);
RzIOMap *current_map;
char *absfile = rz_file_abspath(desc->uri);
rz_list_foreach (maps, it3, current_map) {
if (current_map) {
rz_cons_printf("on %s 0x%" PFMT64x "\n", absfile, current_map->itv.addr);
}
}
rz_list_free(maps);
free(absfile);
}
}
return true;
}

RZ_API bool rz_core_file_print(RzCore *core, RzOutputMode mode) {
int count = 0;
RzCoreFile *f;
RzIODesc *desc;
Expand All @@ -1308,10 +1341,10 @@ RZ_API int rz_core_file_list(RzCore *core, int mode) {
RzBinFile *bf;
RzListIter *iter;
PJ *pj = NULL;
if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj = pj_new();
if (!pj) {
return 0;
return false;
}
pj_a(pj);
}
Expand All @@ -1323,7 +1356,7 @@ RZ_API int rz_core_file_list(RzCore *core, int mode) {
}
from = 0LL;
switch (mode) {
case 'j': { // "oij"
case RZ_OUTPUT_MODE_JSON: { // "oij"
pj_o(pj);
pj_kb(pj, "raised", core->io->desc->fd == f->fd);
pj_ki(pj, "fd", f->fd);
Expand All @@ -1334,8 +1367,7 @@ RZ_API int rz_core_file_list(RzCore *core, int mode) {
pj_end(pj);
break;
}
case '*':
case 'r':
case RZ_OUTPUT_MODE_RIZIN:
// TODO: use a getter
{
bool fileHaveBin = false;
Expand All @@ -1352,28 +1384,6 @@ RZ_API int rz_core_file_list(RzCore *core, int mode) {
free(absfile);
}
break;
case 'n': {
bool header_loaded = false;
rz_list_foreach (core->bin->binfiles, it, bf) {
if (bf->fd == f->fd) {
header_loaded = true;
break;
}
}
if (!header_loaded) {
RzList *maps = rz_io_map_get_for_fd(core->io, f->fd);
RzListIter *iter;
RzIOMap *current_map;
char *absfile = rz_file_abspath(desc->uri);
rz_list_foreach (maps, iter, current_map) {
if (current_map) {
rz_cons_printf("on %s 0x%" PFMT64x "\n", absfile, current_map->itv.addr);
}
}
rz_list_free(maps);
free(absfile);
}
} break;
default: {
ut64 sz = rz_io_desc_size(desc);
const char *fmt;
Expand All @@ -1392,12 +1402,12 @@ RZ_API int rz_core_file_list(RzCore *core, int mode) {
}
count++;
}
if (mode == 'j') {
if (mode == RZ_OUTPUT_MODE_JSON) {
pj_end(pj);
rz_cons_println(pj_string(pj));
pj_free(pj);
}
return count;
return true;
}

// XXX - needs to account for binfile index and bin object index
Expand Down
10 changes: 7 additions & 3 deletions librz/core/cmd_open.c
Original file line number Diff line number Diff line change
Expand Up @@ -1136,7 +1136,7 @@ RZ_IPI int rz_cmd_open(void *data, const char *input) {
}
case 'n': // "on"
if (input[1] == '*') {
rz_core_file_list(core, 'n');
rz_core_raw_file_print(core);
return 0;
}
if (input[1] == '+') { // "on+"
Expand Down Expand Up @@ -1291,7 +1291,7 @@ RZ_IPI int rz_cmd_open(void *data, const char *input) {
rz_core_cmd_help(core, help_msg_o_star);
break;
}
rz_core_file_list(core, (int)(*input));
rz_core_file_print(core, RZ_OUTPUT_MODE_RIZIN);
break;
case 'j': // "oj"
if ('?' == input[1]) {
Expand Down Expand Up @@ -1354,10 +1354,14 @@ RZ_IPI int rz_cmd_open(void *data, const char *input) {
}
} break;
case 'j': // "oij"
rz_core_file_print(core, RZ_OUTPUT_MODE_JSON);
break;
case '*': // "oi*"
rz_core_file_print(core, RZ_OUTPUT_MODE_RIZIN);
break;
case 0: // "oi"
rz_core_file_list(core, input[1]);
break;
rz_core_file_print(core, RZ_OUTPUT_MODE_STANDARD);
}
break;
case 'u': { // "ou"
Expand Down
3 changes: 2 additions & 1 deletion librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,8 @@ RZ_API RzCoreFile *rz_core_file_get_by_fd(RzCore *core, int fd);
RZ_API void rz_core_file_close(RzCoreFile *fh);
RZ_API bool rz_core_file_close_fd(RzCore *core, int fd);
RZ_API bool rz_core_file_close_all_but(RzCore *core);
RZ_API int rz_core_file_list(RzCore *core, int mode);
RZ_API bool rz_core_raw_file_print(RzCore *core);
RZ_API bool rz_core_file_print(RzCore *core, RzOutputMode mode);
RZ_API int rz_core_file_binlist(RzCore *core);
RZ_API bool rz_core_file_bin_raise(RzCore *core, ut32 num);
RZ_API bool rz_core_extend_at(RzCore *core, ut64 addr, int size);
Expand Down

0 comments on commit e75fbc5

Please sign in to comment.