Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Move grep from RzCons to RzCore #3243

Draft
wants to merge 1 commit into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 1 addition & 37 deletions librz/cons/cons.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ typedef struct {
char *buf;
int buf_len;
int buf_size;
RzConsGrep *grep;
bool noflush;
} RzConsStack;

Expand All @@ -36,8 +35,6 @@ typedef struct {
void *event_interrupt_data;
} RzConsBreakStack;

static void cons_grep_reset(RzConsGrep *grep);

static void ctx_rowcol_calc_reset(void) {
CTX(row) = 0;
CTX(col) = 0;
Expand All @@ -52,11 +49,6 @@ static void break_stack_free(void *ptr) {
static void cons_stack_free(void *ptr) {
RzConsStack *s = (RzConsStack *)ptr;
free(s->buf);
if (s->grep) {
RZ_FREE(s->grep->str);
CTX(grep.str) = NULL;
}
free(s->grep);
free(s);
}

Expand All @@ -69,13 +61,6 @@ static RzConsStack *cons_stack_dump(bool recreate) {
data->buf_size = CTX(buffer_sz);
}
data->noflush = CTX(noflush);
data->grep = RZ_NEW0(RzConsGrep);
if (data->grep) {
memcpy(data->grep, &CTX(grep), sizeof(RzConsGrep));
if (CTX(grep).str) {
data->grep->str = strdup(CTX(grep).str);
}
}
if (recreate && CTX(buffer_sz) > 0) {
CTX(buffer) = malloc(CTX(buffer_sz));
ctx_rowcol_calc_reset();
Expand All @@ -100,10 +85,6 @@ static void cons_stack_load(RzConsStack *data, bool free_current) {
data->buf = NULL;
CTX(buffer_len) = data->buf_len;
CTX(buffer_sz) = data->buf_size;
if (data->grep) {
free(CTX(grep).str);
memcpy(&CTX(grep), data->grep, sizeof(RzConsGrep));
}
CTX(noflush) = data->noflush;
ctx_rowcol_calc_reset();
}
Expand Down Expand Up @@ -131,8 +112,6 @@ static void cons_context_init(RzConsContext *context, RZ_NULLABLE RzConsContext
context->color_mode = COLOR_MODE_DISABLED;
rz_cons_pal_init(context);
}

cons_grep_reset(&context->grep);
}

static void cons_context_deinit(RzConsContext *context) {
Expand Down Expand Up @@ -799,22 +778,13 @@ RZ_API void rz_cons_clear(void) {
#endif
}

static void cons_grep_reset(RzConsGrep *grep) {
RZ_FREE(grep->str);
ZERO_FILL(*grep);
grep->line = -1;
grep->sort = -1;
grep->sort_invert = false;
}

RZ_API void rz_cons_reset(void) {
if (CTX(buffer)) {
(CTX(buffer))[0] = '\0';
}
CTX(buffer_len) = 0;
I.lines = 0;
I.lastline = CTX(buffer);
cons_grep_reset(&CTX(grep));
CTX(pageable) = true;
ctx_rowcol_calc_reset();
}
Expand All @@ -840,11 +810,6 @@ RZ_API int rz_cons_get_buffer_len(void) {
}

RZ_API void rz_cons_filter(void) {
/* grep */
if (I.filter || CTX(grep).nstrings > 0 || CTX(grep).tokens_used || CTX(grep).less || CTX(grep).json) {
(void)rz_cons_grepbuf();
I.filter = false;
}
/* html */
if (I.is_html) {
int newlen = 0;
Expand Down Expand Up @@ -939,7 +904,7 @@ RZ_API void rz_cons_last(void) {
}

static bool lastMatters(void) {
return (CTX(buffer_len) > 0) && (CTX(lastEnabled) && !I.filter && CTX(grep).nstrings < 1 && !CTX(grep).tokens_used && !CTX(grep).less && !CTX(grep).json && !I.is_html);
return (CTX(buffer_len) > 0) && (CTX(lastEnabled) && !I.filter && !I.is_html);
}

RZ_API void rz_cons_echo(const char *msg) {
Expand Down Expand Up @@ -1963,7 +1928,6 @@ RZ_API void rz_cons_bind(RzConsBind *bind) {
bind->get_cursor = rz_cons_get_cursor;
bind->cb_printf = rz_cons_printf;
bind->cb_flush = rz_cons_flush;
bind->cb_grep = rz_cons_grep;
bind->is_breaked = rz_cons_is_breaked;
}

Expand Down
1 change: 0 additions & 1 deletion librz/cons/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ rz_cons_sources = [
'cons.c',
'html.c',
# 'dietline.c',
'grep.c',
'hud.c',
'input.c',
'less.c',
Expand Down
99 changes: 46 additions & 53 deletions librz/cons/grep.c → librz/core/grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
#include <rz_util/rz_print.h>
#include <sdb.h>

#define I(x) rz_cons_singleton()->x

static char *strchr_ns(char *s, const char ch) {
char *p = strchr(s, ch);
if (p && p > s) {
Expand Down Expand Up @@ -62,19 +60,14 @@ static const char *help_detail_tilde[] = {
NULL
};

/* TODO: remove globals */
static RzList *sorted_lines = NULL;
static RzList *unsorted_lines = NULL;
static int sorted_column = -1;

RZ_API void rz_cons_grep_help(void) {
RZ_API void rz_core_grep_help(void) {
rz_cons_cmd_help(help_detail_tilde, true);
}

#define RZ_CONS_GREP_BUFSIZE 4096
#define RZ_CORE_GREP_BUFSIZE 4096

static void parse_grep_expression(const char *str) {
static char buf[RZ_CONS_GREP_BUFSIZE];
static char buf[RZ_CORE_GREP_BUFSIZE];
int wlen, len, is_range, num_is_parsed, fail = 0;
char *ptr, *optr, *ptr2, *ptr3, *end_ptr = NULL, last;
ut64 range_begin, range_end;
Expand All @@ -83,8 +76,8 @@ static void parse_grep_expression(const char *str) {
return;
}
RzCons *cons = rz_cons_singleton();
RzConsGrep *grep = &cons->context->grep;
sorted_column = 0;
RzCoreGrep *grep = &cons->context->grep;
grep->sorted_column = 0;
bool first = true;
while (*str) {
switch (*str) {
Expand Down Expand Up @@ -180,7 +173,7 @@ static void parse_grep_expression(const char *str) {
str++;
} else if (*str == '?') {
cons->filter = true;
rz_cons_grep_help();
rz_core_grep_help();
return;
}
break;
Expand All @@ -192,8 +185,8 @@ static void parse_grep_expression(const char *str) {
while_end:

len = strlen(str) - 1;
if (len > RZ_CONS_GREP_BUFSIZE - 1) {
eprintf("rz_cons_grep: too long!\n");
if (len > RZ_CORE_GREP_BUFSIZE - 1) {
eprintf("rz_core_grep: too long!\n");
return;
}
if (len > 0 && str[len] == '?') {
Expand Down Expand Up @@ -233,7 +226,7 @@ static void parse_grep_expression(const char *str) {
case ']': // fallthrough to handle ']' like ','
case ',':
for (; range_begin <= range_end; range_begin++) {
if (range_begin >= RZ_CONS_GREP_TOKENS) {
if (range_begin >= RZ_CORE_GREP_TOKENS) {
fail = 1;
break;
}
Expand Down Expand Up @@ -314,17 +307,17 @@ static void parse_grep_expression(const char *str) {
if (!wlen) {
continue;
}
if (wlen >= RZ_CONS_GREP_WORD_SIZE - 1) {
if (wlen >= RZ_CORE_GREP_WORD_SIZE - 1) {
eprintf("grep string too long\n");
continue;
}
grep->nstrings++;
if (grep->nstrings > RZ_CONS_GREP_WORDS - 1) {
if (grep->nstrings > RZ_CORE_GREP_WORDS - 1) {
eprintf("too many grep strings\n");
break;
}
rz_str_ncpy(grep->strings[grep->nstrings - 1],
optr, RZ_CONS_GREP_WORD_SIZE);
optr, RZ_CORE_GREP_WORD_SIZE);
} while (ptr);
} else {
grep->str = strdup(ptr);
Expand Down Expand Up @@ -398,8 +391,8 @@ static char *preprocess_filter_expr(char *cmd, const char *quotes) {
return ns;
}

RZ_API void rz_cons_grep_parsecmd(char *cmd, const char *quotestr) {
rz_return_if_fail(cmd && quotestr);
RZ_API void rz_core_grep_parsecmd(RzCore *core, char *cmd, const char *quotestr) {
rz_return_if_fail(core && cmd && quotestr);
char *ptr = preprocess_filter_expr(cmd, quotestr);
if (ptr) {
rz_str_trim(cmd);
Expand All @@ -408,7 +401,7 @@ RZ_API void rz_cons_grep_parsecmd(char *cmd, const char *quotestr) {
}
}

RZ_API char *rz_cons_grep_strip(char *cmd, const char *quotestr) {
RZ_API char *rz_core_grep_strip(RzCore *core, char *cmd, const char *quotestr) {
char *ptr = NULL;

if (cmd) {
Expand All @@ -418,7 +411,7 @@ RZ_API char *rz_cons_grep_strip(char *cmd, const char *quotestr) {
return ptr;
}

RZ_API void rz_cons_grep_process(char *grep) {
RZ_API void rz_core_grep_process(RzCore *core, char *grep) {
if (grep) {
parse_grep_expression(grep);
free(grep);
Expand All @@ -433,13 +426,13 @@ static int cmp(const void *a, const void *b) {
if (!a || !b) {
return (int)(size_t)((char *)a - (char *)b);
}
if (sorted_column > 0) {
if (grep->sorted_column > 0) {
da = strdup(ca);
db = strdup(cb);
int colsa = rz_str_word_set0(da);
int colsb = rz_str_word_set0(db);
ca = (colsa > sorted_column) ? rz_str_word_get0(da, sorted_column) : "";
cb = (colsb > sorted_column) ? rz_str_word_get0(db, sorted_column) : "";
ca = (colsa > grep->sorted_column) ? rz_str_word_get0(da, grep->sorted_column) : "";
cb = (colsb > grep->sorted_column) ? rz_str_word_get0(db, grep->sorted_column) : "";
}
if (IS_DIGIT(*ca) && IS_DIGIT(*cb)) {
ut64 na = rz_num_get(NULL, ca);
Expand All @@ -460,14 +453,14 @@ static int cmp(const void *a, const void *b) {
return strcmp(a, b);
}

RZ_API void rz_cons_grepbuf(void) {
RzCons *cons = rz_cons_singleton();
RZ_API void rz_core_grepbuf(RzCore *core) {
RzCons *cons = core->cons;
cons->context->row = 0;
cons->context->col = 0;
cons->context->rowcol_calc_start = 0;
const char *buf = cons->context->buffer;
const int len = cons->context->buffer_len;
RzConsGrep *grep = &cons->context->grep;
RzCoreGrep *grep = &cons->context->grep;
const char *in = buf;
int ret, total_lines = 0, l = 0, tl = 0;
bool show = false;
Expand Down Expand Up @@ -546,7 +539,7 @@ RZ_API void rz_cons_grepbuf(void) {
rz_str_ansi_filter(bb, NULL, NULL, -1);
char *out = (cons->context->grep.human)
? rz_print_json_human(bb)
: rz_print_json_indent(bb, I(context->color_mode), " ", palette);
: rz_print_json_indent(bb, cons->context->color_mode), " ", palette);
free(bb);
if (!out) {
return;
Expand Down Expand Up @@ -637,7 +630,7 @@ RZ_API void rz_cons_grepbuf(void) {
if (tl < 0) {
ret = -1;
} else {
ret = rz_cons_grep_line(tline, tl);
ret = rz_core_grep_line(tline, tl);
if (!grep->range_line) {
if (grep->line == cons->lines) {
show = true;
Expand Down Expand Up @@ -731,24 +724,24 @@ RZ_API void rz_cons_grepbuf(void) {
int nl = 0;
char *ptr = cons->context->buffer;
char *str;
sorted_column = grep->sort;
rz_list_sort(sorted_lines, cmp);
grep->sorted_column = grep->sort;
rz_list_sort(grep->sorted_lines, cmp);
if (grep->sort_invert) {
rz_list_reverse(sorted_lines);
rz_list_reverse(grep->sorted_lines);
}
INSERT_LINES(unsorted_lines);
INSERT_LINES(sorted_lines);
INSERT_LINES(grep->unsorted_lines);
INSERT_LINES(grep->sorted_lines);
cons->lines = nl;
rz_list_free(sorted_lines);
sorted_lines = NULL;
rz_list_free(unsorted_lines);
unsorted_lines = NULL;
rz_list_free(grep->sorted_lines);
grep->sorted_lines = NULL;
rz_list_free(grep->unsorted_lines);
grep->unsorted_lines = NULL;
}
}

RZ_API int rz_cons_grep_line(char *buf, int len) {
RzCons *cons = rz_cons_singleton();
RzConsGrep *grep = &cons->context->grep;
RZ_API int rz_core_grep_line(RzCore *core, char *buf, int len) {
RzCons *cons = core->cons;
RzCoreGrep *grep = &cons->context->grep;
const char *delims = " |,;=\t";
char *tok = NULL;
bool hit = grep->neg;
Expand Down Expand Up @@ -816,7 +809,7 @@ RZ_API int rz_cons_grep_line(char *buf, int len) {
use_tok = true;
}
if (use_tok && grep->tokens_used) {
for (i = 0; i < RZ_CONS_GREP_TOKENS; i++) {
for (i = 0; i < RZ_CORE_GREP_TOKENS; i++) {
tok = strtok(i ? NULL : in, delims);
if (tok) {
if (grep->tokens[i]) {
Expand All @@ -841,7 +834,7 @@ RZ_API int rz_cons_grep_line(char *buf, int len) {
}
outlen = outlen > 0 ? outlen - 1 : 0;
if (outlen > len) { // should never happen
eprintf("rz_cons_grep_line: how you have reached this?\n");
eprintf("rz_core_grep_line: how you have reached this?\n");
free(in);
free(out);
return -1;
Expand All @@ -857,24 +850,24 @@ RZ_API int rz_cons_grep_line(char *buf, int len) {
if (grep->sort != -1) {
char ch = buf[len];
buf[len] = 0;
if (!sorted_lines) {
sorted_lines = rz_list_newf(free);
if (!grep->sorted_lines) {
grep->sorted_lines = rz_list_newf(free);
}
if (!unsorted_lines) {
unsorted_lines = rz_list_newf(free);
if (!grep->unsorted_lines) {
grep->unsorted_lines = rz_list_newf(free);
}
if (cons->lines >= grep->sort_row) {
rz_list_append(sorted_lines, strdup(buf));
rz_list_append(grep->sorted_lines, strdup(buf));
} else {
rz_list_append(unsorted_lines, strdup(buf));
rz_list_append(grep->unsorted_lines, strdup(buf));
}
buf[len] = ch;
}

return len;
}

RZ_API void rz_cons_grep(const char *grep) {
RZ_API void rz_core_grep(RzCore *core, const char *grep) {
parse_grep_expression(grep);
rz_cons_grepbuf();
rz_core_grepbuf(core);
}
1 change: 1 addition & 0 deletions librz/core/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ rz_core_sources = [
'disasm.c',
'fortune.c',
'golang.c',
'grep.c',
'hack.c',
'libs.c',
'linux_heap_glibc.c',
Expand Down
Loading