mirror of
https://github.com/radareorg/radare2.git
synced 2025-02-02 03:32:04 +00:00
* Store flags and config variables in project file
- add header comment before every data * Added r_str_write and r_str_writef to simplify code * Some code cleanup in config and project code
This commit is contained in:
parent
3be83621cb
commit
a28fa26e53
@ -3,11 +3,10 @@
|
||||
#include "r_config.h"
|
||||
#include "r_util.h" // r_str_hash, r_str_chop, ...
|
||||
|
||||
R_API struct r_config_node_t* r_config_node_new(const char *name, const char *value)
|
||||
{
|
||||
struct r_config_node_t *node =
|
||||
(struct r_config_node_t *)
|
||||
malloc(sizeof(struct r_config_node_t));
|
||||
R_API RConfigNode* r_config_node_new(const char *name, const char *value) {
|
||||
RConfigNode *node =
|
||||
(RConfigNode *)
|
||||
malloc(sizeof(RConfigNode));
|
||||
INIT_LIST_HEAD(&(node->list));
|
||||
node->name = strdup(name);
|
||||
node->hash = r_str_hash(name);
|
||||
@ -18,8 +17,7 @@ R_API struct r_config_node_t* r_config_node_new(const char *name, const char *va
|
||||
return node;
|
||||
}
|
||||
|
||||
R_API void r_config_list(struct r_config_t *cfg, const char *str, int rad)
|
||||
{
|
||||
R_API void r_config_list(RConfig *cfg, const char *str, int rad) {
|
||||
struct list_head *i;
|
||||
int len = 0;
|
||||
|
||||
@ -29,38 +27,31 @@ R_API void r_config_list(struct r_config_t *cfg, const char *str, int rad)
|
||||
}
|
||||
|
||||
list_for_each(i, &(cfg->nodes)) {
|
||||
struct r_config_node_t *bt = list_entry(i, struct r_config_node_t, list);
|
||||
RConfigNode *bt = list_entry(i, RConfigNode, list);
|
||||
if (str) {
|
||||
if (strncmp(str, bt->name, len) == 0) {
|
||||
if (rad) cfg->printf("f ");
|
||||
cfg->printf("%s = %s\n", bt->name, bt->value);
|
||||
}
|
||||
} else {
|
||||
if (rad) cfg->printf("f ");
|
||||
cfg->printf("%s = %s\n", bt->name, bt->value);
|
||||
}
|
||||
if (strncmp(str, bt->name, len) == 0)
|
||||
cfg->printf ("%s = %s\n", rad?"e ":"",
|
||||
bt->name, bt->value);
|
||||
} else cfg->printf ("%s%s = %s\n", rad?"e ":"", bt->name, bt->value);
|
||||
}
|
||||
}
|
||||
|
||||
R_API struct r_config_node_t *r_config_node_get(struct r_config_t *cfg, const char *name)
|
||||
{
|
||||
R_API RConfigNode *r_config_node_get(RConfig *cfg, const char *name) {
|
||||
struct list_head *i;
|
||||
int hash;
|
||||
if (strnull(name))
|
||||
if (strnull (name))
|
||||
return NULL;
|
||||
hash = r_str_hash(name);
|
||||
list_for_each_prev(i, &(cfg->nodes)) {
|
||||
struct r_config_node_t *bt = list_entry(i, struct r_config_node_t, list);
|
||||
hash = r_str_hash (name);
|
||||
list_for_each_prev (i, &(cfg->nodes)) {
|
||||
RConfigNode *bt = list_entry (i, RConfigNode, list);
|
||||
if (bt->hash == hash)
|
||||
return bt;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
R_API char *r_config_get(struct r_config_t *cfg, const char *name)
|
||||
{
|
||||
struct r_config_node_t *node =
|
||||
r_config_node_get(cfg, name);
|
||||
R_API char *r_config_get(RConfig *cfg, const char *name) {
|
||||
RConfigNode *node = r_config_node_get (cfg, name);
|
||||
if (node) {
|
||||
cfg->last_notfound = 0;
|
||||
if (node->flags & CN_BOOL)
|
||||
@ -74,10 +65,8 @@ R_API char *r_config_get(struct r_config_t *cfg, const char *name)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
R_API int r_config_swap(struct r_config_t *cfg, const char *name)
|
||||
{
|
||||
struct r_config_node_t *node =
|
||||
r_config_node_get(cfg, name);
|
||||
R_API int r_config_swap(RConfig *cfg, const char *name) {
|
||||
RConfigNode *node = r_config_node_get(cfg, name);
|
||||
if (node && node->flags & CN_BOOL) {
|
||||
r_config_set_i(cfg, name, !node->i_value);
|
||||
return R_TRUE;
|
||||
@ -85,10 +74,8 @@ R_API int r_config_swap(struct r_config_t *cfg, const char *name)
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
R_API ut64 r_config_get_i(struct r_config_t *cfg, const char *name)
|
||||
{
|
||||
struct r_config_node_t *node =
|
||||
r_config_node_get(cfg, name);
|
||||
R_API ut64 r_config_get_i(RConfig *cfg, const char *name) {
|
||||
RConfigNode *node = r_config_node_get(cfg, name);
|
||||
if (node) {
|
||||
if (node->i_value != 0)
|
||||
return node->i_value;
|
||||
@ -97,9 +84,10 @@ R_API ut64 r_config_get_i(struct r_config_t *cfg, const char *name)
|
||||
return (ut64)0LL;
|
||||
}
|
||||
|
||||
R_API struct r_config_node_t *r_config_set_cb(struct r_config_t *cfg, const char *name, const char *value, int (*callback)(void *user, void *data))
|
||||
// TODO: use typedef declaration here
|
||||
R_API RConfigNode *r_config_set_cb(RConfig *cfg, const char *name, const char *value, int (*callback)(void *user, void *data))
|
||||
{
|
||||
struct r_config_node_t *node;
|
||||
RConfigNode *node;
|
||||
node = r_config_set(cfg, name, value);
|
||||
node->callback = callback;
|
||||
if (node->callback)
|
||||
@ -107,10 +95,10 @@ R_API struct r_config_node_t *r_config_set_cb(struct r_config_t *cfg, const char
|
||||
return node;
|
||||
}
|
||||
|
||||
/* TODO: _cb can be a macro */
|
||||
R_API struct r_config_node_t *r_config_set_i_cb(struct r_config_t *cfg, const char *name, int ivalue, int (*callback)(void *user, void *data))
|
||||
// TODO: use typedef declaration here
|
||||
R_API RConfigNode *r_config_set_i_cb(RConfig *cfg, const char *name, int ivalue, int (*callback)(void *user, void *data))
|
||||
{
|
||||
struct r_config_node_t *node = r_config_set_i(cfg, name, ivalue);
|
||||
RConfigNode *node = r_config_set_i(cfg, name, ivalue);
|
||||
node->callback = callback;
|
||||
if (node->callback)
|
||||
node->callback(cfg->user, node);
|
||||
@ -118,41 +106,40 @@ R_API struct r_config_node_t *r_config_set_i_cb(struct r_config_t *cfg, const ch
|
||||
}
|
||||
|
||||
/* TODO: reduce number of strdups here */
|
||||
R_API struct r_config_node_t *r_config_set(struct r_config_t *cfg, const char *name, const char *value)
|
||||
{
|
||||
struct r_config_node_t *node;
|
||||
R_API RConfigNode *r_config_set(RConfig *cfg, const char *name, const char *value) {
|
||||
RConfigNode *node;
|
||||
char *ov = NULL;
|
||||
ut64 oi;
|
||||
|
||||
if (name[0] == '\0')
|
||||
return NULL;
|
||||
|
||||
node = r_config_node_get(cfg, name);
|
||||
node = r_config_node_get (cfg, name);
|
||||
|
||||
// TODO: store old value somewhere..
|
||||
if (node) {
|
||||
if (node->flags & CN_RO) {
|
||||
eprintf("(read only)\n");
|
||||
eprintf ("(read only)\n");
|
||||
return node;
|
||||
}
|
||||
oi = node->i_value;
|
||||
if (node->value)
|
||||
ov = strdup(node->value);
|
||||
else node->value = strdup("");
|
||||
ov = strdup (node->value);
|
||||
else node->value = strdup ("");
|
||||
free(node->value);
|
||||
if (node->flags & CN_BOOL) {
|
||||
int b = (!strcmp(value,"true")||!strcmp(value,"1"));
|
||||
int b = (!strcmp (value,"true")||!strcmp (value,"1"));
|
||||
node->i_value = (ut64)(b==0)?0:1;
|
||||
node->value = strdup(b?"true":"false");
|
||||
node->value = strdup (b?"true":"false");
|
||||
} else {
|
||||
if (value == NULL) {
|
||||
node->value = strdup("");
|
||||
node->value = strdup ("");
|
||||
node->i_value = 0;
|
||||
} else {
|
||||
node->value = strdup(value);
|
||||
node->value = strdup (value);
|
||||
if (strchr(value, '/'))
|
||||
node->i_value = r_num_get(NULL, value);
|
||||
else node->i_value = r_num_math(NULL, value);
|
||||
node->i_value = r_num_get (NULL, value);
|
||||
else node->i_value = r_num_math (NULL, value);
|
||||
node->flags |= CN_INT;
|
||||
}
|
||||
}
|
||||
@ -160,67 +147,63 @@ R_API struct r_config_node_t *r_config_set(struct r_config_t *cfg, const char *n
|
||||
if (cfg->lock) {
|
||||
fprintf(stderr, "config is locked: cannot create '%s'\n", name);
|
||||
} else {
|
||||
node = r_config_node_new(name, value);
|
||||
node = r_config_node_new (name, value);
|
||||
if (value && (!strcmp(value,"true")||!strcmp(value,"false"))) {
|
||||
node->flags|=CN_BOOL;
|
||||
node->i_value = (!strcmp(value,"true"))?1:0;
|
||||
}
|
||||
list_add_tail(&(node->list), &(cfg->nodes));
|
||||
list_add_tail (&(node->list), &(cfg->nodes));
|
||||
cfg->n_nodes++;
|
||||
}
|
||||
}
|
||||
|
||||
if (node && node->callback) {
|
||||
int ret = node->callback(cfg->user, node);
|
||||
int ret = node->callback (cfg->user, node);
|
||||
if (ret == R_FALSE) {
|
||||
node->i_value = oi;
|
||||
free(node->value);
|
||||
free (node->value);
|
||||
node->value = strdup(ov);
|
||||
}
|
||||
}
|
||||
free(ov);
|
||||
free (ov);
|
||||
return node;
|
||||
}
|
||||
|
||||
R_API int r_config_rm(struct r_config_t *cfg, const char *name)
|
||||
{
|
||||
struct r_config_node_t *node =
|
||||
r_config_node_get(cfg, name);
|
||||
R_API int r_config_rm(RConfig *cfg, const char *name) {
|
||||
RConfigNode *node = r_config_node_get (cfg, name);
|
||||
if (node) {
|
||||
list_del(&(node->list));
|
||||
list_del (&(node->list));
|
||||
cfg->n_nodes--;
|
||||
return 1;
|
||||
return R_TRUE;
|
||||
}
|
||||
return 0;
|
||||
return R_FALSE;
|
||||
}
|
||||
|
||||
R_API struct r_config_node_t *r_config_set_i(struct r_config_t *cfg, const char *name, const ut64 i)
|
||||
{
|
||||
R_API RConfigNode *r_config_set_i(RConfig *cfg, const char *name, const ut64 i) {
|
||||
char buf[128];
|
||||
char *ov = NULL;
|
||||
ut64 oi;
|
||||
struct r_config_node_t *node =
|
||||
r_config_node_get(cfg, name);
|
||||
RConfigNode *node = r_config_node_get (cfg, name);
|
||||
|
||||
if (node) {
|
||||
if (node->flags & CN_RO)
|
||||
return NULL;
|
||||
oi = node->i_value;
|
||||
if (node->value)
|
||||
ov = strdup(node->value);
|
||||
ov = strdup (node->value);
|
||||
else node->value = strdup("");
|
||||
free(node->value);
|
||||
free (node->value);
|
||||
if (node->flags & CN_BOOL) {
|
||||
node->value = strdup(i?"true":"false");
|
||||
} else {
|
||||
sprintf(buf, "%lld", i); //0x%08lx", i);
|
||||
sprintf (buf, "%lld", i); //0x%08lx", i);
|
||||
node->value = strdup(buf);
|
||||
}
|
||||
//node->flags = CN_RW | CN_INT;
|
||||
node->i_value = i;
|
||||
} else {
|
||||
if (cfg->lock) {
|
||||
eprintf("(locked: no new keys can be created)");
|
||||
eprintf ("(locked: no new keys can be created)");
|
||||
} else {
|
||||
sprintf(buf, "0x%08llx", i);
|
||||
node = r_config_node_new(name, buf);
|
||||
@ -244,8 +227,7 @@ R_API struct r_config_node_t *r_config_set_i(struct r_config_t *cfg, const char
|
||||
return node;
|
||||
}
|
||||
|
||||
R_API int r_config_eval(struct r_config_t *cfg, const char *str)
|
||||
{
|
||||
R_API int r_config_eval(RConfig *cfg, const char *str) {
|
||||
char *ptr,*a,*b;
|
||||
char *name;
|
||||
int len;
|
||||
@ -295,39 +277,33 @@ R_API int r_config_eval(struct r_config_t *cfg, const char *str)
|
||||
return 1;
|
||||
}
|
||||
|
||||
R_API void r_config_lock(struct r_config_t *cfg, int l)
|
||||
{
|
||||
R_API void r_config_lock(RConfig *cfg, int l) {
|
||||
cfg->lock = l;
|
||||
}
|
||||
|
||||
R_API int r_config_init(struct r_config_t *cfg, void *user)
|
||||
{
|
||||
R_API int r_config_init(RConfig *cfg, void *user) {
|
||||
cfg->user = user;
|
||||
cfg->n_nodes = 0;
|
||||
cfg->lock = 0;
|
||||
cfg->printf = (void *)printf;
|
||||
INIT_LIST_HEAD(&(cfg->nodes));
|
||||
INIT_LIST_HEAD (&(cfg->nodes));
|
||||
return 0;
|
||||
}
|
||||
|
||||
R_API struct r_config_t *r_config_new(void *user)
|
||||
{
|
||||
struct r_config_t *cfg = (struct r_config_t *)
|
||||
malloc(sizeof(struct r_config_t));
|
||||
r_config_init(cfg, user);
|
||||
R_API RConfig *r_config_new(void *user) {
|
||||
RConfig *cfg = R_NEW (RConfig);
|
||||
r_config_init (cfg, user);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
R_API int r_config_free(struct r_config_t *cfg)
|
||||
{
|
||||
R_API int r_config_free(RConfig *cfg) {
|
||||
// TODO: Free node list
|
||||
free(cfg);
|
||||
free (cfg);
|
||||
return 0;
|
||||
}
|
||||
|
||||
R_API void r_config_visual_hit_i(struct r_config_t *cfg, const char *name, int delta)
|
||||
{
|
||||
struct r_config_node_t *node =
|
||||
R_API void r_config_visual_hit_i(RConfig *cfg, const char *name, int delta) {
|
||||
RConfigNode *node =
|
||||
r_config_node_get(cfg, name);
|
||||
if (node && (node->flags & CN_INT || node->flags & CN_OFFT))
|
||||
r_config_set_i(cfg, name, r_config_get_i(cfg, name)+delta);
|
||||
|
@ -13,15 +13,12 @@ static char *r_core_project_file(const char *file) {
|
||||
|
||||
static int r_core_project_init() {
|
||||
int ret;
|
||||
char *str, buf[128];
|
||||
str = r_str_home (".radare2");
|
||||
ret = r_sys_mkdir (str);
|
||||
free (str);
|
||||
if (ret) {
|
||||
char *str = r_str_home (".radare2");
|
||||
if (str && (ret = r_sys_mkdir (str))) {
|
||||
str = r_str_home (".radare2/rdb");
|
||||
ret = r_sys_mkdir (str);
|
||||
free (str);
|
||||
}
|
||||
free (str);
|
||||
return ret;
|
||||
}
|
||||
|
||||
@ -34,15 +31,18 @@ R_API int r_core_project_open(RCore *core, const char *file) {
|
||||
}
|
||||
|
||||
R_API int r_core_project_save(RCore *core, const char *file) {
|
||||
int ret = R_TRUE;
|
||||
int fd, ret = R_TRUE;
|
||||
char *prj = r_core_project_file (file);
|
||||
int fd;
|
||||
r_core_project_init ();
|
||||
fd = open (prj, O_RDWR|O_CREAT, 0644);
|
||||
if (fd != -1) {
|
||||
r_cons_singleton ()->fdout = fd;
|
||||
write (fd, "# r2 rdb project file\n", 22);
|
||||
r_str_write (fd, "# r2 rdb project file\n");
|
||||
r_str_write (fd, "# flags\n");
|
||||
r_flag_list (&core->flags, R_TRUE);
|
||||
r_str_write (fd, "# eval\n");
|
||||
// TODO: r_str_writef (fd, "e asm.arch=%s", r_config_get ("asm.arch"));
|
||||
r_config_list (&core->config, NULL, R_TRUE);
|
||||
r_cons_flush ();
|
||||
r_cons_singleton ()->fdout = 1;
|
||||
close (fd);
|
||||
|
@ -184,6 +184,8 @@ R_API void r_num_init(struct r_num_t *num);
|
||||
#define ishexchar(x) ((x>='0'&&x<='9') || (x>='a'&&x<='f') || (x>='A'&&x<='F')) {
|
||||
|
||||
/* strings */
|
||||
#define r_str_write(x,y) write (x, y, strlen(y))
|
||||
R_API int r_str_writef(int fd, const char *fmt, ...);
|
||||
R_API char **r_str_argv(const char *str, int *_argc);
|
||||
R_API void r_str_argv_free(char **argv);
|
||||
R_API char *r_str_new(char *str);
|
||||
|
@ -280,13 +280,27 @@ R_API char *r_str_dup_printf(const char *fmt, ...) {
|
||||
char *ret;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
if ((ret = malloc(1024)) == NULL)
|
||||
if ((ret = malloc (1024)) == NULL)
|
||||
return NULL;
|
||||
vsnprintf(ret, 1023, fmt, ap);
|
||||
vsnprintf (ret, 1024, fmt, ap);
|
||||
va_end(ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
R_API int r_str_writef(int fd, const char *fmt, ...) {
|
||||
int ret = R_FALSE;
|
||||
char *buf;
|
||||
va_list ap;
|
||||
va_start (ap, fmt);
|
||||
if ((buf = malloc (4096)) != NULL)
|
||||
vsnprintf (buf, 4096, fmt, ap);
|
||||
r_str_write (fd, buf);
|
||||
free (buf);
|
||||
}
|
||||
va_end (ap);
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*
|
||||
* return: the pointer ptr resized to string size.
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user