I am attaching a cleaned up patch for possible submission upstream.

My changes:
- introduced a HAVE_DYNAMIC_LIBPCRE macro which can be used to cleanly
  enable or disable the feature and used #defines to reduce the scope of
  changes
- no reason to use library_mapped, the function pointers are natural
  flags
- the return value of dlopen must be checked!
- why define the path for libpcre? It should be up to ld.so to find it
- switched the return values of map_pcre to be more natural
- error() does not return
- Pexecute is guaranteed to be called after Pcompile, so if map_pcre()
  has failed the other function cannot be called

-- 
ciao,
Marco
--- a/configure
+++ b/configure
@@ -10623,10 +10623,7 @@ _ACEOF
 
 # support for pcre
 if test x"$testpcre" = x"yes"; then
-	if pcre-config --cflags >/dev/null 2>&1; then
-		CPPFLAGS="$CPPFLAGS `pcre-config --cflags`"
-		LIBS="$LIBS `pcre-config --libs`"
-	fi
+  CPPFLAGS="$CPPFLAGS -DHAVE_DYNAMIC_LIBPCRE"
 
 { echo "$as_me:$LINENO: checking for pcre_exec in -lpcre" >&5
 echo $ECHO_N "checking for pcre_exec in -lpcre... $ECHO_C" >&6; }
@@ -10685,8 +10682,8 @@ fi
 
 rm -f core conftest.err conftest.$ac_objext conftest_ipa8_conftest.oo \
       conftest$ac_exeext conftest.$ac_ext
-LIBS=$ac_check_lib_save_LIBS
 fi
+LIBS=$ac_check_lib_save_LIBS
 { echo "$as_me:$LINENO: result: $ac_cv_lib_pcre_pcre_exec" >&5
 echo "${ECHO_T}$ac_cv_lib_pcre_pcre_exec" >&6; }
 if test $ac_cv_lib_pcre_pcre_exec = yes; then
@@ -10694,7 +10691,7 @@ if test $ac_cv_lib_pcre_pcre_exec = yes;
 #define HAVE_LIBPCRE 1
 _ACEOF
 
-  LIBS="-lpcre $LIBS"
+  LIBS="-ldl $LIBS"
 
 fi
 
--- a/src/search.c
+++ b/src/search.c
@@ -46,6 +46,9 @@
 #include "error.h"
 #include "xalloc.h"
 #ifdef HAVE_LIBPCRE
+# ifdef HAVE_DYNAMIC_LIBPCRE
+#  include <dlfcn.h>
+# endif
 # include <pcre.h>
 #endif
 #ifdef HAVE_LANGINFO_CODESET
@@ -105,6 +108,50 @@ static struct patterns
 struct patterns *patterns;
 size_t pcount;
 
+#ifdef HAVE_DYNAMIC_LIBPCRE
+
+# define pcre_compile dl_pcre_compile
+# define pcre_study dl_pcre_study
+# define pcre_exec dl_pcre_exec
+# define pcre_maketables dl_pcre_maketables
+
+static pcre *(*pcre_compile)(const char *pattern, int options,
+			     const char **errptr, int *erroffset,
+			     const unsigned char *tableptr);
+static pcre_extra *(*pcre_study)(const pcre *code, int options,
+				 const char **errptr);
+static int (*pcre_exec)(const pcre *code, const pcre_extra *extra,
+			const char *subject, int length, int startoffset,
+			int options, int *ovector, int ovecsize);
+static const unsigned char *(*pcre_maketables)(void);
+
+static int
+map_pcre(void)
+{
+  void *library;
+
+  if (pcre_maketables)
+    return 1;
+
+  if (!(library = dlopen("libpcre.so.3", RTLD_NOW)))
+    return 0;
+
+  if (!(pcre_compile = dlsym(library, "pcre_compile")))
+    return 0;
+  if (!(pcre_study = dlsym(library, "pcre_study")))
+    return 0;
+  if (!(pcre_exec = dlsym(library, "pcre_exec")))
+    return 0;
+  if (!(pcre_maketables = dlsym(library, "pcre_maketables")))
+    return 0;
+
+  return 1;
+}
+
+#else
+#define map_pcre() (1)
+#endif /* HAVE_DYNAMIC_LIBPCRE */
+
 void
 dfaerror (char const *mesg)
 {
@@ -1141,6 +1188,9 @@ COMPILE_FCT(Pcompile)
   char const *p;
   char const *pnul;
 
+  if (!map_pcre ())
+    error (2, 0, _("The -P option is not supported: libpcre.so.3 is not available"));
+
   /* FIXME: Remove these restrictions.  */
   if (eolbyte != '\n')
     error (2, 0, _("The -P and -z options cannot be combined"));

Attachment: signature.asc
Description: Digital signature

Reply via email to