2014-04-23 01:54:06 +00:00
|
|
|
/* radare - LGPL - Copyright 2009-2014 - pancake */
|
2012-05-24 23:14:49 +00:00
|
|
|
|
2012-02-27 01:40:27 +00:00
|
|
|
static int cmd_macro(void *data, const char *input) {
|
2012-05-30 01:23:53 +00:00
|
|
|
char *buf = NULL;
|
2012-02-27 01:40:27 +00:00
|
|
|
RCore *core = (RCore*)data;
|
2012-05-24 23:14:49 +00:00
|
|
|
|
2012-02-27 01:40:27 +00:00
|
|
|
switch (*input) {
|
2013-03-14 01:32:53 +00:00
|
|
|
case ')': r_cmd_macro_break (&core->rcmd->macro, input+1); break;
|
|
|
|
case '-': r_cmd_macro_rm (&core->rcmd->macro, input+1); break;
|
2016-02-06 23:49:26 +00:00
|
|
|
case '*': r_cmd_macro_meta (&core->rcmd->macro); break;
|
2013-03-14 01:32:53 +00:00
|
|
|
case '\0': r_cmd_macro_list (&core->rcmd->macro); break;
|
2013-12-22 02:24:12 +00:00
|
|
|
case '(':
|
2014-09-09 00:39:07 +00:00
|
|
|
case '?': {
|
|
|
|
const char* help_msg[] = {
|
|
|
|
"Usage:", "(foo args,cmd1,cmd2,..)", "Aliases",
|
|
|
|
"(foo args,..,..)", "", "define a macro",
|
|
|
|
"(foo args,..,..)()", "", "define and call a macro",
|
|
|
|
"(-foo)", "", "remove a macro",
|
|
|
|
".(foo)", "", "to call it",
|
|
|
|
"()", "", "break inside macro",
|
|
|
|
"(*", "", "list all defined macros",
|
|
|
|
"", "Argument support:", "",
|
|
|
|
"(foo x y\\n$0 @ $1)", "", "define fun with args",
|
|
|
|
".(foo 128 0x804800)", "", "call it with args",
|
|
|
|
"", "Iterations:", "",
|
|
|
|
".(foo\\n() $@)", "", "define iterator returning iter index",
|
|
|
|
"x @@ .(foo)", "", "iterate over them",
|
|
|
|
NULL};
|
|
|
|
r_core_cmd_help (core, help_msg);
|
|
|
|
}
|
2012-02-27 01:40:27 +00:00
|
|
|
break;
|
2012-05-24 23:14:49 +00:00
|
|
|
default: {
|
|
|
|
// XXX: stop at first ')'. if next is '(' and last
|
2012-05-30 01:23:53 +00:00
|
|
|
//int lastiscp = input[strlen (input)-1] == ')';
|
2012-05-24 23:14:49 +00:00
|
|
|
int mustcall =0;
|
|
|
|
int i, j = 0;
|
|
|
|
buf = strdup (input);
|
|
|
|
|
|
|
|
for (i=0; buf[i]; i++) {
|
|
|
|
switch (buf[i]) {
|
|
|
|
case '(': j++; break;
|
2016-02-06 23:49:26 +00:00
|
|
|
case ')': j--;
|
2012-05-24 23:14:49 +00:00
|
|
|
if (buf[i+1] =='(') {
|
|
|
|
buf[i+1] = 0;
|
|
|
|
mustcall = i+2;
|
|
|
|
} break;
|
|
|
|
}
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
2012-05-24 23:14:49 +00:00
|
|
|
buf[strlen(buf)-1]=0;
|
2012-09-19 12:08:44 +00:00
|
|
|
r_cmd_macro_add (&core->rcmd->macro, buf);
|
2012-05-24 23:14:49 +00:00
|
|
|
if (mustcall) {
|
2012-07-12 01:55:09 +00:00
|
|
|
char *comma = strchr (buf, ' ');
|
|
|
|
if (!comma)
|
|
|
|
comma = strchr (buf, ',');
|
2012-05-24 23:14:49 +00:00
|
|
|
if (comma) {
|
|
|
|
*comma = ' ';
|
|
|
|
strcpy (comma+1, buf+mustcall);
|
2012-09-19 12:08:44 +00:00
|
|
|
r_cmd_macro_call (&core->rcmd->macro, buf);
|
2012-05-24 23:14:49 +00:00
|
|
|
} else eprintf ("Invalid syntax for macro\n");
|
|
|
|
}
|
2012-02-27 01:40:27 +00:00
|
|
|
free (buf);
|
2012-05-24 23:14:49 +00:00
|
|
|
} break;
|
2012-02-27 01:40:27 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|