From ecf5205e644433748bd4cb58ef8feb90ac761b59 Mon Sep 17 00:00:00 2001 From: pancake Date: Thu, 20 Aug 2015 02:24:46 +0200 Subject: [PATCH] Add support for remote aliases with `=$` command For example =$dr - `dr` will do `=!dr` =$-dr - unset that alias command --- libr/core/cmd.c | 33 +++++++++++++++++++++++++++++---- libr/core/cmd_anal.c | 8 ++++---- libr/core/cmd_api.c | 37 +++++++++++++++++++++++++++++++------ libr/include/r_cmd.h | 5 +++-- 4 files changed, 67 insertions(+), 16 deletions(-) diff --git a/libr/core/cmd.c b/libr/core/cmd.c index e0923bd88c..c76be82e1b 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -129,7 +129,7 @@ static int cmd_alias(void *data, const char *input) { } if (!q || (q && q>def)) { - if (*def) r_cmd_alias_set (core->rcmd, buf, def); + if (*def) r_cmd_alias_set (core->rcmd, buf, def, 0); else r_cmd_alias_del (core->rcmd, buf); } @@ -137,7 +137,7 @@ static int cmd_alias(void *data, const char *input) { } else if (desc && !q) { char *v; *desc = 0; - v = r_cmd_alias_get (core->rcmd, buf); + v = r_cmd_alias_get (core->rcmd, buf, 0); if (v) { r_cons_printf ("%s\n", v); free (buf); @@ -151,7 +151,7 @@ static int cmd_alias(void *data, const char *input) { int i, count = 0; char **keys = r_cmd_alias_keys (core->rcmd, &count); for (i=0; ircmd, keys[i]); + const char *v = r_cmd_alias_get (core->rcmd, keys[i], 0); r_cons_printf ("%s=%s\n", keys[i], v); } } else if (!buf[1]) { @@ -164,7 +164,7 @@ static int cmd_alias(void *data, const char *input) { } else { char *v; if (q) *q = 0; - v = r_cmd_alias_get (core->rcmd, buf); + v = r_cmd_alias_get (core->rcmd, buf, 0); if (v) { if (q) { char *out, *args = q+1; @@ -196,9 +196,34 @@ static int getArg(char ch, int def) { return def; } +static void aliascmd(RCore *core, const char *str) { + switch (str[0]) { + case '-': + if (str[1]) { + r_cmd_alias_del (core->rcmd, str+2); + } else { + r_cmd_alias_del (core->rcmd, NULL); + // r_cmd_alias_reset (core->rcmd); + } + break; + case '?': + eprintf ("Usage: =$[-][remotecmd] # remote command alias\n"); + eprintf (" =$dr # makes 'dr' alias for =!dr\n"); + eprintf (" =$-dr # unset 'dr' alias\n"); + break; + case 0: + r_core_cmd0 (core, "$"); + break; + default: + r_cmd_alias_set (core->rcmd, str, "", 1); + break; + } +} + static int cmd_rap(void *data, const char *input) { RCore *core = (RCore *)data; switch (*input) { + case '$': aliascmd (core, input+1); break; case '\0': r_core_rtr_list (core); break; case 'h': r_core_rtr_http (core, getArg(input[1],'h'), input+1); break; case 'H': while (input[1]==' ') input++; diff --git a/libr/core/cmd_anal.c b/libr/core/cmd_anal.c index 5be24014a9..4e665a358b 100644 --- a/libr/core/cmd_anal.c +++ b/libr/core/cmd_anal.c @@ -1277,13 +1277,13 @@ void cmd_anal_reg(RCore *core, const char *str) { } } if (r) { - eprintf ("%s 0x%08"PFMT64x" -> ", str, - r_reg_get_value (core->dbg->reg, r)); + //eprintf ("%s 0x%08"PFMT64x" -> ", str, + // r_reg_get_value (core->dbg->reg, r)); r_reg_set_value (core->dbg->reg, r, r_num_math (core->num, arg+1)); r_debug_reg_sync (core->dbg, -1, R_TRUE); - eprintf ("0x%08"PFMT64x"\n", - r_reg_get_value (core->dbg->reg, r)); + //eprintf ("0x%08"PFMT64x"\n", + // r_reg_get_value (core->dbg->reg, r)); } else { eprintf ("ar: Unknown register '%s'\n", regname); } diff --git a/libr/core/cmd_api.c b/libr/core/cmd_api.c index 0d4331bd66..8270268f33 100644 --- a/libr/core/cmd_api.c +++ b/libr/core/cmd_api.c @@ -61,6 +61,7 @@ R_API void r_cmd_alias_free (RCmd *cmd) { cmd->aliases.count = 0; free (cmd->aliases.keys); free (cmd->aliases.values); + free (cmd->aliases.remote); cmd->aliases.keys = NULL; cmd->aliases.values = NULL; } @@ -68,7 +69,7 @@ R_API void r_cmd_alias_free (RCmd *cmd) { R_API int r_cmd_alias_del (RCmd *cmd, const char *k) { int i; // find for (i=0; ialiases.count; i++) { - if (!strcmp (k, cmd->aliases.keys[i])) { + if (!k || !strcmp (k, cmd->aliases.keys[i])) { free (cmd->aliases.values[i]); cmd->aliases.values[i] = NULL; cmd->aliases.count--; @@ -92,10 +93,12 @@ R_API int r_cmd_alias_del (RCmd *cmd, const char *k) { return 0; } -R_API int r_cmd_alias_set (RCmd *cmd, const char *k, const char *v) { +R_API int r_cmd_alias_set (RCmd *cmd, const char *k, const char *v, int remote) { int i; // find for (i=0; ialiases.count; i++) { - if (!strcmp (k, cmd->aliases.keys[i])) { + int matches; + matches = !strcmp (k, cmd->aliases.keys[i]); + if (matches) { free (cmd->aliases.values[i]); cmd->aliases.values[i] = strdup (v); return 1; @@ -105,18 +108,30 @@ R_API int r_cmd_alias_set (RCmd *cmd, const char *k, const char *v) { i = cmd->aliases.count++; cmd->aliases.keys = (char **)realloc (cmd->aliases.keys, sizeof (char**)*cmd->aliases.count); + cmd->aliases.remote = (int *)realloc (cmd->aliases.remote, + sizeof (int*)*cmd->aliases.count); cmd->aliases.values = (char **)realloc (cmd->aliases.values, sizeof (char**)*cmd->aliases.count); cmd->aliases.keys[i] = strdup (k); cmd->aliases.values[i] = strdup (v); + cmd->aliases.remote[i] = remote; return 0; } -R_API char *r_cmd_alias_get (RCmd *cmd, const char *k) { +R_API char *r_cmd_alias_get (RCmd *cmd, const char *k, int remote) { int i; // find for (i=0; ialiases.count; i++) { - if (!strcmp (k, cmd->aliases.keys[i])) + int matches; + if (remote && cmd->aliases.remote[i]) { + matches = !strncmp (k, cmd->aliases.keys[i], + strlen (cmd->aliases.keys[i])); + } else { + matches = !strcmp (k, cmd->aliases.keys[i]); + } + if (matches) { + //eprintf ("MATHI S memleak asegurado\n"); return cmd->aliases.values[i]; + } } return NULL; } @@ -172,11 +187,20 @@ R_API int r_cmd_call(RCmd *cmd, const char *input) { if (cmd->nullcallback != NULL) ret = cmd->nullcallback (cmd->data); } else { + char *nstr = NULL; + const char *ji = r_cmd_alias_get (cmd, input, 1); + if (ji) { + nstr = r_str_newf ("=!%s", input); + input = nstr; + } r_list_foreach (cmd->plist, iter, cp) { - if (cp->call (cmd->data, input)) + if (cp->call (cmd->data, input)) { + free (nstr); return R_TRUE; + } } if (input[0] == -1) { + free (nstr); return -1; } c = cmd->cmds[((ut8)input[0]) & 0xff]; @@ -184,6 +208,7 @@ R_API int r_cmd_call(RCmd *cmd, const char *input) { const char *inp = (input && *input)? input+1: ""; ret = c->callback (cmd->data, inp); } else ret = -1; + free (nstr); } return ret; } diff --git a/libr/include/r_cmd.h b/libr/include/r_cmd.h index 061fe5c569..c6b066c799 100644 --- a/libr/include/r_cmd.h +++ b/libr/include/r_cmd.h @@ -64,6 +64,7 @@ typedef struct r_cmd_alias_t { int count; char **keys; char **values; + int *remote; } RCmdAlias; typedef struct r_cmd_t { @@ -112,8 +113,8 @@ R_API int r_cmd_macro_break(RCmdMacro *mac, const char *value); R_API int r_cmd_alias_del (RCmd *cmd, const char *k); R_API char **r_cmd_alias_keys(RCmd *cmd, int *sz); -R_API int r_cmd_alias_set (RCmd *cmd, const char *k, const char *v); -R_API char *r_cmd_alias_get (RCmd *cmd, const char *k); +R_API int r_cmd_alias_set (RCmd *cmd, const char *k, const char *v, int remote); +R_API char *r_cmd_alias_get (RCmd *cmd, const char *k, int remote); R_API void r_cmd_alias_free (RCmd *cmd); #ifdef __cplusplus