Skip to content

Commit

Permalink
Fix fl. commands and add tests (#3678)
Browse files Browse the repository at this point in the history
  • Loading branch information
XVilka authored Jul 20, 2023
1 parent f1006f9 commit 47701f6
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 46 deletions.
75 changes: 29 additions & 46 deletions librz/core/cflag.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@
struct print_flag_t {
RzFlag *f;
PJ *pj;
RzTable *tbl;
bool in_range;
ut64 range_from;
ut64 range_to;
RzSpace *fs;
bool real;
};

static bool print_flag_name(RzFlagItem *fi, void *user) {
rz_cons_printf("%s\n", fi->name);
static bool print_flag_name(RzFlagItem *flag, void *user) {
struct print_flag_t *u = (struct print_flag_t *)user;
if (u->in_range && (flag->offset < u->range_from || flag->offset >= u->range_to)) {
return true;
}
rz_cons_printf("%s\n", flag->name);
return true;
}

Expand Down Expand Up @@ -95,71 +100,49 @@ static bool print_flag_orig_name(RzFlagItem *flag, void *user) {
return true;
}

typedef struct {
RzTable *t;
} FlagTableData;

static bool __tableItemCallback(RzFlagItem *flag, void *user) {
FlagTableData *ftd = user;
static bool print_flag_table(RzFlagItem *flag, void *user) {
struct print_flag_t *u = (struct print_flag_t *)user;
if (u->in_range && (flag->offset < u->range_from || flag->offset >= u->range_to)) {
return true;
}
if (!RZ_STR_ISEMPTY(flag->name)) {
RzTable *t = ftd->t;
const char *spaceName = (flag->space && flag->space->name) ? flag->space->name : "";
rz_table_add_rowf(t, "Xdss", flag->offset, flag->size, spaceName, flag->name);
rz_table_add_rowf(u->tbl, "Xdss", flag->offset, flag->size, spaceName, flag->name);
}
return true;
}

static void flag_print(RzFlag *f, RzCmdStateOutput *state, ut64 range_from, ut64 range_to, bool in_range) {
rz_return_if_fail(f);
struct print_flag_t u = {
.f = f,
.in_range = in_range,
.range_from = range_from,
.range_to = range_to,
.real = false
};

switch (state->mode) {
case RZ_OUTPUT_MODE_QUIET:
rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_name, f);
rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_name, &u);
break;
case RZ_OUTPUT_MODE_STANDARD: {
struct print_flag_t u = {
.f = f,
.in_range = in_range,
.range_from = range_from,
.range_to = range_to,
.real = false
};
case RZ_OUTPUT_MODE_STANDARD:
rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_orig_name, &u);
break;
}
case RZ_OUTPUT_MODE_JSON: {
struct print_flag_t u = {
.f = f,
.pj = state->d.pj,
.in_range = in_range,
.range_from = range_from,
.range_to = range_to,
.real = false
};
case RZ_OUTPUT_MODE_JSON:
u.pj = state->d.pj;
pj_a(state->d.pj);
rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_json, &u);
pj_end(state->d.pj);
break;
}
case RZ_OUTPUT_MODE_RIZIN: {
struct print_flag_t u = {
.f = f,
.in_range = in_range,
.range_from = range_from,
.range_to = range_to,
.fs = NULL,
};
case RZ_OUTPUT_MODE_RIZIN:
rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_rizin, &u);
break;
}
case RZ_OUTPUT_MODE_TABLE: {
FlagTableData ftd = { 0 };
ftd.t = state->d.t;
case RZ_OUTPUT_MODE_TABLE:
u.tbl = state->d.t;
rz_cmd_state_output_set_columnsf(state, "Xdss", "addr", "size", "space", "name");

RzSpace *curSpace = rz_flag_space_cur(f);
rz_flag_foreach_space(f, curSpace, __tableItemCallback, &ftd);
rz_flag_foreach_space(f, rz_flag_space_cur(f), print_flag_table, &u);
break;
}
default:
rz_warn_if_reached();
break;
Expand Down
30 changes: 30 additions & 0 deletions test/db/cmd/cmd_flags
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,36 @@ bla ~
EOF
RUN

NAME=flag list at
FILE==
CMDS=<<EOF
f blah 0x0000000a @ 0x0000000b
f bloh 0x00000003 @ 0x00000002
fl. @ 0x0000000b
fl.j @ 0x0000000b
fl.t @ 0x0000000b
fl.q @ 0x0000000b
fl. @ 0x00000002
fl.j @ 0x00000002
fl.t @ 0x00000002
fl.q @ 0x00000002
EOF
EXPECT=<<EOF
0x0000000b 10 blah
[{"name":"blah","size":10,"offset":11}]
addr size space name
---------------------------
0x0000000b 10 blah
blah
0x00000002 3 bloh
[{"name":"bloh","size":3,"offset":2}]
addr size space name
---------------------------
0x00000002 3 bloh
bloh
EOF
RUN

NAME=no useless flags at 0
FILE=bins/elf/ls
CMDS=<<EOF
Expand Down

0 comments on commit 47701f6

Please sign in to comment.