Fix #23639 - Implement e+ command to set config vars in r2rc ##core

This commit is contained in:
pancake 2024-11-15 20:06:31 +01:00 committed by pancake
parent 41b9e74305
commit 0c9f8c4266
2 changed files with 74 additions and 5 deletions

View File

@ -627,6 +627,72 @@ static bool cmd_ec(RCore *core, const char *input) {
return true; return true;
} }
static void r2rc_set(RCore *core, R_NULLABLE const char *k, R_NULLABLE const char *v) {
char *rcfile = r_file_home (".radare2rc");
char *rcdata = r_file_slurp (rcfile, NULL);
if (k) {
char *line;
RListIter *iter;
RList *lines = r_str_split_list (rcdata, "\n", 0);
RStrBuf *sb = r_strbuf_new ("");
bool found = false;
char *kk = r_str_newf ("e %s", k);
r_list_foreach (lines, iter, line) {
const char *oline = line;
if (*oline == '\'') {
oline++;
}
if (r_str_startswith (oline, kk)) {
if (v) {
if (!found && *v) {
r_strbuf_appendf (sb, "'e %s=%s\n", k, v);
}
} else {
r_cons_println (line);
}
found = true;
} else {
r_strbuf_appendf (sb, "%s\n", line);
}
}
free (kk);
if (v) {
if (!found && *v) {
r_strbuf_appendf (sb, "'e %s=%s\n", k, v);
}
char *out = r_strbuf_drain (sb);
r_list_free (lines);
r_str_trim (out);
r_file_dump (rcfile, (const ut8*)out, -1, false);
free (out);
}
} else {
r_cons_println (rcdata);
}
free (rcdata);
free (rcfile);
}
static void cmd_eplus(RCore *core, const char *input) {
char *s = r_str_trim_dup (input);
char *eq = strchr (s, '=');
if (*s) {
if (eq) {
r_str_trim (s);
*eq++ = 0;
r_str_trim (eq);
const char *k = s;
const char *v = eq;
r2rc_set (core, k, v);
} else {
r2rc_set (core, s, NULL);
}
} else {
r2rc_set (core, NULL, NULL);
}
free (s);
}
static int cmd_eval(void *data, const char *input) { static int cmd_eval(void *data, const char *input) {
RCore *core = (RCore *)data; RCore *core = (RCore *)data;
switch (input[0]) { switch (input[0]) {
@ -640,9 +706,6 @@ static int cmd_eval(void *data, const char *input) {
default: r_config_list (core->config, input + 1, 3); break; default: r_config_list (core->config, input + 1, 3); break;
} }
break; break;
default:
r_core_return_invalid_command (core, "e", *input);
break;
case 't': // "et" case 't': // "et"
if (input[1] == 'a') { if (input[1] == 'a') {
r_cons_printf ("%s\n", (r_num_rand (10) % 2)? "wen": "son"); r_cons_printf ("%s\n", (r_num_rand (10) % 2)? "wen": "son");
@ -774,6 +837,9 @@ static int cmd_eval(void *data, const char *input) {
free (file); free (file);
} }
break; break;
case '+': // "e+"
cmd_eplus (core, input + 1);
break;
case 'e': // "ee" case 'e': // "ee"
if (input[1] == ' ') { if (input[1] == ' ') {
char *p; char *p;
@ -842,6 +908,9 @@ static int cmd_eval(void *data, const char *input) {
} }
} }
break; break;
default:
r_core_return_invalid_command (core, "e", *input);
break;
} }
return 0; return 0;
} }

View File

@ -494,7 +494,7 @@ static const char *radare_argv[] = {
"dts?", "dts", "dts+", "dts-", "dtsf", "dtst", "dtsC", "dtt", "dts?", "dts", "dts+", "dts-", "dtsf", "dtst", "dtsC", "dtt",
"dw", "dw",
"dx?", "dx", "dxa", "dxe", "dxr", "dxs", "dx?", "dx", "dxa", "dxe", "dxr", "dxs",
"e?", "e", "-e", "-i", "e-", "e*", "e!", "ec", "ee?", "ee", "?ed", "ed", "ej", "env", "er", "es", "et", "ev", "evj", "e?", "e", "e+", "-e", "-i", "e-", "e*", "e!", "ec", "ee?", "ee", "?ed", "ed", "ej", "env", "er", "es", "et", "ev", "evj",
"ec?", "ec", "ec*", "ecd", "ecr", "ecs", "ecj", "ecc", "eco", "ecp", "ecn", "ec?", "ec", "ec*", "ecd", "ecr", "ecs", "ecj", "ecc", "eco", "ecp", "ecn",
"ecH?", "ecH", "ecHi", "ecHw", "ecH-", "ecH?", "ecH", "ecHi", "ecHw", "ecH-",
"f?", "f", "f.", "f*", "f-", "f--", "f+", "f=", "fa", "fb", "fc?", "fc", "fC", "fd", "fe-", "fe", "f?", "f", "f.", "f*", "f-", "f--", "f+", "f=", "fa", "fb", "fc?", "fc", "fC", "fd", "fe-", "fe",
@ -2212,7 +2212,7 @@ static void __init_autocomplete_default(RCore* core) {
"afc", "axg", "axt", "axf", "dcu", "ag", "agfl", "aecu", "aesu", "aeim", NULL "afc", "axg", "axt", "axf", "dcu", "ag", "agfl", "aecu", "aesu", "aeim", NULL
}; };
const char *evals[] = { const char *evals[] = {
"-e", "e", "ee", "et", "e?", "e!", "ev", "evj", NULL "-e", "e", "e+", "ee", "et", "e?", "e!", "ev", "evj", NULL
}; };
const char *breaks[] = { const char *breaks[] = {
"db-", "dbc", "dbC", "dbd", "dbe", "dbs", "dbi", "dbte", "dbtd", "dbts", NULL "db-", "dbc", "dbC", "dbd", "dbe", "dbs", "dbi", "dbte", "dbtd", "dbts", NULL