From ce0ddf682c2a5e4e79081f47d3f09be7e85a387a Mon Sep 17 00:00:00 2001 From: Anton Kochkov Date: Wed, 29 Sep 2021 14:36:28 +0800 Subject: [PATCH] Use RzOutputMode for `rz_core_file_print()` (`o` cmd) --- librz/core/cfile.c | 70 +++++++++++++++++++++++------------------ librz/core/cmd_open.c | 10 ++++-- librz/include/rz_core.h | 3 +- 3 files changed, 49 insertions(+), 34 deletions(-) diff --git a/librz/core/cfile.c b/librz/core/cfile.c index 01a26f642da..c555f25edc2 100644 --- a/librz/core/cfile.c +++ b/librz/core/cfile.c @@ -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; @@ -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); } @@ -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); @@ -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; @@ -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; @@ -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 diff --git a/librz/core/cmd_open.c b/librz/core/cmd_open.c index c710e722f26..54bdf583c0e 100644 --- a/librz/core/cmd_open.c +++ b/librz/core/cmd_open.c @@ -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+" @@ -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]) { @@ -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" diff --git a/librz/include/rz_core.h b/librz/include/rz_core.h index fc36abf886b..4a8da464d28 100644 --- a/librz/include/rz_core.h +++ b/librz/include/rz_core.h @@ -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);