grep: don't bail out on first mismatch if '-w' option is set
authorBartosz Golaszewski <bartekgola@gmail.com>
Wed, 15 May 2013 01:53:26 +0000 (03:53 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Wed, 15 May 2013 07:20:40 +0000 (09:20 +0200)
Signed-off-by: Bartosz Golaszewski <bartekgola@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
findutils/grep.c
testsuite/grep.tests

index 70f3516a53eef31b232cddf067a28b64e247d645..b808ad92b2c50545a8e9b2893b74d02a390bfe67 100644 (file)
@@ -373,6 +373,8 @@ static int grep_file(FILE *file)
  opt_f_not_found: ;
                                }
                        } else {
+                               char *match_at;
+
                                if (!(gl->flg_mem_alocated_compiled & COMPILED)) {
                                        gl->flg_mem_alocated_compiled |= COMPILED;
 #if !ENABLE_EXTRA_COMPAT
@@ -388,32 +390,36 @@ static int grep_file(FILE *file)
                                gl->matched_range.rm_so = 0;
                                gl->matched_range.rm_eo = 0;
 #endif
+                               match_at = line;
+ opt_w_again:
                                if (
 #if !ENABLE_EXTRA_COMPAT
-                                       regexec(&gl->compiled_regex, line, 1, &gl->matched_range, 0) == 0
+                                       regexec(&gl->compiled_regex, match_at, 1, &gl->matched_range, 0) == 0
 #else
-                                       re_search(&gl->compiled_regex, line, line_len,
+                                       re_search(&gl->compiled_regex, match_at, line_len,
                                                        /*start:*/ 0, /*range:*/ line_len,
                                                        &gl->matched_range) >= 0
 #endif
                                ) {
                                        if (option_mask32 & OPT_x) {
                                                found = (gl->matched_range.rm_so == 0
-                                                        && line[gl->matched_range.rm_eo] == '\0');
+                                                        && match_at[gl->matched_range.rm_eo] == '\0');
                                        } else
                                        if (!(option_mask32 & OPT_w)) {
                                                found = 1;
                                        } else {
                                                char c = ' ';
                                                if (gl->matched_range.rm_so)
-                                                       c = line[gl->matched_range.rm_so - 1];
+                                                       c = match_at[gl->matched_range.rm_so - 1];
                                                if (!isalnum(c) && c != '_') {
-                                                       c = line[gl->matched_range.rm_eo];
-                                                       if (!c || (!isalnum(c) && c != '_'))
+                                                       c = match_at[gl->matched_range.rm_eo];
+                                                       if (!c || (!isalnum(c) && c != '_')) {
                                                                found = 1;
+                                                       } else {
+                                                               match_at += gl->matched_range.rm_eo;
+                                                               goto opt_w_again;
+                                                       }
                                                }
-//BUG: "echo foop foo | grep -w foo" should match, but doesn't:
-//we bail out on first "mismatch" because it's not a word.
                                        }
                                }
                        }
index 4781f228472e6a68ffee772ce8e2ab3cd0ac1d01..5696fa7e1c91371aae3a2fc06eeb8c7a69f82c44 100755 (executable)
@@ -127,6 +127,12 @@ testing "grep -Fw doesn't stop on 1st mismatch" \
        "foop foo\n" \
        ""
 
+testing "grep -w doesn't stop on 1st mismatch" \
+       "grep -w foo input" \
+       "foop foo\n" \
+       "foop foo\n" \
+       ""
+
 # testing "test name" "commands" "expected result" "file input" "stdin"
 #   file input will be file called "input"
 #   test can create a file "actual" instead of writing to stdout