(jm_INCLUDED_REGEX): Don't use Latin-1 in C source code; use \371
[gnulib.git] / m4 / regex.m4
1 #serial 17
2
3 dnl Initially derived from code in GNU grep.
4 dnl Mostly written by Jim Meyering.
5
6 AC_DEFUN([gl_REGEX],
7 [
8   jm_INCLUDED_REGEX([lib/regex.c])
9 ])
10
11 dnl Usage: jm_INCLUDED_REGEX([lib/regex.c])
12 dnl
13 AC_DEFUN([jm_INCLUDED_REGEX],
14   [
15     dnl Even packages that don't use regex.c can use this macro.
16     dnl Of course, for them it doesn't do anything.
17
18     # Assume we'll default to using the included regex.c.
19     ac_use_included_regex=yes
20
21     # However, if the system regex support is good enough that it passes the
22     # the following run test, then default to *not* using the included regex.c.
23     # If cross compiling, assume the test would fail and use the included
24     # regex.c.  The first failing regular expression is from `Spencer ere
25     # test #75' in grep-2.3.
26     AC_CACHE_CHECK([for working re_compile_pattern],
27                    jm_cv_func_working_re_compile_pattern,
28       AC_TRY_RUN(
29 [#include <stdio.h>
30 #include <string.h>
31 #include <regex.h>
32           int
33           main ()
34           {
35             static struct re_pattern_buffer regex;
36             const char *s;
37             struct re_registers regs;
38             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
39             memset (&regex, 0, sizeof (regex));
40             [s = re_compile_pattern ("a[[:@:>@:]]b\n", 9, &regex);]
41             /* This should fail with _Invalid character class name_ error.  */
42             if (!s)
43               exit (1);
44
45             /* This should succeed, but doesn't for e.g. glibc-2.1.3.  */
46             memset (&regex, 0, sizeof (regex));
47             s = re_compile_pattern ("{1", 2, &regex);
48
49             if (s)
50               exit (1);
51
52             /* The following example is derived from a problem report
53                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
54             memset (&regex, 0, sizeof (regex));
55             s = re_compile_pattern ("[[an\371]]*n", 7, &regex);
56             if (s)
57               exit (1);
58
59             /* This should match, but doesn't for e.g. glibc-2.2.1.  */
60             if (re_match (&regex, "an", 2, 0, &regs) != 2)
61               exit (1);
62
63             memset (&regex, 0, sizeof (regex));
64             s = re_compile_pattern ("x", 1, &regex);
65             if (s)
66               exit (1);
67
68             /* The version of regex.c in e.g. GNU libc-2.2.93 didn't
69                work with a negative RANGE argument.  */
70             if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
71               exit (1);
72
73             exit (0);
74           }
75         ],
76                jm_cv_func_working_re_compile_pattern=yes,
77                jm_cv_func_working_re_compile_pattern=no,
78                dnl When crosscompiling, assume it's broken.
79                jm_cv_func_working_re_compile_pattern=no))
80     if test $jm_cv_func_working_re_compile_pattern = yes; then
81       ac_use_included_regex=no
82     fi
83
84     test -n "$1" || AC_MSG_ERROR([missing argument])
85     m4_syscmd([test -f $1])
86     ifelse(m4_sysval, 0,
87       [
88         AC_ARG_WITH(included-regex,
89         [  --without-included-regex don't compile regex; this is the default on
90                           systems with version 2 of the GNU C library
91                           (use with caution on other system)],
92                     jm_with_regex=$withval,
93                     jm_with_regex=$ac_use_included_regex)
94         if test "$jm_with_regex" = yes; then
95           AC_LIBOBJ(regex)
96           jm_PREREQ_REGEX
97         fi
98       ],
99     )
100   ]
101 )
102
103 # Prerequisites of lib/regex.c.
104 AC_DEFUN([jm_PREREQ_REGEX],
105 [
106   dnl FIXME: Maybe provide a btowc replacement someday: Solaris 2.5.1 lacks it.
107   dnl FIXME: Check for wctype and iswctype, and and add -lw if necessary
108   dnl to get them.
109
110   dnl Persuade glibc <string.h> to declare mempcpy().
111   AC_REQUIRE([AC_GNU_SOURCE])
112
113   AC_REQUIRE([AC_FUNC_ALLOCA])
114   AC_REQUIRE([AC_HEADER_STDC])
115   AC_CHECK_HEADERS_ONCE(limits.h string.h wchar.h wctype.h)
116   AC_CHECK_FUNCS_ONCE(isascii mempcpy)
117   AC_CHECK_FUNCS(btowc)
118 ])