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