radare2/libr/core/core.c
pancake 9bd9cbe135 * Fix LIBR_PLUGINS in env.sh
* Initial implementation of the visual mode in core
* Added pc and ps print formats (c code and string)
* Added '/' command in core (search hexa and str)
* Add '-d' to radare2 test program
* New r_file_path to resolve file path thru $PATH
* 'make install' works everywhere
2009-02-09 01:54:09 +01:00

165 lines
4.0 KiB
C

/* radare - LGPL - Copyright 2009 pancake<nopcode.org> */
#include "r_core.h"
static u64 num_callback(void *userptr, const char *str, int *ok)
{
struct r_core_t *core = userptr;
struct r_flag_item_t *flag;
if (str[0]=='$') {
switch(str[1]) {
case '$': return core->seek;
case 'b': return core->blocksize;
//case '?': return core->blocksize; // HELP
}
}
flag = r_flag_get(&(core->flags), str);
if (flag != NULL) {
*ok = 1;
return flag->offset;
}
*ok = 0;
return 0LL;
}
struct r_core_t *r_core_new()
{
struct r_core_t *c = MALLOC_STRUCT(struct r_core_t);
r_core_init(c);
return c;
}
/* TODO: move to a separated file */
/* io callback */
int __lib_io_cb(struct r_lib_plugin_t *pl, void *user, void *data)
{
struct r_io_handle_t *hand = (struct r_io_handle_t *)data;
struct r_core_t *core = (struct r_core_t *)user;
//printf(" * Added IO handler\n");
r_io_handle_add(&core->io, hand);
return R_TRUE;
}
int __lib_io_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
/* debug callback */
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");
r_debug_handle_add(&core->dbg, hand);
return R_TRUE;
}
int __lib_dbg_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
/* debug callback */
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");
r_lang_add(&core->lang, hand);
return R_TRUE;
}
int __lib_lng_dt(struct r_lib_plugin_t *pl, void *p, void *u) { return R_TRUE; }
int r_core_init(struct r_core_t *core)
{
core->oobi = NULL;
core->oobi_len = 0;
core->num.callback = &num_callback;
core->num.userptr = core;
r_lang_init(&core->lang);
r_cons_init();
core->search = r_search_new(R_SEARCH_KEYWORD);
r_io_init(&core->io);
r_macro_init(&core->macro);
core->macro.num = &core->num;
core->macro.user = core;
core->macro.cmd = r_core_cmd0;
core->file = NULL;
INIT_LIST_HEAD(&core->files);
core->seek = 0LL;
core->blocksize = R_CORE_BLOCKSIZE;
core->block = (u8*)malloc(R_CORE_BLOCKSIZE);
r_core_cmd_init(core);
r_core_config_init(core);
r_flag_init(&core->flags);
r_debug_init(&core->dbg);
r_lib_init(&core->lib, "radare_plugin");
r_lib_add_handler(&core->lib, R_LIB_TYPE_IO, "io plugins",
&__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",
&__lib_lng_cb, &__lib_lng_dt, core);
r_lib_opendir(&core->lib, getenv("LIBR_PLUGINS"));
{
char *homeplugindir = r_str_home(".radare/plugins");
r_lib_opendir(&core->lib, homeplugindir);
free(homeplugindir);
}
// XXX fix path here
return 0;
}
struct r_core_t *r_core_free(struct r_core_t *c)
{
free(c);
return NULL;
}
int r_core_prompt(struct r_core_t *r)
{
char prompt[32];
char line[1024];
int ret;
sprintf(prompt, "[0x%08llx]> ", r->seek);
r_line_prompt = prompt;
ret = r_cons_fgets(line, sizeof(line), 0 , NULL);
if (ret<0)
return -1;
ret = r_core_cmd(r, line, R_TRUE);
r_cons_flush();
return ret;
}
int r_core_block_size(struct r_core_t *core, u32 bsize)
{
int ret = 0;
if (bsize<1)
bsize = 1;
else if (bsize> R_CORE_BLOCKSIZE_MAX)
bsize = R_CORE_BLOCKSIZE_MAX;
else ret = 1;
core->block = realloc(core->block, bsize);
core->blocksize = bsize;
r_core_block_read(core, 0);
return ret;
}
int r_core_block_read(struct r_core_t *core, int next)
{
if (core->file == NULL)
return -1;
r_io_lseek(&core->io, core->file->fd, core->seek+((next)?core->blocksize:0), R_IO_SEEK_SET);
return r_io_read(&core->io, core->file->fd, core->block, core->blocksize);
}
int r_core_seek(struct r_core_t *core, u64 addr)
{
u64 tmp = core->seek;
int ret;
core->seek = addr;
ret = r_core_block_read(core, 0);
if (ret == -1)
core->seek = tmp;
return ret;
}