add regexp to /c/

This commit is contained in:
Jeffrey Crowell 2015-07-08 14:02:36 -04:00
parent 05c8e5c198
commit f5095cff88
3 changed files with 25 additions and 6 deletions

View File

@ -59,15 +59,17 @@ R_API char* r_core_asm_search(RCore *core, const char *input, ut64 from, ut64 to
#define OPSZ 8
// TODO: add support for byte-per-byte opcode search
R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut64 to, int maxhits) {
R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut64 to, int maxhits, int regexp) {
RCoreAsmHit *hit;
RAsmOp op;
RList *hits;
ut64 at, toff = core->offset;
ut8 *buf;
RRegex* rx = NULL;
char *tok, *tokens[1024], *code = NULL, *ptr;
int idx, tidx = 0, ret, len;
int tokcount, matchcount, count = 0;
int matches = 0;
if (!*input)
return NULL;
@ -96,6 +98,7 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6
tokens[tokcount] = NULL;
r_cons_break (NULL, NULL);
for (at = from, matchcount = 0; at < to; at += core->blocksize-OPSZ) {
matches = 0;
if (r_cons_singleton ()->breaked)
break;
ret = r_io_read_at (core->io, at, buf, core->blocksize);
@ -111,7 +114,15 @@ R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut6
matchcount = 0;
continue;
}
if (tokens[matchcount] && strstr (op.buf_asm, tokens[matchcount])) {
if (tokens[matchcount]) {
if (!regexp) matches = strstr(op.buf_asm, tokens[matchcount]);
else {
rx = r_regex_new (tokens[matchcount], "");
matches = r_regex_exec (rx, op.buf_asm, 0, 0, 0) == 0;
r_regex_free (rx);
}
}
if (matches) {
code = r_str_concatf (code, "%s; ", op.buf_asm);
if (matchcount == tokcount-1) {
if (tokcount == 1)

View File

@ -1288,7 +1288,14 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch
int kwidx = core->search->n_kws; //(int)r_config_get_i (core->config, "search.kwidx")-1;
RList *hits;
RIOMap *map;
int outmode = input[1];
int regexp = input[1] == '/';
char *end_cmd = strstr (input, " ");
int outmode;
if (!end_cmd) {
outmode = input[1];
} else {
outmode = *(end_cmd - 1);
}
if (outmode != 'j')
json = 0;
@ -1322,7 +1329,7 @@ static void do_asm_search(RCore *core, struct search_parameters *param, const ch
if (outmode == 0) hits = NULL;
else hits = r_core_asm_strsearch (core, input+2,
param->from, param->to, maxhits);
param->from, param->to, maxhits, regexp);
if (hits) {
r_list_foreach (hits, iter, hit) {
@ -1998,8 +2005,9 @@ static int cmd_search(void *data, const char *input) {
case 'c': /* search asm */
if (input[1] == '?') {
const char* help_msg[] = {
"Usage:", "/c [inst]", "Search for asm",
"Usage:", "/c [inst]", " Search for asm",
"/c ", "instr", "search for instruction 'instr'",
"/c/ ", "instr", "search for instruction that matches regexp 'instr'",
"/c ", "instr1;instr2", "search for instruction 'instr1' followed by 'instr2'",
"/cj ", "instr", "json output",
"/c* ", "instr", "r2 command output",

View File

@ -322,7 +322,7 @@ R_API RCoreAsmHit *r_core_asm_hit_new(void);
R_API RList *r_core_asm_hit_list_new(void);
R_API void r_core_asm_hit_free(void *_hit);
R_API char* r_core_asm_search(RCore *core, const char *input, ut64 from, ut64 to);
R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut64 to, int maxhits);
R_API RList *r_core_asm_strsearch(RCore *core, const char *input, ut64 from, ut64 to, int maxhits, int regexp);
R_API RList *r_core_asm_bwdisassemble (RCore *core, ut64 addr, int n, int len);
R_API RList *r_core_asm_back_disassemble_instr (RCore *core, ut64 addr, int len, ut32 hit_count, ut32 extra_padding);
R_API RList *r_core_asm_back_disassemble_byte (RCore *core, ut64 addr, int len, ut32 hit_count, ut32 extra_padding);