bug-grep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] Make 'grep -1 -2' and 'grep -1v2' equivalent to grep -2


From: Paolo Bonzini
Subject: [PATCH] Make 'grep -1 -2' and 'grep -1v2' equivalent to grep -2
Date: Tue, 22 Dec 2009 09:59:26 +0100

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.
---
 src/grep.c     |   26 +++++++++++++++++++-------
 tests/yesno.sh |    3 +++
 2 files changed, 22 insertions(+), 7 deletions(-)

diff --git a/src/grep.c b/src/grep.c
index 6539d4c..eabd304 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -1637,19 +1637,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)
        {
@@ -1661,6 +1669,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 4852a33..2e21b5f 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" \
-- 
1.6.5.2





reply via email to

[Prev in Thread] Current Thread [Next in Thread]