Avoid the need for AC_LIBSOURCES in m4 macros.
[gnulib.git] / m4 / regex.m4
1 #serial 39
2
3 # Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2005,
4 # 2006 Free 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_ARG_WITH([included-regex],
18     [AC_HELP_STRING([--without-included-regex],
19                     [don't compile regex; this is the default on
20                      systems with recent-enough versions of the GNU C
21                      Library (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.  The first failing regular expression is from `Spencer ere
31     # test #75' in grep-2.3.
32     AC_CACHE_CHECK([for working re_compile_pattern],
33                    [gl_cv_func_re_compile_pattern_working],
34       [AC_RUN_IFELSE(
35         [AC_LANG_PROGRAM(
36           [AC_INCLUDES_DEFAULT
37            #include <limits.h>
38            #include <regex.h>
39            ],
40           [[static struct re_pattern_buffer regex;
41             unsigned char folded_chars[UCHAR_MAX + 1];
42             int i;
43             const char *s;
44             struct re_registers regs;
45             re_set_syntax (RE_SYNTAX_POSIX_EGREP);
46             memset (&regex, 0, sizeof (regex));
47             for (i = 0; i <= UCHAR_MAX; i++)
48               folded_chars[i] = i;
49             regex.translate = folded_chars;
50             s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &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 RE_ICASE.  Detect that problem too.  */
85             memset (&regex, 0, sizeof (regex));
86             re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
87             s = re_compile_pattern ("x", 1, &regex);
88             if (s)
89               exit (1);
90
91             if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
92               exit (1);
93
94             /* REG_STARTEND was added to glibc on 2004-01-15.
95                Reject older versions.  */
96             if (! REG_STARTEND)
97               exit (1);
98
99             /* Reject hosts whose regoff_t values are too narrow.
100                These include glibc 2.3.5 on hosts with 64-bit ptrdiff_t
101                and 32-bit int.  */
102             if (sizeof (regoff_t) < sizeof (ptrdiff_t)
103                 || sizeof (regoff_t) < sizeof (ssize_t))
104               exit (1);
105
106             exit (0);]])],
107        [gl_cv_func_re_compile_pattern_working=yes],
108        [gl_cv_func_re_compile_pattern_working=no],
109        dnl When crosscompiling, assume it is not working.
110        [gl_cv_func_re_compile_pattern_working=no])])
111     case $gl_cv_func_re_compile_pattern_working in #(
112     yes) ac_use_included_regex=no;; #(
113     no) ac_use_included_regex=yes;;
114     esac
115     ;;
116   *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
117     ;;
118   esac
119
120   if test $ac_use_included_regex = yes; then
121     AC_DEFINE([_REGEX_LARGE_OFFSETS], 1,
122       [Define if you want regoff_t to be at least as wide POSIX requires.])
123     AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
124       [Define to rpl_re_syntax_options if the replacement should be used.])
125     AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
126       [Define to rpl_re_set_syntax if the replacement should be used.])
127     AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
128       [Define to rpl_re_compile_pattern if the replacement should be used.])
129     AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
130       [Define to rpl_re_compile_fastmap if the replacement should be used.])
131     AC_DEFINE([re_search], [rpl_re_search],
132       [Define to rpl_re_search if the replacement should be used.])
133     AC_DEFINE([re_search_2], [rpl_re_search_2],
134       [Define to rpl_re_search_2 if the replacement should be used.])
135     AC_DEFINE([re_match], [rpl_re_match],
136       [Define to rpl_re_match if the replacement should be used.])
137     AC_DEFINE([re_match_2], [rpl_re_match_2],
138       [Define to rpl_re_match_2 if the replacement should be used.])
139     AC_DEFINE([re_set_registers], [rpl_re_set_registers],
140       [Define to rpl_re_set_registers if the replacement should be used.])
141     AC_DEFINE([re_comp], [rpl_re_comp],
142       [Define to rpl_re_comp if the replacement should be used.])
143     AC_DEFINE([re_exec], [rpl_re_exec],
144       [Define to rpl_re_exec if the replacement should be used.])
145     AC_DEFINE([regcomp], [rpl_regcomp],
146       [Define to rpl_regcomp if the replacement should be used.])
147     AC_DEFINE([regexec], [rpl_regexec],
148       [Define to rpl_regexec if the replacement should be used.])
149     AC_DEFINE([regerror], [rpl_regerror],
150       [Define to rpl_regerror if the replacement should be used.])
151     AC_DEFINE([regfree], [rpl_regfree],
152       [Define to rpl_regfree if the replacement should be used.])
153     AC_LIBOBJ([regex])
154     gl_PREREQ_REGEX
155   fi
156 ])
157
158 # Prerequisites of lib/regex.c and lib/regex_internal.c.
159 AC_DEFUN([gl_PREREQ_REGEX],
160 [
161   AC_REQUIRE([AC_GNU_SOURCE])
162   AC_REQUIRE([AC_C_RESTRICT])
163   AC_REQUIRE([AM_LANGINFO_CODESET])
164   AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
165   AC_CHECK_FUNCS_ONCE([mbrtowc mempcpy wcrtomb wcscoll])
166   AC_CHECK_DECLS([isblank], [], [], [#include <ctype.h>])
167 ])