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