regex: avoid glibc deadlock during configure
[gnulib.git] / m4 / regex.m4
1 # serial 65
2
3 # Copyright (C) 1996-2001, 2003-2013 Free Software Foundation, Inc.
4 #
5 # This file is free software; the Free Software Foundation
6 # gives unlimited permission to copy and/or distribute it,
7 # with or without modifications, as long as this notice is preserved.
8
9 dnl Initially derived from code in GNU grep.
10 dnl Mostly written by Jim Meyering.
11
12 AC_PREREQ([2.50])
13
14 AC_DEFUN([gl_REGEX],
15 [
16   AC_ARG_WITH([included-regex],
17     [AS_HELP_STRING([--without-included-regex],
18                     [don't compile regex; this is the default on systems
19                      with recent-enough versions of the GNU C Library
20                      (use with caution on other systems).])])
21
22   case $with_included_regex in #(
23   yes|no) ac_use_included_regex=$with_included_regex
24         ;;
25   '')
26     # If the system regex support is good enough that it passes the
27     # following run test, then default to *not* using the included regex.c.
28     # If cross compiling, assume the test would fail and use the included
29     # regex.c.
30     AC_CHECK_DECLS_ONCE([alarm])
31     AC_CHECK_HEADERS_ONCE([malloc.h])
32     AC_CACHE_CHECK([for working re_compile_pattern],
33                    [gl_cv_func_re_compile_pattern_working],
34       [AC_RUN_IFELSE(
35         [AC_LANG_PROGRAM(
36           [[#include <regex.h>
37
38             #include <locale.h>
39             #include <limits.h>
40             #include <string.h>
41             #if HAVE_DECL_ALARM
42             # include <unistd.h>
43             # include <signal.h>
44             #endif
45             #if HAVE_MALLOC_H
46             # include <malloc.h>
47             #endif
48           ]],
49           [[int result = 0;
50             static struct re_pattern_buffer regex;
51             unsigned char folded_chars[UCHAR_MAX + 1];
52             int i;
53             const char *s;
54             struct re_registers regs;
55
56             /* Some builds of glibc go into an infinite loop on this
57                test.  Use alarm to force death, and mallopt to avoid
58                malloc recursion in diagnosing the corrupted heap. */
59 #if HAVE_DECL_ALARM
60             signal (SIGALRM, SIG_DFL);
61             alarm (2);
62 #endif
63 #ifdef M_CHECK_ACTION
64             mallopt(M_CHECK_ACTION, 2);
65 #endif
66
67             if (setlocale (LC_ALL, "en_US.UTF-8"))
68               {
69                 {
70                   /* http://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
71                      This test needs valgrind to catch the bug on Debian
72                      GNU/Linux 3.1 x86, but it might catch the bug better
73                      on other platforms and it shouldn't hurt to try the
74                      test here.  */
75                   static char const pat[] = "insert into";
76                   static char const data[] =
77                     "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
78                   re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
79                                  | RE_ICASE);
80                   memset (&regex, 0, sizeof regex);
81                   s = re_compile_pattern (pat, sizeof pat - 1, &regex);
82                   if (s)
83                     result |= 1;
84                   else if (re_search (&regex, data, sizeof data - 1,
85                                       0, sizeof data - 1, &regs)
86                            != -1)
87                     result |= 1;
88                 }
89
90                 {
91                   /* This test is from glibc bug 15078.
92                      The test case is from Andreas Schwab in
93                      <http://www.sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
94                      */
95                   static char const pat[] = "[^x]x";
96                   static char const data[] =
97                     /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
98                     "\xe1\x80\x80"
99                     "\xe1\x80\xbb"
100                     "\xe1\x80\xbd"
101                     "\xe1\x80\x94"
102                     "\xe1\x80\xba"
103                     "\xe1\x80\xaf"
104                     "\xe1\x80\x95"
105                     "\xe1\x80\xba"
106                     "x";
107                   re_set_syntax (0);
108                   memset (&regex, 0, sizeof regex);
109                   s = re_compile_pattern (pat, sizeof pat - 1, &regex);
110                   if (s)
111                     result |= 1;
112                   else
113                     {
114                       i = re_search (&regex, data, sizeof data - 1,
115                                      0, sizeof data - 1, 0);
116                       if (i != 0 && i != 21)
117                         result |= 1;
118                     }
119                 }
120
121                 if (! setlocale (LC_ALL, "C"))
122                   return 1;
123               }
124
125             /* This test is from glibc bug 3957, reported by Andrew Mackey.  */
126             re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
127             memset (&regex, 0, sizeof regex);
128             s = re_compile_pattern ("a[^x]b", 6, &regex);
129             if (s)
130               result |= 2;
131             /* This should fail, but succeeds for glibc-2.5.  */
132             else if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
133               result |= 2;
134
135             /* This regular expression is from Spencer ere test number 75
136                in grep-2.3.  */
137             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
138             memset (&regex, 0, sizeof regex);
139             for (i = 0; i <= UCHAR_MAX; i++)
140               folded_chars[i] = i;
141             regex.translate = folded_chars;
142             s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
143             /* This should fail with _Invalid character class name_ error.  */
144             if (!s)
145               result |= 4;
146
147             /* Ensure that [b-a] is diagnosed as invalid, when
148                using RE_NO_EMPTY_RANGES. */
149             re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
150             memset (&regex, 0, sizeof regex);
151             s = re_compile_pattern ("a[b-a]", 6, &regex);
152             if (s == 0)
153               result |= 8;
154
155             /* This should succeed, but does not for glibc-2.1.3.  */
156             memset (&regex, 0, sizeof regex);
157             s = re_compile_pattern ("{1", 2, &regex);
158             if (s)
159               result |= 8;
160
161             /* The following example is derived from a problem report
162                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
163             memset (&regex, 0, sizeof regex);
164             s = re_compile_pattern ("[an\371]*n", 7, &regex);
165             if (s)
166               result |= 8;
167             /* This should match, but does not for glibc-2.2.1.  */
168             else if (re_match (&regex, "an", 2, 0, &regs) != 2)
169               result |= 8;
170
171             memset (&regex, 0, sizeof regex);
172             s = re_compile_pattern ("x", 1, &regex);
173             if (s)
174               result |= 8;
175             /* glibc-2.2.93 does not work with a negative RANGE argument.  */
176             else if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
177               result |= 8;
178
179             /* The version of regex.c in older versions of gnulib
180                ignored RE_ICASE.  Detect that problem too.  */
181             re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
182             memset (&regex, 0, sizeof regex);
183             s = re_compile_pattern ("x", 1, &regex);
184             if (s)
185               result |= 16;
186             else if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
187               result |= 16;
188
189             /* Catch a bug reported by Vin Shelton in
190                http://lists.gnu.org/archive/html/bug-coreutils/2007-06/msg00089.html
191                */
192             re_set_syntax (RE_SYNTAX_POSIX_BASIC
193                            & ~RE_CONTEXT_INVALID_DUP
194                            & ~RE_NO_EMPTY_RANGES);
195             memset (&regex, 0, sizeof regex);
196             s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, &regex);
197             if (s)
198               result |= 32;
199
200             /* REG_STARTEND was added to glibc on 2004-01-15.
201                Reject older versions.  */
202             if (! REG_STARTEND)
203               result |= 64;
204
205 #if 0
206             /* It would be nice to reject hosts whose regoff_t values are too
207                narrow (including glibc on hosts with 64-bit ptrdiff_t and
208                32-bit int), but we should wait until glibc implements this
209                feature.  Otherwise, support for equivalence classes and
210                multibyte collation symbols would always be broken except
211                when compiling --without-included-regex.   */
212             if (sizeof (regoff_t) < sizeof (ptrdiff_t)
213                 || sizeof (regoff_t) < sizeof (ssize_t))
214               result |= 64;
215 #endif
216
217             return result;
218           ]])],
219        [gl_cv_func_re_compile_pattern_working=yes],
220        [gl_cv_func_re_compile_pattern_working=no],
221        dnl When crosscompiling, assume it is not working.
222        [gl_cv_func_re_compile_pattern_working=no])])
223     case $gl_cv_func_re_compile_pattern_working in #(
224     yes) ac_use_included_regex=no;; #(
225     no) ac_use_included_regex=yes;;
226     esac
227     ;;
228   *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
229     ;;
230   esac
231
232   if test $ac_use_included_regex = yes; then
233     AC_DEFINE([_REGEX_INCLUDE_LIMITS_H], [1],
234       [Define if you want <regex.h> to include <limits.h>, so that it
235        consistently overrides <limits.h>'s RE_DUP_MAX.])
236     AC_DEFINE([_REGEX_LARGE_OFFSETS], [1],
237       [Define if you want regoff_t to be at least as wide POSIX requires.])
238     AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
239       [Define to rpl_re_syntax_options if the replacement should be used.])
240     AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
241       [Define to rpl_re_set_syntax if the replacement should be used.])
242     AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
243       [Define to rpl_re_compile_pattern if the replacement should be used.])
244     AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
245       [Define to rpl_re_compile_fastmap if the replacement should be used.])
246     AC_DEFINE([re_search], [rpl_re_search],
247       [Define to rpl_re_search if the replacement should be used.])
248     AC_DEFINE([re_search_2], [rpl_re_search_2],
249       [Define to rpl_re_search_2 if the replacement should be used.])
250     AC_DEFINE([re_match], [rpl_re_match],
251       [Define to rpl_re_match if the replacement should be used.])
252     AC_DEFINE([re_match_2], [rpl_re_match_2],
253       [Define to rpl_re_match_2 if the replacement should be used.])
254     AC_DEFINE([re_set_registers], [rpl_re_set_registers],
255       [Define to rpl_re_set_registers if the replacement should be used.])
256     AC_DEFINE([re_comp], [rpl_re_comp],
257       [Define to rpl_re_comp if the replacement should be used.])
258     AC_DEFINE([re_exec], [rpl_re_exec],
259       [Define to rpl_re_exec if the replacement should be used.])
260     AC_DEFINE([regcomp], [rpl_regcomp],
261       [Define to rpl_regcomp if the replacement should be used.])
262     AC_DEFINE([regexec], [rpl_regexec],
263       [Define to rpl_regexec if the replacement should be used.])
264     AC_DEFINE([regerror], [rpl_regerror],
265       [Define to rpl_regerror if the replacement should be used.])
266     AC_DEFINE([regfree], [rpl_regfree],
267       [Define to rpl_regfree if the replacement should be used.])
268   fi
269 ])
270
271 # Prerequisites of lib/regex.c and lib/regex_internal.c.
272 AC_DEFUN([gl_PREREQ_REGEX],
273 [
274   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
275   AC_REQUIRE([AC_C_INLINE])
276   AC_REQUIRE([AC_C_RESTRICT])
277   AC_REQUIRE([AC_TYPE_MBSTATE_T])
278   AC_REQUIRE([gl_EEMALLOC])
279   AC_REQUIRE([gl_GLIBC21])
280   AC_CHECK_HEADERS([libintl.h])
281   AC_CHECK_FUNCS_ONCE([isblank iswctype])
282   AC_CHECK_DECLS([isblank], [], [], [[#include <ctype.h>]])
283 ])