Fix #3763 - Add oi command to raise/close fd by index

This commit is contained in:
pancake 2016-11-02 03:27:37 +01:00
parent f411df9881
commit d7c13a36b1
3 changed files with 100 additions and 36 deletions

View File

@ -317,6 +317,7 @@ static int cmd_open(void *data, const char *input) {
"ob","[lbdos] [...]","list open binary files backed by fd",
"ob"," 4","priorize io and fd on 4 (bring to binfile to front)",
"oc"," [file]","open core file, like relaunching r2",
"oi","[-|idx]","alias for o, but using index instead of fd",
"oj","","list opened files in JSON format",
"oL","","list all IO plugins registered",
"om","[?]","create, list, remove IO maps",
@ -440,12 +441,52 @@ static int cmd_open(void *data, const char *input) {
return 0;
}
if (input[1]==' ') {
if (r_lib_open (core->lib, input+2) == R_FAIL)
if (r_lib_open (core->lib, input+2) == R_FAIL) {
eprintf ("Oops\n");
}
} else {
eprintf ("Usage: op [r2plugin."R_LIB_EXT"]\n");
}
break;
case 'i': // "oi"
switch (input[1]) {
case ' ': // "oi "
{
RListIter *iter = NULL;
RCoreFile *f;
int nth = r_num_math (core->num, input + 2);
int count = 0;
r_list_foreach (core->files, iter, f) {
if (count == nth) {
r_io_raise (core->io, num);
break;
}
count++;
}
}
break;
case '-': // "oi-"
{
RListIter *iter = NULL;
RCoreFile *f;
int nth = r_num_math (core->num, input + 2);
int count = 0;
r_list_foreach (core->files, iter, f) {
if (count == nth) {
r_core_file_close_fd (core, f->desc->fd);
break;
}
count++;
}
}
break;
case 'j':
case '*':
case 0:
r_core_file_list (core, input[1]);
break;
}
break;
case '+':
perms = R_IO_READ|R_IO_WRITE;
/* fall through */
@ -520,9 +561,10 @@ static int cmd_open(void *data, const char *input) {
// TODO: rbin?
break;
default:
if (!r_core_file_close_fd (core, atoi (input+1)))
if (!r_core_file_close_fd (core, atoi (input + 1))) {
eprintf ("Unable to find filedescriptor %d\n",
atoi (input + 1));
}
break;
case '?':
eprintf ("Usage: o-# or o-*, where # is the filedescriptor number\n");

View File

@ -879,8 +879,9 @@ R_API int r_core_file_list(RCore *core, int mode) {
RCoreFile *f;
ut64 from;
RListIter *iter;
if (mode=='j')
if (mode == 'j') {
r_cons_printf ("[");
}
r_list_foreach (core->files, iter, f) {
if (f->map) {
from = f->map->from;
@ -909,12 +910,13 @@ R_API int r_core_file_list(RCore *core, int mode) {
ut64 sz = r_io_desc_size (core->io, f->desc);
const char *fmt;
if (sz == UT64_MAX) {
fmt = "%c %d %s @ 0x%"PFMT64x" ; %s size=%"PFMT64d" %s\n";
fmt = "%c %d %d %s @ 0x%"PFMT64x" ; %s size=%"PFMT64d" %s\n";
} else {
fmt = "%c %d %s @ 0x%"PFMT64x" ; %s size=%"PFMT64u" %s\n";
fmt = "%c %d %d %s @ 0x%"PFMT64x" ; %s size=%"PFMT64u" %s\n";
}
r_cons_printf (fmt,
core->io->raised == f->desc->fd?'*':'-',
count,
(int)f->desc->fd, f->desc->uri, (ut64)from,
f->desc->flags & R_IO_WRITE? "rw": "r",
r_io_desc_size (core->io, f->desc),
@ -1039,9 +1041,10 @@ R_API int r_core_hash_load(RCore *r, const char *file) {
R_API RCoreFile * r_core_file_find_by_fd (RCore *core, ut64 fd) {
RListIter *iter;
RCoreFile *cf = NULL;
r_list_foreach (core->files, iter, cf) {
if (cf && cf->desc && cf->desc->fd == fd) break;
if (cf && cf->desc && cf->desc->fd == fd) {
break;
}
cf = NULL;
}
return cf;
@ -1052,7 +1055,9 @@ R_API RCoreFile * r_core_file_find_by_name (RCore * core, const char * name) {
RCoreFile *cf = NULL;
r_list_foreach (core->files, iter, cf) {
if (cf && cf->desc && !strcmp (cf->desc->name, name)) break;
if (cf && cf->desc && !strcmp (cf->desc->name, name)) {
break;
}
cf = NULL;
}
return cf;
@ -1087,7 +1092,7 @@ R_API ut32 r_core_file_cur_fd (RCore *core) {
if (desc) {
return desc->fd;
}
return (ut32)-1; //WTF
return UT32_MAX;
}
R_API RCoreFile * r_core_file_cur (RCore *r) {

View File

@ -5,16 +5,19 @@
R_API int r_space_get(RSpaces *f, const char *name) {
int i;
for (i = 0; i < R_SPACES_MAX; i++) {
if (f->spaces[i] != NULL)
if (!strcmp (name, f->spaces[i]))
if (f->spaces[i]) {
if (!strcmp (name, f->spaces[i])) {
return i;
}
}
}
return -1;
}
R_API const char *r_space_get_i (RSpaces *f, int idx) {
if (idx==-1 || idx>=R_SPACES_MAX|| !f || !f->spaces[idx] || !*f->spaces[idx])
if (idx==-1 || idx>=R_SPACES_MAX|| !f || !f->spaces[idx] || !*f->spaces[idx]) {
return "";
}
return f->spaces[idx];
}
@ -27,15 +30,15 @@ R_API void r_space_init(RSpaces *f, void (*unset_for)(void*,int), int (*count_fo
f->unset_for = unset_for;
f->count_for = count_for;
f->user = user;
for (i=0;i<R_SPACES_MAX;i++)
for (i = 0; i < R_SPACES_MAX; i++) {
f->spaces[i] = NULL;
}
}
R_API void r_space_fini(RSpaces *f) {
int i;
for (i = 0; i < R_SPACES_MAX; i++) {
free (f->spaces[i]);
f->spaces[i] = NULL;
R_FREE (f->spaces[i]);
}
r_list_free (f->spacestack);
}
@ -96,7 +99,9 @@ R_API int r_space_unset (RSpaces *f, const char *fs) {
return r_space_set (f, NULL);
}
for (i = 0; i < R_SPACES_MAX; i++) {
if (!f->spaces[i]) continue;
if (!f->spaces[i]) {
continue;
}
if (!fs || !strcmp (fs, f->spaces[i])) {
if (f->space_idx == i) {
f->space_idx = -1;
@ -124,8 +129,9 @@ static int r_space_count (RSpaces *f, int n) {
R_API int r_space_list(RSpaces *f, int mode) {
const char *defspace = NULL;
int count, len, i, j = 0;
if (mode == 'j')
if (mode == 'j') {
f->cb_printf ("[");
}
for (i = 0; i < R_SPACES_MAX; i++) {
if (!f->spaces[i]) continue;
count = r_space_count (f, i);
@ -146,30 +152,41 @@ R_API int r_space_list(RSpaces *f, int mode) {
len = strlen (num0) + strlen (num1);
if (len < INDENT) {
spaces[INDENT-len] = 0;
} else spaces[0] = 0;
} else {
spaces[0] = 0;
}
f->cb_printf ("%s%s %s %c %s\n", num0, spaces, num1,
(i==f->space_idx)?'*':'.',
f->spaces[i]);
}
j++;
}
if (defspace)
if (defspace) {
f->cb_printf ("fs %s # current\n", defspace);
if (mode == 'j')
}
if (mode == 'j') {
f->cb_printf ("]\n");
}
return j;
}
R_API int r_space_rename (RSpaces *f, const char *oname, const char *nname) {
int i;
if (!oname) {
if (f->space_idx == -1)
if (f->space_idx == -1) {
return false;
}
oname = f->spaces[f->space_idx];
}
if (!nname) return false;
while (*oname==' ') oname++;
while (*nname==' ') nname++;
if (!nname) {
return false;
}
while (*oname==' ') {
oname++;
}
while (*nname==' ') {
nname++;
}
for (i = 0; i < R_SPACES_MAX; i++) {
if (f->spaces[i] && !strcmp (oname, f->spaces[i])) {
free (f->spaces[i]);