Avoid some possibly wrong configure test results.
[gnulib.git] / m4 / regex.m4
blobf0101fe67c63e5c53493c8b712eee085e3a3675b
1 # regex.m4
2 # serial 75
3 dnl Copyright (C) 1996-2001, 2003-2024 Free Software Foundation, Inc.
4 dnl This file is free software; the Free Software Foundation
5 dnl gives unlimited permission to copy and/or distribute it,
6 dnl with or without modifications, as long as this notice is preserved.
8 dnl Initially derived from code in GNU grep.
9 dnl Mostly written by Jim Meyering.
11 AC_PREREQ([2.50])
13 AC_DEFUN([gl_REGEX],
15   AC_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
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).])])
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>
38             #include <locale.h>
39             #include <limits.h>
40             #include <string.h>
42             #if defined M_CHECK_ACTION || HAVE_DECL_ALARM
43             # include <signal.h>
44             # include <unistd.h>
45             #endif
47             #if HAVE_MALLOC_H
48             # include <malloc.h>
49             #endif
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;
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
75             if (setlocale (LC_ALL, "en_US.UTF-8"))
76               {
77                 {
78                   /* https://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
93                     {
94                       if (re_search (&regex, data, sizeof data - 1,
95                                      0, sizeof data - 1, &regs)
96                           != -1)
97                         result |= 1;
98                       regfree (&regex);
99                     }
100                 }
102                 {
103                   /* This test is from glibc bug 15078.
104                      The test case is from Andreas Schwab in
105                      <https://sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
106                      */
107                   static char const pat[] = "[^x]x";
108                   static char const data[] =
109                     /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
110                     "\xe1\x80\x80"
111                     "\xe1\x80\xbb"
112                     "\xe1\x80\xbd"
113                     "\xe1\x80\x94"
114                     "\xe1\x80\xba"
115                     "\xe1\x80\xaf"
116                     "\xe1\x80\x95"
117                     "\xe1\x80\xba"
118                     "x";
119                   re_set_syntax (0);
120                   memset (&regex, 0, sizeof regex);
121                   s = re_compile_pattern (pat, sizeof pat - 1, &regex);
122                   if (s)
123                     result |= 1;
124                   else
125                     {
126                       i = re_search (&regex, data, sizeof data - 1,
127                                      0, sizeof data - 1, 0);
128                       if (i != 0 && i != 21)
129                         result |= 1;
130                       regfree (&regex);
131                     }
132                 }
134                 if (! setlocale (LC_ALL, "C"))
135                   return 1;
136               }
138             /* This test is from glibc bug 3957, reported by Andrew Mackey.  */
139             re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
140             memset (&regex, 0, sizeof regex);
141             s = re_compile_pattern ("a[^x]b", 6, &regex);
142             if (s)
143               result |= 2;
144             else
145               {
146                 /* This should fail, but succeeds for glibc-2.5.  */
147                 if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
148                   result |= 2;
149                 regfree (&regex);
150               }
152             /* This regular expression is from Spencer ere test number 75
153                in grep-2.3.  */
154             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
155             memset (&regex, 0, sizeof regex);
156             for (i = 0; i <= UCHAR_MAX; i++)
157               folded_chars[i] = i;
158             regex.translate = folded_chars;
159             s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
160             /* This should fail with _Invalid character class name_ error.  */
161             if (!s)
162               {
163                 result |= 4;
164                 regfree (&regex);
165               }
167             /* Ensure that [b-a] is diagnosed as invalid, when
168                using RE_NO_EMPTY_RANGES. */
169             re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
170             memset (&regex, 0, sizeof regex);
171             s = re_compile_pattern ("a[b-a]", 6, &regex);
172             if (s == 0)
173               {
174                 result |= 8;
175                 regfree (&regex);
176               }
178             /* This should succeed, but does not for glibc-2.1.3.  */
179             memset (&regex, 0, sizeof regex);
180             s = re_compile_pattern ("{1", 2, &regex);
181             if (s)
182               result |= 8;
183             else
184               regfree (&regex);
186             /* The following example is derived from a problem report
187                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
188             memset (&regex, 0, sizeof regex);
189             s = re_compile_pattern ("[an\371]*n", 7, &regex);
190             if (s)
191               result |= 8;
192             else
193               {
194                 /* This should match, but does not for glibc-2.2.1.  */
195                 if (re_match (&regex, "an", 2, 0, &regs) != 2)
196                   result |= 8;
197                 else
198                   {
199                     free (regs.start);
200                     free (regs.end);
201                   }
202                 regfree (&regex);
203               }
205             memset (&regex, 0, sizeof regex);
206             s = re_compile_pattern ("x", 1, &regex);
207             if (s)
208               result |= 8;
209             else
210               {
211                 /* glibc-2.2.93 does not work with a negative RANGE argument.  */
212                 if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
213                   result |= 8;
214                 else
215                   {
216                     free (regs.start);
217                     free (regs.end);
218                   }
219                 regfree (&regex);
220               }
222             /* The version of regex.c in older versions of gnulib
223                ignored RE_ICASE.  Detect that problem too.  */
224             re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
225             memset (&regex, 0, sizeof regex);
226             s = re_compile_pattern ("x", 1, &regex);
227             if (s)
228               result |= 16;
229             else
230               {
231                 if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
232                   result |= 16;
233                 else
234                   {
235                     free (regs.start);
236                     free (regs.end);
237                   }
238                 regfree (&regex);
239               }
241             /* Catch a bug reported by Vin Shelton in
242                https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
243                */
244             re_set_syntax (RE_SYNTAX_POSIX_BASIC
245                            & ~RE_CONTEXT_INVALID_DUP
246                            & ~RE_NO_EMPTY_RANGES);
247             memset (&regex, 0, sizeof regex);
248             s = re_compile_pattern ("[[:alnum:]_-]\\\\+\$", 16, &regex);
249             if (s)
250               result |= 32;
251             else
252               regfree (&regex);
254             /* REG_STARTEND was added to glibc on 2004-01-15.
255                Reject older versions.  */
256             if (! REG_STARTEND)
257               result |= 64;
259             /* Matching with the compiled form of this regexp would provoke
260                an assertion failure prior to glibc-2.28:
261                  regexec.c:1375: pop_fail_stack: Assertion 'num >= 0' failed
262                With glibc-2.28, compilation fails and reports the invalid
263                back reference.  */
264             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
265             memset (&regex, 0, sizeof regex);
266             s = re_compile_pattern ("0|()0|\\\\1|0", 10, &regex);
267             if (!s)
268               {
269                 memset (&regs, 0, sizeof regs);
270                 i = re_search (&regex, "x", 1, 0, 1, &regs);
271                 if (i != -1)
272                   result |= 64;
273                 if (0 <= i)
274                   {
275                     free (regs.start);
276                     free (regs.end);
277                   }
278                 regfree (&regex);
279               }
280             else
281               {
282                 if (strcmp (s, "Invalid back reference"))
283                   result |= 64;
284               }
286             /* glibc bug 11053.  */
287             re_set_syntax (RE_SYNTAX_POSIX_BASIC);
288             memset (&regex, 0, sizeof regex);
289             static char const pat_sub2[] = "\\\\(a*\\\\)*a*\\\\1";
290             s = re_compile_pattern (pat_sub2, sizeof pat_sub2 - 1, &regex);
291             if (s)
292               result |= 64;
293             else
294               {
295                 memset (&regs, 0, sizeof regs);
296                 static char const data[] = "a";
297                 int datalen = sizeof data - 1;
298                 i = re_search (&regex, data, datalen, 0, datalen, &regs);
299                 if (i != 0)
300                   result |= 64;
301                 else if (regs.num_regs < 2)
302                   result |= 64;
303                 else if (! (regs.start[0] == 0 && regs.end[0] == 1))
304                   result |= 64;
305                 else if (! (regs.start[1] == 0 && regs.end[1] == 0))
306                   result |= 64;
307                 regfree (&regex);
308                 free (regs.start);
309                 free (regs.end);
310               }
312 #if 0
313             /* It would be nice to reject hosts whose regoff_t values are too
314                narrow (including glibc on hosts with 64-bit ptrdiff_t and
315                32-bit int), but we should wait until glibc implements this
316                feature.  Otherwise, support for equivalence classes and
317                multibyte collation symbols would always be broken except
318                when compiling --without-included-regex.   */
319             if (sizeof (regoff_t) < sizeof (ptrdiff_t)
320                 || sizeof (regoff_t) < sizeof (ssize_t))
321               result |= 64;
322 #endif
324             return result;
325           ]])],
326         [gl_cv_func_re_compile_pattern_working=yes],
327         [gl_cv_func_re_compile_pattern_working=no],
328         [case "$host_os" in
329                               # Guess no on native Windows.
330            mingw* | windows*) gl_cv_func_re_compile_pattern_working="guessing no" ;;
331                               # Otherwise obey --enable-cross-guesses.
332            *)                 gl_cv_func_re_compile_pattern_working="$gl_cross_guess_normal" ;;
333          esac
334         ])
335       ])
336     case "$gl_cv_func_re_compile_pattern_working" in #(
337       *yes) ac_use_included_regex=no;; #(
338       *no) ac_use_included_regex=yes;;
339     esac
340     ;;
341   *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
342     ;;
343   esac
345   if test $ac_use_included_regex = yes; then
346     AC_DEFINE([_REGEX_INCLUDE_LIMITS_H], [1],
347       [Define if you want <regex.h> to include <limits.h>, so that it
348        consistently overrides <limits.h>'s RE_DUP_MAX.])
349     AC_DEFINE([_REGEX_LARGE_OFFSETS], [1],
350       [Define if you want regoff_t to be at least as wide POSIX requires.])
351     AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
352       [Define to rpl_re_syntax_options if the replacement should be used.])
353     AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
354       [Define to rpl_re_set_syntax if the replacement should be used.])
355     AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
356       [Define to rpl_re_compile_pattern if the replacement should be used.])
357     AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
358       [Define to rpl_re_compile_fastmap if the replacement should be used.])
359     AC_DEFINE([re_search], [rpl_re_search],
360       [Define to rpl_re_search if the replacement should be used.])
361     AC_DEFINE([re_search_2], [rpl_re_search_2],
362       [Define to rpl_re_search_2 if the replacement should be used.])
363     AC_DEFINE([re_match], [rpl_re_match],
364       [Define to rpl_re_match if the replacement should be used.])
365     AC_DEFINE([re_match_2], [rpl_re_match_2],
366       [Define to rpl_re_match_2 if the replacement should be used.])
367     AC_DEFINE([re_set_registers], [rpl_re_set_registers],
368       [Define to rpl_re_set_registers if the replacement should be used.])
369     AC_DEFINE([re_comp], [rpl_re_comp],
370       [Define to rpl_re_comp if the replacement should be used.])
371     AC_DEFINE([re_exec], [rpl_re_exec],
372       [Define to rpl_re_exec if the replacement should be used.])
373     AC_DEFINE([regcomp], [rpl_regcomp],
374       [Define to rpl_regcomp if the replacement should be used.])
375     AC_DEFINE([regexec], [rpl_regexec],
376       [Define to rpl_regexec if the replacement should be used.])
377     AC_DEFINE([regerror], [rpl_regerror],
378       [Define to rpl_regerror if the replacement should be used.])
379     AC_DEFINE([regfree], [rpl_regfree],
380       [Define to rpl_regfree if the replacement should be used.])
381   fi
384 # Prerequisites of lib/regex.c and lib/regex_internal.c.
385 AC_DEFUN([gl_PREREQ_REGEX],
387   AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
388   AC_REQUIRE([AC_C_INLINE])
389   AC_REQUIRE([AC_C_RESTRICT])
390   AC_REQUIRE([AC_TYPE_MBSTATE_T])
391   AC_REQUIRE([gl_EEMALLOC])
392   AC_CHECK_HEADERS([libintl.h])
393   AC_CHECK_FUNCS_ONCE([isblank iswctype])
394   AC_CHECK_DECLS([isblank], [], [], [[#include <ctype.h>]])