* regex.m4 (gl_INCLUDED_REGEX): Use AC_RUN_ELSE instead of the
[gnulib.git] / m4 / regex.m4
1 #serial 24
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_RUN_IFELSE(
40          [AC_LANG_PROGRAM(
41             [AC_INCLUDES_DEFAULT
42              #include <regex.h>],
43             [[static struct re_pattern_buffer regex;
44               const char *s;
45               struct re_registers regs;
46               re_set_syntax (RE_SYNTAX_POSIX_EGREP);
47               memset (&regex, 0, sizeof (regex));
48               s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
49               /* This should fail with _Invalid character class name_ error.  */
50               if (!s)
51                 exit (1);
52
53               /* This should succeed, but does not for e.g. glibc-2.1.3.  */
54               memset (&regex, 0, sizeof (regex));
55               s = re_compile_pattern ("{1", 2, &regex);
56
57               if (s)
58                 exit (1);
59
60               /* The following example is derived from a problem report
61                  against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
62               memset (&regex, 0, sizeof (regex));
63               s = re_compile_pattern ("[an\371]*n", 7, &regex);
64               if (s)
65                 exit (1);
66
67               /* This should match, but does not for e.g. glibc-2.2.1.  */
68               if (re_match (&regex, "an", 2, 0, &regs) != 2)
69                 exit (1);
70
71               memset (&regex, 0, sizeof (regex));
72               s = re_compile_pattern ("x", 1, &regex);
73               if (s)
74                 exit (1);
75
76               /* The version of regex.c in e.g. GNU libc-2.2.93 did not
77                  work with a negative RANGE argument.  */
78               if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
79                 exit (1);
80
81               /* The version of regex.c in older versions of gnulib
82                * ignored RE_ICASE.  Detect that problem too. */
83               memset (&regex, 0, sizeof (regex));
84               re_set_syntax(RE_SYNTAX_EMACS|RE_ICASE);
85               s = re_compile_pattern ("x", 1, &regex);
86               if (s)
87                 exit (1);
88
89               if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
90                 exit (1);
91
92               /* REG_STARTEND was added to glibc on 2004-01-15.
93                  Reject older versions.  */
94               if (! REG_STARTEND)
95                 exit (1);
96
97               exit (0);]])],
98          [gl_cv_func_working_re_compile_pattern=yes],
99          [gl_cv_func_working_re_compile_pattern=no],
100          dnl When crosscompiling, assume it is broken.
101          [gl_cv_func_working_re_compile_pattern=no])])
102     if test $gl_cv_func_working_re_compile_pattern = yes; then
103       ac_use_included_regex=no
104     fi
105
106     test -n "$1" || AC_MSG_ERROR([missing argument])
107     m4_syscmd([test -f '$1'])
108     ifelse(m4_sysval, 0,
109       [
110         AC_ARG_WITH([included-regex],
111           [  --without-included-regex don't compile regex; this is the default on
112                           systems with recent-enough versions of the GNU C
113                           Library (use with caution on other systems)],
114           [gl_with_regex=$withval],
115           [gl_with_regex=$ac_use_included_regex])
116         if test "X$gl_with_regex" = Xyes; then
117           AC_LIBOBJ([regex])
118           gl_PREREQ_REGEX
119         fi
120       ],
121     )
122   ]
123 )
124
125 # Prerequisites of lib/regex.c and lib/regex_internal.c.
126 AC_DEFUN([gl_PREREQ_REGEX],
127 [
128   AC_REQUIRE([gl_C_RESTRICT])
129   AC_REQUIRE([AM_LANGINFO_CODESET])
130   AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
131   AC_CHECK_FUNCS_ONCE([isblank mbrtowc mempcpy wcrtomb wcscoll])
132 ])