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