diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-07-29 01:20:09 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-07-29 01:20:09 +0200 |
commit | 09449630fbf0331a76a9d055fcbff0774c57ab65 (patch) | |
tree | ce6e43bbb9efe4a76296f04457e98b79cf874459 | |
parent | 97af2ff8caa782f623e79408f0dfe608c878d5a6 (diff) | |
download | busybox-w32-09449630fbf0331a76a9d055fcbff0774c57ab65.tar.gz busybox-w32-09449630fbf0331a76a9d055fcbff0774c57ab65.tar.bz2 busybox-w32-09449630fbf0331a76a9d055fcbff0774c57ab65.zip |
grep: fix -E + {range}; fix -o not printing all matches (bug 489)
two different bugs, one with EXTRA_COMPAT, other without.
function old new delta
grep_file 1132 1129 -3
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r-- | findutils/grep.c | 36 | ||||
-rwxr-xr-x | testsuite/grep.tests | 5 |
2 files changed, 26 insertions, 15 deletions
diff --git a/findutils/grep.c b/findutils/grep.c index 774f04d5d..776a629be 100644 --- a/findutils/grep.c +++ b/findutils/grep.c | |||
@@ -120,17 +120,18 @@ struct globals { | |||
120 | } while (0) | 120 | } while (0) |
121 | #define max_matches (G.max_matches ) | 121 | #define max_matches (G.max_matches ) |
122 | #if !ENABLE_EXTRA_COMPAT | 122 | #if !ENABLE_EXTRA_COMPAT |
123 | #define reflags (G.reflags ) | 123 | # define reflags (G.reflags ) |
124 | #else | 124 | #else |
125 | #define case_fold (G.case_fold ) | 125 | # define case_fold (G.case_fold ) |
126 | /* http://www.delorie.com/gnu/docs/regex/regex_46.html */ | 126 | /* http://www.delorie.com/gnu/docs/regex/regex_46.html */ |
127 | #define reflags re_syntax_options | 127 | # define reflags re_syntax_options |
128 | #undef REG_NOSUB | 128 | # undef REG_NOSUB |
129 | #undef REG_EXTENDED | 129 | # undef REG_EXTENDED |
130 | #undef REG_ICASE | 130 | # undef REG_ICASE |
131 | #define REG_NOSUB bug:is:here /* should not be used */ | 131 | # define REG_NOSUB bug:is:here /* should not be used */ |
132 | #define REG_EXTENDED RE_SYNTAX_EGREP | 132 | /* Just RE_SYNTAX_EGREP is not enough, need to enable {n[,[m]]} too */ |
133 | #define REG_ICASE bug:is:here /* should not be used */ | 133 | # define REG_EXTENDED (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES) |
134 | # define REG_ICASE bug:is:here /* should not be used */ | ||
134 | #endif | 135 | #endif |
135 | #define invert_search (G.invert_search ) | 136 | #define invert_search (G.invert_search ) |
136 | #define print_filename (G.print_filename ) | 137 | #define print_filename (G.print_filename ) |
@@ -370,17 +371,22 @@ static int grep_file(FILE *file) | |||
370 | if (found) | 371 | if (found) |
371 | print_line(gl->pattern, strlen(gl->pattern), linenum, ':'); | 372 | print_line(gl->pattern, strlen(gl->pattern), linenum, ':'); |
372 | } else while (1) { | 373 | } else while (1) { |
373 | char old = line[gl->matched_range.rm_eo]; | 374 | unsigned end = gl->matched_range.rm_eo; |
374 | line[gl->matched_range.rm_eo] = '\0'; | 375 | char old = line[end]; |
376 | line[end] = '\0'; | ||
375 | print_line(line + gl->matched_range.rm_so, | 377 | print_line(line + gl->matched_range.rm_so, |
376 | gl->matched_range.rm_eo - gl->matched_range.rm_so, | 378 | end - gl->matched_range.rm_so, |
377 | linenum, ':'); | 379 | linenum, ':'); |
378 | line[gl->matched_range.rm_eo] = old; | 380 | line[end] = old; |
379 | #if !ENABLE_EXTRA_COMPAT | 381 | #if !ENABLE_EXTRA_COMPAT |
380 | break; | 382 | if (regexec(&gl->compiled_regex, line + end, |
383 | 1, &gl->matched_range, REG_NOTBOL) != 0) | ||
384 | break; | ||
385 | gl->matched_range.rm_so += end; | ||
386 | gl->matched_range.rm_eo += end; | ||
381 | #else | 387 | #else |
382 | if (re_search(&gl->compiled_regex, line, line_len, | 388 | if (re_search(&gl->compiled_regex, line, line_len, |
383 | gl->matched_range.rm_eo, line_len - gl->matched_range.rm_eo, | 389 | end, line_len - end, |
384 | &gl->matched_range) < 0) | 390 | &gl->matched_range) < 0) |
385 | break; | 391 | break; |
386 | #endif | 392 | #endif |
diff --git a/testsuite/grep.tests b/testsuite/grep.tests index 8cee1b9ee..7e3dff0d6 100755 --- a/testsuite/grep.tests +++ b/testsuite/grep.tests | |||
@@ -85,4 +85,9 @@ testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n" | |||
85 | testing "egrep is not case insensitive" \ | 85 | testing "egrep is not case insensitive" \ |
86 | "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n" | 86 | "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n" |
87 | 87 | ||
88 | testing "grep -E -o prints all matches" \ | ||
89 | "grep -E -o '([[:xdigit:]]{2}[:-]){5}[[:xdigit:]]{2}'" \ | ||
90 | "00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \ | ||
91 | "" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n" | ||
92 | |||
88 | exit $FAILCOUNT | 93 | exit $FAILCOUNT |