mirror of
https://github.com/radareorg/radare2.git
synced 2024-11-23 13:19:54 +00:00
* Initial integrated implementation of r_lang in r_core
- Added two dummy plugins for r_lang (dummy and lua) - Accessible thru the '#!' command.
This commit is contained in:
parent
e8b70a1a0b
commit
008112edba
@ -603,8 +603,15 @@ static int cmd_hash(void *data, const char *input)
|
||||
#!lua
|
||||
#!lua foo bar
|
||||
#endif
|
||||
r_lang_run(&core->lang, core->oobi, core->oobi_len);
|
||||
r_cons_printf("TODO\n");
|
||||
if (input[1]=='\0') {
|
||||
r_lang_list(&core->lang);
|
||||
return R_TRUE;
|
||||
}
|
||||
// TODO: set argv here
|
||||
r_lang_set(&core->lang, input+1);
|
||||
if (core->oobi)
|
||||
r_lang_run(&core->lang, core->oobi, core->oobi_len);
|
||||
else r_lang_prompt(&core->lang, core->oobi, core->oobi_len);
|
||||
return R_TRUE;
|
||||
}
|
||||
|
||||
|
@ -49,7 +49,7 @@ int __lib_dbg_cb(struct r_lib_plugin_t *pl, void *user, void *data)
|
||||
{
|
||||
struct r_debug_handle_t *hand = (struct r_debug_handle_t *)data;
|
||||
struct r_core_t *core = (struct r_core_t *)user;
|
||||
printf(" * Added debugger handler\n");
|
||||
//printf(" * Added debugger handler\n");
|
||||
r_debug_handle_add(&core->dbg, hand);
|
||||
return R_TRUE;
|
||||
}
|
||||
@ -61,7 +61,7 @@ int __lib_lng_cb(struct r_lib_plugin_t *pl, void *user, void *data)
|
||||
{
|
||||
struct r_debug_handle_t *hand = (struct r_debug_handle_t *)data;
|
||||
struct r_core_t *core = (struct r_core_t *)user;
|
||||
printf(" * Added language handler\n");
|
||||
//printf(" * Added language handler\n");
|
||||
r_lang_add(&core->lang, hand);
|
||||
return R_TRUE;
|
||||
}
|
||||
@ -75,6 +75,7 @@ int r_core_init(struct r_core_t *core)
|
||||
core->num.callback = &num_callback;
|
||||
core->num.userptr = core;
|
||||
r_lang_init(&core->lang);
|
||||
r_lang_set_user_ptr(&core->lang, core);
|
||||
r_cons_init();
|
||||
core->search = r_search_new(R_SEARCH_KEYWORD);
|
||||
r_io_init(&core->io);
|
||||
@ -96,7 +97,7 @@ int r_core_init(struct r_core_t *core)
|
||||
&__lib_io_cb, &__lib_io_dt, core);
|
||||
r_lib_add_handler(&core->lib, R_LIB_TYPE_DBG, "debug plugins",
|
||||
&__lib_dbg_cb, &__lib_dbg_dt, core);
|
||||
r_lib_add_handler(&core->lib, R_LIB_TYPE_LNG, "language plugins",
|
||||
r_lib_add_handler(&core->lib, R_LIB_TYPE_LANG, "language plugins",
|
||||
&__lib_lng_cb, &__lib_lng_dt, core);
|
||||
r_lib_opendir(&core->lib, getenv("LIBR_PLUGINS"));
|
||||
{
|
||||
|
@ -7,11 +7,15 @@
|
||||
struct r_lang_handle_t {
|
||||
char *name;
|
||||
char *desc;
|
||||
int (*run)(void *user, const char *code, int len);
|
||||
int (*run_file)(void *user, const char *file);
|
||||
int (*set_argv)(void *user, int argc, char **argv);
|
||||
struct list_head list;
|
||||
};
|
||||
|
||||
struct r_lang_t {
|
||||
struct r_lang_handle_t *cur;
|
||||
void *user;
|
||||
struct list_head langs;
|
||||
};
|
||||
|
||||
@ -20,5 +24,9 @@ int r_lang_add(struct r_lang_t *lang, struct r_lang_handle_t *foo);
|
||||
int r_lang_list(struct r_lang_t *lang);
|
||||
int r_lang_set(struct r_lang_t *lang, const char *name);
|
||||
int r_lang_run(struct r_lang_t *lang, const char *code, int len);
|
||||
void r_lang_set_user_ptr(struct r_lang_t *lang, void *user);
|
||||
int r_lang_set_argv(struct r_lang_t *lang, int argc, char **argv);
|
||||
int r_lang_run(struct r_lang_t *lang, const char *code, int len);
|
||||
int r_lang_run_file(struct r_lang_t *lang, const char *file);
|
||||
|
||||
#endif
|
||||
|
@ -36,10 +36,10 @@ struct r_lib_struct_t {
|
||||
};
|
||||
|
||||
enum {
|
||||
R_LIB_TYPE_IO, /* io layer */
|
||||
R_LIB_TYPE_DBG, /* debugger */
|
||||
R_LIB_TYPE_LNG, /* language */
|
||||
R_LIB_TYPE_ASM, /* assembler */
|
||||
R_LIB_TYPE_IO, /* io layer */
|
||||
R_LIB_TYPE_DBG, /* debugger */
|
||||
R_LIB_TYPE_LANG, /* language */
|
||||
R_LIB_TYPE_ASM, /* assembler */
|
||||
//R_LIB_TYPE_DIS, /* disassembler */
|
||||
};
|
||||
|
||||
|
@ -85,7 +85,7 @@ int r_hex_to_byte(u8 *val, u8 c);
|
||||
|
||||
/* file */
|
||||
char *r_file_path(const char *bin);
|
||||
char *r_file_slurp(const char *str, u32 *usz);
|
||||
char *r_file_slurp(const char *str, int *usz);
|
||||
char *r_file_slurp_range(const char *str, u64 off, u64 sz);
|
||||
char *r_file_slurp_random_line(const char *file);
|
||||
int r_file_dump(const char *file, const u8 *buf, int len);
|
||||
|
@ -1,9 +1,11 @@
|
||||
/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
|
||||
|
||||
#include <r_lang.h>
|
||||
#include <r_util.h>
|
||||
|
||||
int r_lang_init(struct r_lang_t *lang)
|
||||
{
|
||||
lang->user = NULL;
|
||||
INIT_LIST_HEAD(&lang->langs);
|
||||
return R_TRUE;
|
||||
}
|
||||
@ -21,7 +23,6 @@ int r_lang_list(struct r_lang_t *lang)
|
||||
struct r_lang_handle_t *h = list_entry(pos, struct r_lang_handle_t, list);
|
||||
printf(" %s: %s\n", h->name, h->desc);
|
||||
}
|
||||
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
@ -30,22 +31,62 @@ int r_lang_set(struct r_lang_t *lang, const char *name)
|
||||
struct list_head *pos;
|
||||
list_for_each_prev(pos, &lang->langs) {
|
||||
struct r_lang_handle_t *h = list_entry(pos, struct r_lang_handle_t, list);
|
||||
if (!strcmp(h->name, name)) {
|
||||
if (!memcmp(h->name, name, strlen(h->name))) {
|
||||
lang->cur = h;
|
||||
return R_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
//int r_lang_set_args
|
||||
//int r_lang_set_code(struct r_lang_t *lang, const char *code, int len)
|
||||
int r_lang_run(struct r_lang_t *lang, const char *code, int len)
|
||||
{
|
||||
if (lang->cur) {
|
||||
//lang->cur->run(code, len);
|
||||
return R_TRUE;
|
||||
}
|
||||
void r_lang_set_user_ptr(struct r_lang_t *lang, void *user)
|
||||
{
|
||||
lang->user = user;
|
||||
}
|
||||
|
||||
int r_lang_set_argv(struct r_lang_t *lang, int argc, char **argv)
|
||||
{
|
||||
if (lang->cur && lang->cur->set_argv)
|
||||
return lang->cur->set_argv(lang->user, argc, argv);
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
int r_lang_run(struct r_lang_t *lang, const char *code, int len)
|
||||
{
|
||||
if (lang->cur && lang->cur->run)
|
||||
return lang->cur->run(lang->user, code, len);
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
int r_lang_run_file(struct r_lang_t *lang, const char *file)
|
||||
{
|
||||
int len, ret = R_FALSE;
|
||||
if (lang->cur) {
|
||||
if (lang->cur->run_file == NULL) {
|
||||
if (lang->cur->run != NULL) {
|
||||
char *code = r_file_slurp(file, &len);
|
||||
ret = lang->cur->run(lang->user, code, len);
|
||||
free(code);
|
||||
}
|
||||
} else ret = lang->cur->run_file(lang->user, file);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
int r_lang_prompt(struct r_lang_t *lang)
|
||||
{
|
||||
char buf[1024];
|
||||
if (lang->cur == NULL)
|
||||
return R_FALSE;
|
||||
while(1) {
|
||||
printf("%s> ", lang->cur->name);
|
||||
fflush(stdout);
|
||||
fgets(buf, 1023, stdin);
|
||||
if (feof(stdin)) break;
|
||||
buf[strlen(buf)-1]='\0';
|
||||
if (!strcmp(buf, "q"))
|
||||
return R_TRUE;
|
||||
r_lang_run(lang, buf, strlen(buf));
|
||||
}
|
||||
return R_TRUE;
|
||||
}
|
||||
|
15
libr/lang/p/Makefile
Normal file
15
libr/lang/p/Makefile
Normal file
@ -0,0 +1,15 @@
|
||||
CFLAGS=-I../../include -Wall
|
||||
BINDEPS=
|
||||
|
||||
all: lua.so dummy.so
|
||||
@true
|
||||
|
||||
lua.so: lua.o
|
||||
${CC} ${CFLAGS} -fPIC -shared -o lua.so lua.c -Wl,-R..
|
||||
@#strip -s lua.so
|
||||
|
||||
dummy.so: dummy.o
|
||||
${CC} ${CFLAGS} -fPIC -shared -o dummy.so dummy.c -Wl,-R..
|
||||
@#strip -s lua.so
|
||||
clean:
|
||||
-rm -f *.so *.o
|
23
libr/lang/p/dummy.c
Normal file
23
libr/lang/p/dummy.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* dummy extension for libr (radare2) */
|
||||
|
||||
#include "r_lib.h"
|
||||
#include "r_lang.h"
|
||||
|
||||
static int dummy_run(void *user, const char *code, int len)
|
||||
{
|
||||
printf("Evaluating '%s'\n", code);
|
||||
return R_TRUE;
|
||||
}
|
||||
|
||||
static struct r_lang_handle_t r_lang_plugin_dummy = {
|
||||
.name = "dummy",
|
||||
.desc = "Dummy language extension",
|
||||
.run = &dummy_run,
|
||||
.run_file = NULL,
|
||||
.set_argv = NULL,
|
||||
};
|
||||
|
||||
struct r_lib_struct_t radare_plugin = {
|
||||
.type = R_LIB_TYPE_LANG,
|
||||
.data = &r_lang_plugin_dummy
|
||||
};
|
23
libr/lang/p/lua.c
Normal file
23
libr/lang/p/lua.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* lua extension for libr (radare2) */
|
||||
|
||||
#include "r_lib.h"
|
||||
#include "r_lang.h"
|
||||
|
||||
static int lua_run(void *user, const char *code, int len)
|
||||
{
|
||||
printf("Evaluating '%s'\n", code);
|
||||
return R_TRUE;
|
||||
}
|
||||
|
||||
static struct r_lang_handle_t r_lang_plugin_lua = {
|
||||
.name = "lua",
|
||||
.desc = "LUA language extension",
|
||||
.run = &lua_run,
|
||||
.run_file = NULL,
|
||||
.set_argv = NULL,
|
||||
};
|
||||
|
||||
struct r_lib_struct_t radare_plugin = {
|
||||
.type = R_LIB_TYPE_LANG,
|
||||
.data = &r_lang_plugin_lua,
|
||||
};
|
@ -63,7 +63,6 @@ struct r_search_t *r_search_free(struct r_search_t *s)
|
||||
}
|
||||
|
||||
/* control */
|
||||
|
||||
int r_search_initialize(struct r_search_t *s)
|
||||
{
|
||||
struct list_head *pos;
|
||||
|
@ -44,7 +44,7 @@ char *r_file_path(const char *bin)
|
||||
return strdup(bin);
|
||||
}
|
||||
|
||||
char *r_file_slurp(const char *str, u32 *usz)
|
||||
char *r_file_slurp(const char *str, int *usz)
|
||||
{
|
||||
char *ret;
|
||||
long sz;
|
||||
|
@ -18,6 +18,7 @@ core:
|
||||
-lr_macro -Wl,-R../../macro -L../../macro \
|
||||
-lr_print -Wl,-R../../print -L../../print \
|
||||
-lr_config -Wl,-R../../config -L../../config \
|
||||
-lr_search -Wl,-R../../search -L../../search \
|
||||
-lr_cmd -Wl,-R../../cmd -L../../cmd \
|
||||
-lr_hash -Wl,-R../../hash -L../../hash \
|
||||
-g -o core
|
||||
|
Loading…
Reference in New Issue
Block a user