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