Initial implementation of calling the r*2 commands natively from inside r2 ##core

(still far from complete but we need to go forward and kill all the globals \o/)
PD: the broken test is because initializing RCons twice
This commit is contained in:
radare 2019-07-10 18:45:20 +02:00 committed by GitHub
parent c8bda90d59
commit d34ee687d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 83 additions and 16 deletions

View File

@ -1480,19 +1480,54 @@ static int cmd_bsize(void *data, const char *input) {
return 0;
}
static int cmd_r2cmd(RCore *core, const char *input) {
const char *r2cmds[] = {
"rax2", "r2pm", "rasm2", "rabin2", "rahash2", "rafind2", "rarun2", "ragg2",
NULL
};
int i;
for (i = 0; r2cmds[i]; i++) {
if (r_str_startswith (input, r2cmds[i])) {
r_sys_cmdf ("r%s", input);
return true;
static int __runMain(RMainCallback cb, const char *arg) {
char *a = r_str_trim_dup (arg);
int argc = 0;
char **args = r_str_argv (a, &argc);
int res = cb (argc, args);
free (args);
free (a);
return res;
}
static bool cmd_r2cmd(RCore *core, const char *_input) {
char *input = r_str_newf ("r%s", _input);
int rc = 0;
if (r_str_startswith (input, "rax2")) {
rc = __runMain (core->r_main_rax2, input);
} else if (r_str_startswith (input, "radare2")) {
r_sys_cmdf ("%s", input);
// rc = __runMain (core->r_main_radare2, input);
} else if (r_str_startswith (input, "rasm2")) {
r_sys_cmdf ("%s", input);
// rc = __runMain (core->r_main_rasm2, input);
} else if (r_str_startswith (input, "rabin2")) {
r_sys_cmdf ("%s", input);
// rc = __runMain (core->r_main_rabin2, input);
} else if (r_str_startswith (input, "ragg2")) {
r_sys_cmdf ("%s", input);
// rc = __runMain (core->r_main_ragg2, input);
} else if (r_str_startswith (input, "r2pm")) {
r_sys_cmdf ("%s", input);
// rc = __runMain (core->r_main_r2pm, input);
} else if (r_str_startswith (input, "radiff2")) {
rc = __runMain (core->r_main_radiff2, input);
} else {
const char *r2cmds[] = {
"rax2", "r2pm", "rasm2", "rabin2", "rahash2", "rafind2", "rarun2", "ragg2", "radare2", "r2", NULL
};
int i;
for (i = 0; r2cmds[i]; i++) {
if (r_str_startswith (input, r2cmds[i])) {
free (input);
return true;
}
}
return false;
}
return false;
free (input);
core->num->value = rc;
return true;
}
static int cmd_resize(void *data, const char *input) {
@ -1507,7 +1542,12 @@ static int cmd_resize(void *data, const char *input) {
ut64 oldsize = (core->file) ? r_io_fd_size (core->io, core->file->fd): 0;
switch (*input) {
case '2': // "r2"
case 'a': // "r..."
if (r_str_startswith (input, "adare2")) {
__runMain (core->r_main_radare2, input - 1);
}
return true;
case '2': // "r2" // XXX should be handled already in cmd_r2cmd()
// TODO: use argv[0] instead of 'radare2'
r_sys_cmdf ("radare%s", input);
return true;

View File

@ -2291,18 +2291,24 @@ static bool r_core_anal_read_at(struct r_anal_t *anal, ut64 addr, ut8 *buf, int
static void r_core_break (RCore *core) {
// if we are not in the main thread we hold in a lock
RCoreTask *task = r_core_task_self (core);
r_core_task_continue (task);
if (task) {
r_core_task_continue (task);
}
}
static void *r_core_sleep_begin (RCore *core) {
RCoreTask *task = r_core_task_self (core);
r_core_task_sleep_begin (task);
if (task) {
r_core_task_sleep_begin (task);
}
return task;
}
static void r_core_sleep_end (RCore *core, void *user) {
RCoreTask *task = (RCoreTask *)user;
r_core_task_sleep_end (task);
if (task) {
r_core_task_sleep_end (task);
}
}
static void __init_autocomplete_default (RCore* core) {

View File

@ -3,6 +3,7 @@
#ifndef R2_CORE_H
#define R2_CORE_H
#include <r_main.h>
#include "r_socket.h"
#include "r_types.h"
#include "r_magic.h"
@ -332,6 +333,16 @@ typedef struct r_core_t {
bool scr_gadgets;
bool log_events; // core.c:cb_event_handler : log actions from events if cfg.log.events is set
RList *ropchain;
RMainCallback r_main_radare2;
// int (*r_main_radare2)(int argc, char **argv);
int (*r_main_rafind2)(int argc, char **argv);
int (*r_main_radiff2)(int argc, char **argv);
int (*r_main_rabin2)(int argc, char **argv);
int (*r_main_rarun2)(int argc, char **argv);
int (*r_main_ragg2)(int argc, char **argv);
int (*r_main_rasm2)(int argc, char **argv);
int (*r_main_rax2)(int argc, char **argv);
} RCore;
// maybe move into RAnal

View File

@ -13,6 +13,8 @@ typedef struct r_main_t {
// stdin/stdout
} RMain;
typedef int (*RMainCallback)(int argc, char **argv);
R_API RMain *r_main_new(const char *name);
R_API void r_main_free(RMain *m);
R_API int r_main_run(RMain *m, int argc, char **argv);

View File

@ -508,7 +508,15 @@ R_API int r_main_radare2(int argc, char **argv) {
LISTS_FREE ();
return main_help (1);
}
r_core_init (&r);
r_core_init (&r); // TODO: use r_core_new() for simplicity
r.r_main_radare2 = r_main_radare2;
r.r_main_radiff2 = r_main_radiff2;
r.r_main_rafind2 = r_main_rafind2;
r.r_main_rabin2 = r_main_rabin2;
r.r_main_ragg2 = r_main_ragg2;
r.r_main_rasm2 = r_main_rasm2;
r.r_main_rax2 = r_main_rax2;
r_core_task_sync_begin (&r);
if (argc == 2 && !strcmp (argv[1], "-p")) {
r_core_project_list (&r, 0);