regex-posix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004 Free Software Foundation, Inc.
  2. *
  3. * This program is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2, or (at your option)
  6. * any later version.
  7. *
  8. * This program 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
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; see the file COPYING. If not, write to
  15. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  16. * Boston, MA 02110-1301 USA
  17. *
  18. * As a special exception, the Free Software Foundation gives permission
  19. * for additional uses of the text contained in its release of GUILE.
  20. *
  21. * The exception is that, if you link the GUILE library with other files
  22. * to produce an executable, this does not by itself cause the
  23. * resulting executable to be covered by the GNU General Public License.
  24. * Your use of that executable is in no way restricted on account of
  25. * linking the GUILE library code into it.
  26. *
  27. * This exception does not however invalidate any other reasons why
  28. * the executable file might be covered by the GNU General Public License.
  29. *
  30. * This exception applies only to the code released by the
  31. * Free Software Foundation under the name GUILE. If you copy
  32. * code from other Free Software Foundation releases into a copy of
  33. * GUILE, as the General Public License permits, the exception does
  34. * not apply to the code that you add in this way. To avoid misleading
  35. * anyone as to the status of such modified files, you must delete
  36. * this exception notice from them.
  37. *
  38. * If you write modifications of your own for GUILE, it is your choice
  39. * whether to permit this exception to apply to your modifications.
  40. * If you do not wish that, delete this exception notice.
  41. */
  42. /* regex-posix.c -- POSIX regular expression support.
  43. This code was written against Henry Spencer's famous regex package.
  44. The principal reference for POSIX behavior was the man page for this
  45. library, not the 1003.2 document itself. Ergo, other `POSIX'
  46. libraries which do not agree with the Spencer implementation may
  47. produce varying behavior. Sigh. */
  48. #include <sys/types.h>
  49. #include "libguile/_scm.h"
  50. /* Supposedly, this file is never compiled unless we know we have
  51. POSIX regular expressions. But we still put this in an #ifdef so
  52. the file is CPP'able (for dependency scanning) even on systems that
  53. don't have a <regex.h> header. */
  54. #ifdef HAVE_REGCOMP
  55. #ifdef HAVE_REGEX_H
  56. #include <regex.h>
  57. #else
  58. #ifdef HAVE_RXPOSIX_H
  59. #include <rxposix.h> /* GNU Rx library */
  60. #else
  61. #ifdef HAVE_RX_RXPOSIX_H
  62. #include <rx/rxposix.h> /* GNU Rx library on Linux */
  63. #endif
  64. #endif
  65. #endif
  66. #endif
  67. #include "libguile/smob.h"
  68. #include "libguile/symbols.h"
  69. #include "libguile/vectors.h"
  70. #include "libguile/strports.h"
  71. #include "libguile/ports.h"
  72. #include "libguile/feature.h"
  73. #include "libguile/strings.h"
  74. #include "libguile/validate.h"
  75. #include "libguile/regex-posix.h"
  76. /* This is defined by some regex libraries and omitted by others. */
  77. #ifndef REG_BASIC
  78. #define REG_BASIC 0
  79. #endif
  80. scm_t_bits scm_tc16_regex;
  81. static size_t
  82. regex_free (SCM obj)
  83. {
  84. regfree (SCM_RGX (obj));
  85. free (SCM_RGX (obj));
  86. return sizeof(regex_t);
  87. }
  88. SCM_SYMBOL (scm_regexp_error_key, "regular-expression-syntax");
  89. static char *
  90. scm_regexp_error_msg (int regerrno, regex_t *rx)
  91. {
  92. SCM errmsg;
  93. int l;
  94. /* FIXME: must we wrap any external calls in SCM_DEFER_INTS...SCM_ALLOW_INTS?
  95. Or are these only necessary when a SCM object may be left in an
  96. undetermined state (half-formed)? If the latter then I believe we
  97. may do without the critical section code. -twp */
  98. /* We could simply make errmsg a char pointer, and allocate space with
  99. malloc. But since we are about to pass the pointer to scm_error, which
  100. never returns, we would never have the opportunity to free it. Creating
  101. it as a SCM object means that the system will GC it at some point. */
  102. errmsg = scm_make_string (SCM_MAKINUM (80), SCM_UNDEFINED);
  103. SCM_DEFER_INTS;
  104. l = regerror (regerrno, rx, SCM_STRING_CHARS (errmsg), 80);
  105. if (l > 80)
  106. {
  107. errmsg = scm_make_string (SCM_MAKINUM (l), SCM_UNDEFINED);
  108. regerror (regerrno, rx, SCM_STRING_CHARS (errmsg), l);
  109. }
  110. SCM_ALLOW_INTS;
  111. return SCM_STRING_CHARS (errmsg);
  112. }
  113. SCM_DEFINE (scm_regexp_p, "regexp?", 1, 0, 0,
  114. (SCM obj),
  115. "Return @code{#t} if @var{obj} is a compiled regular expression,\n"
  116. "or @code{#f} otherwise.")
  117. #define FUNC_NAME s_scm_regexp_p
  118. {
  119. return SCM_BOOL(SCM_RGXP (obj));
  120. }
  121. #undef FUNC_NAME
  122. SCM_DEFINE (scm_make_regexp, "make-regexp", 1, 0, 1,
  123. (SCM pat, SCM flags),
  124. "Compile the regular expression described by @var{pat}, and\n"
  125. "return the compiled regexp structure. If @var{pat} does not\n"
  126. "describe a legal regular expression, @code{make-regexp} throws\n"
  127. "a @code{regular-expression-syntax} error.\n"
  128. "\n"
  129. "The @var{flags} arguments change the behavior of the compiled\n"
  130. "regular expression. The following flags may be supplied:\n"
  131. "\n"
  132. "@table @code\n"
  133. "@item regexp/icase\n"
  134. "Consider uppercase and lowercase letters to be the same when\n"
  135. "matching.\n"
  136. "@item regexp/newline\n"
  137. "If a newline appears in the target string, then permit the\n"
  138. "@samp{^} and @samp{$} operators to match immediately after or\n"
  139. "immediately before the newline, respectively. Also, the\n"
  140. "@samp{.} and @samp{[^...]} operators will never match a newline\n"
  141. "character. The intent of this flag is to treat the target\n"
  142. "string as a buffer containing many lines of text, and the\n"
  143. "regular expression as a pattern that may match a single one of\n"
  144. "those lines.\n"
  145. "@item regexp/basic\n"
  146. "Compile a basic (``obsolete'') regexp instead of the extended\n"
  147. "(``modern'') regexps that are the default. Basic regexps do\n"
  148. "not consider @samp{|}, @samp{+} or @samp{?} to be special\n"
  149. "characters, and require the @samp{@{...@}} and @samp{(...)}\n"
  150. "metacharacters to be backslash-escaped (@pxref{Backslash\n"
  151. "Escapes}). There are several other differences between basic\n"
  152. "and extended regular expressions, but these are the most\n"
  153. "significant.\n"
  154. "@item regexp/extended\n"
  155. "Compile an extended regular expression rather than a basic\n"
  156. "regexp. This is the default behavior; this flag will not\n"
  157. "usually be needed. If a call to @code{make-regexp} includes\n"
  158. "both @code{regexp/basic} and @code{regexp/extended} flags, the\n"
  159. "one which comes last will override the earlier one.\n"
  160. "@end table")
  161. #define FUNC_NAME s_scm_make_regexp
  162. {
  163. SCM flag;
  164. regex_t *rx;
  165. int status, cflags, argnum;
  166. SCM_VALIDATE_STRING (1, pat);
  167. SCM_VALIDATE_REST_ARGUMENT (flags);
  168. SCM_STRING_COERCE_0TERMINATION_X (pat);
  169. /* Examine list of regexp flags. If REG_BASIC is supplied, then
  170. turn off REG_EXTENDED flag (on by default). */
  171. cflags = REG_EXTENDED;
  172. flag = flags;
  173. argnum = 2;
  174. while (!SCM_NULLP (flag))
  175. {
  176. int f;
  177. SCM_VALIDATE_INT_COPY (argnum, SCM_CAR (flag), f);
  178. if (f == REG_BASIC)
  179. cflags &= ~REG_EXTENDED;
  180. else
  181. cflags |= f;
  182. flag = SCM_CDR (flag);
  183. argnum++;
  184. }
  185. rx = SCM_MUST_MALLOC_TYPE (regex_t);
  186. status = regcomp (rx, SCM_STRING_CHARS (pat),
  187. /* Make sure they're not passing REG_NOSUB;
  188. regexp-exec assumes we're getting match data. */
  189. cflags & ~REG_NOSUB);
  190. if (status != 0)
  191. {
  192. char *errmsg = scm_regexp_error_msg (status, rx);
  193. free (rx);
  194. scm_done_free (sizeof (regex_t));
  195. scm_error (scm_regexp_error_key,
  196. FUNC_NAME,
  197. errmsg,
  198. SCM_BOOL_F,
  199. SCM_BOOL_F);
  200. /* never returns */
  201. }
  202. SCM_RETURN_NEWSMOB (scm_tc16_regex, rx);
  203. }
  204. #undef FUNC_NAME
  205. SCM_DEFINE (scm_regexp_exec, "regexp-exec", 2, 2, 0,
  206. (SCM rx, SCM str, SCM start, SCM flags),
  207. "Match the compiled regular expression @var{rx} against\n"
  208. "@code{str}. If the optional integer @var{start} argument is\n"
  209. "provided, begin matching from that position in the string.\n"
  210. "Return a match structure describing the results of the match,\n"
  211. "or @code{#f} if no match could be found.\n"
  212. "\n"
  213. "The @var{flags} arguments change the matching behavior.\n"
  214. "The following flags may be supplied:\n"
  215. "\n"
  216. "@table @code\n"
  217. "@item regexp/notbol\n"
  218. "Operator @samp{^} always fails (unless @code{regexp/newline}\n"
  219. "is used). Use this when the beginning of the string should\n"
  220. "not be considered the beginning of a line.\n"
  221. "@item regexp/noteol\n"
  222. "Operator @samp{$} always fails (unless @code{regexp/newline}\n"
  223. "is used). Use this when the end of the string should not be\n"
  224. "considered the end of a line.\n"
  225. "@end table")
  226. #define FUNC_NAME s_scm_regexp_exec
  227. {
  228. int status, nmatches, offset;
  229. regmatch_t *matches;
  230. SCM mvec = SCM_BOOL_F;
  231. SCM_VALIDATE_RGXP (1,rx);
  232. SCM_VALIDATE_STRING (2, str);
  233. SCM_VALIDATE_INUM_DEF_COPY (3,start,0,offset);
  234. SCM_ASSERT_RANGE (3,start, offset >= 0 && offset <= SCM_STRING_LENGTH (str));
  235. if (SCM_UNBNDP (flags))
  236. flags = SCM_INUM0;
  237. SCM_VALIDATE_INUM (4,flags);
  238. SCM_STRING_COERCE_0TERMINATION_X (str);
  239. /* re_nsub doesn't account for the `subexpression' representing the
  240. whole regexp, so add 1 to nmatches. */
  241. nmatches = SCM_RGX(rx)->re_nsub + 1;
  242. SCM_DEFER_INTS;
  243. matches = SCM_MUST_MALLOC_TYPE_NUM (regmatch_t,nmatches);
  244. status = regexec (SCM_RGX (rx), SCM_STRING_CHARS (str) + offset,
  245. nmatches, matches,
  246. SCM_INUM (flags));
  247. if (!status)
  248. {
  249. int i;
  250. /* The match vector must include a cell for the string that was matched,
  251. so add 1. */
  252. mvec = scm_c_make_vector (nmatches + 1, SCM_UNSPECIFIED);
  253. SCM_VELTS(mvec)[0] = str;
  254. for (i = 0; i < nmatches; ++i)
  255. if (matches[i].rm_so == -1)
  256. SCM_VELTS(mvec)[i+1] = scm_cons (SCM_MAKINUM (-1), SCM_MAKINUM (-1));
  257. else
  258. SCM_VELTS(mvec)[i+1]
  259. = scm_cons (scm_long2num (matches[i].rm_so + offset),
  260. scm_long2num (matches[i].rm_eo + offset));
  261. }
  262. scm_must_free ((char *) matches);
  263. SCM_ALLOW_INTS;
  264. if (status != 0 && status != REG_NOMATCH)
  265. scm_error (scm_regexp_error_key,
  266. FUNC_NAME,
  267. scm_regexp_error_msg (status, SCM_RGX (rx)),
  268. SCM_BOOL_F,
  269. SCM_BOOL_F);
  270. return mvec;
  271. }
  272. #undef FUNC_NAME
  273. void
  274. scm_init_regex_posix ()
  275. {
  276. scm_tc16_regex = scm_make_smob_type ("regexp", sizeof (regex_t));
  277. scm_set_smob_free (scm_tc16_regex, regex_free);
  278. /* Compilation flags. */
  279. scm_c_define ("regexp/basic", scm_long2num (REG_BASIC));
  280. scm_c_define ("regexp/extended", scm_long2num (REG_EXTENDED));
  281. scm_c_define ("regexp/icase", scm_long2num (REG_ICASE));
  282. scm_c_define ("regexp/newline", scm_long2num (REG_NEWLINE));
  283. /* Execution flags. */
  284. scm_c_define ("regexp/notbol", scm_long2num (REG_NOTBOL));
  285. scm_c_define ("regexp/noteol", scm_long2num (REG_NOTEOL));
  286. #include "libguile/regex-posix.x"
  287. scm_add_feature ("regex");
  288. }
  289. /*
  290. Local Variables:
  291. c-file-style: "gnu"
  292. End:
  293. */