PageRenderTime 30ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/m4/regex.m4

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