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