-
-
Notifications
You must be signed in to change notification settings - Fork 363
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
9,744 additions
and
8,867 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
// SPDX-FileCopyrightText: 2024 RizinOrg <[email protected]> | ||
// SPDX-License-Identifier: LGPL-3.0-only | ||
|
||
#include "rz_cmd.h" | ||
#include "rz_core.h" | ||
|
||
static const char *help_msg_dollar[] = { | ||
"Usage:", "$alias[=cmd] [args...]", "Alias commands and strings (see %$? for help on $variables)", | ||
"$", "", "list all defined aliases", | ||
"$*", "", "list all the aliases as rizin commands in base64", | ||
"$**", "", "same as above, but using plain text", | ||
"$", "foo:=123", "alias for 'f foo @ 123'", | ||
"$", "foo-=4", "alias for 'fm $$-4 @ foo'", | ||
"$", "foo+=4", "alias for 'fm $$+4 @ foo'", | ||
"$", "foo", "alias for 's foo' (note that command aliases can override flag resolution)", | ||
"$", "dis=base64:cGRm", "alias this base64 encoded text to be executed when $dis is called", | ||
"$", "dis=$hello world", "alias this text to be printed when $dis is called", | ||
"$", "dis=-", "open cfg.editor to set the new value for dis alias", | ||
"\"$", "dis=af;pdf\"", "create command - analyze to show function", // Move quotation marks inside | ||
"\"$", "test=#!pipe node /tmp/test.js\"", "create command - rlangpipe script", // Move quotation marks inside | ||
"$", "dis=", "undefine alias", | ||
"$", "dis", "execute the previously defined alias", | ||
"$", "dis?", "show commands aliased by $dis", | ||
"$", "dis?n", "show commands aliased by $dis, without a new line", | ||
NULL | ||
}; | ||
|
||
static int rz_cmd_alias(void *data, const char *input) { | ||
RzCore *core = (RzCore *)data; | ||
if (*input == '?') { | ||
rz_core_cmd_help(core, help_msg_dollar); | ||
return 0; | ||
} | ||
int i = strlen(input); | ||
char *buf = malloc(i + 2); | ||
if (!buf) { | ||
return 0; | ||
} | ||
*buf = '$'; // prefix aliases with a dollar | ||
memcpy(buf + 1, input, i + 1); | ||
char *q = strchr(buf, ' '); | ||
char *def = strchr(buf, '='); | ||
char *desc = strchr(buf, '?'); | ||
char *nonl = strchr(buf, 'n'); | ||
|
||
int defmode = 0; | ||
if (def && def > buf) { | ||
char *prev = def - 1; | ||
switch (*prev) { | ||
case ':': | ||
defmode = *prev; | ||
*prev = 0; | ||
break; | ||
case '+': | ||
defmode = *prev; | ||
*prev = 0; | ||
break; | ||
case '-': | ||
defmode = *prev; | ||
*prev = 0; | ||
break; | ||
} | ||
} | ||
|
||
/* create alias */ | ||
if ((def && q && (def < q)) || (def && !q)) { | ||
*def++ = 0; | ||
size_t len = strlen(def); | ||
if (defmode) { | ||
ut64 at = rz_num_math(core->num, def); | ||
switch (defmode) { | ||
case ':': | ||
rz_flag_set(core->flags, buf + 1, at, 1); | ||
return 1; | ||
case '+': | ||
at = rz_num_get(core->num, buf + 1) + at; | ||
rz_flag_set(core->flags, buf + 1, at, 1); | ||
return 1; | ||
case '-': | ||
at = rz_num_get(core->num, buf + 1) - at; | ||
rz_flag_set(core->flags, buf + 1, at, 1); | ||
return 1; | ||
} | ||
} | ||
/* Remove quotes */ | ||
if (len > 0 && (def[0] == '\'') && (def[len - 1] == '\'')) { | ||
def[len - 1] = 0x00; | ||
def++; | ||
} | ||
if (!q || (q && q > def)) { | ||
if (*def) { | ||
if (!strcmp(def, "-")) { | ||
char *v = rz_cmd_alias_get(core->rcmd, buf, 0); | ||
char *n = rz_core_editor(core, NULL, v); | ||
if (n) { | ||
rz_cmd_alias_set(core->rcmd, buf, n, 0); | ||
free(n); | ||
} | ||
} else { | ||
rz_cmd_alias_set(core->rcmd, buf, def, 0); | ||
} | ||
} else { | ||
rz_cmd_alias_del(core->rcmd, buf); | ||
} | ||
} | ||
/* Show command for alias */ | ||
} else if (desc && !q) { | ||
*desc = 0; | ||
char *v = rz_cmd_alias_get(core->rcmd, buf, 0); | ||
if (v) { | ||
if (nonl == desc + 1) { | ||
rz_cons_print(v); | ||
} else { | ||
rz_cons_println(v); | ||
} | ||
free(buf); | ||
return 1; | ||
} else { | ||
RZ_LOG_ERROR("core: unknown key '%s'\n", buf); | ||
} | ||
} else if (buf[1] == '*') { | ||
/* Show aliases */ | ||
int i, count = 0; | ||
char **keys = rz_cmd_alias_keys(core->rcmd, &count); | ||
for (i = 0; i < count; i++) { | ||
char *v = rz_cmd_alias_get(core->rcmd, keys[i], 0); | ||
char *q = rz_base64_encode_dyn((const ut8 *)v, strlen(v)); | ||
if (buf[2] == '*') { | ||
rz_cons_printf("%s=%s\n", keys[i], v); | ||
} else { | ||
rz_cons_printf("%s=base64:%s\n", keys[i], q); | ||
} | ||
free(q); | ||
} | ||
} else if (!buf[1]) { | ||
int i, count = 0; | ||
char **keys = rz_cmd_alias_keys(core->rcmd, &count); | ||
for (i = 0; i < count; i++) { | ||
rz_cons_println(keys[i]); | ||
} | ||
} else { | ||
/* Execute alias */ | ||
if (q) { | ||
*q = 0; | ||
} | ||
char *v = rz_cmd_alias_get(core->rcmd, buf, 0); | ||
if (v) { | ||
if (*v == '$') { | ||
rz_cons_strcat(v + 1); | ||
rz_cons_newline(); | ||
} else if (q) { | ||
char *out = rz_str_newf("%s %s", v, q + 1); | ||
rz_core_cmd0(core, out); | ||
free(out); | ||
} else { | ||
rz_core_cmd0(core, v); | ||
} | ||
} else { | ||
ut64 at = rz_num_get(core->num, buf + 1); | ||
if (at != UT64_MAX) { | ||
rz_core_seek(core, at, true); | ||
} else { | ||
RZ_LOG_ERROR("core: unknown alias '%s'\n", buf + 1); | ||
} | ||
} | ||
} | ||
free(buf); | ||
return 0; | ||
} | ||
|
||
RZ_IPI RzCmdStatus rz_alias_handler(RzCore *core, int argc, const char **argv) { | ||
rz_cmd_alias(core, argc > 1 ? argv[1] : ""); | ||
return RZ_CMD_STATUS_OK; | ||
} | ||
|
||
RZ_IPI RzCmdStatus rz_alias_list_cmd_base64_handler(RzCore *core, int argc, const char **argv) { | ||
rz_cmd_alias(core, "*"); | ||
return RZ_CMD_STATUS_OK; | ||
} | ||
|
||
RZ_IPI RzCmdStatus rz_alias_list_cmd_plain_handler(RzCore *core, int argc, const char **argv) { | ||
rz_cmd_alias(core, "**"); | ||
return RZ_CMD_STATUS_OK; | ||
} |
Oops, something went wrong.