Make r_cons_pal_parse to always return ansi + RColor ##cons

This commit is contained in:
pancake 2023-10-06 11:13:53 +02:00 committed by pancake
parent 02c2039c00
commit f641c61f08
3 changed files with 78 additions and 21 deletions

60
libr/anal/btrace.c Normal file
View File

@ -0,0 +1,60 @@
/* radare - LGPL - Copyright 2009-2023 - pancake, nibble */
#include <r_anal.h>
typedef struct {
HtUP *ht;
} RAnalBacktracesPrivate;
R_API void r_anal_backtrace_add(RAnal *a, ut64 addr, RVecBacktrace *bt) {
RAnalBacktracesPrivate *b = a->btstore.priv;
// save backtrace in given offset
ht_up_insert (b->ht, addr, bt);
}
R_API void r_anal_backtrace_del(RAnal *a, ut64 addr) {
RAnalBacktracesPrivate *b = a->btstore.priv;
ht_up_delete (b->ht, addr);
}
typedef struct {
RAnal *a;
ut64 arg;
int opt;
} Args;
static bool cblist(void *user, const ut64 offset, const void *val) {
RVecBacktrace *bt = (RVecBacktrace*)val;
Args *args = (Args *)user;
if (args->opt == 'x') {
ut64 *addr;
R_VEC_FOREACH (bt, addr) {
r_cons_printf ("ax 0x%08"PFMT64x" 0x%08"PFMT64x"\n", offset, *addr);
}
return true;
}
r_cons_printf ("-> 0x%08"PFMT64x"\n", offset);
ut64 addr;
R_VEC_FOREACH (bt, addr) {
r_cons_printf (" `-> 0x%08"PFMT64x"\n", addr);
}
return true;
}
R_API void r_anal_backtrace_list(RAnal *a, ut64 addr, int opt) {
RAnalBacktracesPrivate *b = a->btstore.priv;
Args args = { a, addr, opt };
ht_up_foreach (b->ht, cblist, &args);
}
R_API void r_anal_backtrace_fini(RAnal *a) {
RAnalBacktracesPrivate *b = a->btstore.priv;
ht_up_free (b->ht);
R_FREE (a->btstore.priv);
}
R_API void r_anal_backtrace_init(RAnal *a) {
a->btstore.priv = R_NEW0 (RAnalBacktracesPrivate);
RAnalBacktracesPrivate *b = a->btstore.priv;
b->ht = ht_up_new0 ();
}

View File

@ -8,6 +8,9 @@
R_API RCodeMetaItem *r_codemeta_item_clone(RCodeMetaItem *code) {
r_return_val_if_fail (code, NULL);
RCodeMetaItem *mi = r_codemeta_item_new ();
if (!mi) {
return NULL;
}
memcpy (mi, code, sizeof (RCodeMetaItem));
switch (mi->type) {
case R_CODEMETA_TYPE_FUNCTION_NAME:
@ -28,21 +31,23 @@ R_API RCodeMetaItem *r_codemeta_item_clone(RCodeMetaItem *code) {
R_API RCodeMeta *r_codemeta_clone(RCodeMeta *code) {
RCodeMeta *r = r_codemeta_new (code->code);
RCodeMetaItem *mi;
r_vector_foreach (&code->annotations, mi) {
r_codemeta_add_item (r, r_codemeta_item_clone (mi));
if (r) {
RCodeMetaItem *mi;
r_vector_foreach (&code->annotations, mi) {
r_codemeta_add_item (r, r_codemeta_item_clone (mi));
}
}
return r;
}
R_API RCodeMeta *r_codemeta_new(const char *code) {
RCodeMeta *r = R_NEW0 (RCodeMeta);
if (!r) {
return NULL;
if (r) {
r->tree = r_crbtree_new (NULL);
r->code = code? strdup (code): NULL;
r_vector_init (&r->annotations, sizeof (RCodeMetaItem),
(RVectorFree)r_codemeta_item_fini, NULL);
}
r->tree = r_crbtree_new (NULL);
r->code = code? strdup (code): NULL;
r_vector_init (&r->annotations, sizeof (RCodeMetaItem), (RVectorFree)r_codemeta_item_fini, NULL);
return r;
}

View File

@ -318,18 +318,14 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) {
// Handle first color (fgcolor)
if (!strcmp (fgcolor, "random")) {
rcolor = r_cons_color_random (ALPHA_FG);
if (!outcol) {
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
r_cons_rgb_str (out, sizeof (out), &rcolor);
} else if (fgcolor[0] == '#') { // "#00ff00" HTML format
if (strlen (fgcolor + 1) == 6) {
const char *kule = fgcolor + 1;
rcolor.r = rgbnum (kule[0], kule[1]);
rcolor.g = rgbnum (kule[2], kule[3]);
rcolor.b = rgbnum (kule[4], kule[5]);
if (!outcol) {
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
r_cons_rgb_str (out, sizeof (out), &rcolor);
} else {
R_LOG_WARN ("Invalid html color code");
}
@ -338,16 +334,12 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) {
rcolor.r = rgbnum (fgcolor[4], '0');
rcolor.g = rgbnum (fgcolor[5], '0');
rcolor.b = rgbnum (fgcolor[6], '0');
if (!outcol) {
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
r_cons_rgb_str (out, sizeof (out), &rcolor);
} else if (strlen (fgcolor + 4) == 6) { // rgb:RRGGBB
rcolor.r = rgbnum (fgcolor[4], fgcolor[5]);
rcolor.g = rgbnum (fgcolor[6], fgcolor[7]);
rcolor.b = rgbnum (fgcolor[8], fgcolor[9]);
if (!outcol) {
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
r_cons_rgb_str (out, sizeof (out), &rcolor);
}
}
// Handle second color (bgcolor)
@ -423,7 +415,7 @@ R_API char *r_cons_pal_parse(const char *str, R_NULLABLE RColor *outcol) {
*outcol = rcolor;
}
free (fgcolor);
return (*out && !outcol) ? strdup (out) : NULL;
return *out ? strdup (out) : NULL;
}
static void r_cons_pal_show_gs(void) {