diff options
-rw-r--r-- | NEWS | 6 | ||||
-rw-r--r-- | doc/grep.texi | 13 | ||||
-rw-r--r-- | src/main.c | 25 | ||||
-rwxr-xr-x | tests/r-dot | 7 |
4 files changed, 38 insertions, 13 deletions
@@ -35,8 +35,10 @@ GNU grep NEWS -*- outline -*- ** New features - If no file operand is given, grep -r now searches the working directory. - Formerly it ignored the -r and searched standard input nonrecursively. + If no file operand is given, and a command-line -r or equivalent + option is given, grep now searches the working directory. Formerly + grep ignored the -r and searched standard input nonrecursively. + An -r found in GREP_OPTIONS does not have this new effect. grep now supports color highlighting of matches on MS-Windows. diff --git a/doc/grep.texi b/doc/grep.texi index 5d779c11..78efb189 100644 --- a/doc/grep.texi +++ b/doc/grep.texi @@ -801,6 +801,13 @@ whitespace. A backslash escapes the next character, so it can be used to specify an option containing whitespace or a backslash. +The @code{GREP_OPTIONS} value does not affect whether @command{grep} +without file operands searches standard input or the working +directory; that is affected only by command-line options. For +example, the command @samp{grep PAT} searches standard input and the +command @samp{grep -r PAT} searches the working directory, regardless +of whether @code{GREP_OPTIONS} contains @option{-r}. + @item GREP_COLOR @vindex GREP_COLOR @r{environment variable} @cindex highlight markers @@ -1014,9 +1021,9 @@ instead of strict equality with@ 2. for lines containing a match to the given pattern. By default, @command{grep} prints the matching lines. A file named @file{-} stands for standard input. -If no input is specified, a recursive @command{grep} -searches the working directory @file{.}, and a -nonrecursive @command{grep} searches standard input. +If no input is specified, @command{grep} searches the working +directory @file{.} if given a command-line option specifying +recursion; otherwise, @command{grep} searches standard input. There are four major variants of @command{grep}, controlled by the following options. @@ -1522,8 +1522,8 @@ Context control:\n\ \n")); fputs (_(after_options), stdout); printf (_("\ -When FILE is -, read standard input. With no FILE, read '.' if recursive,\n\ -standard input if not. If fewer than two FILEs are given, assume -h.\n\ +When FILE is -, read standard input. With no FILE, read . if a command-line\n\ +-r is given, - otherwise. If fewer than two FILEs are given, assume -h.\n\ Exit status is 0 if any line is selected, 1 otherwise;\n\ if any error occurs and -q is not given, the exit status is 2.\n")); printf (_("\nReport bugs to: %s\n"), PACKAGE_BUGREPORT); @@ -1646,8 +1646,8 @@ prepend_args (char const *options, char *buf, char **argv) /* Prepend the whitespace-separated options in OPTIONS to the argument vector of a main program with argument count *PARGC and argument - vector *PARGV. */ -static void + vector *PARGV. Return the number of options prepended. */ +static int prepend_default_options (char const *options, int *pargc, char ***pargv) { if (options && *options) @@ -1663,7 +1663,10 @@ prepend_default_options (char const *options, int *pargc, char ***pargv) pp += prepend_args (options, buf, pp); while ((*pp++ = *argv++)) continue; + return prepended; } + + return 0; } /* Get the next non-digit option from ARGC and ARGV. @@ -1807,7 +1810,8 @@ main (int argc, char **argv) char *keys; size_t keycc, oldcc, keyalloc; int with_filenames; - int opt, cc, status; + int opt, cc, status, prepended; + int prev_optind, last_recursive; int default_context; FILE *fp; @@ -1843,10 +1847,12 @@ main (int argc, char **argv) exit_failure = EXIT_TROUBLE; atexit (close_stdout); - prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv); + last_recursive = 0; + prepended = prepend_default_options (getenv ("GREP_OPTIONS"), &argc, &argv); setmatcher (NULL); - while ((opt = get_nondigit_option (argc, argv, &default_context)) != -1) + while (prev_optind = optind, + (opt = get_nondigit_option (argc, argv, &default_context)) != -1) switch (opt) { case 'A': @@ -1936,6 +1942,8 @@ main (int argc, char **argv) case 'd': directories = XARGMATCH ("--directories", optarg, directories_args, directories_types); + if (directories == RECURSE_DIRECTORIES) + last_recursive = prev_optind; break; case 'e': @@ -2024,6 +2032,7 @@ main (int argc, char **argv) case 'R': case 'r': directories = RECURSE_DIRECTORIES; + last_recursive = prev_optind; break; case 's': @@ -2250,7 +2259,7 @@ main (int argc, char **argv) } while (++optind < argc); } - else if (directories == RECURSE_DIRECTORIES) + else if (directories == RECURSE_DIRECTORIES && prepended < last_recursive) { status = 1; if (stat (".", &stats_base.stat) == 0) diff --git a/tests/r-dot b/tests/r-dot index 73e593cf..a1d5c0a5 100755 --- a/tests/r-dot +++ b/tests/r-dot @@ -11,4 +11,11 @@ echo a:aaa > exp || framework_failure_ (cd dir && grep -r aaa) > out || fail=1 compare exp out || fail=1 +(cd dir && grep -r aaa < a) > out || fail=1 +compare exp out || fail=1 + +echo aaa > exp || framework_failure_ +(cd dir && GREP_OPTIONS=-r grep aaa < a) > out || fail=1 +compare exp out || fail=1 + Exit $fail |