On 6/4/22 18:45, Bruno Haible wrote:
And maybe some people find it OK to get the "stray \ before -" warning for
   grep -e '\-x' FILE
and to inhibit it only when the option -e or the marker '--' is not used?

Yes, thanks, this narrower exception sounds better. Revised grep patch attached.

[cc'ing this to grep-devel as this is no longer a Gnulib patch (should have done that in my previous email...).]
From 019745515bbc6c515dee600f24e80c4003f76b79 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sun, 5 Jun 2022 10:42:22 -0700
Subject: [PATCH] =?UTF-8?q?grep:=20don=E2=80=99t=20diagnose=20"grep=20'\-c?=
 =?UTF-8?q?'"?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

* src/grep.c (main): Skip past leading backslash of a pattern that
begins with "\-".  Inspired by a remark by Bruno Haible in:
https://lists.gnu.org/r/bug-gnulib/2022-06/msg00022.html
---
 src/grep.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

diff --git a/src/grep.c b/src/grep.c
index 59d3431..6b976b4 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2848,8 +2848,16 @@ main (int argc, char **argv)
     }
   else if (optind < argc)
     {
+      /* If a command-line regular expression operand starts with '\-',
+         skip the '\'.  This suppress a stray-backslash warning if a
+         script uses the non-POSIX "grep '\-x'" to avoid treating
+         "-x" as an option.  */
+      char const *pat = argv[optind++];
+      bool skip_bs = (matcher != F_MATCHER_INDEX
+                      && pat[0] == '\\' && pat[1] == '-');
+
       /* Make a copy so that it can be reallocated or freed later.  */
-      pattern_array = keys = xstrdup (argv[optind++]);
+      pattern_array = keys = xstrdup (pat + skip_bs);
       idx_t patlen = strlen (keys);
       keys[patlen] = '\n';
       keycc = update_patterns (keys, 0, patlen + 1, "");
-- 
2.36.1

Reply via email to