regex-posix.c 10 KB

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