Skip to content

Commit

Permalink
core: extract RzCorePlugins from RzCmd to RzCore
Browse files Browse the repository at this point in the history
  • Loading branch information
ret2libc committed Mar 1, 2021
1 parent c8cb2dd commit 38e8b2f
Show file tree
Hide file tree
Showing 12 changed files with 83 additions and 125 deletions.
2 changes: 1 addition & 1 deletion librz/core/cmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6638,7 +6638,7 @@ RZ_API void rz_core_cmd_init(RzCore *core) {
{ "z", "zignatures", rz_cmd_zign },
};

core->rcmd = rz_cmd_new(!!core->cons, true);
core->rcmd = rz_cmd_new(!!core->cons);
core->rcmd->macro.user = core;
core->rcmd->macro.num = core->num;
core->rcmd->macro.cmd = core_cmd0_wrapper;
Expand Down
36 changes: 1 addition & 35 deletions librz/core/cmd_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ RZ_API void rz_cmd_alias_init(RzCmd *cmd) {
cmd->aliases.values = NULL;
}

RZ_API RzCmd *rz_cmd_new(bool has_cons, bool add_core_plugins) {
RZ_API RzCmd *rz_cmd_new(bool has_cons) {
int i;
RzCmd *cmd = RZ_NEW0(RzCmd);
if (!cmd) {
Expand All @@ -175,9 +175,6 @@ RZ_API RzCmd *rz_cmd_new(bool has_cons, bool add_core_plugins) {
cmd->nullcallback = cmd->data = NULL;
cmd->ht_cmds = ht_pp_new0();
cmd->root_cmd_desc = create_cmd_desc(cmd, NULL, RZ_CMD_DESC_TYPE_GROUP, "", &root_help, true);
if (add_core_plugins) {
rz_core_plugin_init(cmd);
}
rz_cmd_macro_init(&cmd->macro);
rz_cmd_alias_init(cmd);
return cmd;
Expand All @@ -192,9 +189,6 @@ RZ_API RzCmd *rz_cmd_free(RzCmd *cmd) {
rz_cmd_alias_free(cmd);
rz_cmd_macro_fini(&cmd->macro);
ht_pp_free(cmd->ht_cmds);
// dinitialize plugin commands
rz_core_plugin_fini(cmd);
rz_list_free(cmd->plist);
rz_list_free(cmd->lcmds);
for (i = 0; i < NCMDS; i++) {
if (cmd->cmds[i]) {
Expand Down Expand Up @@ -417,8 +411,6 @@ RZ_API int rz_cmd_del(RzCmd *cmd, const char *command) {
RZ_API int rz_cmd_call(RzCmd *cmd, const char *input) {
struct rz_cmd_item_t *c;
int ret = -1;
RzListIter *iter;
RzCorePlugin *cp;
rz_return_val_if_fail(cmd && input, -1);
if (!*input) {
if (cmd->nullcallback) {
Expand All @@ -436,12 +428,6 @@ RZ_API int rz_cmd_call(RzCmd *cmd, const char *input) {
input = nstr;
}
}
rz_list_foreach (cmd->plist, iter, cp) {
if (cp->call && cp->call(cmd->data, input)) {
free(nstr);
return true;
}
}
if (!*input) {
free(nstr);
return -1;
Expand Down Expand Up @@ -626,26 +612,6 @@ static RzCmdStatus call_cd(RzCmd *cmd, RzCmdDesc *cd, RzCmdParsedArgs *args) {
}

RZ_API RzCmdStatus rz_cmd_call_parsed_args(RzCmd *cmd, RzCmdParsedArgs *args) {
RzCmdStatus res = RZ_CMD_STATUS_INVALID;

// As old RzCorePlugin do not register new commands in RzCmd, we have no
// way of knowing if one of those is able to handle the input, so we
// have to pass the input to all of them before looking into the
// RzCmdDesc tree
RzListIter *iter;
RzCorePlugin *cp;
char *exec_string = rz_cmd_parsed_args_execstr(args);
rz_list_foreach (cmd->plist, iter, cp) {
if (cp->call && cp->call(cmd->data, exec_string)) {
res = RZ_CMD_STATUS_OK;
break;
}
}
RZ_FREE(exec_string);
if (res == RZ_CMD_STATUS_OK) {
return res;
}

RzCmdDesc *cd = rz_cmd_get_desc(cmd, rz_cmd_parsed_args_cmd(args));
if (!cd) {
return RZ_CMD_STATUS_NONEXISTINGCMD;
Expand Down
4 changes: 2 additions & 2 deletions librz/core/cmd_plugins.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ RZ_IPI int rz_cmd_plugins(void *data, const char *input) {
case 'j': {
rz_cons_printf("[");
bool is_first_element = true;
rz_list_foreach (core->rcmd->plist, iter, cp) {
rz_list_foreach (core->plugins, iter, cp) {
rz_cons_printf("%s{\"Name\":\"%s\",\"Description\":\"%s\"}",
is_first_element ? "" : ",", cp->name, cp->desc);
is_first_element = false;
Expand All @@ -81,7 +81,7 @@ RZ_IPI int rz_cmd_plugins(void *data, const char *input) {
}
case 0:
rz_lib_list(core->lib);
rz_list_foreach (core->rcmd->plist, iter, cp) {
rz_list_foreach (core->plugins, iter, cp) {
rz_cons_printf("%s: %s\n", cp->name, cp->desc);
}
break;
Expand Down
3 changes: 3 additions & 0 deletions librz/core/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,8 @@ RZ_API bool rz_core_init(RzCore *core) {
core->offset = 0LL;
core->prompt_offset = 0LL;
rz_core_cmd_init(core);
rz_core_plugin_init(core);

core->dbg = rz_debug_new(true);

rz_io_bind(core->io, &(core->dbg->iob));
Expand Down Expand Up @@ -2680,6 +2682,7 @@ RZ_API void rz_core_fini(RzCore *c) {
if (!c) {
return;
}
rz_core_plugin_fini(c);
rz_core_task_break_all(&c->tasks);
rz_core_task_join(&c->tasks, NULL, -1);
rz_core_wait(c);
Expand Down
53 changes: 20 additions & 33 deletions librz/core/cplugin.c
Original file line number Diff line number Diff line change
@@ -1,61 +1,48 @@
// SPDX-License-Identifier: LGPL-3.0-only

/* covardly copied from rz_cmd */

#include <config.h>
#include <rz_core.h>
#include <rz_cmd.h>
#include <rz_list.h>
#include <stdio.h>

static RzCorePlugin *cmd_static_plugins[] = {
static RzCorePlugin *core_static_plugins[] = {
RZ_CORE_STATIC_PLUGINS
};

RZ_API int rz_core_plugin_fini(RzCmd *cmd) {
RZ_API bool rz_core_plugin_fini(RzCore *core) {
rz_return_val_if_fail(core->plugins, false);

RzListIter *iter;
RzCorePlugin *plugin;
if (!cmd->plist) {
return false;
}
rz_list_foreach (cmd->plist, iter, plugin) {
if (plugin && plugin->fini) {
plugin->fini(cmd, NULL);
rz_list_foreach (core->plugins, iter, plugin) {
if (plugin->fini) {
plugin->fini(core);
}
}
/* empty the list */
rz_list_free(cmd->plist);
cmd->plist = NULL;
rz_list_free(core->plugins);
core->plugins = NULL;
return true;
}

RZ_API int rz_core_plugin_add(RzCmd *cmd, RzCorePlugin *plugin) {
if (!cmd || (plugin && plugin->init && !plugin->init(cmd, NULL))) {
RZ_API bool rz_core_plugin_add(RzCore *core, RzCorePlugin *plugin) {
rz_return_val_if_fail(core, false);
rz_return_val_if_fail(plugin && plugin->init && plugin->name && plugin->author && plugin->license, false);
if (!plugin->init(core)) {
return false;
}
rz_list_append(cmd->plist, plugin);
rz_list_append(core->plugins, plugin);
return true;
}

RZ_API int rz_core_plugin_init(RzCmd *cmd) {
RZ_API bool rz_core_plugin_init(RzCore *core) {
int i;
cmd->plist = rz_list_newf(NULL); // memleak or dblfree
for (i = 0; cmd_static_plugins[i]; i++) {
if (!rz_core_plugin_add(cmd, cmd_static_plugins[i])) {
eprintf("Error loading cmd plugin\n");
core->plugins = rz_list_newf(NULL); // memleak or dblfree
for (i = 0; core_static_plugins[i]; i++) {
if (!rz_core_plugin_add(core, core_static_plugins[i])) {
eprintf("Error loading core plugin\n");
return false;
}
}
return true;
}

RZ_API int rz_core_plugin_check(RzCmd *cmd, const char *a0) {
RzListIter *iter;
RzCorePlugin *cp;
rz_list_foreach (cmd->plist, iter, cp) {
if (cp->call(NULL, a0)) {
return true;
}
}
return false;
}
}
14 changes: 12 additions & 2 deletions librz/core/libs.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,20 @@
static int __lib_##x##_dt(RzLibPlugin *pl, void *p, void *u) { return true; }

// XXX api consistency issues
static int __lib_core_cb(RzLibPlugin *pl, void *user, void *data) {
struct rz_core_plugin_t *hand = (struct rz_core_plugin_t *)data;
RzCore *core = (RzCore *)user;
pl->free = NULL;
rz_core_plugin_add(core, hand);
return true;
}

static int __lib_core_dt(RzLibPlugin *pl, void *p, void *u) {
return true;
}

#define rz_io_add rz_io_plugin_add
CB_COPY(io, io)
#define rz_core_add rz_core_plugin_add
CB(core, rcmd)
#define rz_debug_add rz_debug_plugin_add
CB(debug, dbg)
#define rz_bp_add rz_bp_plugin_add
Expand Down
14 changes: 3 additions & 11 deletions librz/core/p/core_java.c
Original file line number Diff line number Diff line change
Expand Up @@ -1643,12 +1643,8 @@ static const RzCmdDescHelp cmd_java_help = {
.args = cmd_java_args,
};

static int rz_cmd_java_init_handler(void *user, const char *unused) {
RzCmd *cmd = (RzCmd *)user;
if (!cmd) {
return false;
}

static bool rz_cmd_java_init_handler(RzCore *core) {
RzCmd *cmd = core->rcmd;
RzCmdDesc *root_cd = rz_cmd_get_root(cmd);
if (!root_cd) {
return false;
Expand All @@ -1659,17 +1655,13 @@ static int rz_cmd_java_init_handler(void *user, const char *unused) {
return cmd_java_cd != NULL;
}

static int rz_cmd_java_call_handler(void *user, const char *unused) {
return false;
}

// PLUGIN Definition Info
RzCorePlugin rz_core_plugin_java = {
.name = "java",
.desc = "Suite of java commands, java help for more info",
.author = "RizinOrg",
.license = "Apache",
.init = rz_cmd_java_init_handler,
.call = rz_cmd_java_call_handler,
};

#ifndef RZ_PLUGIN_INCORE
Expand Down
20 changes: 1 addition & 19 deletions librz/include/rz_cmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -397,7 +397,6 @@ typedef struct rz_cmd_t {
RzCmdItem *cmds[UT8_MAX];
RzCmdMacro macro;
RzList *lcmds;
RzList *plist;
RzCmdAlias aliases;
void *language; // used to store TSLanguage *
HtUP *ts_symbols_ht;
Expand All @@ -420,27 +419,10 @@ typedef struct rz_cmd_descriptor_t {
struct rz_cmd_descriptor_t *sub[127];
} RzCmdDescriptor;

// TODO: move into rz_core.h
typedef struct rz_core_plugin_t {
const char *name;
const char *desc;
const char *license;
const char *author;
const char *version;
RzCmdCb call; // returns true if command was handled, false otherwise.
RzCmdCb init;
RzCmdCb fini;
} RzCorePlugin;

typedef bool (*RzCmdForeachNameCb)(RzCmd *cmd, const RzCmdDesc *desc, void *user);

#ifdef RZ_API
RZ_API int rz_core_plugin_init(RzCmd *cmd);
RZ_API int rz_core_plugin_add(RzCmd *cmd, RzCorePlugin *plugin);
RZ_API int rz_core_plugin_check(RzCmd *cmd, const char *a0);
RZ_API int rz_core_plugin_fini(RzCmd *cmd);

RZ_API RzCmd *rz_cmd_new(bool has_cons, bool add_core_plugins);
RZ_API RzCmd *rz_cmd_new(bool has_cons);
RZ_API RzCmd *rz_cmd_free(RzCmd *cmd);
RZ_API int rz_cmd_set_data(RzCmd *cmd, void *data);
RZ_API int rz_cmd_add(RzCmd *cmd, const char *command, RzCmdCb callback);
Expand Down
18 changes: 18 additions & 0 deletions librz/include/rz_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,19 @@ typedef enum {
RZ_CORE_VISUAL_MODE_CD = 4
} RzCoreVisualMode;

typedef bool (*RzCorePluginInit)(RzCore *core);
typedef bool (*RzCorePluginFini)(RzCore *core);

typedef struct rz_core_plugin_t {
const char *name;
const char *desc;
const char *license;
const char *author;
const char *version;
RzCorePluginInit init;
RzCorePluginFini fini;
} RzCorePlugin;

typedef struct rz_core_rtr_host_t {
int proto;
char host[512];
Expand Down Expand Up @@ -251,6 +264,7 @@ typedef struct rz_core_seek_history_t {

struct rz_core_t {
RzBin *bin;
RzList *plugins; ///< List of registered core plugins
RzConfig *config;
ut64 offset; // current seek
ut64 prompt_offset; // temporarily set to offset to have $$ in expressions always stay the same during temp seeks
Expand Down Expand Up @@ -382,6 +396,10 @@ typedef struct rz_core_cmpwatch_t {
typedef int (*RzCoreSearchCallback)(RzCore *core, ut64 from, ut8 *buf, int len);

#ifdef RZ_API
RZ_API bool rz_core_plugin_init(RzCore *core);
RZ_API bool rz_core_plugin_add(RzCore *core, RzCorePlugin *plugin);
RZ_API bool rz_core_plugin_fini(RzCore *core);

//#define rz_core_ncast(x) (RzCore*)(size_t)(x)
RZ_API RzList *rz_core_list_themes(RzCore *core);
RZ_API char *rz_core_get_theme(void);
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_autocmplt.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ static RzCore *fake_core_new(void) {
mu_assert_notnull(cf, "file should be opened");
rz_core_bin_load(core, "bins/elf/hello_world", 0);
rz_cmd_free(core->rcmd);
RzCmd *cmd = rz_cmd_new(true, false);
RzCmd *cmd = rz_cmd_new(true);
mu_assert_notnull(cmd, "cmd should be created");
RzCmdDesc *root = rz_cmd_get_root(cmd);
mu_assert_notnull(root, "root should be present");
Expand Down
Loading

0 comments on commit 38e8b2f

Please sign in to comment.