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