Skip to content

Commit

Permalink
Use pj in anj ##refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Maijin authored Jan 17, 2019
1 parent 457154a commit c3bf8d1
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
2 changes: 1 addition & 1 deletion libr/core/canal.c
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ static char *anal_fcn_autoname(RCore *core, RAnalFunction *fcn, int dump, int mo
if (mode == 'j') {
// start a new JSON object
pj = pj_new ();
pj_a(pj);
pj_a (pj);
}
r_list_foreach (refs, iter, ref) {
RFlagItem *f = r_flag_get_i (core->flags, ref->addr);
Expand Down
46 changes: 35 additions & 11 deletions libr/core/cmd_anal.c
Original file line number Diff line number Diff line change
Expand Up @@ -865,10 +865,12 @@ static int cmd_an(RCore *core, bool use_json, const char *name)
ut64 off = core->offset;
RAnalOp op;
char *q = NULL;
PJ *pj = NULL;
ut64 tgt_addr = UT64_MAX;

if (use_json) {
r_cons_print ("[");
pj = pj_new ();
pj_a (pj);
}

r_anal_op (core->anal, &op, off,
Expand All @@ -886,8 +888,11 @@ static int cmd_an(RCore *core, bool use_json, const char *name)
} else if (!use_json) {
r_cons_println (bar->name);
} else {
r_cons_printf ("{\"type\":\"var\",\"name\":\"%s\"}",
bar->name);
pj_o (pj);
pj_ks (pj, "name", bar->name);
pj_ks (pj, "type", "var");
pj_kn (pj, "offset", tgt_addr);
pj_end (pj);
}
} else {
eprintf ("Cannot find variable\n");
Expand All @@ -904,32 +909,51 @@ static int cmd_an(RCore *core, bool use_json, const char *name)
} else if (!use_json) {
r_cons_println (fcn->name);
} else {
r_cons_printf ("{\"type\":\"function\",\"name\":\"%s\"}",
fcn->name);
pj_o (pj);
pj_ks (pj, "name", fcn->name);
pj_ks (pj, "type", "function");
pj_kn (pj, "offset", tgt_addr);
pj_end (pj);
}
} else if (f) {
if (name) {
q = r_str_newf ("fr %s %s", f->name, name);
} else if (!use_json) {
r_cons_println (f->name);
} else {
r_cons_printf ("{\"type\":\"flag\",\"name\":\"%s\"}",
f->name);
pj_o (pj);
if (name) {
pj_ks (pj, "old_name", f->name);
pj_ks (pj, "new_name", name);
} else {
pj_ks (pj, "name", f->name);
}
pj_ks (pj, "type", "flag");
pj_kn (pj, "offset", tgt_addr);
pj_end (pj);
}
} else {
if (name) {
q = r_str_newf ("f %s @ 0x%"PFMT64x, name, tgt_addr);
} else if (!use_json) {
r_cons_printf ("0x%" PFMT64x "\n", tgt_addr);
} else {
r_cons_printf ("{\"type\":\"address\",\"offset\":"
"%" PFMT64u "}", tgt_addr);
pj_o (pj);
pj_ks (pj, "name", name);
pj_ks (pj, "type", "address");
pj_kn (pj, "offset", tgt_addr);
pj_end (pj);
}
}
}

if (use_json) {
r_cons_print ("]\n");
pj_end (pj);
}

if (pj) {
r_cons_printf ("%s\n", pj_string (pj));
pj_free (pj);
}

if (q) {
Expand Down Expand Up @@ -5959,7 +5983,7 @@ static bool cmd_anal_refs(RCore *core, const char *input) {
if (input[2] == 'j') { // "axffj"
// start a new JSON object
pj = pj_new ();
pj_a(pj);
pj_a (pj);
}
if (fcn) {
RList *refs = r_anal_fcn_get_refs (core->anal, fcn);
Expand Down

0 comments on commit c3bf8d1

Please sign in to comment.