No current caller except grep needs this, but consistency seems like a
good idea.

Also change the xregcomp error message to be a bit more human-readable,
rather than mention an implementation detail.
---
 lib/xwrap.c       | 13 ++++++++++---
 toys/posix/grep.c |  5 ++---
 2 files changed, 12 insertions(+), 6 deletions(-)
From 42f97b7b41d1ffcc661e0306b91fc6453e69a1c9 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Tue, 30 Jul 2019 09:56:28 -0700
Subject: [PATCH] Move the empty regex workaround into xregcomp.

No current caller except grep needs this, but consistency seems like a
good idea.

Also change the xregcomp error message to be a bit more human-readable,
rather than mention an implementation detail.
---
 lib/xwrap.c       | 13 ++++++++++---
 toys/posix/grep.c |  5 ++---
 2 files changed, 12 insertions(+), 6 deletions(-)

diff --git a/lib/xwrap.c b/lib/xwrap.c
index 817037db..c3483e6a 100644
--- a/lib/xwrap.c
+++ b/lib/xwrap.c
@@ -922,11 +922,18 @@ long long xparsemillitime(char *arg)
 // Compile a regular expression into a regex_t
 void xregcomp(regex_t *preg, char *regex, int cflags)
 {
-  int rc = regcomp(preg, regex, cflags);
+  int rc;
+
+  // BSD regex implementations don't support the empty regex (which isn't
+  // allowed in the POSIX grammar), but glibc does. Fake it for BSD.
+  if (!*regex) {
+    regex = "()";
+    cflags |= REG_EXTENDED;
+  }
 
-  if (rc) {
+  if ((rc = regcomp(preg, regex, cflags))) {
     regerror(rc, preg, libbuf, sizeof(libbuf));
-    error_exit("xregcomp: %s", libbuf);
+    error_exit("bad regex: %s", libbuf);
   }
 }
 
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index f2ab2a0d..1fa1a7c4 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -404,9 +404,8 @@ static void parse_regex(void)
 
       if (FLAG(o) && !*al->arg) continue;
       dlist_add_nomalloc(&TT.reg, (void *)(shoe = xmalloc(sizeof(struct reg))));
-      // BSD regcomp doesn't support empty regex, so we fake that.
-      xregcomp(&shoe->r, *al->arg ? al->arg : "()",
-               (REG_EXTENDED*(!!FLAG(E)|!*al->arg))|(REG_ICASE*!!FLAG(i)));
+      xregcomp(&shoe->r, al->arg,
+               (REG_EXTENDED*!!FLAG(E))|(REG_ICASE*!!FLAG(i)));
     }
     dlist_terminate(TT.reg);
   }
-- 
2.22.0.709.g102302147b-goog

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to