PageRenderTime 23ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/m4/regex.m4

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