Fix removing core plugins from Lc after L- ##core

This commit is contained in:
Luc Tielen 2023-06-23 14:50:26 +02:00 committed by pancake
parent 64e2b0efd5
commit a40d2e8006
33 changed files with 306 additions and 82 deletions

View File

@ -138,13 +138,18 @@ R_API RAnal *r_anal_new(void) {
anal->plugins = r_list_newf ((RListFree) r_anal_plugin_free);
if (anal->plugins) {
for (i = 0; anal_static_plugins[i]; i++) {
r_anal_add (anal, anal_static_plugins[i]);
r_anal_plugin_add (anal, anal_static_plugins[i]);
}
}
R_DIRTY (anal);
return anal;
}
R_API bool r_anal_plugin_remove(RAnal *anal, RAnalPlugin *plugin) {
// R2_590 TODO
return true;
}
R_API void r_anal_plugin_free(RAnalPlugin *p) {
if (p && p->fini) {
p->fini (NULL);
@ -209,7 +214,7 @@ R_API bool r_esil_use(RAnal *anal, const char *name) {
return false;
}
R_API int r_anal_add(RAnal *anal, RAnalPlugin *foo) {
R_API int r_anal_plugin_add(RAnal *anal, RAnalPlugin *foo) {
if (foo->init) {
foo->init (anal->user);
}

View File

@ -23,7 +23,7 @@ R_API RArch *r_arch_new(void) {
a->cfg = r_arch_config_new ();
ut32 i = 0;
while (arch_static_plugins[i]) {
r_arch_add (a, (RArchPlugin*)arch_static_plugins[i++]);
r_arch_plugin_add (a, (RArchPlugin*)arch_static_plugins[i++]);
}
return a;
}
@ -201,7 +201,7 @@ R_API bool r_arch_set_arch(RArch *arch, char *archname) {
return true;
}
R_API bool r_arch_add(RArch *a, RArchPlugin *ap) {
R_API bool r_arch_plugin_add(RArch *a, RArchPlugin *ap) {
r_return_val_if_fail (a && ap, false);
if (!ap->meta.name || !ap->arch) {
return false;
@ -209,6 +209,11 @@ R_API bool r_arch_add(RArch *a, RArchPlugin *ap) {
return r_list_append (a->plugins, ap) != NULL;
}
R_API bool r_arch_plugin_remove(RArch *arch, RArchPlugin *ap) {
// R2_590 TODO
return true;
}
R_API bool r_arch_del(RArch *arch, const char *name) {
r_return_val_if_fail (arch && arch->plugins && name, false);
RArchPlugin *ap = find_bestmatch (arch, NULL, name, false);

View File

@ -59,7 +59,7 @@ R_API RParse *r_parse_new(void) {
p->localvar_only = false;
size_t i;
for (i = 0; parse_static_plugins[i]; i++) {
r_parse_add (p, parse_static_plugins[i]);
r_parse_plugin_add (p, parse_static_plugins[i]);
}
return p;
}
@ -71,7 +71,7 @@ R_API void r_parse_free(RParse *p) {
}
}
R_API bool r_parse_add(RParse *p, RParsePlugin *foo) {
R_API bool r_parse_plugin_add(RParse *p, RParsePlugin *foo) {
r_return_val_if_fail (p && foo, false);
bool itsFine = foo->init? foo->init (p, p->user): true;
if (itsFine) {
@ -80,6 +80,10 @@ R_API bool r_parse_add(RParse *p, RParsePlugin *foo) {
return true;
}
R_API bool r_parse_plugin_remove(RParse *p, RParsePlugin *plugin) {
return true;
}
static char *predotname(const char *name) {
char *sname = strdup (name);
char *dot = strchr (sname, '.');

View File

@ -418,7 +418,7 @@ static void r_bin_plugin_free(RBinPlugin *p) {
}
// rename to r_bin_plugin_add like the rest
R_API bool r_bin_add(RBin *bin, RBinPlugin *foo) {
R_API bool r_bin_plugin_add(RBin *bin, RBinPlugin *foo) {
RListIter *it;
RBinPlugin *plugin;
@ -438,6 +438,11 @@ R_API bool r_bin_add(RBin *bin, RBinPlugin *foo) {
return true;
}
R_API bool r_bin_plugin_remove(RBin *bin, RBinPlugin *plugin) {
// R2_590 TODO
return true;
}
R_API bool r_bin_ldr_add(RBin *bin, RBinLdrPlugin *foo) {
RListIter *it;
RBinLdrPlugin *ldr;
@ -851,7 +856,7 @@ R_API RBin *r_bin_new(void) {
/* bin parsers */
bin->binfiles = r_list_newf ((RListFree)r_bin_file_free);
for (i = 0; bin_static_plugins[i]; i++) {
r_bin_add (bin, bin_static_plugins[i]);
r_bin_plugin_add (bin, bin_static_plugins[i]);
}
/* extractors */
bin->binxtrs = r_list_new ();

View File

@ -38,6 +38,11 @@ R_API int r_bp_plugin_add(RBreakpoint *bp, RBreakpointPlugin *foo) {
return true;
}
R_API int r_bp_plugin_remove(RBreakpoint *bp, RBreakpointPlugin *plugin) {
// R2_590 TODO
return true;
}
R_API int r_bp_use(RBreakpoint *bp, const char *name, int bits) {
RListIter *iter;
bp->bits = bits;

View File

@ -31,6 +31,20 @@ R_API bool r_core_plugin_add(RCmd *cmd, RCorePlugin *plugin) {
return true;
}
R_API bool r_core_plugin_remove(RCmd *cmd, RCorePlugin *plugin) {
const char *name = plugin->name;
RListIter *iter, *iter2;
RCorePlugin *p;
r_list_foreach_safe (cmd->plist, iter, iter2, p) {
if (p && !strcmp (name, p->name)) {
r_list_delete (cmd->plist, iter);
return true;
}
}
return false;
}
R_API bool r_core_plugin_init(RCmd *cmd) {
r_return_val_if_fail (cmd, false);
size_t i;

View File

@ -8,10 +8,14 @@
struct r_ ## x ## _plugin_t *hand = (struct r_ ## x ## _plugin_t *)data;\
RCore *core = (RCore *) user;\
pl->free = NULL; \
r_ ## x ## _add (core->y, hand);\
r_ ## x ## _plugin_add (core->y, hand);\
return true;\
}\
static int __lib_ ## x ## _dt (RLibPlugin * pl, void *p, void *u) { return true; }
static int __lib_ ## x ## _dt (RLibPlugin * pl, void *user, void *data) { \
struct r_ ## x ## _plugin_t *hand = (struct r_ ## x ## _plugin_t *)data; \
RCore *core = (RCore *) user; \
return r_ ## x ## _plugin_remove (core->y, hand); \
}
// TODO: deprecate this
#define CB_COPY(x, y)\
@ -21,23 +25,21 @@
RCore *core = (RCore *) user;\
instance = R_NEW (struct r_ ## x ## _plugin_t);\
memcpy (instance, hand, sizeof (struct r_ ## x ## _plugin_t));\
r_ ## x ## _add (core->y, instance);\
r_ ## x ## _plugin_add (core->y, instance);\
return true;\
}\
static int __lib_ ## x ## _dt (RLibPlugin * pl, void *p, void *u) { return true; }
static int __lib_ ## x ## _dt (RLibPlugin *pl, void *user, void *data) { \
struct r_ ## x ## _plugin_t *hand = (struct r_ ## x ## _plugin_t *)data; \
RCore *core = (RCore *) user; \
return r_ ## x ## _plugin_remove (core->y, hand); \
}
// XXX R2_590 : api consistency issues
#define r_io_add r_io_plugin_add
CB_COPY (io, io)
#define r_core_add r_core_plugin_add
CB (core, rcmd)
#define r_debug_add r_debug_plugin_add
CB (debug, dbg)
#define r_bp_add r_bp_plugin_add
CB (bp, dbg->bp)
CB (lang, lang)
CB (anal, anal)
#define r_esil_add r_esil_plugin_add
CB (esil, anal->esil)
CB (parse, parser)
CB (bin, bin)

View File

@ -100,6 +100,11 @@ R_API bool r_debug_plugin_add(RDebug *dbg, RDebugPlugin *foo) {
return true;
}
R_API bool r_debug_plugin_remove(RDebug *dbg, RDebugPlugin *plugin) {
// R2_590 TODO
return true;
}
R_API bool r_debug_plugin_set_reg_profile(RDebug *dbg, const char *profile) {
char *str = r_file_slurp (profile, NULL);
if (!str) {

View File

@ -75,7 +75,7 @@ R_API REgg *r_egg_new(void) {
}
egg->plugins = r_list_new ();
for (i = 0; egg_static_plugins[i]; i++) {
r_egg_add (egg, egg_static_plugins[i]);
r_egg_plugin_add (egg, egg_static_plugins[i]);
}
return egg;
@ -84,7 +84,7 @@ beach:
return NULL;
}
R_API bool r_egg_add(REgg *a, REggPlugin *foo) {
R_API bool r_egg_plugin_add(REgg *a, REggPlugin *foo) {
r_return_val_if_fail (a && foo, false);
RListIter *iter;
// TODO: cache foo->name length and use memcmp instead of strcmp
@ -101,6 +101,11 @@ R_API bool r_egg_add(REgg *a, REggPlugin *foo) {
return true;
}
R_API bool r_egg_plugin_remove(REgg *a, REggPlugin *plugin) {
// R2_590 TODO
return true;
}
R_API char *r_egg_tostring(REgg *egg) {
r_return_val_if_fail (egg, NULL);
return r_buf_tostring (egg->buf);

View File

@ -42,6 +42,11 @@ R_API bool r_esil_plugin_add(REsil *esil, REsilPlugin *plugin) {
return true;
}
R_API bool r_esil_plugin_remove(REsil *esil, REsilPlugin *plugin) {
// R2_590 TODO
return true;
}
static REsilActivePlugin *_get_active_plugin(REsil *esil, const char *name) {
RListIter *iter;
REsilActivePlugin *eap;

View File

@ -65,7 +65,7 @@ R_API R_MUSTUSE RFS* r_fs_new(void) {
continue;
}
memcpy (static_plugin, fs_static_plugins[i], sizeof (RFSPlugin));
r_fs_add (fs, static_plugin);
r_fs_plugin_add (fs, static_plugin);
free (static_plugin);
}
}
@ -95,8 +95,8 @@ R_API void r_fs_free(RFS* fs) {
}
/* plugins */
R_API void r_fs_add(RFS* fs, RFSPlugin* p) {
r_return_if_fail (fs && p);
R_API bool r_fs_plugin_add(RFS* fs, RFSPlugin* p) {
r_return_val_if_fail (fs && p, false);
if (p->init) {
p->init ();
}
@ -104,7 +104,14 @@ R_API void r_fs_add(RFS* fs, RFSPlugin* p) {
if (sp) {
memcpy (sp, p, sizeof (RFSPlugin));
r_list_append (fs->plugins, sp);
return true;
}
return false;
}
R_API bool r_fs_plugin_remove(RFS *fs, RFSPlugin *p) {
// R2_590 TODO
return true;
}
R_API void r_fs_del(RFS* fs, RFSPlugin* p) {

View File

@ -1008,7 +1008,8 @@ R_API void r_anal_purge(RAnal *anal);
R_API void r_anal_free(RAnal *r);
R_API void r_anal_set_user_ptr(RAnal *anal, void *user);
R_API void r_anal_plugin_free(RAnalPlugin *p);
R_API int r_anal_add(RAnal *anal, RAnalPlugin *foo);
R_API int r_anal_plugin_add(RAnal *anal, RAnalPlugin *plugin);
R_API bool r_anal_plugin_remove(RAnal *anal, RAnalPlugin *plugin);
R_API int r_anal_archinfo(RAnal *anal, int query);
R_API bool r_anal_is_aligned(RAnal *anal, const ut64 addr);
R_API bool r_anal_use(RAnal *anal, const char *name);

View File

@ -208,7 +208,8 @@ R_API RArch *r_arch_new(void);
R_API bool r_arch_use(RArch *arch, RArchConfig *config, const char *name);
// arch plugins management apis
R_API bool r_arch_add(RArch *arch, RArchPlugin *ap);
R_API bool r_arch_plugin_add(RArch *arch, RArchPlugin *ap);
R_API bool r_arch_plugin_remove(RArch *arch, RArchPlugin *ap);
R_API bool r_arch_del(RArch *arch, const char *name);
R_API void r_arch_free(RArch *arch);

View File

@ -699,7 +699,8 @@ R_API RBinFile *r_bin_file_open(RBin *bin, const char *file, RBinFileOptions *op
// plugins/bind functions
R_API void r_bin_bind(RBin *b, RBinBind *bnd);
R_API bool r_bin_add(RBin *bin, RBinPlugin *foo);
R_API bool r_bin_plugin_add(RBin *bin, RBinPlugin *plugin);
R_API bool r_bin_plugin_remove(RBin *bin, RBinPlugin *plugin);
R_API bool r_bin_xtr_add(RBin *bin, RBinXtrPlugin *foo);
R_API bool r_bin_ldr_add(RBin *bin, RBinLdrPlugin *foo);
R_API void r_bin_list(RBin *bin, PJ *pj, int format);

View File

@ -113,7 +113,8 @@ R_API RBreakpoint *r_bp_free(RBreakpoint *bp);
R_API bool r_bp_del(RBreakpoint *bp, ut64 addr);
R_API bool r_bp_del_all(RBreakpoint *bp);
R_API int r_bp_plugin_add(RBreakpoint *bp, RBreakpointPlugin *foo);
R_API int r_bp_plugin_add(RBreakpoint *bp, RBreakpointPlugin *plugin);
R_API int r_bp_plugin_remove(RBreakpoint *bp, RBreakpointPlugin *plugin);
R_API int r_bp_use(RBreakpoint *bp, const char *name, int bits);
R_API int r_bp_plugin_del(RBreakpoint *bp, const char *name);
R_API void r_bp_plugin_list(RBreakpoint *bp);

View File

@ -32,7 +32,7 @@
#include <r_bind.h>
#include <r_codemeta.h>
// TODO: thois var should be 1 at some point :D
// TODO: this var should be 1 at some point :D
#define SHELLFILTER 0
#ifdef __cplusplus
@ -992,6 +992,7 @@ extern RCorePlugin r_core_plugin_a2f;
extern RCorePlugin r_core_plugin_sixref;
R_API bool r_core_plugin_init(RCmd *cmd);
R_API bool r_core_plugin_add(RCmd *cmd, RCorePlugin *plugin);
R_API bool r_core_plugin_remove(RCmd *cmd, RCorePlugin *plugin);
R_API bool r_core_plugin_check(RCmd *cmd, const char *a0);
R_API void r_core_plugin_fini(RCmd *cmd);

View File

@ -513,7 +513,8 @@ R_API int r_debug_kill_setup(RDebug *dbg, int sig, int action);
R_API void r_debug_plugin_init(RDebug *dbg);
R_API int r_debug_plugin_set(RDebug *dbg, const char *str);
R_API bool r_debug_plugin_list(RDebug *dbg, int mode);
R_API bool r_debug_plugin_add(RDebug *dbg, RDebugPlugin *foo);
R_API bool r_debug_plugin_add(RDebug *dbg, RDebugPlugin *plugin);
R_API bool r_debug_plugin_remove(RDebug *dbg, RDebugPlugin *plugin);
R_API bool r_debug_plugin_set_reg_profile(RDebug *dbg, const char *str);
/* memory */

View File

@ -187,7 +187,8 @@ R_API void r_egg_lang_init(REgg *egg);
R_API void r_egg_lang_free(REgg *egg);
R_API char *r_egg_tostring(REgg *egg);
R_API void r_egg_free(REgg *egg);
R_API bool r_egg_add(REgg *a, REggPlugin *foo);
R_API bool r_egg_plugin_add(REgg *a, REggPlugin *plugin);
R_API bool r_egg_plugin_remove(REgg *a, REggPlugin *plugin);
R_API void r_egg_reset(REgg *egg);
R_API bool r_egg_setup(REgg *egg, const char *arch, int bits, int endian, const char *os);
R_API bool r_egg_include(REgg *egg, const char *file, int format);

View File

@ -326,6 +326,7 @@ R_API void r_esil_handlers_fini(REsil *esil);
R_API void r_esil_plugins_init(REsil *esil);
R_API void r_esil_plugins_fini(REsil *esil);
R_API bool r_esil_plugin_add(REsil *esil, REsilPlugin *plugin);
R_API bool r_esil_plugin_remove(REsil *esil, REsilPlugin *plugin);
R_API bool r_esil_plugin_activate(REsil *esil, const char *name);
R_API void r_esil_plugin_deactivate(REsil *esil, const char *name);

View File

@ -136,7 +136,8 @@ R_API RFS *r_fs_new(void);
R_API void r_fs_free(RFS* fs);
R_API void r_fs_view(RFS* fs, int view);
R_API void r_fs_add(RFS *fs, RFSPlugin *p);
R_API bool r_fs_plugin_add(RFS *fs, RFSPlugin *p);
R_API bool r_fs_plugin_remove(RFS *fs, RFSPlugin *p);
R_API void r_fs_del(RFS *fs, RFSPlugin *p);
R_API RFSRoot *r_fs_mount(RFS* fs, const char *fstype, const char *path, ut64 delta);

View File

@ -454,6 +454,7 @@ 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);

View File

@ -69,7 +69,8 @@ typedef struct r_lang_session_t {
R_API RLang *r_lang_new(void);
R_API void r_lang_free(RLang *lang);
R_API bool r_lang_setup(RLang *lang);
R_API bool r_lang_add(RLang *lang, RLangPlugin *foo);
R_API bool r_lang_plugin_add(RLang *lang, RLangPlugin *plugin);
R_API bool r_lang_plugin_remove(RLang *lang, RLangPlugin *plugin);
R_API void r_lang_list(RLang *lang, int mode);
R_API bool r_lang_use(RLang *lang, const char *name);
R_API bool r_lang_use_plugin(RLang *lang, RLangPlugin *h);
@ -82,7 +83,6 @@ R_API bool r_lang_run_file(RLang *lang, const char *file);
R_API bool r_lang_prompt(RLang *lang);
R_API RLangPlugin *r_lang_get_by_name(RLang *lang, const char *name);
R_API RLangPlugin *r_lang_get_by_extension(RLang *lang, const char *ext);
// TODO: rename r_Lang_add for r_lang_plugin_add
R_API bool r_lang_define(RLang *lang, const char *type, const char *name, void *value);
R_API void r_lang_undef(RLang *lang, const char *name);

View File

@ -62,7 +62,8 @@ R_API void r_parse_free(RParse *p);
/* plugins */
R_API void r_parse_set_user_ptr(RParse *p, void *user);
R_API bool r_parse_add(RParse *p, RParsePlugin *foo);
R_API bool r_parse_plugin_add(RParse *p, RParsePlugin *plugin);
R_API bool r_parse_plugin_remove(RParse *p, RParsePlugin *plugin);
R_API bool r_parse_use(RParse *p, const char *name);
/* action */

View File

@ -16,6 +16,11 @@ R_API bool r_io_plugin_add(RIO *io, RIOPlugin *plugin) {
return true;
}
R_API bool r_io_plugin_remove(RIO *io, RIOPlugin *plugin) {
// R2_590 TODO
return true;
}
R_API bool r_io_plugin_init(RIO *io) {
RIOPlugin *static_plugin;
int i;

View File

@ -47,24 +47,24 @@ R_API RLang *r_lang_new(void) {
lang->cb_printf = (PrintfCallback)printf;
#if HAVE_SYSTEM
#if R2__UNIX__
r_lang_add (lang, &r_lang_plugin_s);
r_lang_add (lang, &r_lang_plugin_c);
r_lang_add (lang, &r_lang_plugin_cpipe);
r_lang_plugin_add (lang, &r_lang_plugin_s);
r_lang_plugin_add (lang, &r_lang_plugin_c);
r_lang_plugin_add (lang, &r_lang_plugin_cpipe);
#endif
r_lang_add (lang, &r_lang_plugin_v);
r_lang_add (lang, &r_lang_plugin_vala);
r_lang_add (lang, &r_lang_plugin_rust);
r_lang_add (lang, &r_lang_plugin_zig);
r_lang_add (lang, &r_lang_plugin_pipe);
r_lang_plugin_add (lang, &r_lang_plugin_v);
r_lang_plugin_add (lang, &r_lang_plugin_vala);
r_lang_plugin_add (lang, &r_lang_plugin_rust);
r_lang_plugin_add (lang, &r_lang_plugin_zig);
r_lang_plugin_add (lang, &r_lang_plugin_pipe);
#endif
r_lang_add (lang, &r_lang_plugin_go);
r_lang_add (lang, &r_lang_plugin_poke);
r_lang_add (lang, &r_lang_plugin_spp);
r_lang_add (lang, &r_lang_plugin_lib);
r_lang_add (lang, &r_lang_plugin_asm);
r_lang_add (lang, &r_lang_plugin_qjs);
r_lang_add (lang, &r_lang_plugin_tsc);
r_lang_add (lang, &r_lang_plugin_nim);
r_lang_plugin_add (lang, &r_lang_plugin_go);
r_lang_plugin_add (lang, &r_lang_plugin_poke);
r_lang_plugin_add (lang, &r_lang_plugin_spp);
r_lang_plugin_add (lang, &r_lang_plugin_lib);
r_lang_plugin_add (lang, &r_lang_plugin_asm);
r_lang_plugin_add (lang, &r_lang_plugin_qjs);
r_lang_plugin_add (lang, &r_lang_plugin_tsc);
r_lang_plugin_add (lang, &r_lang_plugin_nim);
return lang;
}
@ -148,7 +148,7 @@ R_API bool r_lang_setup(RLang *lang) {
return false;
}
R_API bool r_lang_add(RLang *lang, RLangPlugin *foo) {
R_API bool r_lang_plugin_add(RLang *lang, RLangPlugin *foo) {
if (foo && !r_lang_get_by_name (lang, foo->name)) {
bool supported = true;
if (foo->init) {
@ -166,6 +166,10 @@ R_API bool r_lang_add(RLang *lang, RLangPlugin *foo) {
return false;
}
R_API bool r_lang_plugin_remove(RLang *lang, RLangPlugin *plugin) {
return true;
}
/* TODO: deprecate all list methods */
R_API void r_lang_list(RLang *lang, int mode) {
RListIter *iter;

View File

@ -24,6 +24,7 @@ typedef struct qjs_core_plugin {
R_GENERATE_VEC_IMPL_FOR(CorePlugin, QjsCorePlugin);
typedef struct qjs_arch_plugin_t {
char *name;
char *arch;
R_BORROW JSContext *ctx;
JSValue decode_func;
@ -45,6 +46,7 @@ static void core_plugin_fini(QjsCorePlugin *cp, void *user) {
}
static void arch_plugin_fini(QjsArchPlugin *ap, void *user) {
free (ap->name);
free (ap->arch);
}
@ -96,6 +98,7 @@ static bool plugin_manager_remove_core_plugin(QjsPluginManager *pm, const char *
}
if (found) {
pm->core->lang->cmdf (pm->core, "L-%s", name);
RVecCorePlugin_remove (&pm->core_plugins, i, core_plugin_fini, NULL);
return true;
}
@ -103,13 +106,14 @@ static bool plugin_manager_remove_core_plugin(QjsPluginManager *pm, const char *
return false;
}
static void plugin_manager_add_arch_plugin(QjsPluginManager *pm, const char *arch,
JSContext *ctx, JSValue decode_func) {
static void plugin_manager_add_arch_plugin(QjsPluginManager *pm, const char *name,
const char *arch, JSContext *ctx, JSValue decode_func) {
r_return_if_fail (pm);
QjsArchPlugin *ap = RVecArchPlugin_emplace_back (&pm->arch_plugins);
if (ap) {
ap->arch = arch? strdup (arch): NULL;
ap->name = strdup (name);
ap->arch = strdup (arch);
ap->ctx = ctx;
ap->decode_func = decode_func;
}
@ -145,6 +149,8 @@ static bool plugin_manager_remove_arch_plugin(QjsPluginManager *pm, const char *
}
if (found) {
// XXX Can arch plugins be properly removed with L-name?
pm->core->lang->cmdf (pm->core, "L-%s", ap->name);
RVecArchPlugin_remove (&pm->arch_plugins, i, arch_plugin_fini, NULL);
return true;
}
@ -152,6 +158,25 @@ static bool plugin_manager_remove_arch_plugin(QjsPluginManager *pm, const char *
return false;
}
static bool plugin_manager_remove_plugin(QjsPluginManager *pm, const char *type, const char *plugin_id) {
r_return_val_if_fail (pm, false);
if (R_STR_ISNOTEMPTY (type)) {
if (!strcmp (type, "core")) {
return plugin_manager_remove_core_plugin (pm, plugin_id);
}
if (!strcmp (type, "arch")) {
return plugin_manager_remove_arch_plugin (pm, plugin_id);
}
}
// TODO extend for bin / io / ... plugins
// invalid throw exception here
// return JS_ThrowRangeError(ctx, "invalid r2plugin type");
return false;
}
static void plugin_manager_fini (QjsPluginManager *pm) {
RVecCorePlugin_fini (&pm->core_plugins, core_plugin_fini, NULL);
RVecArchPlugin_fini (&pm->arch_plugins, arch_plugin_fini, NULL);
@ -256,7 +281,7 @@ static JSValue r2plugin(JSContext *ctx, JSValueConst this_val, int argc, JSValue
if (!strcmp (n, "core")) {
return r2plugin_core_load (ctx, this_val, argc, argv);
} else if (!strcmp (n, "arch")) {
return r2plugin_arch (ctx, this_val, argc, argv);
return r2plugin_arch_load (ctx, this_val, argc, argv);
#if 0
} else if (!strcmp (n, "bin")) {
return r2plugin_bin (ctx, this_val, argc, argv);
@ -272,17 +297,16 @@ static JSValue r2plugin(JSContext *ctx, JSValueConst this_val, int argc, JSValue
}
static JSValue r2plugin_unload(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
if (argc != 1 || !JS_IsString (argv[0])) {
if (argc != 2 || !JS_IsString (argv[0]) || !JS_IsString (argv[1])) {
return JS_ThrowRangeError (ctx, "r2.unload takes only one string as argument");
}
JSRuntime *rt = JS_GetRuntime (ctx);
QjsPluginManager *pm = JS_GetRuntimeOpaque (rt);
size_t plen;
const char *name = JS_ToCStringLen2 (ctx, &plen, argv[0], false);
pm->core->lang->cmdf (pm->core, "L-%s", name);
bool res = plugin_manager_remove_core_plugin (pm, name);
// invalid throw exception here
// return JS_ThrowRangeError(ctx, "invalid r2plugin type");
size_t len;
const char *type = JS_ToCStringLen2 (ctx, &len, argv[0], false);
const char *name_or_arch = JS_ToCStringLen2 (ctx, &len, argv[1], false);
const bool res = plugin_manager_remove_plugin (pm, type, name_or_arch);
return JS_NewBool (ctx, res);
}
@ -423,9 +447,9 @@ static JSModuleDef *js_init_module_os(JSContext *ctx) {
}
static const JSCFunctionListEntry js_r2_funcs[] = {
JS_CFUNC_DEF ("cmd", 1, r2cmd),
JS_CFUNC_DEF ("cmd", 1, r2cmd), // XXX deprecate, we have r2.cmd already
JS_CFUNC_DEF ("plugin", 2, r2plugin),
JS_CFUNC_DEF ("unload", 1, r2plugin_unload),
JS_CFUNC_DEF ("unload", 2, r2plugin_unload),
// JS_CFUNC_DEF ("cmdj", 1, r2cmdj), // can be implemented in js
JS_CFUNC_DEF ("log", 1, r2log),
JS_CFUNC_DEF ("error", 1, r2error),
@ -624,15 +648,14 @@ static bool fini(RLangSession *s) {
QjsPluginManager *pm = s->plugin_data;
plugin_manager_fini (pm);
QjsContext *qctx = &pm->default_ctx;
JS_FreeContext (qctx->ctx);
qctx->ctx = NULL;
plugin_manager_fini (pm);
free (pm);
s->plugin_data = NULL;
return true;
}

View File

@ -20,6 +20,7 @@
}
r2.plugin("arch", archPlugin);
r2.unload("arch", archPlugin);
})()
```
@ -140,7 +141,7 @@ static bool r2qjs_arch_decode(RArchSession *s, RAnalOp *op, RArchDecodeMask mask
return JS_ToBool (ctx, res);
}
static JSValue r2plugin_arch(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
static JSValue r2plugin_arch_load(JSContext *ctx, JSValueConst this_val, int argc, JSValueConst *argv) {
JSRuntime *rt = JS_GetRuntime (ctx);
QjsPluginManager *pm = JS_GetRuntimeOpaque (rt);
RCore *core = pm->core;
@ -203,7 +204,7 @@ static JSValue r2plugin_arch(JSContext *ctx, JSValueConst this_val, int argc, JS
return JS_NewBool (ctx, false);
}
plugin_manager_add_arch_plugin (pm, ap->arch, ctx, decode_func);
plugin_manager_add_arch_plugin (pm, ap->meta.name, ap->arch, ctx, decode_func);
RLibStruct *lib = R_NEW0 (RLibStruct);
if (!lib) {

View File

@ -45,11 +45,11 @@
console.log("load true", r2.plugin("core", examplePlugin));
console.log("load true", r2.plugin("core", examplePlugin2));
console.log("load false", r2.plugin("core", examplePlugin));
console.log("unload false", r2.unload("false"));
console.log("unload true", r2.unload("qjs-example"));
console.log("unload false", r2.unload("qjs-example"));
console.log("unload false", r2.unload("core", "false"));
console.log("unload true", r2.unload("core", "qjs-example"));
console.log("unload false", r2.unload("core", "qjs-example"));
log("Plugins:");
log(r2cmd("Lc"));
log(r2.cmd("Lc"));
}
})();
@ -146,5 +146,5 @@ static JSValue r2plugin_core_load(JSContext *ctx, JSValueConst this_val, int arg
lib->data = ap;
lib->version = R2_VERSION;
int ret = r_lib_open_ptr (pm->core->lib, ap->name, NULL, lib);
return JS_NewBool (ctx, ret == 0);
return JS_NewBool (ctx, ret == 1);
}

View File

@ -477,7 +477,7 @@ static int __lib_bin_cb(RLibPlugin *pl, void *user, void *data) {
struct r_bin_plugin_t *hand = (struct r_bin_plugin_t *)data;
RBin *bin = user;
//printf(" * Added (dis)assembly plugin\n");
r_bin_add (bin, hand);
r_bin_plugin_add (bin, hand);
return true;
}

View File

@ -22,7 +22,7 @@ typedef struct {
static int __lib_egg_cb(RLibPlugin *pl, void *user, void *data) {
REggPlugin *hand = (REggPlugin *)data;
REggState *es = (REggState *)user;
r_egg_add (es->e, hand);
r_egg_plugin_add (es->e, hand);
return true;
}

View File

@ -635,7 +635,7 @@ static bool rasm_asm(RAsmState *as, const char *buf, ut64 offset, ut64 len, int
static int __lib_anal_cb(RLibPlugin *pl, void *user, void *data) {
RAnalPlugin *hand = (RAnalPlugin *)data;
RAsmState *as = (RAsmState *)user;
r_anal_add (as->anal, hand);
r_anal_plugin_add (as->anal, hand);
return true;
}
@ -643,7 +643,7 @@ static int __lib_anal_cb(RLibPlugin *pl, void *user, void *data) {
static int __lib_arch_cb(RLibPlugin *pl, void *user, void *data) {
RArchPlugin *hand = (RArchPlugin *)data;
RAsmState *as = (RAsmState *)user;
r_arch_add (as->anal->arch, hand);
r_arch_plugin_add (as->anal->arch, hand);
return true;
}

View File

@ -245,18 +245,20 @@ R_API int r_lib_close(RLib *lib, const char *file) {
}
// delete similar plugin name
r_list_foreach (lib->plugins, iter, p) {
eprintf("similar p->file: %s\n", p->file);
if (strstr (p->file, file)) {
int ret = 0;
if (p->handler && p->handler->destructor) {
ret = p->handler->destructor (p,
p->handler->user, p->data);
}
eprintf("similar deleting: %s\n", p->file);
free (p->file);
r_list_delete (lib->plugins, iter);
{
const char *fileName = r_str_rstr (file, R_SYS_DIR);
if (fileName) {
ht_pp_delete (lib->plugins_ht, fileName + 1);
const char *file_name = r_str_rstr (file, R_SYS_DIR);
if (file_name) {
ht_pp_delete (lib->plugins_ht, file_name + 1);
}
}
return ret;

View File

@ -1,4 +1,4 @@
NAME=js tests
NAME=js tests, variable scoping
FILE=-
CMDS=<<EOF
js var a = 123
@ -28,3 +28,119 @@ EOF
EXPECT_ERR=
RUN
NAME=qjs loading, running and unloading of core plugins
FILE=-
CMDS=<<EOF
Lc~qjs?
?e
""js (function() { function examplePlugin() { function coreCall(input) { if (input.startsWith("t1")) { console.log("This is a QJS test"); return true; } return false; } return { name: "qjs-example", desc: "Example QJS plugin (type 't1') in the r2 shell", call: coreCall, }; }; console.log("load qjs-example", r2.plugin("core", examplePlugin)); })();
Lc~qjs
?e
""js (function() { function examplePlugin() { function coreCall(input) { if (input.startsWith("t1")) { console.log("This is a QJS test"); return true; } return false; } return { name: "qjs-example", desc: "Example QJS plugin (type 't1') in the r2 shell", call: coreCall, }; }; console.log("load qjs-example", r2.plugin("core", examplePlugin)); })();
Lc~qjs
?e
""js (function() { function examplePlugin2() { function coreCall(input) { if (input.startsWith("t2")) { console.log("This is another QJS test"); return true; } return false; } return { name: "qjs-example2", desc: "Example QJS plugin (type 't2') in the r2 shell", call: coreCall, }; }; console.log("load qjs-example2", r2.plugin("core", examplePlugin2)); })();
Lc~qjs
t1
t2
""js console.log("unload unknown", r2.unload("core", "unknownPlugin"))
Lc~qjs
?e
""js console.log("unload qjs-example", r2.unload("core", "qjs-example"))
Lc~qjs
?e
""js console.log("unload qjs-example again", r2.unload("core", "qjs-example"))
Lc~qjs
?e
""js console.log("unload qjs-example2", r2.unload("core", "qjs-example2"))
?e
Lc~qjs?
EOF
EXPECT=<<EOF
0
qjs-example Example QJS plugin (type 't1') in the r2 shell
qjs-example Example QJS plugin (type 't1') in the r2 shell
qjs-example Example QJS plugin (type 't1') in the r2 shell
qjs-example2 Example QJS plugin (type 't2') in the r2 shell
qjs-example Example QJS plugin (type 't1') in the r2 shell
qjs-example2 Example QJS plugin (type 't2') in the r2 shell
qjs-example2 Example QJS plugin (type 't2') in the r2 shell
qjs-example2 Example QJS plugin (type 't2') in the r2 shell
0
load qjs-example true
load qjs-example false
load qjs-example2 true
This is a QJS test
This is another QJS test
unload unknown false
unload qjs-example true
unload qjs-example again false
unload qjs-example2 true
EOF
EXPECT_ERR=<<EOF
WARN: r2.plugin with name qjs-example is already registered
EOF
RUN
NAME=qjs loading and unloading of arch plugins
FILE=-
CMDS=<<EOF
LA~qjs?
?e
""js (function() { function archPlugin() { return { name: "myarch qjs plugin", arch: "myarch_qjs", desc: "this is a test arch", license: "LGPL3", decode: function(op) { op.mnemonic = "nop"; return true; } } }; console.log('load plugin1', r2.plugin("arch", archPlugin)); })()
LA~qjs
?e
""js (function() { function archPlugin() { return { name: "myarch qjs plugin", arch: "myarch_qjs", desc: "this is a test arch", license: "LGPL3", decode: function(op) { op.mnemonic = "nop"; return true; } } }; console.log('load plugin1 again', r2.plugin("arch", archPlugin)); })()
LA~qjs
?e
""js (function() { function archPlugin2() { return { name: "myarch qjs plugin2", arch: "myarch_qjs2", desc: "this is a test arch2", license: "LGPL3", decode: function(op) { op.mnemonic = "nop"; return true; } } }; console.log('load plugin2', r2.plugin("arch", archPlugin2)); })()
LA~qjs
?e
""js console.log("unload unknown", r2.unload("arch", "unknownPlugin"))
LA~qjs
?e
""js console.log("unload qjs-example", r2.unload("arch", "myarch_qjs"))
LA~qjs
?e
""js console.log("unload qjs-example again", r2.unload("arch", "myarch_qjs"))
LA~qjs
?e
""js console.log("unload qjs-example2", r2.unload("arch", "myarch_qjs2"))
?e
LA~qjs?
EOF
EXPECT=<<EOF
0
myarch qjs plugin LGPL3 this is a test arch (myarch_qjs)
myarch qjs plugin LGPL3 this is a test arch (myarch_qjs)
myarch qjs plugin LGPL3 this is a test arch (myarch_qjs)
myarch qjs plugin2 LGPL3 this is a test arch2 (myarch_qjs2)
myarch qjs plugin2 LGPL3 this is a test arch2 (myarch_qjs2)
myarch qjs plugin2 LGPL3 this is a test arch2 (myarch_qjs2)
0
load plugin1 true
load plugin1 again false
load plugin2 true
unload unknown false
unload qjs-example true
unload qjs-example again false
unload qjs-example2 true
EOF
EXPECT_ERR=<<EOF
WARN: r2.plugin with name myarch qjs plugin is already registered
EOF
RUN