From c938df90b384c052f268f7ad71384000935936b9 Mon Sep 17 00:00:00 2001 From: pancake Date: Mon, 2 Dec 2024 01:00:31 +0100 Subject: [PATCH] Move IO plugin listing from IO to core ##io --- libr/core/clist.c | 69 +++++++++++++++++++++++++++++++++ libr/core/cmd_open.inc.c | 6 +-- libr/include/r_core.h | 2 +- libr/include/r_io.h | 2 - libr/include/r_lib.h | 2 +- libr/io/io_plugin.c | 83 ---------------------------------------- libr/main/radare2.c | 5 ++- libr/main/rafind2.c | 3 ++ libr/util/lib.c | 2 +- 9 files changed, 79 insertions(+), 95 deletions(-) diff --git a/libr/core/clist.c b/libr/core/clist.c index 7860f886527ce..61396ca4dc909 100644 --- a/libr/core/clist.c +++ b/libr/core/clist.c @@ -52,3 +52,72 @@ R_API void r_core_list_lang(RCore *core, int mode) { r_table_free (table); } } + +R_API int r_core_list_io(RCore *core, const char *name, int mode) { + RIOPlugin *plugin; + SdbListIter *iter; + char str[4]; + int n = 0; + PJ *pj = NULL; + if (mode == 'j') { + pj = r_core_pj_new (core); + pj_a (pj); + } + + ls_foreach (core->io->plugins, iter, plugin) { + const char *plugin_name = r_str_get (plugin->meta.name); + if (name && strcmp (plugin_name, name)) { + continue; + } + if (mode == 'j') { + pj_o (pj); + pj_ks (pj, "permissions", str); + r_lib_meta_pj (pj, &plugin->meta); + if (plugin->uris) { + char *uri; + char *uris = strdup (plugin->uris); + RList *plist = r_str_split_list (uris, ",", 0); + RListIter *piter; + pj_k (pj, "uris"); + pj_a (pj); + r_list_foreach (plist, piter, uri) { + pj_s (pj, uri); + } + pj_end (pj); + r_list_free (plist); + free (uris); + } + pj_end (pj); + } else if (name) { + r_cons_printf ("name: %s\n", plugin->meta.name); + r_cons_printf ("auth: %s\n", plugin->meta.author); + r_cons_printf ("lice: %s\n", plugin->meta.license); + r_cons_printf ("desc: %s\n", plugin->meta.desc); + r_cons_printf ("uris: %s\n", plugin->uris); + if (*str) { + r_cons_printf ("perm: %s\n", str); + } + r_cons_printf ("sysc: %s\n", r_str_bool (plugin->system)); + } else { + str[0] = 'r'; + str[1] = plugin->write ? 'w' : '_'; + str[2] = plugin->isdbg ? 'd' : '_'; + str[3] = 0; + r_cons_printf ("%s %-8s %s.", str, + r_str_get (plugin->meta.name), + r_str_get (plugin->meta.desc)); + if (plugin->uris) { + r_cons_printf (" %s", plugin->uris); + } + r_cons_printf ("\n"); + } + n++; + } + if (pj) { + pj_end (pj); + char *s = pj_drain (pj); + r_cons_printf ("%s\n", s); + free (s); + } + return n; +} diff --git a/libr/core/cmd_open.inc.c b/libr/core/cmd_open.inc.c index d29b05d5149f7..b83e54542b04b 100644 --- a/libr/core/cmd_open.inc.c +++ b/libr/core/cmd_open.inc.c @@ -2209,11 +2209,7 @@ static int cmd_open(void *data, const char *input) { R_LOG_ERROR ("Oops. Cannot open library"); } } else { - if ('j' == input[1]) { - r_io_plugin_list_json (core->io); - } else { - r_io_plugin_list (core->io); - } + r_core_list_io (core, NULL, input[1]); } break; case 'u': { // "ou" diff --git a/libr/include/r_core.h b/libr/include/r_core.h index da8a449dd3547..933bfdc1957af 100644 --- a/libr/include/r_core.h +++ b/libr/include/r_core.h @@ -603,7 +603,7 @@ R_API bool cmd_anal_objc(RCore *core, const char *input, bool auto_anal); R_API void r_core_anal_cc_init(RCore *core); R_API void r_core_anal_paths(RCore *core, ut64 from, ut64 to, bool followCalls, int followDepth, bool is_json); -R_API void r_core_list_io(RCore *core); +R_API int r_core_list_io(RCore *core, const char *name, int mode); R_API void r_core_list_lang(RCore *core, int mode); R_API void r_core_visual_slides(RCore *core, const char *file); diff --git a/libr/include/r_io.h b/libr/include/r_io.h index 4de0117c387b1..6b7f00b07c25b 100644 --- a/libr/include/r_io.h +++ b/libr/include/r_io.h @@ -461,8 +461,6 @@ R_API void r_io_free(RIO *io); R_API bool r_io_plugin_init(RIO *io); R_API bool r_io_plugin_add(RIO *io, RIOPlugin *plugin); R_API bool r_io_plugin_remove(RIO *io, RIOPlugin *plugin); -R_API int r_io_plugin_list(RIO *io); -R_API int r_io_plugin_list_json(RIO *io); R_API int r_io_plugin_read(RIODesc *desc, ut8 *buf, int len); R_API int r_io_plugin_write(RIODesc *desc, const ut8 *buf, int len); R_API int r_io_plugin_read_at(RIODesc *desc, ut64 addr, ut8 *buf, int len); diff --git a/libr/include/r_lib.h b/libr/include/r_lib.h index 524c88b2dfb73..53415d470eb75 100644 --- a/libr/include/r_lib.h +++ b/libr/include/r_lib.h @@ -150,7 +150,7 @@ R_API const char *r_lib_types_get(int idx); R_API int r_lib_types_get_i(const char *str); #include -R_API void r_lib_meta_pj(PJ *pj, RPluginMeta *meta); +R_API void r_lib_meta_pj(PJ *pj, const RPluginMeta *meta); #endif #ifdef __cplusplus diff --git a/libr/io/io_plugin.c b/libr/io/io_plugin.c index bac845a5acff3..4fd47475ec406 100644 --- a/libr/io/io_plugin.c +++ b/libr/io/io_plugin.c @@ -64,89 +64,6 @@ R_API RIOPlugin *r_io_plugin_byname(RIO *io, const char *name) { return NULL; } -R_API int r_io_plugin_list(RIO *io) { - RIOPlugin *plugin; - SdbListIter *iter; - char str[4]; - int n = 0; - - ls_foreach (io->plugins, iter, plugin) { - str[0] = 'r'; - str[1] = plugin->write ? 'w' : '_'; - str[2] = plugin->isdbg ? 'd' : '_'; - str[3] = 0; - io->cb_printf ("%s %-8s %s.", str, - r_str_get (plugin->meta.name), - r_str_get (plugin->meta.desc)); - if (plugin->uris) { - io->cb_printf (" %s", plugin->uris); - } - io->cb_printf ("\n"); - n++; - } - return n; -} - -R_API int r_io_plugin_list_json(RIO *io) { - RIOPlugin *plugin; - SdbListIter *iter; - PJ *pj = pj_new (); - if (!pj) { - return 0; - } - - char str[4]; - int n = 0; - pj_a (pj); - ls_foreach (io->plugins, iter, plugin) { - str[0] = 'r'; - str[1] = plugin->write ? 'w' : '_'; - str[2] = plugin->isdbg ? 'd' : '_'; - str[3] = 0; - - pj_o (pj); - pj_ks (pj, "permissions", str); - if (plugin->meta.name) { - pj_ks (pj, "name", plugin->meta.name); - } - if (plugin->meta.desc) { - pj_ks (pj, "description", plugin->meta.desc); - } - if (plugin->meta.license) { - pj_ks (pj, "license", plugin->meta.license); - } - if (plugin->meta.version) { - pj_ks (pj, "version", plugin->meta.version); - } - if (plugin->uris) { - char *uri; - char *uris = strdup (plugin->uris); - RList *plist = r_str_split_list (uris, ",", 0); - RListIter *piter; - pj_k (pj, "uris"); - pj_a (pj); - r_list_foreach (plist, piter, uri) { - pj_s (pj, uri); - } - pj_end (pj); - r_list_free (plist); - free (uris); - } - if (plugin->meta.version) { - pj_ks (pj, "version", plugin->meta.version); - } - if (plugin->meta.author) { - pj_ks (pj, "author", plugin->meta.author); - } - pj_end (pj); - n++; - } - pj_end (pj); - io->cb_printf ("%s", pj_string (pj)); - pj_free (pj); - return n; -} - R_API int r_io_plugin_read(RIODesc *desc, ut8 *buf, int len) { if (!buf || !desc || !desc->plugin || len < 1 || !(desc->perm & R_PERM_R)) { return 0; diff --git a/libr/main/radare2.c b/libr/main/radare2.c index aabbd8056fac5..b0e491200b2f1 100644 --- a/libr/main/radare2.c +++ b/libr/main/radare2.c @@ -1120,10 +1120,11 @@ R_API int r_main_radare2(int argc, const char **argv) { if (mr.quiet_leak) { exit (0); } + const char *arg = argv[opt.ind]; if (mr.json) { - r_io_plugin_list_json (r->io); + r_core_list_io (r, arg, 'j'); } else { - r_io_plugin_list (r->io); + r_core_list_io (r, arg, 0); } r_cons_flush (); mainr2_fini (&mr); diff --git a/libr/main/rafind2.c b/libr/main/rafind2.c index 3b7b617406b98..277b9d79713ba 100644 --- a/libr/main/rafind2.c +++ b/libr/main/rafind2.c @@ -579,11 +579,14 @@ R_API int r_main_rafind2(int argc, const char **argv) { } } if (ro.pluglist) { + // list search plugins when implemented +#if 0 if (ro.json) { r_io_plugin_list_json (ro.io); } else { r_io_plugin_list (ro.io); } +#endif r_cons_flush (); return 0; } diff --git a/libr/util/lib.c b/libr/util/lib.c index 177fd6e89a802..958f4d5d1de3e 100644 --- a/libr/util/lib.c +++ b/libr/util/lib.c @@ -527,7 +527,7 @@ R_API void r_lib_list(RLib *lib) { } // TODO: pj_o should be inside rlibmetapj -R_API void r_lib_meta_pj(PJ *pj, RPluginMeta *meta) { +R_API void r_lib_meta_pj(PJ *pj, const RPluginMeta *meta) { R_RETURN_IF_FAIL (pj && meta); if (meta->name) { pj_ks (pj, "name", meta->name);