* m4/regex.m4: Add AC_PREREQ(2.50).
[gnulib.git] / m4 / regex.m4
1 #serial 28
2
3 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005 Free
4 # Software Foundation, Inc.
5 #
6 # This file is free software; the Free Software Foundation
7 # gives unlimited permission to copy and/or distribute it,
8 # with or without modifications, as long as this notice is preserved.
9
10 dnl Initially derived from code in GNU grep.
11 dnl Mostly written by Jim Meyering.
12
13 AC_PREREQ([2.50])
14
15 AC_DEFUN([gl_REGEX],
16 [
17   AC_LIBSOURCES(
18     [regcomp.c, regex.c, regex.h,
19      regex_internal.c, regex_internal.h, regexec.c])
20
21   AC_ARG_WITH([included-regex],
22     [AC_HELP_STRING([--without-included-regex],
23                     [don't compile regex; this is the default on
24                      systems with recent-enough versions of the GNU C
25                      Library (use with caution on other systems)])])
26
27   case $with_included_regex in
28   yes|no) ac_use_included_regex=$with_included_regex
29         ;;
30   '')
31     # If the system regex support is good enough that it passes the the
32     # following run test, then default to *not* using the included regex.c.
33     # If cross compiling, assume the test would fail and use the included
34     # regex.c.  The first failing regular expression is from `Spencer ere
35     # test #75' in grep-2.3.
36     AC_CACHE_CHECK([for working re_compile_pattern],
37                    [gl_cv_func_re_compile_pattern_broken],
38       [AC_RUN_IFELSE(
39         [AC_LANG_PROGRAM(
40           [AC_INCLUDES_DEFAULT
41            #include <regex.h>],
42           [[static struct re_pattern_buffer regex;
43             const char *s;
44             struct re_registers regs;
45             /* Use the POSIX-compliant spelling with leading REG_,
46                rather than the traditional GNU spelling with leading RE_,
47                so that we reject older libc implementations.  */
48             re_set_syntax (REG_SYNTAX_POSIX_EGREP);
49             memset (&regex, 0, sizeof (regex));
50             s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
51             /* This should fail with _Invalid character class name_ error.  */
52             if (!s)
53               exit (1);
54
55             /* This should succeed, but does not for e.g. glibc-2.1.3.  */
56             memset (&regex, 0, sizeof (regex));
57             s = re_compile_pattern ("{1", 2, &regex);
58
59             if (s)
60               exit (1);
61
62             /* The following example is derived from a problem report
63                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
64             memset (&regex, 0, sizeof (regex));
65             s = re_compile_pattern ("[an\371]*n", 7, &regex);
66             if (s)
67               exit (1);
68
69             /* This should match, but does not for e.g. glibc-2.2.1.  */
70             if (re_match (&regex, "an", 2, 0, &regs) != 2)
71               exit (1);
72
73             memset (&regex, 0, sizeof (regex));
74             s = re_compile_pattern ("x", 1, &regex);
75             if (s)
76               exit (1);
77
78             /* The version of regex.c in e.g. GNU libc-2.2.93 did not
79                work with a negative RANGE argument.  */
80             if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
81               exit (1);
82
83             /* The version of regex.c in older versions of gnulib
84                ignored REG_IGNORE_CASE (which was then called RE_ICASE).
85                Detect that problem too.  */
86             memset (&regex, 0, sizeof (regex));
87             re_set_syntax (REG_SYNTAX_EMACS | REG_IGNORE_CASE);
88             s = re_compile_pattern ("x", 1, &regex);
89             if (s)
90               exit (1);
91
92             if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
93               exit (1);
94
95             /* REG_STARTEND was added to glibc on 2004-01-15.
96                Reject older versions.  */
97             if (! REG_STARTEND)
98               exit (1);
99
100             exit (0);]])],
101        [gl_cv_func_re_compile_pattern_broken=no],
102        [gl_cv_func_re_compile_pattern_broken=yes],
103        dnl When crosscompiling, assume it is broken.
104        [gl_cv_func_re_compile_pattern_broken=yes])])
105     ac_use_included_regex=$gl_cv_func_re_compile_pattern_broken
106     ;;
107   *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
108     ;;
109   esac
110
111   if test $ac_use_included_regex = yes; then
112     AC_LIBOBJ([regex])
113     gl_PREREQ_REGEX
114   fi
115 ])
116
117 # Prerequisites of lib/regex.c and lib/regex_internal.c.
118 AC_DEFUN([gl_PREREQ_REGEX],
119 [
120   AC_REQUIRE([AC_GNU_SOURCE])
121   AC_REQUIRE([gl_C_RESTRICT])
122   AC_REQUIRE([AM_LANGINFO_CODESET])
123   AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
124   AC_CHECK_FUNCS_ONCE([isblank mbrtowc mempcpy wcrtomb wcscoll])
125 ])