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>
This commit is contained in:
Denys Vlasenko 2009-07-29 01:20:09 +02:00
parent 97af2ff8ca
commit 09449630fb
2 changed files with 26 additions and 15 deletions

View File

@ -129,7 +129,8 @@ struct globals {
# undef REG_EXTENDED # undef REG_EXTENDED
# undef REG_ICASE # undef REG_ICASE
# define REG_NOSUB bug:is:here /* should not be used */ # define REG_NOSUB bug:is:here /* should not be used */
#define REG_EXTENDED RE_SYNTAX_EGREP /* Just RE_SYNTAX_EGREP is not enough, need to enable {n[,[m]]} too */
# define REG_EXTENDED (RE_SYNTAX_EGREP | RE_INTERVALS | RE_NO_BK_BRACES)
# define REG_ICASE bug:is:here /* should not be used */ # define REG_ICASE bug:is:here /* should not be used */
#endif #endif
#define invert_search (G.invert_search ) #define invert_search (G.invert_search )
@ -370,17 +371,22 @@ static int grep_file(FILE *file)
if (found) if (found)
print_line(gl->pattern, strlen(gl->pattern), linenum, ':'); print_line(gl->pattern, strlen(gl->pattern), linenum, ':');
} else while (1) { } else while (1) {
char old = line[gl->matched_range.rm_eo]; unsigned end = gl->matched_range.rm_eo;
line[gl->matched_range.rm_eo] = '\0'; char old = line[end];
line[end] = '\0';
print_line(line + gl->matched_range.rm_so, print_line(line + gl->matched_range.rm_so,
gl->matched_range.rm_eo - gl->matched_range.rm_so, end - gl->matched_range.rm_so,
linenum, ':'); linenum, ':');
line[gl->matched_range.rm_eo] = old; line[end] = old;
#if !ENABLE_EXTRA_COMPAT #if !ENABLE_EXTRA_COMPAT
if (regexec(&gl->compiled_regex, line + end,
1, &gl->matched_range, REG_NOTBOL) != 0)
break; break;
gl->matched_range.rm_so += end;
gl->matched_range.rm_eo += end;
#else #else
if (re_search(&gl->compiled_regex, line, line_len, if (re_search(&gl->compiled_regex, line, line_len,
gl->matched_range.rm_eo, line_len - gl->matched_range.rm_eo, end, line_len - end,
&gl->matched_range) < 0) &gl->matched_range) < 0)
break; break;
#endif #endif

View File

@ -85,4 +85,9 @@ testing "grep is also egrep" "egrep foo" "foo\n" "" "foo\nbar\n"
testing "egrep is not case insensitive" \ testing "egrep is not case insensitive" \
"egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n" "egrep foo ; [ \$? -ne 0 ] && echo yes" "yes\n" "" "FOO\n"
testing "grep -E -o prints all matches" \
"grep -E -o '([[:xdigit:]]{2}[:-]){5}[[:xdigit:]]{2}'" \
"00:19:3E:00:AA:5E\n00:1D:60:3D:3A:FB\n00:22:43:49:FB:AA\n" \
"" "00:19:3E:00:AA:5E 00:1D:60:3D:3A:FB 00:22:43:49:FB:AA\n"
exit $FAILCOUNT exit $FAILCOUNT