regex-posix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2006, 2007, 2010, 2011, 2012 Free Software Foundation, Inc.
  2. *
  3. * This library is free software; you can redistribute it and/or
  4. * modify it under the terms of the GNU Lesser General Public License
  5. * as published by the Free Software Foundation; either version 3 of
  6. * the License, or (at your option) any later version.
  7. *
  8. * This library is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * Lesser General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU Lesser General Public
  14. * License along with this library; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301 USA
  17. */
  18. /* regex-posix.c -- POSIX regular expression support.
  19. This code was written against Henry Spencer's famous regex package.
  20. The principal reference for POSIX behavior was the man page for this
  21. library, not the 1003.2 document itself. Ergo, other `POSIX'
  22. libraries which do not agree with the Spencer implementation may
  23. produce varying behavior. Sigh. */
  24. #ifdef HAVE_CONFIG_H
  25. # include <config.h>
  26. #endif
  27. #include <sys/types.h>
  28. #include "libguile/_scm.h"
  29. #include <regex.h>
  30. #ifdef HAVE_WCHAR_H
  31. #include <wchar.h>
  32. #endif
  33. #include "libguile/async.h"
  34. #include "libguile/smob.h"
  35. #include "libguile/symbols.h"
  36. #include "libguile/vectors.h"
  37. #include "libguile/strports.h"
  38. #include "libguile/ports.h"
  39. #include "libguile/feature.h"
  40. #include "libguile/strings.h"
  41. #include "libguile/validate.h"
  42. #include "libguile/regex-posix.h"
  43. /* This is defined by some regex libraries and omitted by others. */
  44. #ifndef REG_BASIC
  45. #define REG_BASIC 0
  46. #endif
  47. scm_t_bits scm_tc16_regex;
  48. static size_t
  49. regex_free (SCM obj)
  50. {
  51. regfree (SCM_RGX (obj));
  52. scm_gc_free (SCM_RGX (obj), sizeof(regex_t), "regex");
  53. return 0;
  54. }
  55. SCM_SYMBOL (scm_regexp_error_key, "regular-expression-syntax");
  56. static SCM
  57. scm_regexp_error_msg (int regerrno, regex_t *rx)
  58. {
  59. char *errmsg;
  60. int l;
  61. errmsg = scm_malloc (80);
  62. l = regerror (regerrno, rx, errmsg, 80);
  63. if (l > 80)
  64. {
  65. free (errmsg);
  66. errmsg = scm_malloc (l);
  67. regerror (regerrno, rx, errmsg, l);
  68. }
  69. return scm_take_locale_string (errmsg);
  70. }
  71. SCM_DEFINE (scm_regexp_p, "regexp?", 1, 0, 0,
  72. (SCM obj),
  73. "Return @code{#t} if @var{obj} is a compiled regular expression,\n"
  74. "or @code{#f} otherwise.")
  75. #define FUNC_NAME s_scm_regexp_p
  76. {
  77. return scm_from_bool(SCM_RGXP (obj));
  78. }
  79. #undef FUNC_NAME
  80. SCM_DEFINE (scm_make_regexp, "make-regexp", 1, 0, 1,
  81. (SCM pat, SCM flags),
  82. "Compile the regular expression described by @var{pat}, and\n"
  83. "return the compiled regexp structure. If @var{pat} does not\n"
  84. "describe a legal regular expression, @code{make-regexp} throws\n"
  85. "a @code{regular-expression-syntax} error.\n"
  86. "\n"
  87. "The @var{flags} arguments change the behavior of the compiled\n"
  88. "regular expression. The following flags may be supplied:\n"
  89. "\n"
  90. "@table @code\n"
  91. "@item regexp/icase\n"
  92. "Consider uppercase and lowercase letters to be the same when\n"
  93. "matching.\n"
  94. "@item regexp/newline\n"
  95. "If a newline appears in the target string, then permit the\n"
  96. "@samp{^} and @samp{$} operators to match immediately after or\n"
  97. "immediately before the newline, respectively. Also, the\n"
  98. "@samp{.} and @samp{[^...]} operators will never match a newline\n"
  99. "character. The intent of this flag is to treat the target\n"
  100. "string as a buffer containing many lines of text, and the\n"
  101. "regular expression as a pattern that may match a single one of\n"
  102. "those lines.\n"
  103. "@item regexp/basic\n"
  104. "Compile a basic (``obsolete'') regexp instead of the extended\n"
  105. "(``modern'') regexps that are the default. Basic regexps do\n"
  106. "not consider @samp{|}, @samp{+} or @samp{?} to be special\n"
  107. "characters, and require the @samp{@{...@}} and @samp{(...)}\n"
  108. "metacharacters to be backslash-escaped (@pxref{Backslash\n"
  109. "Escapes}). There are several other differences between basic\n"
  110. "and extended regular expressions, but these are the most\n"
  111. "significant.\n"
  112. "@item regexp/extended\n"
  113. "Compile an extended regular expression rather than a basic\n"
  114. "regexp. This is the default behavior; this flag will not\n"
  115. "usually be needed. If a call to @code{make-regexp} includes\n"
  116. "both @code{regexp/basic} and @code{regexp/extended} flags, the\n"
  117. "one which comes last will override the earlier one.\n"
  118. "@end table")
  119. #define FUNC_NAME s_scm_make_regexp
  120. {
  121. SCM flag;
  122. regex_t *rx;
  123. int status, cflags;
  124. char *c_pat;
  125. SCM_VALIDATE_STRING (1, pat);
  126. SCM_VALIDATE_REST_ARGUMENT (flags);
  127. /* Examine list of regexp flags. If REG_BASIC is supplied, then
  128. turn off REG_EXTENDED flag (on by default). */
  129. cflags = REG_EXTENDED;
  130. flag = flags;
  131. while (!scm_is_null (flag))
  132. {
  133. if (scm_to_int (SCM_CAR (flag)) == REG_BASIC)
  134. cflags &= ~REG_EXTENDED;
  135. else
  136. cflags |= scm_to_int (SCM_CAR (flag));
  137. flag = SCM_CDR (flag);
  138. }
  139. rx = scm_gc_malloc_pointerless (sizeof (regex_t), "regex");
  140. c_pat = scm_to_locale_string (pat);
  141. status = regcomp (rx, c_pat,
  142. /* Make sure they're not passing REG_NOSUB;
  143. regexp-exec assumes we're getting match data. */
  144. cflags & ~REG_NOSUB);
  145. free (c_pat);
  146. if (status != 0)
  147. {
  148. SCM errmsg = scm_regexp_error_msg (status, rx);
  149. scm_gc_free (rx, sizeof(regex_t), "regex");
  150. scm_error_scm (scm_regexp_error_key,
  151. scm_from_locale_string (FUNC_NAME),
  152. errmsg,
  153. SCM_BOOL_F,
  154. scm_list_1 (pat));
  155. /* never returns */
  156. }
  157. SCM_RETURN_NEWSMOB (scm_tc16_regex, rx);
  158. }
  159. #undef FUNC_NAME
  160. #ifdef HAVE_WCHAR_H
  161. /*
  162. * While regexec does respect the current locale, it returns byte
  163. * offsets instead of character offsets. This routine fixes up the
  164. * regmatch_t structures to refer to characters instead. See "Converting
  165. * a Character" in the libc manual, for more details.
  166. */
  167. static void
  168. fixup_multibyte_match (regmatch_t *matches, int nmatches, char *str)
  169. {
  170. mbstate_t state;
  171. int i;
  172. size_t char_idx, byte_idx;
  173. size_t nbytes = 1; /* just to kick off the for loop */
  174. memset (&state, '\0', sizeof (state));
  175. for (char_idx = byte_idx = 0; nbytes > 0; char_idx++, byte_idx += nbytes)
  176. {
  177. for (i = 0; i < nmatches; ++i)
  178. {
  179. if (matches[i].rm_so == byte_idx)
  180. matches[i].rm_so = char_idx;
  181. if (matches[i].rm_eo == byte_idx)
  182. matches[i].rm_eo = char_idx;
  183. }
  184. nbytes = mbrlen (str + byte_idx, MB_LEN_MAX, &state);
  185. if (nbytes == (size_t) -2 || nbytes == (size_t) -1)
  186. /* Something is wrong. Shouldn't be possible, as the regex match
  187. succeeded. */
  188. abort ();
  189. }
  190. }
  191. #endif
  192. SCM_DEFINE (scm_regexp_exec, "regexp-exec", 2, 2, 0,
  193. (SCM rx, SCM str, SCM start, SCM flags),
  194. "Match the compiled regular expression @var{rx} against\n"
  195. "@code{str}. If the optional integer @var{start} argument is\n"
  196. "provided, begin matching from that position in the string.\n"
  197. "Return a match structure describing the results of the match,\n"
  198. "or @code{#f} if no match could be found.\n"
  199. "\n"
  200. "The @var{flags} arguments change the matching behavior.\n"
  201. "The following flags may be supplied:\n"
  202. "\n"
  203. "@table @code\n"
  204. "@item regexp/notbol\n"
  205. "Operator @samp{^} always fails (unless @code{regexp/newline}\n"
  206. "is used). Use this when the beginning of the string should\n"
  207. "not be considered the beginning of a line.\n"
  208. "@item regexp/noteol\n"
  209. "Operator @samp{$} always fails (unless @code{regexp/newline}\n"
  210. "is used). Use this when the end of the string should not be\n"
  211. "considered the end of a line.\n"
  212. "@end table")
  213. #define FUNC_NAME s_scm_regexp_exec
  214. {
  215. /* We used to have an SCM_DEFER_INTS, and then later an
  216. SCM_CRITICAL_SECTION_START, around the regexec() call. Can't quite
  217. remember what defer ints was for, but a critical section would only be
  218. wanted now if we think regexec() is not thread-safe. The posix spec
  219. http://www.opengroup.org/onlinepubs/009695399/functions/regcomp.html
  220. reads like regexec is meant to be both thread safe and reentrant
  221. (mentioning simultaneous use in threads, and in signal handlers). So
  222. for now believe no protection needed. */
  223. int status, nmatches, offset;
  224. regmatch_t *matches;
  225. char *c_str;
  226. SCM mvec = SCM_BOOL_F;
  227. SCM substr;
  228. SCM_VALIDATE_RGXP (1, rx);
  229. SCM_VALIDATE_STRING (2, str);
  230. if (SCM_UNBNDP (start))
  231. {
  232. substr = str;
  233. offset = 0;
  234. }
  235. else
  236. {
  237. substr = scm_substring (str, start, SCM_UNDEFINED);
  238. offset = scm_to_int (start);
  239. }
  240. if (SCM_UNBNDP (flags))
  241. flags = SCM_INUM0;
  242. /* re_nsub doesn't account for the `subexpression' representing the
  243. whole regexp, so add 1 to nmatches. */
  244. c_str = scm_to_locale_string (substr);
  245. nmatches = SCM_RGX(rx)->re_nsub + 1;
  246. matches = scm_malloc (sizeof (regmatch_t) * nmatches);
  247. status = regexec (SCM_RGX (rx), c_str, nmatches, matches,
  248. scm_to_int (flags));
  249. #ifdef HAVE_WCHAR_H
  250. if (!status)
  251. fixup_multibyte_match (matches, nmatches, c_str);
  252. #endif
  253. free (c_str);
  254. if (!status)
  255. {
  256. int i;
  257. /* The match vector must include a cell for the string that was matched,
  258. so add 1. */
  259. mvec = scm_c_make_vector (nmatches + 1, SCM_UNSPECIFIED);
  260. SCM_SIMPLE_VECTOR_SET(mvec,0, str);
  261. for (i = 0; i < nmatches; ++i)
  262. if (matches[i].rm_so == -1)
  263. SCM_SIMPLE_VECTOR_SET(mvec, i+1,
  264. scm_cons (scm_from_int (-1), scm_from_int (-1)));
  265. else
  266. SCM_SIMPLE_VECTOR_SET(mvec, i+1,
  267. scm_cons (scm_from_long (matches[i].rm_so + offset),
  268. scm_from_long (matches[i].rm_eo + offset)));
  269. }
  270. free (matches);
  271. if (status != 0 && status != REG_NOMATCH)
  272. scm_error_scm (scm_regexp_error_key,
  273. scm_from_locale_string (FUNC_NAME),
  274. scm_regexp_error_msg (status, SCM_RGX (rx)),
  275. SCM_BOOL_F, SCM_BOOL_F);
  276. return mvec;
  277. }
  278. #undef FUNC_NAME
  279. void
  280. scm_init_regex_posix ()
  281. {
  282. scm_tc16_regex = scm_make_smob_type ("regexp", sizeof (regex_t));
  283. scm_set_smob_free (scm_tc16_regex, regex_free);
  284. /* Compilation flags. */
  285. scm_c_define ("regexp/basic", scm_from_int (REG_BASIC));
  286. scm_c_define ("regexp/extended", scm_from_int (REG_EXTENDED));
  287. scm_c_define ("regexp/icase", scm_from_int (REG_ICASE));
  288. scm_c_define ("regexp/newline", scm_from_int (REG_NEWLINE));
  289. /* Execution flags. */
  290. scm_c_define ("regexp/notbol", scm_from_int (REG_NOTBOL));
  291. scm_c_define ("regexp/noteol", scm_from_int (REG_NOTEOL));
  292. #include "libguile/regex-posix.x"
  293. scm_add_feature ("regex");
  294. }
  295. /*
  296. Local Variables:
  297. c-file-style: "gnu"
  298. End:
  299. */