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