diff --git a/binr/rafind2/rafind2.c b/binr/rafind2/rafind2.c index 7a6499394b..bbac7e16df 100644 --- a/binr/rafind2/rafind2.c +++ b/binr/rafind2/rafind2.c @@ -22,7 +22,7 @@ static char *mask = ""; static int nonstop = 0; static int mode = R_SEARCH_STRING; static ut64 cur = 0; -static ut8 *buffer = NULL; +static ut8 *buf = NULL; static char *curfile = NULL; static ut64 bsize = 4096; static int hexstr = 0; @@ -35,11 +35,11 @@ static int hit(RSearchKeyword *kw, void *user, ut64 addr) { printf ("f hit%d_%d 0x%08"PFMT64x" ; %s\n", 0, kw->count, addr, curfile); } else { if (showstr) { - printf ("0x%"PFMT64x" %s\n", addr, buffer+delta); + printf ("0x%"PFMT64x" %s\n", addr, buf+delta); } else { printf ("0x%"PFMT64x"\n", addr); if (pr) { - r_print_hexdump (pr, addr, (ut8*)buffer+delta, 78, 16, R_TRUE); + r_print_hexdump (pr, addr, (ut8*)buf+delta, 78, 16, R_TRUE); r_cons_flush (); } } @@ -86,12 +86,12 @@ static int rafind_open(char *file) { r_cons_new (); rs = r_search_new (mode); - buffer = malloc (bsize); - if (buffer==NULL) { + buf = malloc (bsize); + if (buf==NULL) { eprintf ("Cannot allocate %"PFMT64d" bytes\n", bsize); return 1; } - r_search_set_callback (rs, &hit, buffer); + r_search_set_callback (rs, &hit, buf); if (to == -1) to = r_io_size(io); if (mode == R_SEARCH_STRING) { @@ -117,7 +117,7 @@ static int rafind_open(char *file) { bsize = to-cur; last=1; } - ret = r_io_read_at (io, cur, buffer, bsize); + ret = r_io_read_at (io, cur, buf, bsize); if (ret == 0) { if (nonstop) continue; // fprintf(stderr, "Error reading at 0x%08"PFMT64x"\n", cur); @@ -126,14 +126,14 @@ static int rafind_open(char *file) { if (ret != bsize) bsize = ret; - if (r_search_update (rs, &cur, buffer, ret) == -1) { + if (r_search_update (rs, &cur, buf, ret) == -1) { eprintf ("search: update read error at 0x%08"PFMT64x"\n", cur); break; } } rs = r_search_free (rs); - free (buffer); + free (buf); return 0; } diff --git a/libr/cons/Makefile b/libr/cons/Makefile index a137631d29..e1ec068491 100644 --- a/libr/cons/Makefile +++ b/libr/cons/Makefile @@ -2,8 +2,8 @@ include ../config.mk NAME=r_cons OBJS=cons.o pipe.o output.o grep.o -OBJS+=line.o hud.o rgb.o input.o -DEPS=r_util +OBJS+=line.o hud.o rgb.o input.o pal.o +DEPS=r_util r_db include ../rules.mk diff --git a/libr/cons/cons.c b/libr/cons/cons.c index 3b074771c2..57c5c4ceb5 100644 --- a/libr/cons/cons.c +++ b/libr/cons/cons.c @@ -139,7 +139,8 @@ R_API RCons *r_cons_new () { eprintf ("r_cons: Cannot set control console handler\n"); #endif I.pager = NULL; /* no pager by default */ - //r_cons_palette_init(NULL); + r_cons_pal_init (NULL); + r_cons_rgb_init (); r_cons_reset (); return &I; } diff --git a/libr/cons/input.c b/libr/cons/input.c index f68a2846c4..db93268675 100644 --- a/libr/cons/input.c +++ b/libr/cons/input.c @@ -125,15 +125,30 @@ R_API int r_cons_arrow_to_hjkl(int ch) { // XXX no control for max length here?!?! R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv) { RCons *cons = r_cons_singleton (); - if (cons->user_fgets) - return cons->user_fgets (buf, len); + int color = cons->pal.input && *cons->pal.input; + if (cons->user_fgets) { + int ret = cons->user_fgets (buf, len); + return ret; + } *buf = '\0'; fflush (cons->fdin); - if (fgets (buf, len, cons->fdin) == NULL) + if (color) { + printf (cons->pal.input); + fflush (stdout); + } + if (fgets (buf, len, cons->fdin) == NULL) { + if (color) { + printf (Color_RESET); + fflush (stdout); + } return -1; - if (feof (cons->fdin)) + } + if (feof (cons->fdin)) { + if (color) printf (Color_RESET); return -2; + } buf[strlen (buf)-1] = '\0'; + if (color) printf (Color_RESET); return strlen (buf); } diff --git a/libr/cons/pal.c b/libr/cons/pal.c new file mode 100644 index 0000000000..0b8921901a --- /dev/null +++ b/libr/cons/pal.c @@ -0,0 +1,102 @@ +/* radare - LGPL - Copyright 2013 - pancake */ + +#include + +R_API void r_cons_pal_init(const char *foo) { + RCons *cons = r_cons_singleton (); + cons->pal.prompt = Color_YELLOW; + cons->pal.offset = Color_GREEN; + cons->pal.input = Color_WHITE; + cons->pal.comment = Color_TURQOISE; + cons->pal.reset = "\x1b[0m"; + cons->pal.db = NULL; +} + +struct { + const char *name; + const char *code; + const char *bgcode; +} colors[] = { + { "black", Color_BLACK, Color_BGBLACK }, + { "red", Color_RED, Color_BGRED }, + { "white", Color_WHITE, Color_BGWHITE }, + { "green", Color_GREEN, Color_BGGREEN }, + { "magenta", Color_MAGENTA, Color_BGMAGENTA }, + { "yellow", Color_YELLOW, Color_BGYELLOW }, + { "turqoise", Color_TURQOISE, Color_BGTURQOISE }, + { "blue", Color_BLUE, Color_BGBLUE }, + { "gray", Color_GRAY Color_BGGRAY }, + { NULL, NULL } +}; + +static inline ut8 rgbnum (const char ch) { + ut8 r; + r_hex_to_byte (&r, ch); + return r*16; +} + +R_API char *r_cons_pal_parse(const char *str) { + int i; + ut8 r, g, b; + char out[64]; + char *s = strdup (str); + char *p = strchr (s+1, ' '); + out[0] = 0; + if (p) *p++ = 0; + if (!strncmp (s, "rgb:", 4)) { + r = rgbnum (s[4]); + g = rgbnum (s[5]); + b = rgbnum (s[6]); + r_cons_rgb_str (out, r, g, b, 0); + if (p && !strncmp (p, "rgb:", 4)) { + r = rgbnum (p[4]); + g = rgbnum (p[5]); + b = rgbnum (p[6]); + r_cons_rgb_str (out+strlen (out), r, g, b, 1); + } + } + for (i=0; colors[i].name; i++) { + if (!strcmp (s, colors[i].name)) + strcat (out, colors[i].code); + if (p && !strcmp (p, colors[i].name)) + strcat (out, colors[i].bgcode); + } + free (s); + return *out? strdup (out): NULL; +} + +struct { + const char *name; + int off; +} keys[] = { + { "comment", r_offsetof (RConsPalette, comment) }, + { "prompt", r_offsetof (RConsPalette, prompt) }, + { "offset", r_offsetof (RConsPalette, offset) }, + { "input", r_offsetof (RConsPalette, input) }, + { NULL, 0 } +}; + +R_API void r_cons_pal_walk() { + RCons *c = r_cons_singleton (); + // const char *color = sdb_get (c->pal.db, "color.prompt") + //c->pal.prompt = r_cons_pal_parse ("green"); +} + +R_API void r_cons_pal_load(const char *sdbfile) { +} + +R_API void r_cons_pal_save(const char *sdbfile) { +} + +R_API void r_cons_pal_set (const char *key, const char *val) { + int i; + char **p; + for (i=0; keys[i].name; i++) { + if (!strcmp (key, keys[i].name)) { + p = (char **)((char *)&(r_cons_singleton()->pal) + keys[i].off); +// free (*p); + *p = r_cons_pal_parse (val); + break; + } + } +} diff --git a/libr/cons/rgb.c b/libr/cons/rgb.c index f00e343e4d..3d7011f348 100644 --- a/libr/cons/rgb.c +++ b/libr/cons/rgb.c @@ -17,16 +17,16 @@ static int rgb(int r, int g, int b) { r = R_DIM (r/k, 0, 5); g = R_DIM (g/k, 0, 5); b = R_DIM (b/k, 0, 5); - return 16 + r*36 + g*6 +b; + return 16 + ((r&7)*29) + ((g&7)*12) + (b&7); } static inline void rgbinit(int r, int g, int b) { #if __UNIX__ r_cons_printf ("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\", - 16 + (r* 36) + (g* 6) + b, + 16 + (r * 36) + (g * 6) + b, (r ? (r * 40 + 55) : 0), - (g ? (g* 40 + 55) : 0), - (b ? (b* 40 + 55) : 0)); + (g ? (g * 40 + 55) : 0), + (b ? (b * 40 + 55) : 0)); #endif } diff --git a/libr/core/cmd.c b/libr/core/cmd.c index f2c415e9e4..a14550758b 100644 --- a/libr/core/cmd.c +++ b/libr/core/cmd.c @@ -437,6 +437,24 @@ static int cmd_eval(void *data, const char *input) { case '\0': r_config_list (core->config, NULL, 0); break; + case 'c': + { + char *p = strdup (input+2); + char *q = strchr (p, ' '); + if (p) { + if (q) { + // set + *q++ = 0; + r_cons_pal_set (p, q); + } else { + // get + eprintf ("(%s)(%s)\n", p, q); + } + } else { + eprintf ("TODO: ec list\n"); + } + } + break; case 'e': if (input[1]==' ') { char *p; @@ -482,7 +500,8 @@ static int cmd_eval(void *data, const char *input) { " e- ; reset config vars\n" " e* ; dump config vars in r commands\n" " e!a ; invert the boolean value of 'a' var\n" - " er [key] ; set config key as readonly. no way back\n" + " er [key] ; set config key as readonly. no way back\n" + " ec [k] [color] ; set color for given key (prompt, offset, ...)\n" " e a ; get value of var 'a'\n" " e a=b ; set var 'a' the 'b' value\n"); } diff --git a/libr/core/core.c b/libr/core/core.c index f72f3577af..3ebc5de97c 100644 --- a/libr/core/core.c +++ b/libr/core/core.c @@ -484,16 +484,16 @@ R_API int r_core_init(RCore *core) { r_egg_setup (core->egg, R_SYS_ARCH, R_SYS_BITS, 0, R_SYS_OS); /* initialize libraries */ + core->cons = r_cons_singleton (); if (singleton) { RLine *line = r_line_new (); r_cons_new (); line->user = core; - r_cons_singleton()->user_fgets = (void *)myfgets; + core->cons->user_fgets = (void *)myfgets; //r_line_singleton()->user = (void *)core; r_line_hist_load (".radare2_history"); singleton = R_FALSE; } - core->cons = r_cons_singleton (); core->cons->num = core->num; core->blocksize = R_CORE_BLOCKSIZE; core->block = (ut8*)malloc (R_CORE_BLOCKSIZE); @@ -626,6 +626,9 @@ R_API int r_core_prompt(RCore *r, int sync) { char prompt[32]; const char *cmdprompt = r_config_get (r->config, "cmd.prompt"); + const char *BEGIN = r->cons->pal.prompt; + const char *END = r->cons->pal.reset; + // hacky fix fo rio r_core_block_read (r, 0); if (cmdprompt && *cmdprompt) @@ -641,20 +644,20 @@ R_API int r_core_prompt(RCore *r, int sync) { #if __UNIX__ if (r_config_get_i (r->config, "scr.color")) snprintf (prompt, sizeof (prompt), - Color_YELLOW"[%04x:%04x]> " - Color_RESET, a, b); + "%s[%04x:%04x]>%s ", + BEGIN, a, b, END); else #endif - sprintf (prompt, "[%04x:%04x]> ", a, b); + snprintf (prompt, sizeof (prompt), "[%04x:%04x]> ", a, b); } else { #if __UNIX__ if (r_config_get_i (r->config, "scr.color")) snprintf (prompt, sizeof (prompt), - Color_YELLOW"[0x%08"PFMT64x"]> " - Color_RESET, r->offset); + "%s[0x%08"PFMT64x"]>%s ", + BEGIN, r->offset, END); else #endif - sprintf (prompt, "[0x%08"PFMT64x"]> ", r->offset); + snprintf (prompt, sizeof (prompt), "[0x%08"PFMT64x"]> ", r->offset); } r_line_set_prompt (prompt); ret = r_cons_fgets (line, sizeof (line), 0, NULL); diff --git a/libr/core/disasm.c b/libr/core/disasm.c index 4aa4dd516c..0cfa9cb016 100644 --- a/libr/core/disasm.c +++ b/libr/core/disasm.c @@ -52,16 +52,15 @@ static char *filter_refline(const char *str) { static void printoffset(ut64 off, int show_color, int invert, int opt) { if (show_color) { + const char *k = r_cons_singleton ()->pal.offset; // TODO etooslow. must cache if (invert) r_cons_invert (R_TRUE, R_TRUE); if (opt) { ut32 s, a; a = off & 0xffff; s = (off-a)>>4; - r_cons_printf (Color_GREEN"%04x:%04x"Color_RESET, s, a); - } else r_cons_printf (Color_GREEN"0x%08"PFMT64x""Color_RESET, off); - if (invert) - r_cons_printf (Color_RESET); + r_cons_printf ("%s%04x:%04x"Color_RESET, k, s, a); + } else r_cons_printf ("%s0x%08"PFMT64x""Color_RESET, k, off); r_cons_puts (" "); } else r_cons_printf ("0x%08"PFMT64x" ", off); } diff --git a/libr/core/visual.c b/libr/core/visual.c index 8a21bd2fed..c52c901354 100644 --- a/libr/core/visual.c +++ b/libr/core/visual.c @@ -822,6 +822,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) { #define PIDX (R_ABS(core->printidx%NPF)) R_API void r_core_visual_title (RCore *core, int color) { + const char *BEGIN = core->cons->pal.prompt; const char *filename; char pos[512], foo[512], bar[512]; /* automatic block size */ @@ -864,7 +865,7 @@ R_API void r_core_visual_title (RCore *core, int color) { if (cursor<0) cursor = 0; - if (color) r_cons_strcat (Color_YELLOW); + if (color) r_cons_strcat (BEGIN); strncpy (bar, printfmt[PIDX], sizeof (bar)-1); bar[sizeof (bar)-1] = 0; // '\0'-terminate bar diff --git a/libr/include/r_cons.h b/libr/include/r_cons.h index 9e2a27c227..6d0ee92945 100644 --- a/libr/include/r_cons.h +++ b/libr/include/r_cons.h @@ -5,6 +5,7 @@ #include #include +#include #include #include @@ -48,6 +49,15 @@ typedef struct r_cons_grep_t { int end; } RConsGrep; +typedef struct r_cons_palette_t { + char *prompt; + char *offset; + char *comment; + char *input; + char *reset; + Sdb *db; +} RConsPalette; + typedef void (*RConsEvent)(void *); typedef struct r_cons_t { @@ -83,6 +93,7 @@ typedef struct r_cons_t { char *pager; int blankline; int widthfix; + RConsPalette pal; } RCons; // XXX THIS MUST BE A SINGLETON AND WRAPPED INTO RCons */ @@ -115,18 +126,25 @@ typedef struct r_cons_t { #define Color_INVERT "\x1b[7m" #define Color_INVERT_RESET "\x1b[27m" /* plain colors */ -#define Color_BLACK "\x1b[30m" -#define Color_BGBLACK "\x1b[40m" -#define Color_RED "\x1b[31m" -#define Color_BGRED "\x1b[41m" -#define Color_WHITE "\x1b[37m" -#define Color_RESET "\x1b[0m" -#define Color_GREEN "\x1b[32m" -#define Color_MAGENTA "\x1b[35m" -#define Color_YELLOW "\x1b[33m" -#define Color_TURQOISE "\x1b[36m" -#define Color_BLUE "\x1b[34m" -#define Color_GRAY "\x1b[38m" +#define Color_RESET "\x1b[0m" +#define Color_BLACK "\x1b[30m" +#define Color_BGBLACK "\x1b[40m" +#define Color_RED "\x1b[31m" +#define Color_BGRED "\x1b[41m" +#define Color_WHITE "\x1b[37m" +#define Color_BGWHITE "\x1b[47m" +#define Color_GREEN "\x1b[32m" +#define Color_BGGREEN "\x1b[42m" +#define Color_MAGENTA "\x1b[35m" +#define Color_BGMAGENTA "\x1b[45m" +#define Color_YELLOW "\x1b[33m" +#define Color_BGYELLOW "\x1b[43m" +#define Color_TURQOISE "\x1b[36m" +#define Color_BGTURQOISE "\x1b[46m" +#define Color_BLUE "\x1b[34m" +#define Color_BGBLUE "\x1b[44m" +#define Color_GRAY "\x1b[38m" +#define Color_BGGRAY "\x1b[48m" /* bold colors */ #define Color_BBLACK "\x1b[1;30m" #define Color_BRED "\x1b[1;31m" @@ -315,6 +333,9 @@ R_API const char *r_line_hist_get(int n); #define R_CONS_INVERT(x,y) (y? (x?Color_INVERT: Color_INVERT_RESET): (x?"[":"]")) +/* palette */ +R_API void r_cons_pal_init(const char *foo); + #endif #endif diff --git a/shlr/sdb/src/sdb-version.h b/shlr/sdb/src/sdb-version.h index 876c971528..8db49888ce 100644 --- a/shlr/sdb/src/sdb-version.h +++ b/shlr/sdb/src/sdb-version.h @@ -1 +1 @@ -#define SDB_VERSION "0.6.4" +#define SDB_VERSION "0.9.5git"