1#serial 24
2
3dnl Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2003, 2004, 2006 Free
4dnl Software Foundation, Inc.
5dnl
6dnl This file is free software; the Free Software Foundation
7dnl gives unlimited permission to copy and/or distribute it,
8dnl with or without modifications, as long as this notice is preserved.
9
10dnl Initially derived from code in GNU grep.
11dnl Mostly written by Jim Meyering.
12
13AC_DEFUN([gl_REGEX],
14[
15  gl_INCLUDED_REGEX([lib/regex.c])
16])
17
18dnl Usage: gl_INCLUDED_REGEX([lib/regex.c])
19dnl
20AC_DEFUN([gl_INCLUDED_REGEX],
21  [
22    dnl Even packages that don't use regex.c can use this macro.
23    dnl Of course, for them it doesn't do anything.
24
25    # Assume we'll default to using the included regex.c.
26    ac_use_included_regex=yes
27
28    # However, if the system regex support is good enough that it passes the
29    # the following run test, then default to *not* using the included regex.c.
30    # If cross compiling, assume the test would fail and use the included
31    # regex.c.  The first failing regular expression is from `Spencer ere
32    # test #75' in grep-2.3.
33    AC_CACHE_CHECK([for working re_compile_pattern],
34		   jm_cv_func_working_re_compile_pattern,
35      [AC_TRY_RUN(
36[#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <regex.h>
40	  int
41	  main ()
42	  {
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 doesn't 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 doesn't 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 didn't
77	       work with a negative RANGE argument.  */
78	    if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
79	      exit (1);
80
81	    exit (0);
82	  }
83	],
84	       jm_cv_func_working_re_compile_pattern=yes,
85	       jm_cv_func_working_re_compile_pattern=no,
86	       dnl When crosscompiling, assume it's broken.
87	       jm_cv_func_working_re_compile_pattern=no)])
88    if test $jm_cv_func_working_re_compile_pattern = yes; then
89      ac_use_included_regex=no
90    fi
91
92    test -n "$1" || AC_MSG_ERROR([missing argument])
93    m4_syscmd([test -f $1])
94    ifelse(m4_sysval, 0,
95      [
96	AC_ARG_WITH(included-regex,
97	[  --without-included-regex don't compile regex; this is the default on
98                          systems with version 2 of the GNU C library
99                          (use with caution on other system)],
100		    jm_with_regex=$withval,
101		    jm_with_regex=$ac_use_included_regex)
102	if test "$jm_with_regex" = yes; then
103	  AC_LIBOBJ(regex)
104	  gl_PREREQ_REGEX
105	fi
106      ],
107    )
108  ]
109)
110
111# Prerequisites of lib/regex.c.
112AC_DEFUN([gl_PREREQ_REGEX],
113[
114  dnl FIXME: Maybe provide a btowc replacement someday: Solaris 2.5.1 lacks it.
115  dnl FIXME: Check for wctype and iswctype, and and add -lw if necessary
116  dnl to get them.
117
118  dnl Persuade glibc <string.h> to declare mempcpy().
119  AC_REQUIRE([AC_GNU_SOURCE])
120
121  AC_REQUIRE([AC_C_RESTRICT])
122  AC_REQUIRE([AC_FUNC_ALLOCA])
123  AC_REQUIRE([AC_HEADER_STDC])
124  AC_CHECK_HEADERS_ONCE(wchar.h wctype.h)
125  AC_CHECK_FUNCS_ONCE(isascii mempcpy)
126  AC_CHECK_FUNCS(btowc)
127])
128