Initial r_cons palette support. See 'ec' command

This commit is contained in:
pancake 2013-05-22 04:22:49 +02:00
parent 074e92646e
commit 9d65bfe70c
12 changed files with 208 additions and 47 deletions

View File

@ -22,7 +22,7 @@ static char *mask = "";
static int nonstop = 0; static int nonstop = 0;
static int mode = R_SEARCH_STRING; static int mode = R_SEARCH_STRING;
static ut64 cur = 0; static ut64 cur = 0;
static ut8 *buffer = NULL; static ut8 *buf = NULL;
static char *curfile = NULL; static char *curfile = NULL;
static ut64 bsize = 4096; static ut64 bsize = 4096;
static int hexstr = 0; 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); printf ("f hit%d_%d 0x%08"PFMT64x" ; %s\n", 0, kw->count, addr, curfile);
} else { } else {
if (showstr) { if (showstr) {
printf ("0x%"PFMT64x" %s\n", addr, buffer+delta); printf ("0x%"PFMT64x" %s\n", addr, buf+delta);
} else { } else {
printf ("0x%"PFMT64x"\n", addr); printf ("0x%"PFMT64x"\n", addr);
if (pr) { 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 (); r_cons_flush ();
} }
} }
@ -86,12 +86,12 @@ static int rafind_open(char *file) {
r_cons_new (); r_cons_new ();
rs = r_search_new (mode); rs = r_search_new (mode);
buffer = malloc (bsize); buf = malloc (bsize);
if (buffer==NULL) { if (buf==NULL) {
eprintf ("Cannot allocate %"PFMT64d" bytes\n", bsize); eprintf ("Cannot allocate %"PFMT64d" bytes\n", bsize);
return 1; return 1;
} }
r_search_set_callback (rs, &hit, buffer); r_search_set_callback (rs, &hit, buf);
if (to == -1) if (to == -1)
to = r_io_size(io); to = r_io_size(io);
if (mode == R_SEARCH_STRING) { if (mode == R_SEARCH_STRING) {
@ -117,7 +117,7 @@ static int rafind_open(char *file) {
bsize = to-cur; bsize = to-cur;
last=1; last=1;
} }
ret = r_io_read_at (io, cur, buffer, bsize); ret = r_io_read_at (io, cur, buf, bsize);
if (ret == 0) { if (ret == 0) {
if (nonstop) continue; if (nonstop) continue;
// fprintf(stderr, "Error reading at 0x%08"PFMT64x"\n", cur); // fprintf(stderr, "Error reading at 0x%08"PFMT64x"\n", cur);
@ -126,14 +126,14 @@ static int rafind_open(char *file) {
if (ret != bsize) if (ret != bsize)
bsize = ret; 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); eprintf ("search: update read error at 0x%08"PFMT64x"\n", cur);
break; break;
} }
} }
rs = r_search_free (rs); rs = r_search_free (rs);
free (buffer); free (buf);
return 0; return 0;
} }

View File

@ -2,8 +2,8 @@ include ../config.mk
NAME=r_cons NAME=r_cons
OBJS=cons.o pipe.o output.o grep.o OBJS=cons.o pipe.o output.o grep.o
OBJS+=line.o hud.o rgb.o input.o OBJS+=line.o hud.o rgb.o input.o pal.o
DEPS=r_util DEPS=r_util r_db
include ../rules.mk include ../rules.mk

View File

@ -139,7 +139,8 @@ R_API RCons *r_cons_new () {
eprintf ("r_cons: Cannot set control console handler\n"); eprintf ("r_cons: Cannot set control console handler\n");
#endif #endif
I.pager = NULL; /* no pager by default */ I.pager = NULL; /* no pager by default */
//r_cons_palette_init(NULL); r_cons_pal_init (NULL);
r_cons_rgb_init ();
r_cons_reset (); r_cons_reset ();
return &I; return &I;
} }

View File

@ -125,15 +125,30 @@ R_API int r_cons_arrow_to_hjkl(int ch) {
// XXX no control for max length here?!?! // XXX no control for max length here?!?!
R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv) { R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv) {
RCons *cons = r_cons_singleton (); RCons *cons = r_cons_singleton ();
if (cons->user_fgets) int color = cons->pal.input && *cons->pal.input;
return cons->user_fgets (buf, len); if (cons->user_fgets) {
int ret = cons->user_fgets (buf, len);
return ret;
}
*buf = '\0'; *buf = '\0';
fflush (cons->fdin); 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; return -1;
if (feof (cons->fdin)) }
if (feof (cons->fdin)) {
if (color) printf (Color_RESET);
return -2; return -2;
}
buf[strlen (buf)-1] = '\0'; buf[strlen (buf)-1] = '\0';
if (color) printf (Color_RESET);
return strlen (buf); return strlen (buf);
} }

102
libr/cons/pal.c Normal file
View File

@ -0,0 +1,102 @@
/* radare - LGPL - Copyright 2013 - pancake */
#include <r_cons.h>
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;
}
}
}

View File

@ -17,16 +17,16 @@ static int rgb(int r, int g, int b) {
r = R_DIM (r/k, 0, 5); r = R_DIM (r/k, 0, 5);
g = R_DIM (g/k, 0, 5); g = R_DIM (g/k, 0, 5);
b = R_DIM (b/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) { static inline void rgbinit(int r, int g, int b) {
#if __UNIX__ #if __UNIX__
r_cons_printf ("\x1b]4;%d;rgb:%2.2x/%2.2x/%2.2x\x1b\\", 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), (r ? (r * 40 + 55) : 0),
(g ? (g* 40 + 55) : 0), (g ? (g * 40 + 55) : 0),
(b ? (b* 40 + 55) : 0)); (b ? (b * 40 + 55) : 0));
#endif #endif
} }

View File

@ -437,6 +437,24 @@ static int cmd_eval(void *data, const char *input) {
case '\0': case '\0':
r_config_list (core->config, NULL, 0); r_config_list (core->config, NULL, 0);
break; 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': case 'e':
if (input[1]==' ') { if (input[1]==' ') {
char *p; char *p;
@ -482,7 +500,8 @@ static int cmd_eval(void *data, const char *input) {
" e- ; reset config vars\n" " e- ; reset config vars\n"
" e* ; dump config vars in r commands\n" " e* ; dump config vars in r commands\n"
" e!a ; invert the boolean value of 'a' var\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 ; get value of var 'a'\n"
" e a=b ; set var 'a' the 'b' value\n"); " e a=b ; set var 'a' the 'b' value\n");
} }

View File

@ -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); r_egg_setup (core->egg, R_SYS_ARCH, R_SYS_BITS, 0, R_SYS_OS);
/* initialize libraries */ /* initialize libraries */
core->cons = r_cons_singleton ();
if (singleton) { if (singleton) {
RLine *line = r_line_new (); RLine *line = r_line_new ();
r_cons_new (); r_cons_new ();
line->user = core; line->user = core;
r_cons_singleton()->user_fgets = (void *)myfgets; core->cons->user_fgets = (void *)myfgets;
//r_line_singleton()->user = (void *)core; //r_line_singleton()->user = (void *)core;
r_line_hist_load (".radare2_history"); r_line_hist_load (".radare2_history");
singleton = R_FALSE; singleton = R_FALSE;
} }
core->cons = r_cons_singleton ();
core->cons->num = core->num; core->cons->num = core->num;
core->blocksize = R_CORE_BLOCKSIZE; core->blocksize = R_CORE_BLOCKSIZE;
core->block = (ut8*)malloc (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]; char prompt[32];
const char *cmdprompt = r_config_get (r->config, "cmd.prompt"); 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 // hacky fix fo rio
r_core_block_read (r, 0); r_core_block_read (r, 0);
if (cmdprompt && *cmdprompt) if (cmdprompt && *cmdprompt)
@ -641,20 +644,20 @@ R_API int r_core_prompt(RCore *r, int sync) {
#if __UNIX__ #if __UNIX__
if (r_config_get_i (r->config, "scr.color")) if (r_config_get_i (r->config, "scr.color"))
snprintf (prompt, sizeof (prompt), snprintf (prompt, sizeof (prompt),
Color_YELLOW"[%04x:%04x]> " "%s[%04x:%04x]>%s ",
Color_RESET, a, b); BEGIN, a, b, END);
else else
#endif #endif
sprintf (prompt, "[%04x:%04x]> ", a, b); snprintf (prompt, sizeof (prompt), "[%04x:%04x]> ", a, b);
} else { } else {
#if __UNIX__ #if __UNIX__
if (r_config_get_i (r->config, "scr.color")) if (r_config_get_i (r->config, "scr.color"))
snprintf (prompt, sizeof (prompt), snprintf (prompt, sizeof (prompt),
Color_YELLOW"[0x%08"PFMT64x"]> " "%s[0x%08"PFMT64x"]>%s ",
Color_RESET, r->offset); BEGIN, r->offset, END);
else else
#endif #endif
sprintf (prompt, "[0x%08"PFMT64x"]> ", r->offset); snprintf (prompt, sizeof (prompt), "[0x%08"PFMT64x"]> ", r->offset);
} }
r_line_set_prompt (prompt); r_line_set_prompt (prompt);
ret = r_cons_fgets (line, sizeof (line), 0, NULL); ret = r_cons_fgets (line, sizeof (line), 0, NULL);

View File

@ -52,16 +52,15 @@ static char *filter_refline(const char *str) {
static void printoffset(ut64 off, int show_color, int invert, int opt) { static void printoffset(ut64 off, int show_color, int invert, int opt) {
if (show_color) { if (show_color) {
const char *k = r_cons_singleton ()->pal.offset; // TODO etooslow. must cache
if (invert) if (invert)
r_cons_invert (R_TRUE, R_TRUE); r_cons_invert (R_TRUE, R_TRUE);
if (opt) { if (opt) {
ut32 s, a; ut32 s, a;
a = off & 0xffff; a = off & 0xffff;
s = (off-a)>>4; s = (off-a)>>4;
r_cons_printf (Color_GREEN"%04x:%04x"Color_RESET, s, a); r_cons_printf ("%s%04x:%04x"Color_RESET, k, s, a);
} else r_cons_printf (Color_GREEN"0x%08"PFMT64x""Color_RESET, off); } else r_cons_printf ("%s0x%08"PFMT64x""Color_RESET, k, off);
if (invert)
r_cons_printf (Color_RESET);
r_cons_puts (" "); r_cons_puts (" ");
} else r_cons_printf ("0x%08"PFMT64x" ", off); } else r_cons_printf ("0x%08"PFMT64x" ", off);
} }

View File

@ -822,6 +822,7 @@ R_API int r_core_visual_cmd(RCore *core, int ch) {
#define PIDX (R_ABS(core->printidx%NPF)) #define PIDX (R_ABS(core->printidx%NPF))
R_API void r_core_visual_title (RCore *core, int color) { R_API void r_core_visual_title (RCore *core, int color) {
const char *BEGIN = core->cons->pal.prompt;
const char *filename; const char *filename;
char pos[512], foo[512], bar[512]; char pos[512], foo[512], bar[512];
/* automatic block size */ /* automatic block size */
@ -864,7 +865,7 @@ R_API void r_core_visual_title (RCore *core, int color) {
if (cursor<0) cursor = 0; 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); strncpy (bar, printfmt[PIDX], sizeof (bar)-1);
bar[sizeof (bar)-1] = 0; // '\0'-terminate bar bar[sizeof (bar)-1] = 0; // '\0'-terminate bar

View File

@ -5,6 +5,7 @@
#include <r_types.h> #include <r_types.h>
#include <r_util.h> #include <r_util.h>
#include <sdb.h>
#include <stdio.h> #include <stdio.h>
#include <unistd.h> #include <unistd.h>
@ -48,6 +49,15 @@ typedef struct r_cons_grep_t {
int end; int end;
} RConsGrep; } 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 void (*RConsEvent)(void *);
typedef struct r_cons_t { typedef struct r_cons_t {
@ -83,6 +93,7 @@ typedef struct r_cons_t {
char *pager; char *pager;
int blankline; int blankline;
int widthfix; int widthfix;
RConsPalette pal;
} RCons; } RCons;
// XXX THIS MUST BE A SINGLETON AND WRAPPED INTO 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 "\x1b[7m"
#define Color_INVERT_RESET "\x1b[27m" #define Color_INVERT_RESET "\x1b[27m"
/* plain colors */ /* plain colors */
#define Color_BLACK "\x1b[30m" #define Color_RESET "\x1b[0m"
#define Color_BGBLACK "\x1b[40m" #define Color_BLACK "\x1b[30m"
#define Color_RED "\x1b[31m" #define Color_BGBLACK "\x1b[40m"
#define Color_BGRED "\x1b[41m" #define Color_RED "\x1b[31m"
#define Color_WHITE "\x1b[37m" #define Color_BGRED "\x1b[41m"
#define Color_RESET "\x1b[0m" #define Color_WHITE "\x1b[37m"
#define Color_GREEN "\x1b[32m" #define Color_BGWHITE "\x1b[47m"
#define Color_MAGENTA "\x1b[35m" #define Color_GREEN "\x1b[32m"
#define Color_YELLOW "\x1b[33m" #define Color_BGGREEN "\x1b[42m"
#define Color_TURQOISE "\x1b[36m" #define Color_MAGENTA "\x1b[35m"
#define Color_BLUE "\x1b[34m" #define Color_BGMAGENTA "\x1b[45m"
#define Color_GRAY "\x1b[38m" #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 */ /* bold colors */
#define Color_BBLACK "\x1b[1;30m" #define Color_BBLACK "\x1b[1;30m"
#define Color_BRED "\x1b[1;31m" #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?"[":"]")) #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
#endif #endif

View File

@ -1 +1 @@
#define SDB_VERSION "0.6.4" #define SDB_VERSION "0.9.5git"