Use PJ in oij ##json (#17642)

* Use PJ in `oj`
* Replace `r_cons_printf()` and other minor fixes
* Fix output and add a basic test for `oij`
* Use `pj_kb` in writable and raised
* Move `pj_new()` and `pj_a()` outside the loop
* Fix \`oij\` by creating a new PJ array before the case begins, add the objects, and print and free it at the end of the function.

Co-authored-by: Liumeo <liumeo@pku.edu.cn>
This commit is contained in:
Aswin C 2020-11-25 07:46:00 +05:30 committed by GitHub
parent b81cf6665f
commit 046a1fe324
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 31 additions and 10 deletions

View File

@ -1061,8 +1061,13 @@ R_API int r_core_file_list(RCore *core, int mode) {
RListIter *it;
RBinFile *bf;
RListIter *iter;
PJ *pj;
if (mode == 'j') {
r_cons_printf ("[");
pj = pj_new ();
if (!pj) {
return 0;
}
pj_a (pj);
}
r_list_foreach (core->files, iter, f) {
desc = r_io_desc_get (core->io, f->fd);
@ -1072,15 +1077,17 @@ R_API int r_core_file_list(RCore *core, int mode) {
}
from = 0LL;
switch (mode) {
case 'j':
r_cons_printf ("{\"raised\":%s,\"fd\":%d,\"uri\":\"%s\",\"from\":%"
PFMT64d ",\"writable\":%s,\"size\":%d}%s",
r_str_bool (core->io->desc->fd == f->fd),
(int) f->fd, desc->uri, (ut64) from,
r_str_bool (desc->perm & R_PERM_W),
(int) r_io_desc_size (desc),
iter->n? ",": "");
case 'j': { // "oij"
pj_o (pj);
pj_kb (pj, "raised", core->io->desc->fd == f->fd);
pj_ki (pj, "fd", f->fd);
pj_ks (pj, "uri", desc->uri);
pj_kn (pj, "from", (ut64) from);
pj_kb (pj, "writable", desc->perm & R_PERM_W);
pj_ki (pj, "size", (int) r_io_desc_size (desc));
pj_end (pj);
break;
}
case '*':
case 'r':
// TODO: use a getter
@ -1145,7 +1152,9 @@ R_API int r_core_file_list(RCore *core, int mode) {
count++;
}
if (mode == 'j') {
r_cons_printf ("]\n");
pj_end (pj);
r_cons_println (pj_string (pj));
pj_free (pj);
}
return count;
}

View File

@ -98,6 +98,17 @@ EXPECT=<<EOF
EOF
RUN
NAME=oij-pj
FILE=<<EOF
bins/elf/crackme0x05
bins/elf/crackme0x00b
EOF
CMDS=oij
EXPECT=<<EOF
[{"raised":false,"fd":3,"uri":"bins/elf/crackme0x05","from":0,"writable":false,"size":7656},{"raised":true,"fd":5,"uri":"bins/elf/crackme0x00b","from":0,"writable":false,"size":7375}]
EOF
RUN
NAME=o -
FILE=malloc://1024
CMDS=<<EOF

View File

@ -23,6 +23,7 @@ izj
izzj
Lsj
oj
oij
oLj
omj
p-j