cmd_open refactoring and fix few warnings

This commit is contained in:
pancake 2015-02-03 00:51:20 +01:00
parent b4f506031b
commit b87733d554
3 changed files with 246 additions and 248 deletions

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2008-2014 - pancake */
/* radare - LGPL - Copyright 2008-2015 - pancake */
#include <r_cons.h>
#define I r_cons_singleton()
@ -74,7 +74,7 @@ static void filesave () {
}
R_API char *r_cons_editor (const char *file, const char *str) {
char *line;
const char *line;
_n = 0;
if (I->editor) {
return I->editor (I->user, file, str);
@ -88,7 +88,6 @@ R_API char *r_cons_editor (const char *file, const char *str) {
eprintf ("Loaded %d lines on %d bytes\n",
(nlines?(nlines-1):0), bytes);
} else path = NULL;
//r_cons_new ();
I->line->hist_up = up;
I->line->hist_down = down;
I->line->contents = I->line->buffer.data;

View File

@ -1,15 +1,240 @@
/* radare - LGPL - Copyright 2009-2015 - pancake */
static void cmd_open_bin(RCore *core, const char *input) {
const char* help_msg[] = {
"Usage:", "ob", " # List open binary files backed by fd",
"ob", "", "List opened binfiles and bin objects",
"ob", " [binfile #]", "Same as obo.",
"obb", " [binfile #]", "Prioritize by binfile number with current selected object",
"ob-", " [binfile #]", "Delete binfile",
"obd", " [binobject #]", "Delete binfile object numbers, if more than 1 object is loaded",
"obo", " [binobject #]", "Prioritize by bin object number",
"obs", " [bf #] [bobj #]", "Prioritize by binfile and object numbers",
NULL};
const char *value = NULL;
ut32 binfile_num = -1, binobj_num = -1;
switch (input[1]) {
case 0:
case 'l':
case 'j':
case '*':
r_core_bin_list (core, input[1]);
break;
case 's':
value = *(input+2) ? input+3 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binfile_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binfile_num == UT32_MAX) {
eprintf ("Invalid binfile number.");
break;
}
value = *(value+1) ? r_str_tok (value+1, ' ', -1) : NULL;
value = value && *(value+1) ? value+1 : NULL;
binobj_num = value && binfile_num != -1 &&
r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_raise (core, binfile_num, binobj_num);
break;
case 'b':
value = *(input+2) ? input+3 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binfile_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binfile_num == UT32_MAX) {
eprintf ("Invalid binfile number.");
break;
}
value = *(value+1) ? r_str_tok (value+1, ' ', -1) : NULL;
value = value && *(value+1) ? value+1 : NULL;
r_core_bin_raise (core, binfile_num, -1);
break;
case ' ':
case 'o':
value = *(input+2) ? input+3 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binobj_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_raise (core, -1, binobj_num);
break;
case '-': // "ob-"
if (input[2] == '*') {
r_cons_printf ("[i] Deleted %d binfiles\n", r_bin_file_delete_all (core->bin));
} else {
if (!r_bin_file_delete (core->bin, r_num_math (core->num, input+2))) {
eprintf ("Cant find an RBinFile associated with that fd.\n");
}
}
break;
case 'd': // backward compat, must be deleted
value = *(input+2) ? input+2 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binobj_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_delete (core, -1, binobj_num);
break;
case '?':
r_core_cmd_help (core, help_msg);
break;
}
}
static void cmd_open_map (RCore *core, const char *input) {
const char* help_msg[] = {
"Usage:", "om[-] [arg]", " # map opened files",
"om", "", "list all defined IO maps",
"om", "-0x10000", "remove the map at given address",
"om", " fd addr [size]", "create new io map",
"omr", " fd|0xADDR ADDR", "relocate current map",
NULL };
ut64 fd = 0LL;
ut64 addr = 0LL;
ut64 size = 0LL;
ut64 delta = 0LL;
char *s, *p, *q;
ut64 cur, new;
RIOMap *map = NULL;
const char *P;
switch (input[1]) {
case 'r':
if (input[2] != ' ')
break;
P = strchr (input+3, ' ');
if (P) {
cur = r_num_math (core->num, input+3);
new = r_num_math (core->num, P+1);
map = atoi (input+3)>0?
r_io_map_resolve (core->io, cur):
r_io_map_get (core->io, cur);
if (map) {
ut64 diff = map->to - map->from;
map->from = new;
map->to = new+diff;
} else eprintf ("Cannot find any map here\n");
} else {
cur = core->offset;
new = r_num_math (core->num, input+3);
map = r_io_map_resolve (core->io, core->file->desc->fd);
if (map) {
ut64 diff = map->to - map->from;
map->from = new;
map->to = new+diff;
} else eprintf ("Cannot find any map here\n");
}
break;
case ' ':
// i need to parse delta, offset, size
s = strdup (input+2);
p = strchr (s, ' ');
if (p) {
q = strchr (p+1, ' ');
*p = 0;
fd = r_num_math (core->num, s);
addr = r_num_math (core->num, p+1);
if (q) {
char *r = strchr (q+1, ' ');
*q = 0;
if (r) {
*r = 0;
size = r_num_math (core->num, q+1);
delta = r_num_math (core->num, r+1);
} else size = r_num_math (core->num, q+1);
} else size = r_io_size (core->io);
r_io_map_add (core->io, fd, 0, delta, addr, size);
} else eprintf ("Invalid use of om . See om? for help.");
free (s);
break;
case '-':
if (atoi (input+3)>0) {
r_io_map_del(core->io,
r_num_math (core->num, input+2));
} else {
r_io_map_del_at (core->io,
r_num_math (core->num, input+2));
}
break;
case '\0':
r_io_map_list (core->io);
break;
default:
case '?':
r_core_cmd_help (core, help_msg);
break;
}
r_core_block_read (core, 0);
}
static int cmd_open(void *data, const char *input) {
const char *help_msg[] = {
"Usage: o","[com- ] [file] ([offset])","",
"o","","list opened files",
"o*","","list opened files in r2 commands",
"oa"," [addr]","Open bin info from the given address",
"oj","","list opened files in JSON format",
"oc"," [file]","open core file, like relaunching r2",
"op"," ["R_LIB_EXT"]","open r2 native plugin (asm, bin, core, ..)",
"oo","","reopen current file (kill+fork in debugger)",
"oo","+","reopen current file in read-write",
"o"," 4","priorize io on fd 4 (bring to front)",
"o","-1","close file descriptor 1",
"o-","*","close all opened files",
"o--","","close all files, analysis, binfiles, flags, same as !r2 --",
"o"," /bin/ls","open /bin/ls file in read-only",
"o","+/bin/ls","open /bin/ls file in read-write mode",
"o"," /bin/ls 0x4000","map file at 0x4000",
"on"," /bin/ls 0x4000","map raw file at 0x4000 (no r_bin involved)",
"ob","[lbdos] [...]","list open binary files backed by fd",
"ob"," 4","priorize io and fd on 4 (bring to binfile to front)",
"om","[?]","create, list, remove IO maps",
NULL
};
const char* help_msg_oo[] = {
"Usage:", "oo[-] [arg]", " # map opened files",
"oo", "", "reopen current file",
"oob", "", "reopen loading rbin info",
"oon", "", "reopen without loading rbin info",
"oo+", "", "reopen in read-write",
NULL};
RCore *core = (RCore*)data;
int perms = R_IO_READ;
ut64 addr, baddr = r_config_get_i (core->config, "bin.baddr");
int nowarn = r_config_get_i (core->config, "file.nowarn");
RIOMap *map = NULL;
RCoreFile *file;
int num = -1;
int isn = 0;
char *ptr;
RListIter *iter;
switch (*input) {
case '=':
@ -22,10 +247,7 @@ static int cmd_open(void *data, const char *input) {
r_core_file_list (core, (int)(*input));
break;
case 'a':
{
RListIter *iter;
RCoreFile *file;
ut64 addr = core->offset;
addr = core->offset;
if (input[1]) {
addr = r_num_math (core->num, input+1);
}
@ -35,7 +257,6 @@ static int cmd_open(void *data, const char *input) {
addr, 0, 0); //, addr, "membin");
}
//r_bin_load_io_at_offset_as (core->bin, core->file->desc,
}
break;
case 'p':
if (r_sandbox_enable (0)) {
@ -51,9 +272,11 @@ static int cmd_open(void *data, const char *input) {
break;
case '+':
perms = R_IO_READ|R_IO_WRITE;
/* fall through */
case 'n':
// like in r2 -n
isn = 1;
/* fall through */
case ' ':
ptr = strchr (input+(isn?2:1), ' ');
if (ptr && ptr[1]=='0' && ptr[2]=='x') { // hack to fix opening files with space in path
@ -94,115 +317,7 @@ static int cmd_open(void *data, const char *input) {
r_core_block_read (core, 0);
break;
case 'b':
{
const char *value = NULL;
ut32 binfile_num = -1, binobj_num = -1;
switch (input[1]) {
case 0:
case 'l':
case 'j':
case '*':
r_core_bin_list (core, input[1]);
break;
case 's':
value = *(input+2) ? input+3 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binfile_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binfile_num == UT32_MAX) {
eprintf ("Invalid binfile number.");
break;
}
value = *(value+1) ? r_str_tok (value+1, ' ', -1) : NULL;
value = value && *(value+1) ? value+1 : NULL;
binobj_num = value && binfile_num != -1 && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_raise (core, binfile_num, binobj_num);
break;
case 'b':
value = *(input+2) ? input+3 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binfile_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binfile_num == UT32_MAX) {
eprintf ("Invalid binfile number.");
break;
}
value = *(value+1) ? r_str_tok (value+1, ' ', -1) : NULL;
value = value && *(value+1) ? value+1 : NULL;
r_core_bin_raise (core, binfile_num, -1);
break;
case ' ':
case 'o':
value = *(input+2) ? input+3 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binobj_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_raise (core, -1, binobj_num);
break;
case '-': // "ob-"
if (input[2] == '*') {
r_cons_printf ("[i] Deleted %d binfiles\n", r_bin_file_delete_all (core->bin));
} else {
if (!r_bin_file_delete (core->bin, r_num_math (core->num, input+2))) {
eprintf ("Cant find an RBinFile associated with that fd.\n");
}
}
break;
case 'd': // backward compat, must be deleted
value = *(input+2) ? input+2 : NULL;
if (!value) {
eprintf ("Invalid binfile number.");
break;
}
binobj_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_delete (core, -1, binobj_num);
break;
case '?':{
const char* help_msg[] = {
"Usage:", "ob", " # List open binary files backed by fd",
"ob", "", "List opened binfiles and bin objects",
"ob", " [binfile #]", "Same as obo.",
"obb", " [binfile #]", "Prioritize by binfile number with current selected object",
"ob-", " [binfile #]", "Delete binfile",
"obd", " [binobject #]", "Delete binfile object numbers, if more than 1 object is loaded",
"obo", " [binobject #]", "Prioritize by bin object number",
"obs", " [bf #] [bobj #]", "Prioritize by binfile and object numbers",
NULL};
r_core_cmd_help (core, help_msg);
}
}
}
cmd_open_bin (core, input);
break;
case '-':
switch (input[1]) {
@ -237,91 +352,7 @@ static int cmd_open(void *data, const char *input) {
//r_core_block_read (core, 0);
break;
case 'm':
switch (input[1]) {
case 'r':
{
ut64 cur, new;
const char *p;
if (input[2] != ' ')
break;
p = strchr (input+3, ' ');
if (p) {
cur = r_num_math (core->num, input+3);
new = r_num_math (core->num, p+1);
map = atoi (input+3)>0?
r_io_map_resolve (core->io, cur):
r_io_map_get (core->io, cur);
if (map) {
ut64 diff = map->to - map->from;
map->from = new;
map->to = new+diff;
} else eprintf ("Cannot find any map here\n");
} else {
cur = core->offset;
new = r_num_math (core->num, input+3);
map = r_io_map_resolve (core->io, core->file->desc->fd);
if (map) {
ut64 diff = map->to - map->from;
map->from = new;
map->to = new+diff;
} else eprintf ("Cannot find any map here\n");
}
}
break;
case ' ':
// i need to parse delta, offset, size
{
ut64 fd = 0LL;
ut64 addr = 0LL;
ut64 size = 0LL;
ut64 delta = 0LL;
char *s = strdup (input+2);
char *p = strchr (s, ' ');
if (p) {
char *q = strchr (p+1, ' ');
*p = 0;
fd = r_num_math (core->num, s);
addr = r_num_math (core->num, p+1);
if (q) {
char *r = strchr (q+1, ' ');
*q = 0;
if (r) {
*r = 0;
size = r_num_math (core->num, q+1);
delta = r_num_math (core->num, r+1);
} else size = r_num_math (core->num, q+1);
} else size = r_io_size (core->io);
r_io_map_add (core->io, fd, 0, delta, addr, size);
} else eprintf ("Invalid use of om . See om? for help.");
free (s);
}
break;
case '-':
if (atoi (input+3)>0) {
r_io_map_del(core->io,
r_num_math (core->num, input+2));
} else {
r_io_map_del_at (core->io,
r_num_math (core->num, input+2));
}
break;
case '\0':
r_io_map_list (core->io);
break;
default:
case '?':{
const char* help_msg[] = {
"Usage:", "om[-] [arg]", " # map opened files",
"om", "", "list all defined IO maps",
"om", "-0x10000", "remove the map at given address",
"om", " fd addr [size]", "create new io map",
"omr", " fd|0xADDR ADDR", "relocate current map",
NULL};
r_core_cmd_help (core, help_msg);
}
break;
}
r_core_block_read (core, 0);
cmd_open_map (core, input);
break;
case 'o':
switch (input[1]) {
@ -339,16 +370,7 @@ static int cmd_open(void *data, const char *input) {
break;
case '?':
default:
{
const char* help_msg[] = {
"Usage:", "oo[-] [arg]", " # map opened files",
"oo", "", "reopen current file",
"oob", "", "reopen loading rbin info",
"oon", "", "reopen without loading rbin info",
"oo+", "", "reopen in read-write",
NULL};
r_core_cmd_help (core, help_msg);
}
r_core_cmd_help (core, help_msg_oo);
break;
}
break;
@ -364,32 +386,7 @@ static int cmd_open(void *data, const char *input) {
break;
case '?':
default:
{
const char *help_msg[] = {
"Usage: o","[com- ] [file] ([offset])","",
"o","","list opened files",
"o*","","list opened files in r2 commands",
"oa"," [addr]","Open bin info from the given address",
"oj","","list opened files in JSON format",
"oc"," [file]","open core file, like relaunching r2",
"op"," ["R_LIB_EXT"]","open r2 native plugin (asm, bin, core, ..)",
"oo","","reopen current file (kill+fork in debugger)",
"oo","+","reopen current file in read-write",
"o"," 4","priorize io on fd 4 (bring to front)",
"o","-1","close file descriptor 1",
"o-","*","close all opened files",
"o--","","close all files, analysis, binfiles, flags, same as !r2 --",
"o"," /bin/ls","open /bin/ls file in read-only",
"o","+/bin/ls","open /bin/ls file in read-write mode",
"o"," /bin/ls 0x4000","map file at 0x4000",
"on"," /bin/ls 0x4000","map raw file at 0x4000 (no r_bin involved)",
"ob","[lbdos] [...]","list open binary files backed by fd",
"ob"," 4","priorize io and fd on 4 (bring to binfile to front)",
"om","[?]","create, list, remove IO maps",
NULL
};
r_core_cmd_help (core, help_msg);
}
break;
}
return 0;

View File

@ -185,6 +185,7 @@ R_API int r_lang_run_file(RLang *lang, const char *file) {
/* TODO: deprecate or make it more modular .. reading from stdin in a lib?!? wtf */
R_API int r_lang_prompt(RLang *lang) {
char buf[1024];
const char *p;
if (lang->cur == NULL)
return R_FALSE;
@ -199,6 +200,7 @@ R_API int r_lang_prompt(RLang *lang) {
RLineCompletion oc = line->completion;
RLineCompletion ocnull = {0};
char *prompt = strdup (line->prompt);
line->completion = ocnull;
line->history = histnull;
/* foo */
@ -212,7 +214,7 @@ R_API int r_lang_prompt(RLang *lang) {
if (feof (stdin)) break;
if (*buf) buf[strlen (buf)-1]='\0';
#endif
char *p = r_line_readline ();
p = r_line_readline ();
if (!p) break;
r_line_hist_add (p);
strncpy (buf, p, sizeof (buf) - 1);