Fix #6818 - json indent ~{} honors scr.color

- Cannot configure the color palette
This commit is contained in:
pancake 2017-02-20 02:31:49 +01:00
parent a5a9b8bc4e
commit 4bbc73a0e2
13 changed files with 172 additions and 36 deletions

View File

@ -102,9 +102,9 @@ indent_cpp_lambda_body = false
sp_cpp_lambda_assign = ignore
sp_cpp_lambda_paren = ignore
sp_assign_default = ignore
sp_before_assign = ignore
sp_after_assign = ignore
sp_assign_default = force
sp_before_assign = force
sp_after_assign = force
sp_enum_paren = ignore
sp_enum_assign = ignore
sp_enum_before_assign = ignore
@ -113,7 +113,7 @@ sp_pp_concat = add
sp_pp_stringify = ignore
sp_before_pp_stringify = ignore
sp_bool = force
sp_compare = ignore
sp_compare = force
sp_inside_paren = remove
sp_paren_paren = remove
sp_cparen_oparen = ignore

View File

@ -310,6 +310,7 @@ R_API RCons *r_cons_new() {
I.event_interrupt = NULL;
I.is_wine = -1;
I.fps = 0;
I.use_color = false;
I.blankline = true;
I.teefile = NULL;
I.fix_columns = 0;
@ -510,7 +511,7 @@ R_API const char *r_cons_get_buffer() {
}
R_API void r_cons_filter() {
/* grep*/
/* grep */
if (I.grep.nstrings > 0 || I.grep.tokens_used || I.grep.less || I.grep.json) {
r_cons_grepbuf (I.buffer, I.buffer_len);
}
@ -518,7 +519,6 @@ R_API void r_cons_filter() {
/* TODO */
}
R_API void r_cons_push() {
if (I.cons_stack) {
RConsStack *data = R_NEW0 (RConsStack);

View File

@ -2,6 +2,7 @@
#include <r_cons.h>
#include <r_util.h>
#include <r_print.h>
#include <sdb.h>
#undef SDB_API
#define SDB_API static
@ -10,6 +11,7 @@ int js0n(const ut8 *js, RangstrType len, RangstrType *out);
#include "../../shlr/sdb/src/json/path.c"
// #include "../../shlr/sdb/src/json.c"
#define I(x) r_cons_singleton()->x
/* TODO: remove globals */
static RList *sorted_lines = NULL;
static RList *unsorted_lines = NULL;
@ -323,11 +325,11 @@ R_API int r_cons_grepbuf(char *buf, int len) {
cons->buffer_len = strlen (u);
cons->buffer_sz = cons->buffer_len + 1;
cons->grep.json = 0;
r_cons_newline();
r_cons_newline ();
}
R_FREE (cons->grep.json_path);
} else {
char *out = sdb_json_indent (buf);
char *out = r_print_json_indent (buf, I(use_color));
free (cons->buffer);
cons->buffer = out;
cons->buffer_len = strlen (out);
@ -344,7 +346,7 @@ R_API int r_cons_grepbuf(char *buf, int len) {
int less = cons->grep.less;
cons->grep.less = 0;
if (less == 2) {
char *res = r_cons_hud_string (buf, true);
char *res = r_cons_hud_string (buf);
r_cons_println (res);
free (res);
} else {

View File

@ -3,11 +3,13 @@
#include <r_cons.h>
#include <ctype.h>
#define I(x) r_cons_singleton()->x
// Display the content of a file in the hud
R_API char *r_cons_hud_file(const char *f, const bool usecolor) {
R_API char *r_cons_hud_file(const char *f) {
char *s = r_file_slurp (f, NULL);
if (s) {
char *ret = r_cons_hud_string (s, usecolor);
char *ret = r_cons_hud_string (s);
free (s);
return ret;
}
@ -16,7 +18,7 @@ R_API char *r_cons_hud_file(const char *f, const bool usecolor) {
// Display a buffer in the hud (splitting it line-by-line and ignoring
// the lines starting with # )
R_API char *r_cons_hud_string(const char *s, const bool usecolor) {
R_API char *r_cons_hud_string(const char *s) {
char *os, *track, *ret, *o = strdup (s);
if (!o) {
return NULL;
@ -41,7 +43,7 @@ R_API char *r_cons_hud_string(const char *s, const bool usecolor) {
os = o + i + 1;
}
}
ret = r_cons_hud (fl, NULL, usecolor);
ret = r_cons_hud (fl, NULL);
free (o);
r_list_free (fl);
return ret;
@ -93,7 +95,7 @@ static bool strmatch(char *entry, char *filter, char* mask, const int mask_size)
// Display a list of entries in the hud, filtered and emphasized based
// on the user input.
R_API char *r_cons_hud(RList *list, const char *prompt, const bool usecolor) {
R_API char *r_cons_hud(RList *list, const char *prompt) {
const int buf_size = 128;
int ch, nch, first_line, current_entry_n, j, i = 0;
char *p, *x, user_input[buf_size], mask[buf_size];
@ -143,7 +145,7 @@ R_API char *r_cons_hud(RList *list, const char *prompt, const bool usecolor) {
r_cons_printf (" %c %s\n", first_line? '-': ' ', current_entry);
} else {
// otherwise we need to emphasize the matching part
if (usecolor) {
if (I(use_color)) {
last_color_change = 0;
last_mask = 0;
r_cons_printf (" %c ", first_line? '-': ' ');
@ -266,7 +268,7 @@ R_API char *r_cons_hud(RList *list, const char *prompt, const bool usecolor) {
}
// Display the list of files in a directory
R_API char *r_cons_hud_path(const char *path, int dir, const bool usecolor) {
R_API char *r_cons_hud_path(const char *path, int dir) {
char *tmp, *ret = NULL;
RList *files;
if (path) {
@ -277,7 +279,7 @@ R_API char *r_cons_hud_path(const char *path, int dir, const bool usecolor) {
}
files = r_sys_dir (tmp);
if (files) {
ret = r_cons_hud (files, tmp, usecolor);
ret = r_cons_hud (files, tmp);
if (ret) {
tmp = r_str_concat (tmp, "/");
tmp = r_str_concat (tmp, ret);
@ -285,7 +287,7 @@ R_API char *r_cons_hud_path(const char *path, int dir, const bool usecolor) {
free (tmp);
tmp = ret;
if (r_file_is_directory (tmp)) {
ret = r_cons_hud_path (tmp, dir, usecolor);
ret = r_cons_hud_path (tmp, dir);
free (tmp);
tmp = ret;
}

View File

@ -1,4 +1,4 @@
/* radare - LGPL - Copyright 2009-2016 - pancake */
/* radare - LGPL - Copyright 2009-2017 - pancake */
#include <r_core.h>
@ -756,6 +756,7 @@ static int cb_color(void *user, void *data) {
//c:core->print->flags ^= R_PRINT_FLAGS_COLOR;
core->print->flags &= (~R_PRINT_FLAGS_COLOR);
}
r_cons_singleton ()->use_color = node->i_value? 1: 0;
r_print_set_flags (core->print, core->print->flags);
return true;
}

View File

@ -164,14 +164,15 @@ R_API int r_core_visual_hud(RCore *core) {
char *homehud = r_str_home (R2_HOMEDIR"/hud");
char *res = NULL;
char *p = 0;
r_cons_singleton ()->use_color = use_color;
showcursor (core, true);
if (c && *c && r_file_exists (c))
res = r_cons_hud_file (c, use_color);
res = r_cons_hud_file (c);
if (!res && homehud)
res = r_cons_hud_file (homehud, use_color);
res = r_cons_hud_file (homehud);
if (!res && r_file_exists (f))
res = r_cons_hud_file (f, use_color);
res = r_cons_hud_file (f);
if (!res)
r_cons_message ("Cannot find hud file");

View File

@ -662,7 +662,7 @@ R_API bool r_core_visual_hudclasses(RCore *core) {
m->vaddr, c->name, m->name));
}
}
res = r_cons_hud (list, NULL, r_config_get_i (core->config, "scr.color"));
res = r_cons_hud (list, NULL);
if (res) {
char *p = strchr (res, ' ');
if (p) {
@ -691,7 +691,7 @@ R_API bool r_core_visual_hudstuff(RCore *core) {
flag->offset, flag->name));
}
sdb_foreach (core->anal->sdb_meta, cmtcb, list);
res = r_cons_hud (list, NULL, r_config_get_i (core->config, "scr.color"));
res = r_cons_hud (list, NULL);
if (res) {
char *p = strchr (res, ' ');
if (p) {
@ -715,7 +715,7 @@ static bool r_core_visual_config_hud(RCore *core) {
r_list_foreach (core->config->nodes, iter, bt) {
r_list_append (list, r_str_newf ("%s %s", bt->name, bt->value));
}
res = r_cons_hud (list, NULL, r_config_get_i (core->config, "scr.color"));
res = r_cons_hud (list, NULL);
if (res) {
const char *oldvalue = NULL;
char cmd[512];

View File

@ -310,7 +310,7 @@ R_API int r_core_yank_hud_file(RCore *core, const char *input) {
for (input++; *input == ' '; input++) {
/* nothing */
}
buf = r_cons_hud_file (input, r_config_get_i (core->config, "scr.color"));
buf = r_cons_hud_file (input);
len = buf ? strlen ((const char*)buf) + 1 : 0;
res = r_core_yank_set_str (core, R_CORE_FOREIGN_ADDR, buf, len);
free (buf);
@ -324,7 +324,7 @@ R_API int r_core_yank_hud_path(RCore *core, const char *input, int dir) {
for (input++; *input==' '; input++) {
/* nothing */
}
buf = r_cons_hud_path (input, dir, r_config_get_i (core->config, "scr.color"));
buf = r_cons_hud_path (input, dir);
len = buf ? strlen ((const char*)buf) + 1 : 0;
res = r_core_yank_set_str (core, R_CORE_FOREIGN_ADDR, buf, len);
free (buf);

View File

@ -274,6 +274,7 @@ typedef struct r_cons_t {
char *break_word;
int break_word_len;
ut64 timeout;
bool use_color;
} RCons;
// XXX THIS MUST BE A SINGLETON AND WRAPPED INTO RCons */
@ -518,10 +519,10 @@ R_API int r_cons_html_print(const char *ptr);
// TODO: use gets() .. MUST BE DEPRECATED
R_API int r_cons_fgets(char *buf, int len, int argc, const char **argv);
R_API char *r_cons_hud(RList *list, const char *prompt, const bool usecolor);
R_API char *r_cons_hud_path(const char *path, int dir, const bool usecolor);
R_API char *r_cons_hud_string(const char *s, const bool usecolor);
R_API char *r_cons_hud_file(const char *f, const bool usecolor);
R_API char *r_cons_hud(RList *list, const char *prompt);
R_API char *r_cons_hud_path(const char *path, int dir);
R_API char *r_cons_hud_string(const char *s);
R_API char *r_cons_hud_file(const char *f);
R_API const char *r_cons_get_buffer(void);
R_API void r_cons_grep(const char *str);

View File

@ -168,6 +168,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_set_screenbounds(RPrint *p, ut64 addr);
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);
#endif

View File

@ -12,7 +12,7 @@ OBJS+=strpool.o bitmap.o strht.o p_date.o p_format.o print.o
OBJS+=p_seven.o slist.o randomart.o log.o zip.o debruijn.o
OBJS+=utf8.o strbuf.o lib.o name.o spaces.o signal.o syscmd.o
OBJS+=diff.o bdiff.o stack.o queue.o tree.o des.o idpool.o
OBJS+=punycode.o r_pkcs7.o r_x509.o r_asn1.o
OBJS+=punycode.o r_pkcs7.o r_x509.o r_asn1.o json_indent.o
# DO NOT BUILD r_big api (not yet used and its buggy)
ifeq (1,0)

128
libr/util/json_indent.c Normal file
View File

@ -0,0 +1,128 @@
/* radare2 (from sdb) - MIT - Copyright 2012-2017 - pancake */
#include <r_util.h>
#include <r_print.h>
R_API char* r_print_json_indent(const char* s, bool color) {
if (!color) {
return sdb_json_indent (s);
}
int indent = 0;
int i, instr = 0;
bool isValue = false;
int osz;
char* o, * O, * OE, * tmp;
if (!s) {
return NULL;
}
osz = (1 + strlen (s)) * 20;
if (osz < 1) {
return NULL;
}
O = malloc (osz);
if (!O) {
return NULL;
}
OE = O + osz;
for (o = O; *s; s++) {
if (o + indent + 10 > OE) {
int delta = o - O;
osz += 0x1000 + indent;
if (osz < 1) {
free (O);
return NULL;
}
tmp = realloc (O, osz);
if (!tmp) {
free (O);
return NULL;
}
O = tmp;
OE = tmp + osz;
o = O + delta;
}
if (instr) {
if (s[0] == '"') {
instr = 0;
} else if (s[0] == '\\' && s[1] == '"') {
*o++ = *s;
}
if (instr) {
*o++ = 0x1b;
*o++ = '[';
if (isValue) {
*o++ = '3';
*o++ = '4';
} else {
*o++ = '3';
*o++ = '3';
}
*o++ = 'm';
} else {
*o++ = 0x1b;
*o++ = '[';
*o++ = '0';
*o++ = 'm';
}
*o++ = *s;
continue;
}
if (s[0] == '"') {
instr = 1;
}
if (*s == '\n' || *s == '\r' || *s == '\t' || *s == ' ') {
continue;
}
#define INDENT(x) indent += x; for (i = 0; i < indent; i++) *o++ = '\t'
switch (*s) {
case ':':
*o++ = *s;
*o++ = ' ';
if (!strncmp (s + 1, "true", 4)) {
*o++ = 0x1b;
*o++ = '[';
*o++ = '3';
*o++ = '2';
*o++ = 'm';
} else if (!strncmp (s + 1, "false", 5)) {
*o++ = 0x1b;
*o++ = '[';
*o++ = '3';
*o++ = '1';
*o++ = 'm';
}
isValue = true;
break;
case ',':
*o++ = 0x1b;
*o++ = '[';
*o++ = '0';
*o++ = 'm';
*o++ = *s;
*o++ = '\n';
isValue = false;
INDENT (0);
break;
case '{':
case '[':
isValue = false;
*o++ = *s;
*o++ = (indent != -1)? '\n': ' ';
INDENT (1);
break;
case '}':
case ']':
isValue = false;
*o++ = '\n';
INDENT (-1);
*o++ = *s;
break;
default:
*o++ = *s;
}
}
*o++ = '\n';
*o = 0;
return O;
}

View File

@ -89,12 +89,12 @@ fi
# spaces in { brackets
mv .tmp-format .tmp-format2
#perl -ne 's/{\s/{ /g;print' < .tmp-format2 > .tmp-format
perl -ne 's/{([^ \n])/{ \1/g if(!/"/);print' < .tmp-format2 > .tmp-format
#perl -ne 's/{([^ \n])/{ \1/g if(!/"/);print' < .tmp-format2 > .tmp-format
# spaces in } brackets
mv .tmp-format .tmp-format2
perl -ne 's/([^ \t])}/$1 }/g if(!/"/);print' < .tmp-format2 > .tmp-format
#mv .tmp-format .tmp-format2
#perl -ne 's/([^ \t])}/$1 }/g if(!/"/);print' < .tmp-format2 > .tmp-format
# _( macro
mv .tmp-format .tmp-format2
#mv .tmp-format .tmp-format2
perl -ne 's/_\s\(/_(/g;print' < .tmp-format2 > .tmp-format
# 0xa0
mv .tmp-format .tmp-format2