Fix #8084 - Honor colors in json_indent ~{}

This commit is contained in:
pancake 2017-08-11 12:23:57 +02:00
parent 475edf033d
commit 59a94ae59f
9 changed files with 75 additions and 22 deletions

View File

@ -1286,7 +1286,7 @@ static const ut8 *r_bin_dwarf_parse_attr_value(const ut8 *obuf, int obuf_len,
break; break;
default: default:
eprintf ("Unknown DW_FORM 0x%02"PFMT64x"\n", spec->attr_form); eprintf ("Unknown DW_FORM 0x%02"PFMT64x"\n", spec->attr_form);
value->encoding.data = NULL; value->encoding.data = 0;
return NULL; return NULL;
} }
return buf; return buf;

View File

@ -423,7 +423,15 @@ R_API int r_cons_grepbuf(char *buf, int len) {
} }
R_FREE (cons->grep.json_path); R_FREE (cons->grep.json_path);
} else { } else {
char *out = r_print_json_indent (buf, I (use_color), " "); const char *palette[] = {
cons->pal.graph_false, // f
cons->pal.graph_true, // t
cons->pal.num, // k
cons->pal.comment, // v
Color_RESET,
NULL
};
char *out = r_print_json_indent (buf, I (use_color), " ", palette);
if (!out) { if (!out) {
return 0; return 0;
} }

View File

@ -2417,7 +2417,6 @@ static int validAddress(RCore *core, ut64 addr) {
static void backtrace_vars(RCore *core, RList *frames) { static void backtrace_vars(RCore *core, RList *frames) {
RDebugFrame *f; RDebugFrame *f;
RListIter *iter; RListIter *iter;
bool mymap = false;
// anal vs debug ? // anal vs debug ?
const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP); const char *sp = r_reg_get_name (core->anal->reg, R_REG_NAME_SP);
const char *bp = r_reg_get_name (core->anal->reg, R_REG_NAME_BP); const char *bp = r_reg_get_name (core->anal->reg, R_REG_NAME_BP);
@ -2436,7 +2435,7 @@ static void backtrace_vars(RCore *core, RList *frames) {
ut64 b = f->bp ? f->bp : dbp; ut64 b = f->bp ? f->bp : dbp;
r_reg_setv (r, bp, s); r_reg_setv (r, bp, s);
r_reg_setv (r, sp, b); r_reg_setv (r, sp, b);
char flagdesc[1024], flagdesc2[1024], pcstr[32], spstr[32]; char flagdesc[1024], flagdesc2[1024];
RFlagItem *fi = r_flag_get_at (core->flags, f->addr, true); RFlagItem *fi = r_flag_get_at (core->flags, f->addr, true);
flagdesc[0] = flagdesc2[0] = 0; flagdesc[0] = flagdesc2[0] = 0;
if (f) { if (f) {

View File

@ -176,7 +176,7 @@ R_API char *r_print_stereogram(const char *bump, int w, int h);
R_API void r_print_stereogram_print(RPrint *p, const char *buf); R_API void r_print_stereogram_print(RPrint *p, const char *buf);
R_API void r_print_set_screenbounds(RPrint *p, ut64 addr); R_API void r_print_set_screenbounds(RPrint *p, ut64 addr);
R_API int r_util_lines_getline(ut64 *lines_cache, int lines_cache_sz, ut64 off); R_API int r_util_lines_getline(ut64 *lines_cache, int lines_cache_sz, ut64 off);
R_API char* r_print_json_indent(const char* s, bool color, const char *tab); R_API char* r_print_json_indent(const char* s, bool color, const char *tab, const char **colors);
#endif #endif

View File

@ -16,29 +16,45 @@ static void doIndent(int idt, char** o, const char *tab) {
#define EMIT_ESC(s, code) do { \ #define EMIT_ESC(s, code) do { \
if (color) { \ if (color) { \
char *p = code; \ const char *p = code; \
*s++ = 0x1b; \
while (*p) { \ while (*p) { \
*s++ = *p++; \ *s++ = *p++; \
} \ } \
} \ } \
} while (0); } while (0);
R_API char* r_print_json_indent(const char* s, bool color, const char* tab) { enum {
JC_FALSE, // 31m
JC_TRUE, // 32m
JC_KEY, // 33m
JC_VAL, // 34m
JC_RESET,
};
static const char *origColors[] = {
"\x1b[31m",
"\x1b[32m",
"\x1b[33m",
"\x1b[34m",
"\x1b[0m",
};
// static const char colors
R_API char* r_print_json_indent(const char* s, bool color, const char* tab, const char **palette) {
int indent = 0; int indent = 0;
int instr = 0; int instr = 0;
bool isValue = false; bool isValue = false;
int osz; char *o, *OE, *tmp;
char* o, * O, * OE, * tmp;
if (!s) { if (!s) {
return NULL; return NULL;
} }
osz = (1 + strlen (s)) * 20; const char **colors = palette ? palette: origColors;
int osz = (1 + strlen (s)) * 20;
if (osz < 1) { if (osz < 1) {
return NULL; return NULL;
} }
O = malloc (osz); char *O = malloc (osz);
if (!O) { if (!O) {
return NULL; return NULL;
} }
@ -68,12 +84,13 @@ R_API char* r_print_json_indent(const char* s, bool color, const char* tab) {
} }
if (instr) { if (instr) {
if (isValue) { if (isValue) {
EMIT_ESC (o, "[34m"); // TODO: do not emit color in every char
EMIT_ESC (o, colors[JC_VAL]);
} else { } else {
EMIT_ESC (o, "[33m"); EMIT_ESC (o, colors[JC_KEY]);
} }
} else { } else {
EMIT_ESC (o, "[0m"); EMIT_ESC (o, colors[JC_RESET]);
} }
*o++ = *s; *o++ = *s;
continue; continue;
@ -97,14 +114,14 @@ R_API char* r_print_json_indent(const char* s, bool color, const char* tab) {
*o++ = *s; *o++ = *s;
*o++ = ' '; *o++ = ' ';
if (!strncmp (s + 1, "true", 4)) { if (!strncmp (s + 1, "true", 4)) {
EMIT_ESC (o, "[32m"); EMIT_ESC (o, colors[JC_TRUE]);
} else if (!strncmp (s + 1, "false", 5)) { } else if (!strncmp (s + 1, "false", 5)) {
EMIT_ESC (o, "[31m"); EMIT_ESC (o, colors[JC_FALSE]);
} }
isValue = true; isValue = true;
break; break;
case ',': case ',':
EMIT_ESC (o, "[0m"); EMIT_ESC (o, colors[JC_RESET]);
*o++ = *s; *o++ = *s;
*o++ = '\n'; *o++ = '\n';
isValue = false; isValue = false;
@ -120,7 +137,7 @@ R_API char* r_print_json_indent(const char* s, bool color, const char* tab) {
break; break;
case '}': case '}':
case ']': case ']':
EMIT_ESC (o, "[0m"); EMIT_ESC (o, colors[JC_RESET]);
isValue = false; isValue = false;
*o++ = '\n'; *o++ = '\n';
indent--; indent--;

View File

@ -48,10 +48,10 @@ endif
OBJS= zip_add.o zip_add_dir.o zip_add_entry.o \ OBJS= zip_add.o zip_add_dir.o zip_add_entry.o \
zip_close.o zip_delete.o zip_dir_add.o zip_dirent.o \ zip_close.o zip_delete.o zip_dir_add.o zip_dirent.o \
zip_discard.o zip_entry.o zip_err_str.o zip_error.o \ zip_discard.o zip_entry.o zip_err_str.o zip_error.o \
zip_error_clear.o zip_error_get.o zip_error_get_sys_type.o \ zip_error_get.o zip_error_get_sys_type.o \
zip_error_strerror.o zip_error_to_str.o zip_extra_field.o \ zip_error_strerror.o zip_error_to_str.o zip_extra_field.o \
zip_extra_field_api.o zip_fclose.o zip_fdopen.o \ zip_extra_field_api.o zip_fclose.o zip_fdopen.o \
zip_file_add.o zip_file_error_clear.o zip_file_error_get.o \ zip_file_add.o zip_file_error_get.o \
zip_file_get_comment.o zip_file_get_offset.o \ zip_file_get_comment.o zip_file_get_offset.o \
zip_file_rename.o zip_file_replace.o zip_file_set_comment.o \ zip_file_rename.o zip_file_replace.o zip_file_set_comment.o \
zip_file_strerror.o zip_filerange_crc.o zip_fopen.o \ zip_file_strerror.o zip_filerange_crc.o zip_fopen.o \

View File

@ -113,3 +113,24 @@ _zip_error_set_from_source(struct zip_error *err, struct zip_source *src)
zip_source_error(src, &ze, &se); zip_source_error(src, &ze, &se);
_zip_error_set(err, ze, se); _zip_error_set(err, ze, se);
} }
ZIP_EXTERN void
zip_file_error_clear(struct zip_file *zf)
{
if (zf) {
_zip_error_clear(&zf->error);
}
}
ZIP_EXTERN void
zip_error_clear(struct zip *za)
{
if (za == NULL)
return;
_zip_error_clear(&za->error);
}

View File

@ -208,6 +208,7 @@ _zip_readcdir(FILE *fp, off_t buf_offset, unsigned char *buf, const unsigned cha
zip_uint64_t i, left; zip_uint64_t i, left;
tail_len = buf + buflen - eocd - EOCDLEN; tail_len = buf + buflen - eocd - EOCDLEN;
// eprintf ("[zip] central dir at 0x%08x\n", (int) buf_offset);
if (tail_len < 0) { if (tail_len < 0) {
/* not enough bytes left for comment */ /* not enough bytes left for comment */
_zip_error_set(error, ZIP_ER_NOZIP, 0); _zip_error_set(error, ZIP_ER_NOZIP, 0);
@ -412,8 +413,9 @@ _zip_headercomp(const struct zip_dirent *central, const struct zip_dirent *local
#endif #endif
|| (central->comp_method != local->comp_method) || (central->comp_method != local->comp_method)
|| (central->last_mod != local->last_mod) || (central->last_mod != local->last_mod)
|| !_zip_string_equal(central->filename, local->filename)) || !_zip_string_equal(central->filename, local->filename)) {
return -1; return -1;
}
if ((central->crc != local->crc) || (central->comp_size != local->comp_size) if ((central->crc != local->crc) || (central->comp_size != local->comp_size)

View File

@ -74,6 +74,11 @@ RebuildGdb() {
Rebuild libr/debug Rebuild libr/debug
} }
RebuildZip() {
Rebuild shlr/zip
Rebuild libr/io
}
case "$1" in case "$1" in
grub|fs)RebuildFs; ;; grub|fs)RebuildFs; ;;
bin) RebuildBin ; ;; bin) RebuildBin ; ;;
@ -81,6 +86,7 @@ gdb) RebuildGdb ; ;;
sdb) RebuildSdb ; ;; sdb) RebuildSdb ; ;;
spp) RebuildSpp ; ;; spp) RebuildSpp ; ;;
bin) RebuildBin ; ;; bin) RebuildBin ; ;;
zip) RebuildZip ; ;;
java) RebuildJava ; ;; java) RebuildJava ; ;;
iosdbg) RebuildIOSDebug ; ;; iosdbg) RebuildIOSDebug ; ;;
capstone|cs) RebuildCapstone ; ;; capstone|cs) RebuildCapstone ; ;;