2016-02-15 23:10:46 +00:00
|
|
|
/* radare2 - LGPL - Copyright 2009-2016 - pancake */
|
2016-07-03 18:56:44 +00:00
|
|
|
|
2016-05-03 02:52:41 +00:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include "r_core.h"
|
2014-05-27 23:07:02 +00:00
|
|
|
|
2016-03-06 11:35:07 +00:00
|
|
|
static char *curtheme = NULL;
|
|
|
|
static bool getNext = false;
|
|
|
|
|
2016-08-14 17:29:59 +00:00
|
|
|
static bool load_theme(RCore *core, const char *path) {
|
|
|
|
core->cmdfilter = "ec ";
|
|
|
|
bool res = r_core_cmd_file (core, path);
|
|
|
|
core->cmdfilter = NULL;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2016-06-07 21:19:42 +00:00
|
|
|
static bool nextpal_item(RCore *core, int mode, const char *file, int ctr) {
|
2016-03-06 11:35:07 +00:00
|
|
|
const char *fn = r_str_lchr (file, '/');
|
|
|
|
if (!fn) fn = file;
|
|
|
|
switch (mode) {
|
2016-06-07 21:19:42 +00:00
|
|
|
case 'j': // json
|
|
|
|
r_cons_printf ("%s\"%s\"", ctr?",":"", fn);
|
|
|
|
break;
|
2016-03-06 11:35:07 +00:00
|
|
|
case 'l': // list
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (fn);
|
2016-03-06 11:35:07 +00:00
|
|
|
break;
|
2016-03-23 21:13:55 +00:00
|
|
|
case 'p': // previous
|
|
|
|
// TODO: move logic here
|
|
|
|
break;
|
2016-03-06 11:35:07 +00:00
|
|
|
case 'n': // next
|
|
|
|
if (getNext) {
|
|
|
|
curtheme = r_str_dup (curtheme, fn);
|
|
|
|
getNext = false;
|
|
|
|
return false;
|
|
|
|
} else if (curtheme) {
|
|
|
|
if (!strcmp (curtheme, fn)) {
|
|
|
|
getNext = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
curtheme = r_str_dup (curtheme, fn);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void nextpal(RCore *core, int mode) {
|
2016-03-07 11:54:40 +00:00
|
|
|
RList *files = NULL;
|
2016-03-06 11:35:07 +00:00
|
|
|
RListIter *iter;
|
|
|
|
const char *fn;
|
2016-06-07 21:19:42 +00:00
|
|
|
int ctr = 0;
|
2016-03-06 11:35:07 +00:00
|
|
|
char *home = r_str_home (".config/radare2/cons/");
|
|
|
|
|
|
|
|
getNext = false;
|
2016-06-07 21:19:42 +00:00
|
|
|
if (mode == 'j') {
|
|
|
|
r_cons_printf ("[");
|
|
|
|
}
|
2016-03-06 11:35:07 +00:00
|
|
|
if (home) {
|
|
|
|
files = r_sys_dir (home);
|
|
|
|
r_list_foreach (files, iter, fn) {
|
|
|
|
if (*fn && *fn != '.') {
|
2016-03-23 21:13:55 +00:00
|
|
|
if (mode == 'p') {
|
|
|
|
const char *nfn = iter->n? iter->n->data: NULL;
|
2016-03-31 11:21:51 +00:00
|
|
|
if (!curtheme) {
|
|
|
|
free (home);
|
|
|
|
r_list_free (files);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-23 21:13:55 +00:00
|
|
|
eprintf ("%s %s %s\n", nfn, curtheme, fn);
|
|
|
|
if (nfn && !strcmp (nfn, curtheme)) {
|
|
|
|
r_list_free (files);
|
|
|
|
files = NULL;
|
|
|
|
free (curtheme);
|
|
|
|
curtheme = strdup (fn);
|
|
|
|
R_FREE (home);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-07 21:19:42 +00:00
|
|
|
if (!nextpal_item (core, mode, fn, ctr++)) {
|
2016-03-23 21:13:55 +00:00
|
|
|
r_list_free (files);
|
|
|
|
files = NULL;
|
|
|
|
R_FREE (home);
|
|
|
|
goto done;
|
|
|
|
}
|
2016-03-06 11:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
r_list_free (files);
|
|
|
|
R_FREE (home);
|
|
|
|
}
|
|
|
|
files = r_sys_dir (R2_DATDIR"/radare2/"R2_VERSION"/cons/");
|
|
|
|
r_list_foreach (files, iter, fn) {
|
|
|
|
if (*fn && *fn != '.') {
|
2016-03-23 21:13:55 +00:00
|
|
|
if (mode == 'p') {
|
|
|
|
const char *nfn = iter->n? iter->n->data: NULL;
|
2016-03-31 11:21:51 +00:00
|
|
|
if (!curtheme) {
|
|
|
|
free (home);
|
|
|
|
r_list_free (files);
|
|
|
|
return;
|
|
|
|
}
|
2016-03-23 21:13:55 +00:00
|
|
|
eprintf ("%s %s %s\n", nfn, curtheme, fn);
|
|
|
|
if (nfn && !strcmp (nfn, curtheme)) {
|
|
|
|
free (curtheme);
|
|
|
|
curtheme = strdup (fn);
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
} else {
|
2016-06-07 21:19:42 +00:00
|
|
|
if (!nextpal_item (core, mode, fn, ctr++)) {
|
2016-03-23 21:13:55 +00:00
|
|
|
goto done;
|
2016-06-07 21:19:42 +00:00
|
|
|
}
|
2016-03-23 21:13:55 +00:00
|
|
|
}
|
2016-03-06 11:35:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
done:
|
|
|
|
if (getNext) {
|
|
|
|
R_FREE (curtheme);
|
|
|
|
nextpal (core, mode);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mode == 'l' && !curtheme && !r_list_empty (files)) {
|
2016-03-23 21:13:55 +00:00
|
|
|
//nextpal (core, mode);
|
2016-03-07 11:54:40 +00:00
|
|
|
} else {
|
|
|
|
if (curtheme) {
|
|
|
|
r_core_cmdf (core, "eco %s", curtheme);
|
|
|
|
}
|
2016-03-06 11:35:07 +00:00
|
|
|
}
|
|
|
|
r_list_free (files);
|
2016-03-07 11:54:40 +00:00
|
|
|
files = NULL;
|
2016-06-07 21:19:42 +00:00
|
|
|
if (mode == 'j') {
|
|
|
|
r_cons_printf ("]\n");
|
|
|
|
}
|
2016-03-06 11:35:07 +00:00
|
|
|
}
|
|
|
|
|
2014-05-27 23:07:02 +00:00
|
|
|
static int cmd_eval(void *data, const char *input) {
|
|
|
|
char *p;
|
|
|
|
RCore *core = (RCore *)data;
|
|
|
|
switch (input[0]) {
|
2016-02-15 23:10:46 +00:00
|
|
|
case 't': // env
|
|
|
|
if (input[1]==' ' && input[2]) {
|
|
|
|
RConfigNode *node = r_config_node_get (core->config, input+2);
|
|
|
|
if (node) {
|
|
|
|
const char *type = r_config_node_type (node);
|
|
|
|
if (type && *type) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (type);
|
2016-02-15 23:10:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
eprintf ("Usage: et [varname] ; show type of eval var\n");
|
|
|
|
}
|
|
|
|
break;
|
2014-05-27 23:07:02 +00:00
|
|
|
case 'n': // env
|
|
|
|
if (!strchr (input, '=')) {
|
|
|
|
char *var, *p;
|
|
|
|
var = strchr (input, ' ');
|
|
|
|
if (var) while (*var==' ') var++;
|
|
|
|
p = r_sys_getenv (var);
|
|
|
|
if (p) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (p);
|
2014-05-27 23:07:02 +00:00
|
|
|
free (p);
|
|
|
|
} else {
|
|
|
|
char **e = r_sys_get_environ ();
|
|
|
|
while (e && *e) {
|
2016-06-26 04:51:17 +00:00
|
|
|
r_cons_println (*e);
|
2014-05-27 23:07:02 +00:00
|
|
|
e++;
|
|
|
|
}
|
|
|
|
}
|
2014-11-19 22:23:35 +00:00
|
|
|
} else if (strlen (input)>3) {
|
2014-05-27 23:07:02 +00:00
|
|
|
char *v, *k = strdup (input+3);
|
|
|
|
if (!k) break;
|
|
|
|
v = strchr (k, '=');
|
|
|
|
if (v) {
|
|
|
|
*v++ = 0;
|
|
|
|
r_sys_setenv (k, v);
|
|
|
|
}
|
|
|
|
free (k);
|
|
|
|
}
|
2015-09-14 10:35:38 +00:00
|
|
|
return true;
|
2014-05-27 23:07:02 +00:00
|
|
|
case 'x': // exit
|
2016-05-03 02:52:41 +00:00
|
|
|
// XXX we need headers for the cmd_xxx files.
|
2014-05-27 23:07:02 +00:00
|
|
|
return cmd_quit (data, "");
|
|
|
|
case 'j':
|
|
|
|
r_config_list (core->config, NULL, 'j');
|
|
|
|
break;
|
2016-03-30 23:16:19 +00:00
|
|
|
case '\0': // "e"
|
2014-05-27 23:07:02 +00:00
|
|
|
r_config_list (core->config, NULL, 0);
|
|
|
|
break;
|
2016-03-30 23:16:19 +00:00
|
|
|
case 'c': // "ec"
|
2014-05-27 23:07:02 +00:00
|
|
|
switch (input[1]) {
|
|
|
|
case 'd':
|
|
|
|
r_cons_pal_init (NULL);
|
|
|
|
break;
|
2014-10-29 09:35:21 +00:00
|
|
|
case '?': {
|
|
|
|
const char *helpmsg[] = {
|
2014-10-24 23:10:56 +00:00
|
|
|
"Usage ec[s?] [key][[=| ]fg] [bg]","","",
|
|
|
|
"ec","","list all color keys",
|
|
|
|
"ec*","","same as above, but using r2 commands",
|
|
|
|
"ecd","","set default palette",
|
|
|
|
"ecr","","set random palette",
|
|
|
|
"ecs","","show a colorful palette",
|
2014-11-19 22:23:35 +00:00
|
|
|
"ecj","","show palette in JSON",
|
2016-07-03 18:56:44 +00:00
|
|
|
"ecc"," [prefix]","show palette in CSS",
|
2014-10-29 09:35:21 +00:00
|
|
|
"eco"," dark|white","load white color scheme template",
|
2016-03-23 21:13:55 +00:00
|
|
|
"ecp","","load previous color theme",
|
2016-03-06 11:35:07 +00:00
|
|
|
"ecn","","load next color theme",
|
2014-10-24 23:10:56 +00:00
|
|
|
"ec"," prompt red","change color of prompt",
|
2015-02-04 10:51:57 +00:00
|
|
|
"ec"," prompt red blue","change color and background of prompt",
|
2014-10-24 23:10:56 +00:00
|
|
|
""," ","",
|
|
|
|
"colors:","","rgb:000, red, green, blue, ...",
|
|
|
|
"e scr.rgbcolor","=1|0","for 256 color cube (boolean)",
|
|
|
|
"e scr.truecolor","=1|0","for 256*256*256 colors (boolean)",
|
|
|
|
"$DATADIR/radare2/cons","","~/.config/radare2/cons ./",
|
2014-10-29 09:35:21 +00:00
|
|
|
NULL};
|
|
|
|
r_core_cmd_help (core, helpmsg);
|
|
|
|
}
|
2014-05-27 23:07:02 +00:00
|
|
|
break;
|
2014-10-29 09:35:21 +00:00
|
|
|
case 'o': // "eco"
|
2016-06-07 21:19:42 +00:00
|
|
|
if (input[2] == 'j') {
|
|
|
|
nextpal (core, 'j');
|
|
|
|
} else if (input[2] == ' ') {
|
2016-03-06 11:35:07 +00:00
|
|
|
bool failed = false;
|
2014-05-27 23:07:02 +00:00
|
|
|
char *home, path[512];
|
2016-07-03 18:56:44 +00:00
|
|
|
snprintf (path, sizeof (path), ".config/radare2/cons/%s", input + 3);
|
2014-05-27 23:07:02 +00:00
|
|
|
home = r_str_home (path);
|
|
|
|
snprintf (path, sizeof (path), R2_DATDIR"/radare2/"
|
2016-08-14 17:29:59 +00:00
|
|
|
R2_VERSION"/cons/%s", input + 3);
|
|
|
|
if (!load_theme (core, home)) {
|
|
|
|
if (load_theme (core, path)) {
|
2016-03-06 11:35:07 +00:00
|
|
|
//curtheme = r_str_dup (curtheme, path);
|
|
|
|
curtheme = r_str_dup (curtheme, input + 3);
|
|
|
|
} else {
|
2016-08-14 17:29:59 +00:00
|
|
|
if (load_theme (core, input + 3)) {
|
2016-03-06 11:35:07 +00:00
|
|
|
curtheme = r_str_dup (curtheme, input + 3);
|
|
|
|
} else {
|
2014-10-29 09:35:21 +00:00
|
|
|
eprintf ("eco: cannot open colorscheme profile (%s)\n", path);
|
2016-03-06 11:35:07 +00:00
|
|
|
failed = true;
|
|
|
|
}
|
2014-05-27 23:07:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-03-06 11:35:07 +00:00
|
|
|
free (home);
|
2016-03-16 12:33:50 +00:00
|
|
|
if (failed) {
|
|
|
|
eprintf ("Something went wrong\n");
|
|
|
|
}
|
2016-07-03 18:56:44 +00:00
|
|
|
} else if (input[2] == '?') {
|
2016-07-02 18:36:06 +00:00
|
|
|
eprintf ("Usage: eco [themename] ;load theme from "R2_DATDIR"/radare2/"R2_VERSION"/cons/\n");
|
2016-03-14 16:06:50 +00:00
|
|
|
|
2016-03-06 11:35:07 +00:00
|
|
|
} else {
|
|
|
|
nextpal (core, 'l');
|
2014-05-27 23:07:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-03-30 23:16:19 +00:00
|
|
|
case 's': r_cons_pal_show (); break; // "ecs"
|
2016-07-03 18:56:44 +00:00
|
|
|
case '*': r_cons_pal_list (1, NULL); break; // "ec*"
|
2016-05-03 02:52:41 +00:00
|
|
|
case 'h': // echo
|
2016-03-30 23:16:19 +00:00
|
|
|
if (( p = strchr (input, ' ') )) {
|
|
|
|
r_cons_strcat (p+1);
|
|
|
|
r_cons_newline ();
|
|
|
|
} else {
|
2016-07-03 18:56:44 +00:00
|
|
|
// "ech"
|
|
|
|
r_cons_pal_list ('h', NULL);
|
2016-03-30 23:16:19 +00:00
|
|
|
}
|
|
|
|
break;
|
2016-07-03 18:56:44 +00:00
|
|
|
case 'j': // "ecj"
|
|
|
|
r_cons_pal_list ('j', NULL);
|
|
|
|
break;
|
|
|
|
case 'c': // "ecc"
|
|
|
|
r_cons_pal_list ('c', input + 2);
|
|
|
|
break;
|
|
|
|
case '\0': // "ec"
|
|
|
|
r_cons_pal_list (0, NULL);
|
|
|
|
break;
|
2016-03-06 11:35:07 +00:00
|
|
|
case 'r': // "ecr"
|
|
|
|
r_cons_pal_random ();
|
|
|
|
break;
|
|
|
|
case 'n': // "ecn"
|
|
|
|
nextpal (core, 'n');
|
|
|
|
break;
|
2016-03-23 21:13:55 +00:00
|
|
|
case 'p': // "ecp"
|
|
|
|
nextpal (core, 'p');
|
|
|
|
break;
|
2014-05-27 23:07:02 +00:00
|
|
|
default: {
|
2016-03-06 11:35:07 +00:00
|
|
|
char *p = strdup (input + 2);
|
2014-05-27 23:07:02 +00:00
|
|
|
char *q = strchr (p, '=');
|
|
|
|
if (!q) q = strchr (p, ' ');
|
|
|
|
if (q) {
|
|
|
|
// set
|
|
|
|
*q++ = 0;
|
|
|
|
r_cons_pal_set (p, q);
|
|
|
|
} else {
|
|
|
|
const char *k = r_cons_pal_get (p);
|
|
|
|
if (k)
|
|
|
|
eprintf ("(%s)(%sCOLOR"Color_RESET")\n", p, k);
|
|
|
|
}
|
|
|
|
free (p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'e':
|
|
|
|
if (input[1]==' ') {
|
|
|
|
char *p;
|
2014-10-29 09:35:21 +00:00
|
|
|
const char *val, *input2 = strchr (input+2, ' ');
|
2014-05-27 23:07:02 +00:00
|
|
|
if (input2) input2++; else input2 = input+2;
|
|
|
|
val = r_config_get (core->config, input2);
|
2014-12-19 02:17:28 +00:00
|
|
|
p = r_core_editor (core, NULL, val);
|
2014-05-27 23:07:02 +00:00
|
|
|
if (p) {
|
|
|
|
r_str_replace_char (p, '\n', ';');
|
|
|
|
r_config_set (core->config, input2, p);
|
|
|
|
}
|
|
|
|
} else eprintf ("Usage: ee varname\n");
|
|
|
|
break;
|
|
|
|
case '!':
|
|
|
|
input = r_str_chop_ro (input+1);
|
2015-09-17 18:20:51 +00:00
|
|
|
if (!r_config_toggle (core->config, input))
|
2014-05-27 23:07:02 +00:00
|
|
|
eprintf ("r_config: '%s' is not a boolean variable.\n", input);
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
r_core_config_init (core);
|
2016-01-25 02:27:19 +00:00
|
|
|
//eprintf ("BUG: 'e-' command locks the eval hashtable. patches are welcome :)\n");
|
2014-05-27 23:07:02 +00:00
|
|
|
break;
|
|
|
|
case 'v': eprintf ("Invalid command '%s'. Use 'e?'\n", input); break;
|
|
|
|
case '*': r_config_list (core->config, NULL, 1); break;
|
|
|
|
case '?':
|
|
|
|
switch (input[1]) {
|
|
|
|
case '?': r_config_list (core->config, input+2, 2); break;
|
|
|
|
default: r_config_list (core->config, input+1, 2); break;
|
2014-06-28 14:35:15 +00:00
|
|
|
case 0:{
|
|
|
|
const char* help_msg[] = {
|
|
|
|
"Usage:", "e[?] [var[=value]]", "Evaluable vars",
|
|
|
|
"e","?asm.bytes", "show description",
|
|
|
|
"e", "??", "list config vars with description",
|
|
|
|
"e", "", "list config vars",
|
|
|
|
"e-", "", "reset config vars",
|
|
|
|
"e*", "", "dump config vars in r commands",
|
|
|
|
"e!", "a", "invert the boolean value of 'a' var",
|
2015-12-27 20:32:18 +00:00
|
|
|
"ee", "var", "open editor to change the value of var",
|
2014-06-28 14:35:15 +00:00
|
|
|
"er", " [key]", "set config key as readonly. no way back",
|
|
|
|
"ec", " [k] [color]", "set color for given key (prompt, offset, ...)",
|
2016-02-15 23:10:46 +00:00
|
|
|
"et", " [key]", "show type of given config variable",
|
2014-06-28 14:35:15 +00:00
|
|
|
"e", " a", "get value of var 'a'",
|
|
|
|
"e", " a=b", "set var 'a' the 'b' value",
|
|
|
|
"env", " [k[=v]]", "get/set environment variable",
|
|
|
|
NULL};
|
|
|
|
r_core_cmd_help (core, help_msg);
|
|
|
|
}
|
2014-05-27 23:07:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
if (input[1]) {
|
|
|
|
const char *key = input+((input[1]==' ')?2:1);
|
|
|
|
if (!r_config_readonly (core->config, key))
|
|
|
|
eprintf ("cannot find key '%s'\n", key);
|
|
|
|
} else eprintf ("Usage: er [key]\n");
|
|
|
|
break;
|
|
|
|
case ' ': r_config_eval (core->config, input+1); break;
|
|
|
|
default: r_config_eval (core->config, input); break;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|