Jim Meyering wrote:
Technically, we could fix it by adding more ifdefs, but I think it
would not be worthwhile to accommodate this use case.

I looked into this and discovered that fixing this can actually simplify the source code by removing the need for the "We can't get here" glitch in pcresearch.c. Although the fix does add complexity elsewhere so that the total number of ifdefs and ifs remains the same, overall grep gets a bit simpler and cleaner with the fix so I took the liberty of installing it (attached).
>From f417ea8186c0ef0271ee5cf6943923bd60d06223 Mon Sep 17 00:00:00 2001
From: Paul Eggert <egg...@cs.ucla.edu>
Date: Sun, 20 Jan 2019 09:52:09 -0800
Subject: [PATCH] grep: simplify pcresearch.c ifdefs

This fixes a warning if PCRE is not used (Bug#34054).
* configure.ac (USE_PCRE): New conditional.
* src/Makefile.am (grep_SOURCES) [!USE_PCRE]: Omit pcresearch.c.
* src/grep.c (matchers) [!HAVE_LIBPCRE]: Omit perl matcher.
(setmatcher) [!HAVE_LIBPCRE]: If helpful, mention
--disable-perl-regexp in diagnostic.
* src/pcresearch.c: Simplify by assuming HAVE_LIBPCRE.
---
 configure.ac     |  1 +
 src/Makefile.am  |  4 +++-
 src/grep.c       |  7 +++++++
 src/pcresearch.c | 54 ++++++++++++++++++------------------------------
 4 files changed, 31 insertions(+), 35 deletions(-)

diff --git a/configure.ac b/configure.ac
index 4ce7216..1bbfc4f 100644
--- a/configure.ac
+++ b/configure.ac
@@ -206,6 +206,7 @@ if test "$ac_use_included_regex" = no; then
 fi
 
 gl_FUNC_PCRE
+AM_CONDITIONAL([USE_PCRE], [test $use_pcre = yes])
 
 case $host_os in
   mingw*) suffix=w32 ;;
diff --git a/src/Makefile.am b/src/Makefile.am
index 40e9a96..1cfca3f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -29,8 +29,10 @@ grep_SOURCES =					\
   grep.c					\
   kwsearch.c					\
   kwset.c					\
-  pcresearch.c					\
   searchutils.c
+if USE_PCRE
+grep_SOURCES += pcresearch.c
+endif
 
 noinst_HEADERS = grep.h kwset.h search.h system.h
 
diff --git a/src/grep.c b/src/grep.c
index 95fa809..e53b8cd 100644
--- a/src/grep.c
+++ b/src/grep.c
@@ -2016,7 +2016,9 @@ static struct
   { "awk", RE_SYNTAX_AWK, GEAcompile, EGexecute },
   { "gawk", RE_SYNTAX_GNU_AWK, GEAcompile, EGexecute },
   { "posixawk", RE_SYNTAX_POSIX_AWK, GEAcompile, EGexecute },
+#if HAVE_LIBPCRE
   { "perl", 0, Pcompile, Pexecute, },
+#endif
 };
 /* Keep these in sync with the 'matchers' table.  */
 enum { E_MATCHER_INDEX = 1, F_MATCHER_INDEX = 2, G_MATCHER_INDEX = 0 };
@@ -2035,6 +2037,11 @@ setmatcher (char const *m, int matcher)
         return i;
       }
 
+#if !HAVE_LIBPCRE
+  if (STREQ (m, "perl"))
+    die (EXIT_TROUBLE, 0,
+         _("Perl matching not supported in a --disable-perl-regexp build"));
+#endif
   die (EXIT_TROUBLE, 0, _("invalid matcher %s"), m);
 }
 
diff --git a/src/pcresearch.c b/src/pcresearch.c
index 64e189c..3e45c00 100644
--- a/src/pcresearch.c
+++ b/src/pcresearch.c
@@ -22,22 +22,21 @@
 #include "search.h"
 #include "die.h"
 
-#if HAVE_LIBPCRE
-# include <pcre.h>
+#include <pcre.h>
 
 /* This must be at least 2; everything after that is for performance
    in pcre_exec.  */
 enum { NSUB = 300 };
 
-# ifndef PCRE_EXTRA_MATCH_LIMIT_RECURSION
-#  define PCRE_EXTRA_MATCH_LIMIT_RECURSION 0
-# endif
-# ifndef PCRE_STUDY_JIT_COMPILE
-#  define PCRE_STUDY_JIT_COMPILE 0
-# endif
-# ifndef PCRE_STUDY_EXTRA_NEEDED
-#  define PCRE_STUDY_EXTRA_NEEDED 0
-# endif
+#ifndef PCRE_EXTRA_MATCH_LIMIT_RECURSION
+# define PCRE_EXTRA_MATCH_LIMIT_RECURSION 0
+#endif
+#ifndef PCRE_STUDY_JIT_COMPILE
+# define PCRE_STUDY_JIT_COMPILE 0
+#endif
+#ifndef PCRE_STUDY_EXTRA_NEEDED
+# define PCRE_STUDY_EXTRA_NEEDED 0
+#endif
 
 struct pcre_comp
 {
@@ -47,11 +46,11 @@ struct pcre_comp
   /* Additional information about the pattern.  */
   pcre_extra *extra;
 
-# if PCRE_STUDY_JIT_COMPILE
+#if PCRE_STUDY_JIT_COMPILE
   /* The JIT stack and its maximum size.  */
   pcre_jit_stack *jit_stack;
   int jit_stack_size;
-# endif
+#endif
 
   /* Table, indexed by ! (flag & PCRE_NOTBOL), of whether the empty
      string matches when that flag is used.  */
@@ -72,7 +71,7 @@ jit_exec (struct pcre_comp *pc, char const *subject, int search_bytes,
       int e = pcre_exec (pc->cre, pc->extra, subject, search_bytes,
                          search_offset, options, sub, NSUB);
 
-# if PCRE_STUDY_JIT_COMPILE
+#if PCRE_STUDY_JIT_COMPILE
       if (e == PCRE_ERROR_JIT_STACKLIMIT
           && 0 < pc->jit_stack_size && pc->jit_stack_size <= INT_MAX / 2)
         {
@@ -87,9 +86,9 @@ jit_exec (struct pcre_comp *pc, char const *subject, int search_bytes,
           pcre_assign_jit_stack (pc->extra, NULL, pc->jit_stack);
           continue;
         }
-# endif
+#endif
 
-# if PCRE_EXTRA_MATCH_LIMIT_RECURSION
+#if PCRE_EXTRA_MATCH_LIMIT_RECURSION
       if (e == PCRE_ERROR_RECURSIONLIMIT
           && (PCRE_STUDY_EXTRA_NEEDED || pc->extra)
           && pc->extra->match_limit_recursion <= ULONG_MAX / 2)
@@ -102,22 +101,15 @@ jit_exec (struct pcre_comp *pc, char const *subject, int search_bytes,
             }
           continue;
         }
-# endif
+#endif
 
       return e;
     }
 }
 
-#endif
-
 void *
 Pcompile (char *pattern, size_t size, reg_syntax_t ignored)
 {
-#if !HAVE_LIBPCRE
-  die (EXIT_TROUBLE, 0,
-       _("support for the -P option is not compiled into "
-         "this --disable-perl-regexp binary"));
-#else
   int e;
   char const *ep;
   static char const wprefix[] = "(?<!\\w)(?:";
@@ -184,14 +176,14 @@ Pcompile (char *pattern, size_t size, reg_syntax_t ignored)
   if (ep)
     die (EXIT_TROUBLE, 0, "%s", ep);
 
-# if PCRE_STUDY_JIT_COMPILE
+#if PCRE_STUDY_JIT_COMPILE
   if (pcre_fullinfo (pc->cre, pc->extra, PCRE_INFO_JIT, &e))
     die (EXIT_TROUBLE, 0, _("internal error (should never happen)"));
 
   /* The PCRE documentation says that a 32 KiB stack is the default.  */
   if (e)
     pc->jit_stack_size = 32 << 10;
-# endif
+#endif
 
   free (re);
 
@@ -202,17 +194,12 @@ Pcompile (char *pattern, size_t size, reg_syntax_t ignored)
                                      NSUB);
 
   return pc;
-#endif /* HAVE_LIBPCRE */
 }
 
 size_t
 Pexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
           char const *start_ptr)
 {
-#if !HAVE_LIBPCRE
-  /* We can't get here, because Pcompile would have been called earlier.  */
-  die (EXIT_TROUBLE, 0, _("internal error"));
-#else
   int sub[NSUB];
   char const *p = start_ptr ? start_ptr : buf;
   bool bol = p[-1] == eolbyte;
@@ -312,10 +299,10 @@ Pexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
         case PCRE_ERROR_NOMEMORY:
           die (EXIT_TROUBLE, 0, _("memory exhausted"));
 
-# if PCRE_STUDY_JIT_COMPILE
+#if PCRE_STUDY_JIT_COMPILE
         case PCRE_ERROR_JIT_STACKLIMIT:
           die (EXIT_TROUBLE, 0, _("exhausted PCRE JIT stack"));
-# endif
+#endif
 
         case PCRE_ERROR_MATCHLIMIT:
           die (EXIT_TROUBLE, 0, _("exceeded PCRE's backtracking limit"));
@@ -349,5 +336,4 @@ Pexecute (void *vcp, char const *buf, size_t size, size_t *match_size,
       *match_size = end - beg;
       return beg - buf;
     }
-#endif
 }
-- 
2.17.1

Reply via email to