mirror of
https://github.com/radareorg/radare2.git
synced 2024-11-24 05:40:10 +00:00
Initial r_cons palette support. See 'ec' command
This commit is contained in:
parent
074e92646e
commit
9d65bfe70c
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
102
libr/cons/pal.c
Normal file
102
libr/cons/pal.c
Normal 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;
|
||||
}
|
||||
}
|
||||
}
|
@ -17,7 +17,7 @@ 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) {
|
||||
|
@ -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;
|
||||
@ -483,6 +501,7 @@ static int cmd_eval(void *data, const char *input) {
|
||||
" 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"
|
||||
" 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");
|
||||
}
|
||||
|
@ -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);
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -5,6 +5,7 @@
|
||||
|
||||
#include <r_types.h>
|
||||
#include <r_util.h>
|
||||
#include <sdb.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <unistd.h>
|
||||
@ -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_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_RESET "\x1b[0m"
|
||||
#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
|
||||
|
@ -1 +1 @@
|
||||
#define SDB_VERSION "0.6.4"
|
||||
#define SDB_VERSION "0.9.5git"
|
||||
|
Loading…
Reference in New Issue
Block a user