Package: grep / 2.27-2

03-397262-dlopen-pcre.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=397262
http://savannah.gnu.org/patch/?7017

Patch by Reuben Thomas <rrt@sc3d.org>
Modified by An´┐Żbal Monsalve Salazar <anibal@debian.org>
Modified by Marco d'Itri <md@Linux.IT>

Index: grep/src/pcresearch.c
===================================================================
--- grep.orig/src/pcresearch.c
+++ grep/src/pcresearch.c
@@ -24,6 +24,9 @@
 
 #if HAVE_LIBPCRE
 # include <pcre.h>
+# ifdef HAVE_DYNAMIC_LIBPCRE
+#  include <dlfcn.h>
+# endif
 
 /* This must be at least 2; everything after that is for performance
    in pcre_exec.  */
@@ -87,6 +90,67 @@ jit_exec (char const *subject, int searc
 static int empty_match[2];
 #endif
 
+#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
+# define pcre_fullinfo dl_pcre_fullinfo
+# define pcre_jit_stack_alloc dl_pcre_jit_stack_alloc
+# define pcre_assign_jit_stack dl_pcre_assign_jit_stack
+
+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 (*pcre_fullinfo)(const pcre *, const pcre_extra *, int,
+                  void *);
+
+static pcre_jit_stack *(*pcre_jit_stack_alloc)(int, int);
+
+static void (*pcre_assign_jit_stack)(pcre_extra *,
+                  pcre_jit_callback, 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;
+  if (!(pcre_fullinfo = dlsym(library, "pcre_fullinfo")))
+    return 0;
+  if (!(pcre_jit_stack_alloc = dlsym(library, "pcre_jit_stack_alloc")))
+    return 0;
+  if (!(pcre_assign_jit_stack = dlsym(library, "pcre_assign_jit_stack")))
+    return 0;
+
+  return 1;
+}
+
+#else
+#define map_pcre() (1)
+#endif /* HAVE_DYNAMIC_LIBPCRE */
+
 void
 Pcompile (char const *pattern, size_t size)
 {
@@ -117,6 +181,10 @@ Pcompile (char const *pattern, size_t si
       flags |= PCRE_UTF8;
     }
 
+  if (!map_pcre ())
+    die (EXIT_TROUBLE, 0, "%s",
+      _("The -P option is not supported: libpcre.so.3 is not available"));
+
   /* FIXME: Remove this restriction.  */
   if (memchr (pattern, '\n', size))
     die (EXIT_TROUBLE, 0, _("the -P option only supports a single pattern"));