On 6/3/22 20:18, Bruno Haible wrote:
This warning punishes a traditional habit, namely to backslash-escape
a leading ASCII '-' character, so as to avoid it from being interpreted
as an option.

That's the first I've heard of that habit. Even 7th Edition Unix supported 'grep -e PAT FILE', but I guess some nonstandard grep implementations removed -e. These days, although I would think 'grep -- PAT FILE' would work everywhere, there may be scripts that still use that old habit.

If we want to support "grep '\-PAT' FILE", we can do so in grep rather than in dfa.c; it might not be wise to treat \- as - everywhere, as that would preclude future extensions such as A\-B meaning r.e. subtraction. We could add something like the attached to GNU grep, if you and Jim think it's a good idea (I hope Jim doesn't get tired of these "just one more thing" changes before a release...).
From fe5ed6852a51d6b6ea9c407f10019ecd427d4eb0 Mon Sep 17 00:00:00 2001
From: Paul Eggert <[email protected]>
Date: Sat, 4 Jun 2022 15:41:38 -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/dfasearch.c (GEAcompile): Skip past leading backslash of a
pattern that begins with "\-".  Inspired by a remark by Bruno Haible:
https://lists.gnu.org/r/bug-gnulib/2022-06/msg00022.html
---
 src/dfasearch.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/dfasearch.c b/src/dfasearch.c
index 8d832f0..730d0e4 100644
--- a/src/dfasearch.c
+++ b/src/dfasearch.c
@@ -203,6 +203,13 @@ GEAcompile (char *pattern, idx_t size, reg_syntax_t syntax_bits,
   dfasyntax (dc->dfa, &localeinfo, syntax_bits, dfaopts);
   bool bs_safe = !localeinfo.multibyte | localeinfo.using_utf8;
 
+  /* If a pattern starts with "\-" omit the backslash, to suppress a
+     stray-backslash warning if a script uses the non-POSIX
+     "grep '\-x'" to avoid treating "-x" as an option.  */
+  bool skip_bs = pattern[0] == '\\' && pattern[1] == '-';
+  pattern += skip_bs;
+  size -= skip_bs;
+
   /* For GNU regex, pass the patterns separately to detect errors like
      "[\nallo\n]\n", where the patterns are "[", "allo" and "]", and
      this should be a syntax error.  The same for backref, where the
-- 
2.36.1

Reply via email to