Commit | Line | Data |
---|---|---|
eb4a14ed | 1 | /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2006, 2007, 2010, 2011, 2012 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 | |
f255378e | 38 | #include <regex.h> |
24e37377 | 39 | |
644c5165 AW |
40 | #ifdef HAVE_WCHAR_H |
41 | #include <wchar.h> | |
42 | #endif | |
43 | ||
627c72a9 | 44 | #include "libguile/async.h" |
a0599745 MD |
45 | #include "libguile/smob.h" |
46 | #include "libguile/symbols.h" | |
47 | #include "libguile/vectors.h" | |
48 | #include "libguile/strports.h" | |
49 | #include "libguile/ports.h" | |
50 | #include "libguile/feature.h" | |
51 | #include "libguile/strings.h" | |
f255378e | 52 | |
a0599745 MD |
53 | #include "libguile/validate.h" |
54 | #include "libguile/regex-posix.h" | |
f255378e | 55 | |
5d4774bc TP |
56 | /* This is defined by some regex libraries and omitted by others. */ |
57 | #ifndef REG_BASIC | |
58 | #define REG_BASIC 0 | |
59 | #endif | |
60 | ||
92c2555f | 61 | scm_t_bits scm_tc16_regex; |
f255378e | 62 | |
1be6b49c | 63 | static size_t |
e841c3e0 | 64 | regex_free (SCM obj) |
f255378e JB |
65 | { |
66 | regfree (SCM_RGX (obj)); | |
4c9419ac MV |
67 | scm_gc_free (SCM_RGX (obj), sizeof(regex_t), "regex"); |
68 | return 0; | |
f255378e JB |
69 | } |
70 | ||
f255378e JB |
71 | \f |
72 | ||
73 | SCM_SYMBOL (scm_regexp_error_key, "regular-expression-syntax"); | |
74 | ||
cc95e00a | 75 | static SCM |
435220a7 | 76 | scm_regexp_error_msg (int regerrno, regex_t *rx) |
f255378e | 77 | { |
cc95e00a | 78 | char *errmsg; |
f255378e JB |
79 | int l; |
80 | ||
cc95e00a MV |
81 | errmsg = scm_malloc (80); |
82 | l = regerror (regerrno, rx, errmsg, 80); | |
f255378e JB |
83 | if (l > 80) |
84 | { | |
cc95e00a MV |
85 | free (errmsg); |
86 | errmsg = scm_malloc (l); | |
87 | regerror (regerrno, rx, errmsg, l); | |
f255378e | 88 | } |
cc95e00a | 89 | return scm_take_locale_string (errmsg); |
f255378e JB |
90 | } |
91 | ||
94bb46ab | 92 | SCM_DEFINE (scm_regexp_p, "regexp?", 1, 0, 0, |
1e6808ea MG |
93 | (SCM obj), |
94 | "Return @code{#t} if @var{obj} is a compiled regular expression,\n" | |
95 | "or @code{#f} otherwise.") | |
1bbd0b84 | 96 | #define FUNC_NAME s_scm_regexp_p |
f255378e | 97 | { |
7888309b | 98 | return scm_from_bool(SCM_RGXP (obj)); |
f255378e | 99 | } |
1bbd0b84 | 100 | #undef FUNC_NAME |
f255378e | 101 | |
94bb46ab | 102 | SCM_DEFINE (scm_make_regexp, "make-regexp", 1, 0, 1, |
1bbd0b84 | 103 | (SCM pat, SCM flags), |
1e6808ea MG |
104 | "Compile the regular expression described by @var{pat}, and\n" |
105 | "return the compiled regexp structure. If @var{pat} does not\n" | |
106 | "describe a legal regular expression, @code{make-regexp} throws\n" | |
107 | "a @code{regular-expression-syntax} error.\n" | |
108 | "\n" | |
109 | "The @var{flags} arguments change the behavior of the compiled\n" | |
110 | "regular expression. The following flags may be supplied:\n" | |
111 | "\n" | |
b380b885 MD |
112 | "@table @code\n" |
113 | "@item regexp/icase\n" | |
1e6808ea MG |
114 | "Consider uppercase and lowercase letters to be the same when\n" |
115 | "matching.\n" | |
b380b885 | 116 | "@item regexp/newline\n" |
1e6808ea MG |
117 | "If a newline appears in the target string, then permit the\n" |
118 | "@samp{^} and @samp{$} operators to match immediately after or\n" | |
119 | "immediately before the newline, respectively. Also, the\n" | |
120 | "@samp{.} and @samp{[^...]} operators will never match a newline\n" | |
121 | "character. The intent of this flag is to treat the target\n" | |
122 | "string as a buffer containing many lines of text, and the\n" | |
123 | "regular expression as a pattern that may match a single one of\n" | |
124 | "those lines.\n" | |
b380b885 MD |
125 | "@item regexp/basic\n" |
126 | "Compile a basic (``obsolete'') regexp instead of the extended\n" | |
1e6808ea MG |
127 | "(``modern'') regexps that are the default. Basic regexps do\n" |
128 | "not consider @samp{|}, @samp{+} or @samp{?} to be special\n" | |
129 | "characters, and require the @samp{@{...@}} and @samp{(...)}\n" | |
130 | "metacharacters to be backslash-escaped (@pxref{Backslash\n" | |
131 | "Escapes}). There are several other differences between basic\n" | |
132 | "and extended regular expressions, but these are the most\n" | |
133 | "significant.\n" | |
b380b885 | 134 | "@item regexp/extended\n" |
1e6808ea MG |
135 | "Compile an extended regular expression rather than a basic\n" |
136 | "regexp. This is the default behavior; this flag will not\n" | |
137 | "usually be needed. If a call to @code{make-regexp} includes\n" | |
138 | "both @code{regexp/basic} and @code{regexp/extended} flags, the\n" | |
139 | "one which comes last will override the earlier one.\n" | |
140 | "@end table") | |
1bbd0b84 | 141 | #define FUNC_NAME s_scm_make_regexp |
f255378e | 142 | { |
23a62151 | 143 | SCM flag; |
f255378e | 144 | regex_t *rx; |
5d4774bc | 145 | int status, cflags; |
cc95e00a | 146 | char *c_pat; |
f255378e | 147 | |
a6d9e5ab | 148 | SCM_VALIDATE_STRING (1, pat); |
af45e3b0 | 149 | SCM_VALIDATE_REST_ARGUMENT (flags); |
f255378e | 150 | |
5d4774bc TP |
151 | /* Examine list of regexp flags. If REG_BASIC is supplied, then |
152 | turn off REG_EXTENDED flag (on by default). */ | |
153 | cflags = REG_EXTENDED; | |
154 | flag = flags; | |
d2e53ed6 | 155 | while (!scm_is_null (flag)) |
5d4774bc | 156 | { |
e11e83f3 | 157 | if (scm_to_int (SCM_CAR (flag)) == REG_BASIC) |
5d4774bc TP |
158 | cflags &= ~REG_EXTENDED; |
159 | else | |
e11e83f3 | 160 | cflags |= scm_to_int (SCM_CAR (flag)); |
5d4774bc TP |
161 | flag = SCM_CDR (flag); |
162 | } | |
94bb46ab | 163 | |
92d8fd32 | 164 | rx = scm_gc_malloc_pointerless (sizeof (regex_t), "regex"); |
cc95e00a MV |
165 | c_pat = scm_to_locale_string (pat); |
166 | status = regcomp (rx, c_pat, | |
1a222b91 JB |
167 | /* Make sure they're not passing REG_NOSUB; |
168 | regexp-exec assumes we're getting match data. */ | |
5d4774bc | 169 | cflags & ~REG_NOSUB); |
cc95e00a | 170 | free (c_pat); |
f255378e JB |
171 | if (status != 0) |
172 | { | |
cc95e00a | 173 | SCM errmsg = scm_regexp_error_msg (status, rx); |
f8fc9737 | 174 | scm_gc_free (rx, sizeof(regex_t), "regex"); |
cc95e00a MV |
175 | scm_error_scm (scm_regexp_error_key, |
176 | scm_from_locale_string (FUNC_NAME), | |
177 | errmsg, | |
178 | SCM_BOOL_F, | |
c255614e HWN |
179 | scm_list_1 (pat)); |
180 | ||
f255378e JB |
181 | /* never returns */ |
182 | } | |
23a62151 | 183 | SCM_RETURN_NEWSMOB (scm_tc16_regex, rx); |
f255378e | 184 | } |
1bbd0b84 | 185 | #undef FUNC_NAME |
f255378e | 186 | |
644c5165 AW |
187 | #ifdef HAVE_WCHAR_H |
188 | /* | |
189 | * While regexec does respect the current locale, it returns byte | |
190 | * offsets instead of character offsets. This routine fixes up the | |
191 | * regmatch_t structures to refer to characters instead. See "Converting | |
192 | * a Character" in the libc manual, for more details. | |
193 | */ | |
194 | static void | |
195 | fixup_multibyte_match (regmatch_t *matches, int nmatches, char *str) | |
196 | { | |
197 | mbstate_t state; | |
198 | int i; | |
199 | size_t char_idx, byte_idx; | |
200 | size_t nbytes = 1; /* just to kick off the for loop */ | |
201 | ||
202 | memset (&state, '\0', sizeof (state)); | |
203 | ||
204 | for (char_idx = byte_idx = 0; nbytes > 0; char_idx++, byte_idx += nbytes) | |
205 | { | |
206 | for (i = 0; i < nmatches; ++i) | |
207 | { | |
208 | if (matches[i].rm_so == byte_idx) | |
209 | matches[i].rm_so = char_idx; | |
210 | if (matches[i].rm_eo == byte_idx) | |
211 | matches[i].rm_eo = char_idx; | |
212 | } | |
213 | ||
214 | nbytes = mbrlen (str + byte_idx, MB_LEN_MAX, &state); | |
f756cd30 AW |
215 | if (nbytes == (size_t) -2 || nbytes == (size_t) -1) |
216 | /* Something is wrong. Shouldn't be possible, as the regex match | |
217 | succeeded. */ | |
218 | abort (); | |
644c5165 AW |
219 | } |
220 | ||
644c5165 AW |
221 | } |
222 | #endif | |
223 | ||
94bb46ab | 224 | SCM_DEFINE (scm_regexp_exec, "regexp-exec", 2, 2, 0, |
1bbd0b84 | 225 | (SCM rx, SCM str, SCM start, SCM flags), |
1e6808ea MG |
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" | |
94bb46ab TTN |
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") | |
1bbd0b84 | 245 | #define FUNC_NAME s_scm_regexp_exec |
f255378e | 246 | { |
23d72566 KR |
247 | /* We used to have an SCM_DEFER_INTS, and then later an |
248 | SCM_CRITICAL_SECTION_START, around the regexec() call. Can't quite | |
249 | remember what defer ints was for, but a critical section would only be | |
250 | wanted now if we think regexec() is not thread-safe. The posix spec | |
251 | ||
252 | http://www.opengroup.org/onlinepubs/009695399/functions/regcomp.html | |
253 | ||
254 | reads like regexec is meant to be both thread safe and reentrant | |
255 | (mentioning simultaneous use in threads, and in signal handlers). So | |
256 | for now believe no protection needed. */ | |
257 | ||
0b787875 | 258 | int status, nmatches, offset; |
f255378e | 259 | regmatch_t *matches; |
1eff9c2f | 260 | char *c_str; |
f255378e | 261 | SCM mvec = SCM_BOOL_F; |
21682b20 | 262 | SCM substr; |
f255378e | 263 | |
34d19ef6 | 264 | SCM_VALIDATE_RGXP (1, rx); |
a6d9e5ab | 265 | SCM_VALIDATE_STRING (2, str); |
a55c2b68 MV |
266 | |
267 | if (SCM_UNBNDP (start)) | |
21682b20 KR |
268 | { |
269 | substr = str; | |
270 | offset = 0; | |
271 | } | |
a55c2b68 | 272 | else |
1eff9c2f | 273 | { |
21682b20 | 274 | substr = scm_substring (str, start, SCM_UNDEFINED); |
1eff9c2f MV |
275 | offset = scm_to_int (start); |
276 | } | |
a55c2b68 | 277 | |
bd56d016 JB |
278 | if (SCM_UNBNDP (flags)) |
279 | flags = SCM_INUM0; | |
f255378e JB |
280 | |
281 | /* re_nsub doesn't account for the `subexpression' representing the | |
282 | whole regexp, so add 1 to nmatches. */ | |
283 | ||
644c5165 AW |
284 | c_str = scm_to_locale_string (substr); |
285 | ||
f255378e | 286 | nmatches = SCM_RGX(rx)->re_nsub + 1; |
4c9419ac | 287 | matches = scm_malloc (sizeof (regmatch_t) * nmatches); |
1eff9c2f | 288 | status = regexec (SCM_RGX (rx), c_str, nmatches, matches, |
a55c2b68 | 289 | scm_to_int (flags)); |
644c5165 AW |
290 | |
291 | #ifdef HAVE_WCHAR_H | |
292 | if (!status) | |
293 | fixup_multibyte_match (matches, nmatches, c_str); | |
294 | #endif | |
295 | ||
1eff9c2f MV |
296 | free (c_str); |
297 | ||
f255378e JB |
298 | if (!status) |
299 | { | |
300 | int i; | |
301 | /* The match vector must include a cell for the string that was matched, | |
302 | so add 1. */ | |
00ffa0e7 | 303 | mvec = scm_c_make_vector (nmatches + 1, SCM_UNSPECIFIED); |
4057a3e0 | 304 | SCM_SIMPLE_VECTOR_SET(mvec,0, str); |
f255378e | 305 | for (i = 0; i < nmatches; ++i) |
7c7471d9 | 306 | if (matches[i].rm_so == -1) |
4057a3e0 | 307 | SCM_SIMPLE_VECTOR_SET(mvec, i+1, |
b9bd8526 | 308 | scm_cons (scm_from_int (-1), scm_from_int (-1))); |
7c7471d9 | 309 | else |
4057a3e0 | 310 | SCM_SIMPLE_VECTOR_SET(mvec, i+1, |
b9bd8526 MV |
311 | scm_cons (scm_from_long (matches[i].rm_so + offset), |
312 | scm_from_long (matches[i].rm_eo + offset))); | |
f255378e | 313 | } |
4c9419ac | 314 | free (matches); |
f255378e JB |
315 | |
316 | if (status != 0 && status != REG_NOMATCH) | |
cc95e00a MV |
317 | scm_error_scm (scm_regexp_error_key, |
318 | scm_from_locale_string (FUNC_NAME), | |
319 | scm_regexp_error_msg (status, SCM_RGX (rx)), | |
c255614e | 320 | SCM_BOOL_F, SCM_BOOL_F); |
f255378e JB |
321 | return mvec; |
322 | } | |
1bbd0b84 | 323 | #undef FUNC_NAME |
f255378e JB |
324 | |
325 | void | |
326 | scm_init_regex_posix () | |
327 | { | |
e841c3e0 KN |
328 | scm_tc16_regex = scm_make_smob_type ("regexp", sizeof (regex_t)); |
329 | scm_set_smob_free (scm_tc16_regex, regex_free); | |
bd56d016 JB |
330 | |
331 | /* Compilation flags. */ | |
23d72566 KR |
332 | scm_c_define ("regexp/basic", scm_from_int (REG_BASIC)); |
333 | scm_c_define ("regexp/extended", scm_from_int (REG_EXTENDED)); | |
334 | scm_c_define ("regexp/icase", scm_from_int (REG_ICASE)); | |
335 | scm_c_define ("regexp/newline", scm_from_int (REG_NEWLINE)); | |
bd56d016 JB |
336 | |
337 | /* Execution flags. */ | |
23d72566 KR |
338 | scm_c_define ("regexp/notbol", scm_from_int (REG_NOTBOL)); |
339 | scm_c_define ("regexp/noteol", scm_from_int (REG_NOTEOL)); | |
bd56d016 | 340 | |
a0599745 | 341 | #include "libguile/regex-posix.x" |
20044282 JB |
342 | |
343 | scm_add_feature ("regex"); | |
f255378e | 344 | } |
89e00824 ML |
345 | |
346 | /* | |
347 | Local Variables: | |
348 | c-file-style: "gnu" | |
349 | End: | |
350 | */ |