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

Fix grep : after [] #10749

Merged
merged 4 commits into from Jul 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 15 additions & 13 deletions libr/cons/grep.c
Expand Up @@ -31,7 +31,7 @@ static const char *help_detail_tilde[] = {
" ?", "", "count number of matching lines",
" ?.", "", "count number chars",
" ??", "", "show this help message",
" :[s]-[e]", "", "show lines s-e",
" :s..e", "", "show lines s-e",
" ..", "", "internal 'less'",
" ...", "", "internal 'hud' (like V_)",
" {}", "", "json indentation",
Expand All @@ -46,7 +46,8 @@ static const char *help_detail_tilde[] = {
" [i,j,k]", "", "show the columns i, j and k",
"Examples:", "", "",
" i~:0", "", "show first line of 'i' output",
" i~:-2", "", "show first three lines of 'i' output",
" i~:-2", "", "show the second to last line of 'i' output",
" i~:0..3", "", "show first three lines of 'i' output",
" pd~mov", "", "disasm and grep for mov",
" pi~[0]", "", "show only opcode",
" i~0x400$", "", "show lines ending with 0x400",
Expand All @@ -67,7 +68,7 @@ R_API void r_cons_grep_help(void) {
static void parse_grep_expression(const char *str) {
static char buf[R_CONS_GREP_BUFSIZE];
int wlen, len, is_range, num_is_parsed, fail = 0;
char *ptr, *optr, *ptr2, *ptr3;
char *ptr, *optr, *ptr2, *ptr3, *end_ptr = NULL, last;
ut64 range_begin, range_end;

if (!str || !*str) {
Expand Down Expand Up @@ -198,7 +199,9 @@ static void parse_grep_expression(const char *str) {
range_begin = range_end = -1;

if (ptr2 && ptr3) {
ptr2[0] = '\0';
end_ptr = ptr2;
last = ptr3[1];
ptr3[1] = '\0';
ptr2++;
for (; ptr2 <= ptr3; ++ptr2) {
if (fail) {
Expand Down Expand Up @@ -247,13 +250,14 @@ static void parse_grep_expression(const char *str) {
}
}
}
ptr3[1] = last;
}

ptr2 = strchr_ns (ptr, ':'); // line number
grep->range_line = 2; // there is not :
if (ptr2 && ptr2[1] != ':' && ptr2[1]) {
*ptr2 = '\0';
char *p, *token = ptr + 1;
if (ptr2 && ptr2[1] != ':' && ptr2[1] && (IS_DIGIT (ptr2[1]) || ptr2[1] == '-' || ptr2[1] == '.')) {
end_ptr = end_ptr ? R_MIN (end_ptr, ptr2) : ptr2;
char *p, *token = ptr2 + 1;
p = strstr (token, "..");
if (!p) {
grep->line = r_num_get (cons->num, ptr2 + 1);
Expand All @@ -273,6 +277,9 @@ static void parse_grep_expression(const char *str) {
}
}
}
if (end_ptr) {
*end_ptr = '\0';
}
free (grep->str);
if (*ptr) {
grep->str = (char *) strdup (ptr);
Expand Down Expand Up @@ -714,12 +721,7 @@ R_API int r_cons_grep_line(char *buf, int len) {
use_tok = true;
}
} else if (grep->range_line == 1) {
if (grep->f_line == cons->lines) {
use_tok = true;
}
if (grep->l_line == cons->lines) {
use_tok = false;
}
use_tok = R_BETWEEN (grep->f_line, cons->lines, grep->l_line);
} else {
use_tok = true;
}
Expand Down