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.
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
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
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],
42 #if defined M_CHECK_ACTION || HAVE_DECL_ALARM
52 /* Exit with distinguishable exit code. */
53 static void sigabrt_no_core (int sig) { raise (SIGTERM); }
57 static struct re_pattern_buffer regex;
58 unsigned char folded_chars[UCHAR_MAX + 1];
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. */
67 signal (SIGALRM, SIG_DFL);
71 signal (SIGABRT, sigabrt_no_core);
72 mallopt (M_CHECK_ACTION, 2);
75 if (setlocale (LC_ALL, "en_US.UTF-8"))
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
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
88 memset (®ex, 0, sizeof regex);
89 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
94 if (re_search (®ex, data, sizeof data - 1,
95 0, sizeof data - 1, ®s)
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>.
107 static char const pat[] = "[^x]x";
108 static char const data[] =
109 /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
120 memset (®ex, 0, sizeof regex);
121 s = re_compile_pattern (pat, sizeof pat - 1, ®ex);
126 i = re_search (®ex, data, sizeof data - 1,
127 0, sizeof data - 1, 0);
128 if (i != 0 && i != 21)
134 if (! setlocale (LC_ALL, "C"))
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 (®ex, 0, sizeof regex);
141 s = re_compile_pattern ("a[^x]b", 6, ®ex);
146 /* This should fail, but succeeds for glibc-2.5. */
147 if (re_search (®ex, "a\nb", 3, 0, 3, ®s) != -1)
152 /* This regular expression is from Spencer ere test number 75
154 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
155 memset (®ex, 0, sizeof regex);
156 for (i = 0; i <= UCHAR_MAX; i++)
158 regex.translate = folded_chars;
159 s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, ®ex);
160 /* This should fail with _Invalid character class name_ error. */
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 (®ex, 0, sizeof regex);
171 s = re_compile_pattern ("a[b-a]", 6, ®ex);
178 /* This should succeed, but does not for glibc-2.1.3. */
179 memset (®ex, 0, sizeof regex);
180 s = re_compile_pattern ("{1", 2, ®ex);
186 /* The following example is derived from a problem report
187 against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
188 memset (®ex, 0, sizeof regex);
189 s = re_compile_pattern ("[an\371]*n", 7, ®ex);
194 /* This should match, but does not for glibc-2.2.1. */
195 if (re_match (®ex, "an", 2, 0, ®s) != 2)
205 memset (®ex, 0, sizeof regex);
206 s = re_compile_pattern ("x", 1, ®ex);
211 /* glibc-2.2.93 does not work with a negative RANGE argument. */
212 if (re_search (®ex, "wxy", 3, 2, -2, ®s) != 1)
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 (®ex, 0, sizeof regex);
226 s = re_compile_pattern ("x", 1, ®ex);
231 if (re_search (®ex, "WXY", 3, 0, 3, ®s) < 0)
241 /* Catch a bug reported by Vin Shelton in
242 https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
244 re_set_syntax (RE_SYNTAX_POSIX_BASIC
245 & ~RE_CONTEXT_INVALID_DUP
246 & ~RE_NO_EMPTY_RANGES);
247 memset (®ex, 0, sizeof regex);
248 s = re_compile_pattern ("[[:alnum:]_-]\\\\+\$", 16, ®ex);
254 /* REG_STARTEND was added to glibc on 2004-01-15.
255 Reject older versions. */
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
264 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
265 memset (®ex, 0, sizeof regex);
266 s = re_compile_pattern ("0|()0|\\\\1|0", 10, ®ex);
269 memset (®s, 0, sizeof regs);
270 i = re_search (®ex, "x", 1, 0, 1, ®s);
282 if (strcmp (s, "Invalid back reference"))
286 /* glibc bug 11053. */
287 re_set_syntax (RE_SYNTAX_POSIX_BASIC);
288 memset (®ex, 0, sizeof regex);
289 static char const pat_sub2[] = "\\\\(a*\\\\)*a*\\\\1";
290 s = re_compile_pattern (pat_sub2, sizeof pat_sub2 - 1, ®ex);
295 memset (®s, 0, sizeof regs);
296 static char const data[] = "a";
297 int datalen = sizeof data - 1;
298 i = re_search (®ex, data, datalen, 0, datalen, ®s);
301 else if (regs.num_regs < 2)
303 else if (! (regs.start[0] == 0 && regs.end[0] == 1))
305 else if (! (regs.start[1] == 0 && regs.end[1] == 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))
326 [gl_cv_func_re_compile_pattern_working=yes],
327 [gl_cv_func_re_compile_pattern_working=no],
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" ;;
336 case "$gl_cv_func_re_compile_pattern_working" in #(
337 *yes) ac_use_included_regex=no;; #(
338 *no) ac_use_included_regex=yes;;
341 *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
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.])
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>]])