On 64-bit hosts (where size_t is 64 bits and int is 32 bits), the
[gnulib.git] / m4 / regex.m4
1 #serial 29
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_REQUIRE([AC_SYS_LARGEFILE]) dnl for a sufficently-wide off_t
18   AC_DEFINE([_REGEX_LARGE_OFFSETS], 1,
19     [Define if you want regoff_t to be at least as wide POSIX requires.])
20
21   AC_LIBSOURCES(
22     [regcomp.c, regex.c, regex.h,
23      regex_internal.c, regex_internal.h, regexec.c])
24
25   AC_ARG_WITH([included-regex],
26     [AC_HELP_STRING([--without-included-regex],
27                     [don't compile regex; this is the default on
28                      systems with recent-enough versions of the GNU C
29                      Library (use with caution on other systems)])])
30
31   case $with_included_regex in
32   yes|no) ac_use_included_regex=$with_included_regex
33         ;;
34   '')
35     # If the system regex support is good enough that it passes the the
36     # following run test, then default to *not* using the included regex.c.
37     # If cross compiling, assume the test would fail and use the included
38     # regex.c.  The first failing regular expression is from `Spencer ere
39     # test #75' in grep-2.3.
40     AC_CACHE_CHECK([for working re_compile_pattern],
41                    [gl_cv_func_re_compile_pattern_broken],
42       [AC_RUN_IFELSE(
43         [AC_LANG_PROGRAM(
44           [AC_INCLUDES_DEFAULT
45            #include <regex.h>],
46           [[static struct re_pattern_buffer regex;
47             const char *s;
48             struct re_registers regs;
49             /* Use the POSIX-compliant spelling with leading REG_,
50                rather than the traditional GNU spelling with leading RE_,
51                so that we reject older libc implementations.  */
52             re_set_syntax (REG_SYNTAX_POSIX_EGREP);
53             memset (&regex, 0, sizeof (regex));
54             s = re_compile_pattern ("a[:@:>@:]b\n", 9, &regex);
55             /* This should fail with _Invalid character class name_ error.  */
56             if (!s)
57               exit (1);
58
59             /* This should succeed, but does not for e.g. glibc-2.1.3.  */
60             memset (&regex, 0, sizeof (regex));
61             s = re_compile_pattern ("{1", 2, &regex);
62
63             if (s)
64               exit (1);
65
66             /* The following example is derived from a problem report
67                against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>.  */
68             memset (&regex, 0, sizeof (regex));
69             s = re_compile_pattern ("[an\371]*n", 7, &regex);
70             if (s)
71               exit (1);
72
73             /* This should match, but does not for e.g. glibc-2.2.1.  */
74             if (re_match (&regex, "an", 2, 0, &regs) != 2)
75               exit (1);
76
77             memset (&regex, 0, sizeof (regex));
78             s = re_compile_pattern ("x", 1, &regex);
79             if (s)
80               exit (1);
81
82             /* The version of regex.c in e.g. GNU libc-2.2.93 did not
83                work with a negative RANGE argument.  */
84             if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
85               exit (1);
86
87             /* The version of regex.c in older versions of gnulib
88                ignored REG_IGNORE_CASE (which was then called RE_ICASE).
89                Detect that problem too.  */
90             memset (&regex, 0, sizeof (regex));
91             re_set_syntax (REG_SYNTAX_EMACS | REG_IGNORE_CASE);
92             s = re_compile_pattern ("x", 1, &regex);
93             if (s)
94               exit (1);
95
96             if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
97               exit (1);
98
99             /* REG_STARTEND was added to glibc on 2004-01-15.
100                Reject older versions.  */
101             if (! REG_STARTEND)
102               exit (1);
103
104             /* Reject hosts whose regoff_t values are too narrow.
105                These include glibc 2.3.5 on hosts with 64-bit off_t
106                and 32-bit int, and Solaris 10 on hosts with 32-bit int
107                and _FILE_OFFSET_BITS=64.  */
108             if (sizeof (regoff_t) < sizeof (off_t))
109               exit (1);
110
111             exit (0);]])],
112        [gl_cv_func_re_compile_pattern_broken=no],
113        [gl_cv_func_re_compile_pattern_broken=yes],
114        dnl When crosscompiling, assume it is broken.
115        [gl_cv_func_re_compile_pattern_broken=yes])])
116     ac_use_included_regex=$gl_cv_func_re_compile_pattern_broken
117     ;;
118   *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
119     ;;
120   esac
121
122   if test $ac_use_included_regex = yes; then
123     AC_LIBOBJ([regex])
124     gl_PREREQ_REGEX
125   fi
126 ])
127
128 # Prerequisites of lib/regex.c and lib/regex_internal.c.
129 AC_DEFUN([gl_PREREQ_REGEX],
130 [
131   AC_REQUIRE([AC_GNU_SOURCE])
132   AC_REQUIRE([gl_C_RESTRICT])
133   AC_REQUIRE([AM_LANGINFO_CODESET])
134   AC_CHECK_HEADERS_ONCE([locale.h wchar.h wctype.h])
135   AC_CHECK_FUNCS_ONCE([isblank mbrtowc mempcpy wcrtomb wcscoll])
136 ])