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