summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaolo Bonzini <bonzini@gnu.org>2009-12-14 12:37:53 +0100
committerPaolo Bonzini <bonzini@gnu.org>2009-12-22 12:26:42 +0100
commit62b155de28da2bf54ee63eb56d8cde42f21f179d (patch)
tree4cedac6ab3cab48da2d98661438e5ac6d71f84a4
parentab21b6037ec92eea32c3cf57220f3b2e567d784d (diff)
downloadgrep-62b155de28da2bf54ee63eb56d8cde42f21f179d.tar.gz
Make 'grep -1 -2' and 'grep -1v2' equivalent to grep -2
Fixes bug 12128. * src/grep.c (get_nondigit_option): Reset the buffer every time a non-digit option is found or a new argument is started.
-rw-r--r--NEWS4
-rw-r--r--src/grep.c26
-rwxr-xr-xtests/yesno.sh3
3 files changed, 26 insertions, 7 deletions
diff --git a/NEWS b/NEWS
index 70a6cd4e..2a67a320 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,10 @@ GNU grep NEWS -*- outline -*-
* Noteworthy changes in release ?.? (????-??-??) [?]
+ - A command line like -1 -2 or -1 -v -2 result in two lines of
+ context (the last value that appears on the command line) instead
+ twelve (the concatenation of all the values). This is consistent
+ with the behavior of options -A/-B/-C.
* Noteworthy changes in release 2.5.4 (2009-02-10) [stable]
diff --git a/src/grep.c b/src/grep.c
index c16c0fb3..4bf7df57 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1635,19 +1635,27 @@ prepend_default_options (char const *options, int *pargc, char ***pargv)
static int
get_nondigit_option (int argc, char *const *argv, int *default_context)
{
- int opt;
+ static int prev_digit_optind = -1;
+ int opt, this_digit_optind, was_digit;
char buf[sizeof (uintmax_t) * CHAR_BIT + 4];
char *p = buf;
- /* Set buf[0] to anything but '0', for the leading-zero test below. */
- buf[0] = '\0';
-
+ was_digit = 0;
+ this_digit_optind = optind;
while (opt = getopt_long (argc, argv, short_options, long_options, NULL),
'0' <= opt && opt <= '9')
{
- /* Suppress trivial leading zeros, to avoid incorrect
- diagnostic on strings like 00000000000. */
- p -= buf[0] == '0';
+ if (prev_digit_optind != this_digit_optind || !was_digit)
+ {
+ /* Reset to start another context length argument. */
+ p = buf;
+ }
+ else
+ {
+ /* Suppress trivial leading zeros, to avoid incorrect
+ diagnostic on strings like 00000000000. */
+ p -= buf[0] == '0';
+ }
if (p == buf + sizeof buf - 4)
{
@@ -1659,6 +1667,10 @@ get_nondigit_option (int argc, char *const *argv, int *default_context)
break;
}
*p++ = opt;
+
+ was_digit = 1;
+ prev_digit_optind = this_digit_optind;
+ this_digit_optind = optind;
}
if (p != buf)
{
diff --git a/tests/yesno.sh b/tests/yesno.sh
index 4852a33f..2e21b5f2 100755
--- a/tests/yesno.sh
+++ b/tests/yesno.sh
@@ -64,6 +64,7 @@ set x \
'-o' "$c$d$e$h$i$m$n$z0" \
'-C,1' "$rB$C$D$E$rF$rG$H$I$rJ$s$rL$M$N$z0" \
'-C,1,-o' "$c$d$e$h$i$s$m$n$z0" \
+ '-C,4,-1' "$rB$C$D$E$rF$rG$H$I$rJ$s$rL$M$N$z0" \
'-m,4' "$C$D$E$H$z0$XI$XJ$XK$XL$XM$XN" \
'-m,4,-o' "$c$d$e$h$z0$XI$XJ$XK$XL$XM$XN" \
'-m,4,-C,1' "$rB$C$D$E$rF$rG$H$z0$XI$XJ$XK$XL$XM$XN" \
@@ -79,6 +80,8 @@ set x \
'-v,-o' "$z0" \
'-v,-C,1' "$A$B$rC$s$rE$F$G$rH$rI$J$K$L$rM$z0" \
'-v,-C,1,-o' "$rc$s$re$rh$ri$rm$z0" \
+ '-4,-1' "$rB$C$D$E$rF$rG$H$I$rJ$s$rL$M$N$z0" \
+ '-4,-v,-1' "$A$B$rC$s$rE$F$G$rH$rI$J$K$L$rM$z0" \
'-m,1,-v' "$A$z0$XB$XC$XD$XE$XF$XG$XH$XI$XJ$XK$XL$XM$XN" \
'-m,1,-v,-o' "$z0$XB$XC$XD$XE$XF$XG$XH$XI$XJ$XK$XL$XM$XN" \
'-m,1,-v,-C,1' "$A$z0$XB$XC$XD$XE$XF$XG$XH$XI$XJ$XK$XL$XM$XN" \