* modules/regex (Files): Add lib/regex_internal.c,
[gnulib.git] / m4 / regex.m4
1 #serial 23
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_DEFUN([gl_REGEX],
14 [
15   gl_INCLUDED_REGEX([lib/regex.c])
16 ])
17
18 dnl Usage: gl_INCLUDED_REGEX([lib/regex.c])
19 dnl
20 AC_DEFUN([gl_INCLUDED_REGEX],
21   [
22     AC_LIBSOURCES(
23       [regcomp.c, regex.c, regex.h,
24        regex_internal.c, regex_internal.h, regexec.c])
25
26     dnl Even packages that don't use regex.c can use this macro.
27     dnl Of course, for them it doesn't do anything.
28
29     # Assume we'll default to using the included regex.c.
30     ac_use_included_regex=yes
31
32     # However, if the system regex support is good enough that it passes the
33     # the following run test, then default to *not* using the included regex.c.
34     # If cross compiling, assume the test would fail and use the included
35     # regex.c.  The first failing regular expression is from `Spencer ere
36     # test #75' in grep-2.3.
37     AC_CACHE_CHECK([for working re_compile_pattern],
38                    [gl_cv_func_working_re_compile_pattern],
39       [AC_TRY_RUN(
40          [[
41 #include <stdio.h>
42 #include <string.h>
43 #include <regex.h>
44           int
45           main ()
46           {
47             static struct re_pattern_buffer regex;
48             const char *s;
49             struct re_registers regs;
50             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
51             memset (&regex, 0, sizeof (regex));
52             s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
53             /* This should fail with _Invalid character class name_ error.  */
54             if (!s)
55               exit (1);
56
57             /* This should succeed, but does not for e.g. glibc-2.1.3.  */
58             memset (&regex, 0, sizeof (regex));
59             s = re_compile_pattern ("{1", 2, &regex);
60
61             if (s)
62               exit (1);
63
64             /* The following example is derived from a problem report
65                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
66             memset (&regex, 0, sizeof (regex));
67             s = re_compile_pattern ("[an\371]*n", 7, &regex);
68             if (s)
69               exit (1);
70
71             /* This should match, but does not for e.g. glibc-2.2.1.  */
72             if (re_match (&regex, "an", 2, 0, &regs) != 2)
73               exit (1);
74
75             memset (&regex, 0, sizeof (regex));
76             s = re_compile_pattern ("x", 1, &regex);
77             if (s)
78               exit (1);
79
80             /* The version of regex.c in e.g. GNU libc-2.2.93 did not
81                work with a negative RANGE argument.  */
82             if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
83               exit (1);
84
85             /* The version of regex.c in older versions of gnulib
86              * ignored RE_ICASE.  Detect that problem too. */
87             memset (&regex, 0, sizeof (regex));
88             re_set_syntax(RE_SYNTAX_EMACS|RE_ICASE);
89             s = re_compile_pattern ("x", 1, &regex);
90             if (s)
91               exit (1);
92
93             if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
94               exit (1);
95
96             /* REG_STARTEND was added to glibc on 2004-01-15.
97                Reject older versions.  */
98             if (! REG_STARTEND)
99               exit (1);
100
101             exit (0);
102           }
103          ]],
104          [gl_cv_func_working_re_compile_pattern=yes],
105          [gl_cv_func_working_re_compile_pattern=no],
106          dnl When crosscompiling, assume it is broken.
107          [gl_cv_func_working_re_compile_pattern=no])])
108     if test $gl_cv_func_working_re_compile_pattern = yes; then
109       ac_use_included_regex=no
110     fi
111
112     test -n "$1" || AC_MSG_ERROR([missing argument])
113     m4_syscmd([test -f '$1'])
114     ifelse(m4_sysval, 0,
115       [
116         AC_ARG_WITH([included-regex],
117           [  --without-included-regex don't compile regex; this is the default on
118                           systems with recent-enough versions of the GNU C
119                           Library (use with caution on other systems)],
120           [gl_with_regex=$withval],
121           [gl_with_regex=$ac_use_included_regex])
122         if test "X$gl_with_regex" = Xyes; then
123           AC_LIBOBJ([regex])
124           gl_PREREQ_REGEX
125         fi
126       ],
127     )
128   ]
129 )
130
131 # Prerequisites of lib/regex.c and lib/regex_internal.c.
132 AC_DEFUN([gl_PREREQ_REGEX],
133 [
134   AC_REQUIRE([gl_C_RESTRICT])
135   AC_REQUIRE([AM_LANGINFO_CODESET])
136   AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
137   AC_CHECK_FUNCS_ONCE([isblank mbrtowc mempcpy wcrtomb wcscoll])
138 ])