Debian Codegraph @

m4/regex.m4

raw
1 # regex.m4
2 # serial 81
3 dnl Copyright (C) 1996-2001, 2003-2026 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.
7 dnl This file is offered as-is, without any warranty.
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_REQUIRE([AC_CANONICAL_HOST]) dnl for cross-compiles
17 AC_ARG_WITH([included-regex],
18 [AS_HELP_STRING([[--without-included-regex]],
19 [don't compile regex; this is the default on systems
20 with recent-enough versions of the GNU C Library
21 (use with caution on other systems).])])
22
23 case $with_included_regex in #(
24 yes|no) ac_use_included_regex=$with_included_regex
25 ;;
26 '')
27 # If the system regex support is good enough that it passes the
28 # following run test, then default to *not* using the included regex.c.
29 # If cross compiling, assume the test would fail and use the included
30 # regex.c.
31 AC_CHECK_DECLS_ONCE([alarm])
32 AC_CHECK_HEADERS_ONCE([malloc.h])
33 AC_CACHE_CHECK([for working re_compile_pattern],
34 [gl_cv_func_re_compile_pattern_working],
35 [AC_RUN_IFELSE(
36 [AC_LANG_PROGRAM(
37 [[#include <regex.h>
38
39 #include <locale.h>
40 #include <limits.h>
41 #include <string.h>
42
43 #if HAVE_MALLOC_H
44 # include <malloc.h> /* defines M_CHECK_ACTION on glibc */
45 #endif
46
47 #if defined __HAIKU__ || defined M_CHECK_ACTION || HAVE_DECL_ALARM
48 # include <signal.h>
49 # include <unistd.h>
50 #endif
51
52 #if defined __HAIKU__ || defined M_CHECK_ACTION
53 /* Exit with distinguishable exit code. */
54 static void sigabrt_no_core (int sig) { raise (SIGTERM); }
55 #endif
56
57 /* There is no need to check whether RE_SYNTAX_EMACS is
58 (RE_CHAR_CLASSES | RE_INTERVALS), corresponding to
59 Emacs 21 (2001) and later, because Gnulib's lib/regex.h
60 is always used and has this value. */
61 ]],
62 [[int result = 0;
63 static struct re_pattern_buffer regex;
64 unsigned char folded_chars[UCHAR_MAX + 1];
65 int i;
66 const char *s;
67 struct re_registers regs;
68
69 /* Some builds of glibc go into an infinite loop on this
70 test. Use alarm to force death, and mallopt to avoid
71 malloc recursion in diagnosing the corrupted heap. */
72 #if HAVE_DECL_ALARM
73 signal (SIGALRM, SIG_DFL);
74 alarm (2);
75 #endif
76 #ifdef __HAIKU__
77 signal (SIGABRT, sigabrt_no_core);
78 #endif
79 #ifdef M_CHECK_ACTION
80 signal (SIGABRT, sigabrt_no_core);
81 mallopt (M_CHECK_ACTION, 2);
82 #endif
83
84 if (setlocale (LC_ALL, "en_US.UTF-8"))
85 {
86 {
87 /* https://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
88 This test needs valgrind to catch the bug on Debian
89 GNU/Linux 3.1 x86, but it might catch the bug better
90 on other platforms and it shouldn't hurt to try the
91 test here. */
92 static char const pat[] = "insert into";
93 static char const data[] =
94 "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
95 re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
96 | RE_ICASE);
97 memset (&regex, 0, sizeof regex);
98 s = re_compile_pattern (pat, sizeof pat - 1, &regex);
99 if (s)
100 result |= 1;
101 else
102 {
103 if (re_search (&regex, data, sizeof data - 1,
104 0, sizeof data - 1, &regs)
105 != -1)
106 result |= 1;
107 regfree (&regex);
108 }
109 }
110
111 {
112 /* This test is from glibc bug 15078.
113 The test case is from Andreas Schwab in
114 <https://sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
115 */
116 static char const pat[] = "[^x]x";
117 static char const data[] =
118 /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
119 "\xe1\x80\x80"
120 "\xe1\x80\xbb"
121 "\xe1\x80\xbd"
122 "\xe1\x80\x94"
123 "\xe1\x80\xba"
124 "\xe1\x80\xaf"
125 "\xe1\x80\x95"
126 "\xe1\x80\xba"
127 "x";
128 re_set_syntax (0);
129 memset (&regex, 0, sizeof regex);
130 s = re_compile_pattern (pat, sizeof pat - 1, &regex);
131 if (s)
132 result |= 1;
133 else
134 {
135 i = re_search (&regex, data, sizeof data - 1,
136 0, sizeof data - 1, 0);
137 if (i != 0 && i != 21)
138 result |= 1;
139 regfree (&regex);
140 }
141 }
142
143 if (! setlocale (LC_ALL, "C"))
144 return 1;
145 }
146
147 /* This test is from glibc bug 3957, reported by Andrew Mackey. */
148 re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
149 memset (&regex, 0, sizeof regex);
150 s = re_compile_pattern ("a[^x]b", 6, &regex);
151 if (s)
152 result |= 2;
153 else
154 {
155 /* This should fail, but succeeds for glibc-2.5. */
156 if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
157 result |= 2;
158 regfree (&regex);
159 }
160
161 /* This regular expression is from Spencer ere test number 75
162 in grep-2.3. */
163 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
164 memset (&regex, 0, sizeof regex);
165 for (i = 0; i <= UCHAR_MAX; i++)
166 folded_chars[i] = i;
167 regex.translate = folded_chars;
168 s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
169 /* This should fail with _Invalid character class name_ error. */
170 if (!s)
171 {
172 result |= 4;
173 regfree (&regex);
174 }
175
176 /* Ensure that [b-a] is diagnosed as invalid, when
177 using RE_NO_EMPTY_RANGES. */
178 re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
179 memset (&regex, 0, sizeof regex);
180 s = re_compile_pattern ("a[b-a]", 6, &regex);
181 if (s == 0)
182 {
183 result |= 8;
184 regfree (&regex);
185 }
186
187 /* This should succeed, but does not for glibc-2.1.3. */
188 memset (&regex, 0, sizeof regex);
189 s = re_compile_pattern ("{1", 2, &regex);
190 if (s)
191 result |= 8;
192 else
193 regfree (&regex);
194
195 /* The following example is derived from a problem report
196 against gawk from Jorge Stolfi <[email protected]>. */
197 memset (&regex, 0, sizeof regex);
198 s = re_compile_pattern ("[an\371]*n", 7, &regex);
199 if (s)
200 result |= 8;
201 else
202 {
203 /* This should match, but does not for glibc-2.2.1. */
204 if (re_match (&regex, "an", 2, 0, &regs) != 2)
205 result |= 8;
206 else
207 {
208 free (regs.start);
209 free (regs.end);
210 }
211 regfree (&regex);
212 }
213
214 memset (&regex, 0, sizeof regex);
215 s = re_compile_pattern ("x", 1, &regex);
216 if (s)
217 result |= 8;
218 else
219 {
220 /* glibc-2.2.93 does not work with a negative RANGE argument. */
221 if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
222 result |= 8;
223 else
224 {
225 free (regs.start);
226 free (regs.end);
227 }
228 regfree (&regex);
229 }
230
231 /* The version of regex.c in older versions of gnulib
232 ignored RE_ICASE. Detect that problem too. */
233 re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
234 memset (&regex, 0, sizeof regex);
235 s = re_compile_pattern ("x", 1, &regex);
236 if (s)
237 result |= 16;
238 else
239 {
240 if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
241 result |= 16;
242 else
243 {
244 free (regs.start);
245 free (regs.end);
246 }
247 regfree (&regex);
248 }
249
250 /* Catch a bug reported by Vin Shelton in
251 https://lists.gnu.org/r/bug-coreutils/2007-06/msg00089.html
252 */
253 re_set_syntax (RE_SYNTAX_POSIX_BASIC
254 & ~RE_CONTEXT_INVALID_DUP
255 & ~RE_NO_EMPTY_RANGES);
256 memset (&regex, 0, sizeof regex);
257 s = re_compile_pattern ("[[:alnum:]_-]\\\\+\$", 16, &regex);
258 if (s)
259 result |= 32;
260 else
261 regfree (&regex);
262
263 /* REG_STARTEND was added to glibc on 2004-01-15.
264 Reject older versions. */
265 if (! REG_STARTEND)
266 result |= 64;
267
268 /* Matching with the compiled form of this regexp would provoke
269 an assertion failure prior to glibc-2.28:
270 regexec.c:1375: pop_fail_stack: Assertion 'num >= 0' failed
271 With glibc-2.28, compilation fails and reports the invalid
272 back reference. */
273 re_set_syntax (RE_SYNTAX_POSIX_EGREP);
274 memset (&regex, 0, sizeof regex);
275 s = re_compile_pattern ("0|()0|\\\\1|0", 10, &regex);
276 if (!s)
277 {
278 memset (&regs, 0, sizeof regs);
279 i = re_search (&regex, "x", 1, 0, 1, &regs);
280 if (i != -1)
281 result |= 64;
282 if (0 <= i)
283 {
284 free (regs.start);
285 free (regs.end);
286 }
287 regfree (&regex);
288 }
289 else
290 {
291 if (strcmp (s, "Invalid back reference"))
292 result |= 64;
293 }
294
295 /* glibc bug 11053. */
296 re_set_syntax (RE_SYNTAX_POSIX_BASIC);
297 memset (&regex, 0, sizeof regex);
298 static char const pat_sub2[] = "\\\\(a*\\\\)*a*\\\\1";
299 s = re_compile_pattern (pat_sub2, sizeof pat_sub2 - 1, &regex);
300 if (s)
301 result |= 64;
302 else
303 {
304 memset (&regs, 0, sizeof regs);
305 static char const data[] = "a";
306 int datalen = sizeof data - 1;
307 i = re_search (&regex, data, datalen, 0, datalen, &regs);
308 if (i != 0)
309 result |= 64;
310 else if (regs.num_regs < 2)
311 result |= 64;
312 else if (! (regs.start[0] == 0 && regs.end[0] == 1))
313 result |= 64;
314 else if (! (regs.start[1] == 0 && regs.end[1] == 0))
315 result |= 64;
316 regfree (&regex);
317 free (regs.start);
318 free (regs.end);
319 }
320
321 #if 0
322 /* It would be nice to reject hosts whose regoff_t values are too
323 narrow (including glibc on hosts with 64-bit ptrdiff_t and
324 32-bit int), but we should wait until glibc implements this
325 feature. Otherwise, support for equivalence classes and
326 multibyte collation symbols would always be broken except
327 when compiling --without-included-regex. */
328 if (sizeof (regoff_t) < sizeof (ptrdiff_t)
329 || sizeof (regoff_t) < sizeof (ssize_t))
330 result |= 64;
331 #endif
332
333 return result;
334 ]])],
335 [gl_cv_func_re_compile_pattern_working=yes],
336 [gl_cv_func_re_compile_pattern_working=no],
337 [case "$host_os" in
338 # Guess no on native Windows.
339 mingw* | windows*) gl_cv_func_re_compile_pattern_working="guessing no" ;;
340 # Otherwise obey --enable-cross-guesses.
341 *) gl_cv_func_re_compile_pattern_working="$gl_cross_guess_normal" ;;
342 esac
343 ])
344 ])
345 case "$gl_cv_func_re_compile_pattern_working" in #(
346 *yes) ac_use_included_regex=no;; #(
347 *no) ac_use_included_regex=yes;;
348 esac
349 ;;
350 *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
351 ;;
352 esac
353
354 if test $ac_use_included_regex = yes; then
355 AC_DEFINE([_REGEX_INCLUDE_LIMITS_H], [1],
356 [Define if you want <regex.h> to include <limits.h>, so that it
357 consistently overrides <limits.h>'s RE_DUP_MAX.])
358 AC_DEFINE([_REGEX_LARGE_OFFSETS], [1],
359 [Define if you want regoff_t to be at least as wide POSIX requires.])
360 AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
361 [Define to rpl_re_syntax_options if the replacement should be used.])
362 AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
363 [Define to rpl_re_set_syntax if the replacement should be used.])
364 AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
365 [Define to rpl_re_compile_pattern if the replacement should be used.])
366 AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
367 [Define to rpl_re_compile_fastmap if the replacement should be used.])
368 AC_DEFINE([re_search], [rpl_re_search],
369 [Define to rpl_re_search if the replacement should be used.])
370 AC_DEFINE([re_search_2], [rpl_re_search_2],
371 [Define to rpl_re_search_2 if the replacement should be used.])
372 AC_DEFINE([re_match], [rpl_re_match],
373 [Define to rpl_re_match if the replacement should be used.])
374 AC_DEFINE([re_match_2], [rpl_re_match_2],
375 [Define to rpl_re_match_2 if the replacement should be used.])
376 AC_DEFINE([re_set_registers], [rpl_re_set_registers],
377 [Define to rpl_re_set_registers if the replacement should be used.])
378 AC_DEFINE([re_comp], [rpl_re_comp],
379 [Define to rpl_re_comp if the replacement should be used.])
380 AC_DEFINE([re_exec], [rpl_re_exec],
381 [Define to rpl_re_exec if the replacement should be used.])
382 AC_DEFINE([regcomp], [rpl_regcomp],
383 [Define to rpl_regcomp if the replacement should be used.])
384 AC_DEFINE([regexec], [rpl_regexec],
385 [Define to rpl_regexec if the replacement should be used.])
386 AC_DEFINE([regerror], [rpl_regerror],
387 [Define to rpl_regerror if the replacement should be used.])
388 AC_DEFINE([regfree], [rpl_regfree],
389 [Define to rpl_regfree if the replacement should be used.])
390 fi
391 ])
392
393 # Prerequisites of lib/regex.c and lib/regex_internal.c.
394 AC_DEFUN([gl_PREREQ_REGEX],
395 [
396 AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
397 AC_REQUIRE([AC_C_INLINE])
398 AC_REQUIRE([AC_C_RESTRICT])
399 AC_REQUIRE([AC_TYPE_MBSTATE_T])
400 AC_CHECK_HEADERS([libintl.h])
401 AC_CHECK_FUNCS_ONCE([isblank iswctype])
402 AC_CHECK_DECLS([isblank], [], [], [[#include <ctype.h>]])
403 ])