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