regex.m4 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. # serial 66
  2. # Copyright (C) 1996-2001, 2003-2017 Free Software Foundation, Inc.
  3. #
  4. # This file is free software; the Free Software Foundation
  5. # gives unlimited permission to copy and/or distribute it,
  6. # with or without modifications, as long as this notice is preserved.
  7. dnl Initially derived from code in GNU grep.
  8. dnl Mostly written by Jim Meyering.
  9. AC_PREREQ([2.50])
  10. AC_DEFUN([gl_REGEX],
  11. [
  12. AC_ARG_WITH([included-regex],
  13. [AS_HELP_STRING([--without-included-regex],
  14. [don't compile regex; this is the default on systems
  15. with recent-enough versions of the GNU C Library
  16. (use with caution on other systems).])])
  17. case $with_included_regex in #(
  18. yes|no) ac_use_included_regex=$with_included_regex
  19. ;;
  20. '')
  21. # If the system regex support is good enough that it passes the
  22. # following run test, then default to *not* using the included regex.c.
  23. # If cross compiling, assume the test would fail and use the included
  24. # regex.c.
  25. AC_CHECK_DECLS_ONCE([alarm])
  26. AC_CHECK_HEADERS_ONCE([malloc.h])
  27. AC_CACHE_CHECK([for working re_compile_pattern],
  28. [gl_cv_func_re_compile_pattern_working],
  29. [AC_RUN_IFELSE(
  30. [AC_LANG_PROGRAM(
  31. [[#include <regex.h>
  32. #include <locale.h>
  33. #include <limits.h>
  34. #include <string.h>
  35. #if defined M_CHECK_ACTION || HAVE_DECL_ALARM
  36. # include <signal.h>
  37. # include <unistd.h>
  38. #endif
  39. #if HAVE_MALLOC_H
  40. # include <malloc.h>
  41. #endif
  42. #ifdef M_CHECK_ACTION
  43. /* Exit with distinguishable exit code. */
  44. static void sigabrt_no_core (int sig) { raise (SIGTERM); }
  45. #endif
  46. ]],
  47. [[int result = 0;
  48. static struct re_pattern_buffer regex;
  49. unsigned char folded_chars[UCHAR_MAX + 1];
  50. int i;
  51. const char *s;
  52. struct re_registers regs;
  53. /* Some builds of glibc go into an infinite loop on this
  54. test. Use alarm to force death, and mallopt to avoid
  55. malloc recursion in diagnosing the corrupted heap. */
  56. #if HAVE_DECL_ALARM
  57. signal (SIGALRM, SIG_DFL);
  58. alarm (2);
  59. #endif
  60. #ifdef M_CHECK_ACTION
  61. signal (SIGABRT, sigabrt_no_core);
  62. mallopt (M_CHECK_ACTION, 2);
  63. #endif
  64. if (setlocale (LC_ALL, "en_US.UTF-8"))
  65. {
  66. {
  67. /* http://sourceware.org/ml/libc-hacker/2006-09/msg00008.html
  68. This test needs valgrind to catch the bug on Debian
  69. GNU/Linux 3.1 x86, but it might catch the bug better
  70. on other platforms and it shouldn't hurt to try the
  71. test here. */
  72. static char const pat[] = "insert into";
  73. static char const data[] =
  74. "\xFF\0\x12\xA2\xAA\xC4\xB1,K\x12\xC4\xB1*\xACK";
  75. re_set_syntax (RE_SYNTAX_GREP | RE_HAT_LISTS_NOT_NEWLINE
  76. | RE_ICASE);
  77. memset (&regex, 0, sizeof regex);
  78. s = re_compile_pattern (pat, sizeof pat - 1, &regex);
  79. if (s)
  80. result |= 1;
  81. else if (re_search (&regex, data, sizeof data - 1,
  82. 0, sizeof data - 1, &regs)
  83. != -1)
  84. result |= 1;
  85. regfree (&regex);
  86. }
  87. {
  88. /* This test is from glibc bug 15078.
  89. The test case is from Andreas Schwab in
  90. <http://www.sourceware.org/ml/libc-alpha/2013-01/msg00967.html>.
  91. */
  92. static char const pat[] = "[^x]x";
  93. static char const data[] =
  94. /* <U1000><U103B><U103D><U1014><U103A><U102F><U1015><U103A> */
  95. "\xe1\x80\x80"
  96. "\xe1\x80\xbb"
  97. "\xe1\x80\xbd"
  98. "\xe1\x80\x94"
  99. "\xe1\x80\xba"
  100. "\xe1\x80\xaf"
  101. "\xe1\x80\x95"
  102. "\xe1\x80\xba"
  103. "x";
  104. re_set_syntax (0);
  105. memset (&regex, 0, sizeof regex);
  106. s = re_compile_pattern (pat, sizeof pat - 1, &regex);
  107. if (s)
  108. result |= 1;
  109. else
  110. {
  111. i = re_search (&regex, data, sizeof data - 1,
  112. 0, sizeof data - 1, 0);
  113. if (i != 0 && i != 21)
  114. result |= 1;
  115. }
  116. regfree (&regex);
  117. }
  118. if (! setlocale (LC_ALL, "C"))
  119. return 1;
  120. }
  121. /* This test is from glibc bug 3957, reported by Andrew Mackey. */
  122. re_set_syntax (RE_SYNTAX_EGREP | RE_HAT_LISTS_NOT_NEWLINE);
  123. memset (&regex, 0, sizeof regex);
  124. s = re_compile_pattern ("a[^x]b", 6, &regex);
  125. if (s)
  126. result |= 2;
  127. /* This should fail, but succeeds for glibc-2.5. */
  128. else if (re_search (&regex, "a\nb", 3, 0, 3, &regs) != -1)
  129. result |= 2;
  130. /* This regular expression is from Spencer ere test number 75
  131. in grep-2.3. */
  132. re_set_syntax (RE_SYNTAX_POSIX_EGREP);
  133. memset (&regex, 0, sizeof regex);
  134. for (i = 0; i <= UCHAR_MAX; i++)
  135. folded_chars[i] = i;
  136. regex.translate = folded_chars;
  137. s = re_compile_pattern ("a[[:@:>@:]]b\n", 11, &regex);
  138. /* This should fail with _Invalid character class name_ error. */
  139. if (!s)
  140. result |= 4;
  141. /* Ensure that [b-a] is diagnosed as invalid, when
  142. using RE_NO_EMPTY_RANGES. */
  143. re_set_syntax (RE_SYNTAX_POSIX_EGREP | RE_NO_EMPTY_RANGES);
  144. memset (&regex, 0, sizeof regex);
  145. s = re_compile_pattern ("a[b-a]", 6, &regex);
  146. if (s == 0)
  147. result |= 8;
  148. /* This should succeed, but does not for glibc-2.1.3. */
  149. memset (&regex, 0, sizeof regex);
  150. s = re_compile_pattern ("{1", 2, &regex);
  151. if (s)
  152. result |= 8;
  153. /* The following example is derived from a problem report
  154. against gawk from Jorge Stolfi <stolfi@ic.unicamp.br>. */
  155. memset (&regex, 0, sizeof regex);
  156. s = re_compile_pattern ("[an\371]*n", 7, &regex);
  157. if (s)
  158. result |= 8;
  159. /* This should match, but does not for glibc-2.2.1. */
  160. else if (re_match (&regex, "an", 2, 0, &regs) != 2)
  161. result |= 8;
  162. memset (&regex, 0, sizeof regex);
  163. s = re_compile_pattern ("x", 1, &regex);
  164. if (s)
  165. result |= 8;
  166. /* glibc-2.2.93 does not work with a negative RANGE argument. */
  167. else if (re_search (&regex, "wxy", 3, 2, -2, &regs) != 1)
  168. result |= 8;
  169. /* The version of regex.c in older versions of gnulib
  170. ignored RE_ICASE. Detect that problem too. */
  171. re_set_syntax (RE_SYNTAX_EMACS | RE_ICASE);
  172. memset (&regex, 0, sizeof regex);
  173. s = re_compile_pattern ("x", 1, &regex);
  174. if (s)
  175. result |= 16;
  176. else if (re_search (&regex, "WXY", 3, 0, 3, &regs) < 0)
  177. result |= 16;
  178. /* Catch a bug reported by Vin Shelton in
  179. http://lists.gnu.org/archive/html/bug-coreutils/2007-06/msg00089.html
  180. */
  181. re_set_syntax (RE_SYNTAX_POSIX_BASIC
  182. & ~RE_CONTEXT_INVALID_DUP
  183. & ~RE_NO_EMPTY_RANGES);
  184. memset (&regex, 0, sizeof regex);
  185. s = re_compile_pattern ("[[:alnum:]_-]\\\\+$", 16, &regex);
  186. if (s)
  187. result |= 32;
  188. /* REG_STARTEND was added to glibc on 2004-01-15.
  189. Reject older versions. */
  190. if (! REG_STARTEND)
  191. result |= 64;
  192. #if 0
  193. /* It would be nice to reject hosts whose regoff_t values are too
  194. narrow (including glibc on hosts with 64-bit ptrdiff_t and
  195. 32-bit int), but we should wait until glibc implements this
  196. feature. Otherwise, support for equivalence classes and
  197. multibyte collation symbols would always be broken except
  198. when compiling --without-included-regex. */
  199. if (sizeof (regoff_t) < sizeof (ptrdiff_t)
  200. || sizeof (regoff_t) < sizeof (ssize_t))
  201. result |= 64;
  202. #endif
  203. return result;
  204. ]])],
  205. [gl_cv_func_re_compile_pattern_working=yes],
  206. [gl_cv_func_re_compile_pattern_working=no],
  207. dnl When crosscompiling, assume it is not working.
  208. [gl_cv_func_re_compile_pattern_working=no])])
  209. case $gl_cv_func_re_compile_pattern_working in #(
  210. yes) ac_use_included_regex=no;; #(
  211. no) ac_use_included_regex=yes;;
  212. esac
  213. ;;
  214. *) AC_MSG_ERROR([Invalid value for --with-included-regex: $with_included_regex])
  215. ;;
  216. esac
  217. if test $ac_use_included_regex = yes; then
  218. AC_DEFINE([_REGEX_INCLUDE_LIMITS_H], [1],
  219. [Define if you want <regex.h> to include <limits.h>, so that it
  220. consistently overrides <limits.h>'s RE_DUP_MAX.])
  221. AC_DEFINE([_REGEX_LARGE_OFFSETS], [1],
  222. [Define if you want regoff_t to be at least as wide POSIX requires.])
  223. AC_DEFINE([re_syntax_options], [rpl_re_syntax_options],
  224. [Define to rpl_re_syntax_options if the replacement should be used.])
  225. AC_DEFINE([re_set_syntax], [rpl_re_set_syntax],
  226. [Define to rpl_re_set_syntax if the replacement should be used.])
  227. AC_DEFINE([re_compile_pattern], [rpl_re_compile_pattern],
  228. [Define to rpl_re_compile_pattern if the replacement should be used.])
  229. AC_DEFINE([re_compile_fastmap], [rpl_re_compile_fastmap],
  230. [Define to rpl_re_compile_fastmap if the replacement should be used.])
  231. AC_DEFINE([re_search], [rpl_re_search],
  232. [Define to rpl_re_search if the replacement should be used.])
  233. AC_DEFINE([re_search_2], [rpl_re_search_2],
  234. [Define to rpl_re_search_2 if the replacement should be used.])
  235. AC_DEFINE([re_match], [rpl_re_match],
  236. [Define to rpl_re_match if the replacement should be used.])
  237. AC_DEFINE([re_match_2], [rpl_re_match_2],
  238. [Define to rpl_re_match_2 if the replacement should be used.])
  239. AC_DEFINE([re_set_registers], [rpl_re_set_registers],
  240. [Define to rpl_re_set_registers if the replacement should be used.])
  241. AC_DEFINE([re_comp], [rpl_re_comp],
  242. [Define to rpl_re_comp if the replacement should be used.])
  243. AC_DEFINE([re_exec], [rpl_re_exec],
  244. [Define to rpl_re_exec if the replacement should be used.])
  245. AC_DEFINE([regcomp], [rpl_regcomp],
  246. [Define to rpl_regcomp if the replacement should be used.])
  247. AC_DEFINE([regexec], [rpl_regexec],
  248. [Define to rpl_regexec if the replacement should be used.])
  249. AC_DEFINE([regerror], [rpl_regerror],
  250. [Define to rpl_regerror if the replacement should be used.])
  251. AC_DEFINE([regfree], [rpl_regfree],
  252. [Define to rpl_regfree if the replacement should be used.])
  253. fi
  254. ])
  255. # Prerequisites of lib/regex.c and lib/regex_internal.c.
  256. AC_DEFUN([gl_PREREQ_REGEX],
  257. [
  258. AC_REQUIRE([AC_USE_SYSTEM_EXTENSIONS])
  259. AC_REQUIRE([AC_C_INLINE])
  260. AC_REQUIRE([AC_C_RESTRICT])
  261. AC_REQUIRE([AC_TYPE_MBSTATE_T])
  262. AC_REQUIRE([gl_EEMALLOC])
  263. AC_REQUIRE([gl_GLIBC21])
  264. AC_CHECK_HEADERS([libintl.h])
  265. AC_CHECK_FUNCS_ONCE([isblank iswctype])
  266. AC_CHECK_DECLS([isblank], [], [], [[#include <ctype.h>]])
  267. ])