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