Debian Codegraph @

m4/regex.m4

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