1 /* Copyright (C) 1997, 1998, 1999, 2000, 2001, 2004, 2006, 2007, 2010 Free Software Foundation, Inc.
3 * This library is free software; you can redistribute it and/or
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.
8 * This library is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * Lesser General Public License for more details.
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., 51 Franklin Street, Fifth Floor, Boston, MA
22 /* regex-posix.c -- POSIX regular expression support.
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. */
34 #include <sys/types.h>
36 #include "libguile/_scm.h"
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. */
47 #include <rxposix.h> /* GNU Rx library */
49 #ifdef HAVE_RX_RXPOSIX_H
50 #include <rx/rxposix.h> /* GNU Rx library on Linux */
60 #include "libguile/async.h"
61 #include "libguile/smob.h"
62 #include "libguile/symbols.h"
63 #include "libguile/vectors.h"
64 #include "libguile/strports.h"
65 #include "libguile/ports.h"
66 #include "libguile/feature.h"
67 #include "libguile/strings.h"
69 #include "libguile/validate.h"
70 #include "libguile/regex-posix.h"
72 /* This is defined by some regex libraries and omitted by others. */
77 scm_t_bits scm_tc16_regex
;
82 regfree (SCM_RGX (obj
));
83 scm_gc_free (SCM_RGX (obj
), sizeof(regex_t
), "regex");
89 SCM_SYMBOL (scm_regexp_error_key
, "regular-expression-syntax");
92 scm_regexp_error_msg (int regerrno
, regex_t
*rx
)
97 errmsg
= scm_malloc (80);
98 l
= regerror (regerrno
, rx
, errmsg
, 80);
102 errmsg
= scm_malloc (l
);
103 regerror (regerrno
, rx
, errmsg
, l
);
105 return scm_take_locale_string (errmsg
);
108 SCM_DEFINE (scm_regexp_p
, "regexp?", 1, 0, 0,
110 "Return @code{#t} if @var{obj} is a compiled regular expression,\n"
111 "or @code{#f} otherwise.")
112 #define FUNC_NAME s_scm_regexp_p
114 return scm_from_bool(SCM_RGXP (obj
));
118 SCM_DEFINE (scm_make_regexp
, "make-regexp", 1, 0, 1,
119 (SCM pat
, SCM flags
),
120 "Compile the regular expression described by @var{pat}, and\n"
121 "return the compiled regexp structure. If @var{pat} does not\n"
122 "describe a legal regular expression, @code{make-regexp} throws\n"
123 "a @code{regular-expression-syntax} error.\n"
125 "The @var{flags} arguments change the behavior of the compiled\n"
126 "regular expression. The following flags may be supplied:\n"
129 "@item regexp/icase\n"
130 "Consider uppercase and lowercase letters to be the same when\n"
132 "@item regexp/newline\n"
133 "If a newline appears in the target string, then permit the\n"
134 "@samp{^} and @samp{$} operators to match immediately after or\n"
135 "immediately before the newline, respectively. Also, the\n"
136 "@samp{.} and @samp{[^...]} operators will never match a newline\n"
137 "character. The intent of this flag is to treat the target\n"
138 "string as a buffer containing many lines of text, and the\n"
139 "regular expression as a pattern that may match a single one of\n"
141 "@item regexp/basic\n"
142 "Compile a basic (``obsolete'') regexp instead of the extended\n"
143 "(``modern'') regexps that are the default. Basic regexps do\n"
144 "not consider @samp{|}, @samp{+} or @samp{?} to be special\n"
145 "characters, and require the @samp{@{...@}} and @samp{(...)}\n"
146 "metacharacters to be backslash-escaped (@pxref{Backslash\n"
147 "Escapes}). There are several other differences between basic\n"
148 "and extended regular expressions, but these are the most\n"
150 "@item regexp/extended\n"
151 "Compile an extended regular expression rather than a basic\n"
152 "regexp. This is the default behavior; this flag will not\n"
153 "usually be needed. If a call to @code{make-regexp} includes\n"
154 "both @code{regexp/basic} and @code{regexp/extended} flags, the\n"
155 "one which comes last will override the earlier one.\n"
157 #define FUNC_NAME s_scm_make_regexp
164 SCM_VALIDATE_STRING (1, pat
);
165 SCM_VALIDATE_REST_ARGUMENT (flags
);
167 /* Examine list of regexp flags. If REG_BASIC is supplied, then
168 turn off REG_EXTENDED flag (on by default). */
169 cflags
= REG_EXTENDED
;
171 while (!scm_is_null (flag
))
173 if (scm_to_int (SCM_CAR (flag
)) == REG_BASIC
)
174 cflags
&= ~REG_EXTENDED
;
176 cflags
|= scm_to_int (SCM_CAR (flag
));
177 flag
= SCM_CDR (flag
);
180 rx
= scm_gc_malloc_pointerless (sizeof (regex_t
), "regex");
181 c_pat
= scm_to_locale_string (pat
);
182 status
= regcomp (rx
, c_pat
,
183 /* Make sure they're not passing REG_NOSUB;
184 regexp-exec assumes we're getting match data. */
185 cflags
& ~REG_NOSUB
);
189 SCM errmsg
= scm_regexp_error_msg (status
, rx
);
190 scm_gc_free (rx
, sizeof(regex_t
), "regex");
191 scm_error_scm (scm_regexp_error_key
,
192 scm_from_locale_string (FUNC_NAME
),
199 SCM_RETURN_NEWSMOB (scm_tc16_regex
, rx
);
205 * While regexec does respect the current locale, it returns byte
206 * offsets instead of character offsets. This routine fixes up the
207 * regmatch_t structures to refer to characters instead. See "Converting
208 * a Character" in the libc manual, for more details.
211 fixup_multibyte_match (regmatch_t
*matches
, int nmatches
, char *str
)
215 size_t char_idx
, byte_idx
;
216 size_t nbytes
= 1; /* just to kick off the for loop */
218 memset (&state
, '\0', sizeof (state
));
220 for (char_idx
= byte_idx
= 0; nbytes
> 0; char_idx
++, byte_idx
+= nbytes
)
222 for (i
= 0; i
< nmatches
; ++i
)
224 if (matches
[i
].rm_so
== byte_idx
)
225 matches
[i
].rm_so
= char_idx
;
226 if (matches
[i
].rm_eo
== byte_idx
)
227 matches
[i
].rm_eo
= char_idx
;
230 nbytes
= mbrlen (str
+ byte_idx
, MB_LEN_MAX
, &state
);
233 if (nbytes
>= (size_t) -2)
234 /* Something is wrong. Shouldn't be possible, as the regex match
240 SCM_DEFINE (scm_regexp_exec
, "regexp-exec", 2, 2, 0,
241 (SCM rx
, SCM str
, SCM start
, SCM flags
),
242 "Match the compiled regular expression @var{rx} against\n"
243 "@code{str}. If the optional integer @var{start} argument is\n"
244 "provided, begin matching from that position in the string.\n"
245 "Return a match structure describing the results of the match,\n"
246 "or @code{#f} if no match could be found.\n"
248 "The @var{flags} arguments change the matching behavior.\n"
249 "The following flags may be supplied:\n"
252 "@item regexp/notbol\n"
253 "Operator @samp{^} always fails (unless @code{regexp/newline}\n"
254 "is used). Use this when the beginning of the string should\n"
255 "not be considered the beginning of a line.\n"
256 "@item regexp/noteol\n"
257 "Operator @samp{$} always fails (unless @code{regexp/newline}\n"
258 "is used). Use this when the end of the string should not be\n"
259 "considered the end of a line.\n"
261 #define FUNC_NAME s_scm_regexp_exec
263 /* We used to have an SCM_DEFER_INTS, and then later an
264 SCM_CRITICAL_SECTION_START, around the regexec() call. Can't quite
265 remember what defer ints was for, but a critical section would only be
266 wanted now if we think regexec() is not thread-safe. The posix spec
268 http://www.opengroup.org/onlinepubs/009695399/functions/regcomp.html
270 reads like regexec is meant to be both thread safe and reentrant
271 (mentioning simultaneous use in threads, and in signal handlers). So
272 for now believe no protection needed. */
274 int status
, nmatches
, offset
;
277 SCM mvec
= SCM_BOOL_F
;
280 SCM_VALIDATE_RGXP (1, rx
);
281 SCM_VALIDATE_STRING (2, str
);
283 if (SCM_UNBNDP (start
))
290 substr
= scm_substring (str
, start
, SCM_UNDEFINED
);
291 offset
= scm_to_int (start
);
294 if (SCM_UNBNDP (flags
))
297 /* re_nsub doesn't account for the `subexpression' representing the
298 whole regexp, so add 1 to nmatches. */
300 c_str
= scm_to_locale_string (substr
);
302 nmatches
= SCM_RGX(rx
)->re_nsub
+ 1;
303 matches
= scm_malloc (sizeof (regmatch_t
) * nmatches
);
304 status
= regexec (SCM_RGX (rx
), c_str
, nmatches
, matches
,
309 fixup_multibyte_match (matches
, nmatches
, c_str
);
317 /* The match vector must include a cell for the string that was matched,
319 mvec
= scm_c_make_vector (nmatches
+ 1, SCM_UNSPECIFIED
);
320 SCM_SIMPLE_VECTOR_SET(mvec
,0, str
);
321 for (i
= 0; i
< nmatches
; ++i
)
322 if (matches
[i
].rm_so
== -1)
323 SCM_SIMPLE_VECTOR_SET(mvec
, i
+1,
324 scm_cons (scm_from_int (-1), scm_from_int (-1)));
326 SCM_SIMPLE_VECTOR_SET(mvec
, i
+1,
327 scm_cons (scm_from_long (matches
[i
].rm_so
+ offset
),
328 scm_from_long (matches
[i
].rm_eo
+ offset
)));
332 if (status
!= 0 && status
!= REG_NOMATCH
)
333 scm_error_scm (scm_regexp_error_key
,
334 scm_from_locale_string (FUNC_NAME
),
335 scm_regexp_error_msg (status
, SCM_RGX (rx
)),
336 SCM_BOOL_F
, SCM_BOOL_F
);
342 scm_init_regex_posix ()
344 scm_tc16_regex
= scm_make_smob_type ("regexp", sizeof (regex_t
));
345 scm_set_smob_free (scm_tc16_regex
, regex_free
);
347 /* Compilation flags. */
348 scm_c_define ("regexp/basic", scm_from_int (REG_BASIC
));
349 scm_c_define ("regexp/extended", scm_from_int (REG_EXTENDED
));
350 scm_c_define ("regexp/icase", scm_from_int (REG_ICASE
));
351 scm_c_define ("regexp/newline", scm_from_int (REG_NEWLINE
));
353 /* Execution flags. */
354 scm_c_define ("regexp/notbol", scm_from_int (REG_NOTBOL
));
355 scm_c_define ("regexp/noteol", scm_from_int (REG_NOTEOL
));
357 #include "libguile/regex-posix.x"
359 scm_add_feature ("regex");