2
* Copyright (c) 2002 by The XFree86 Project, Inc.
4
* Permission is hereby granted, free of charge, to any person obtaining a
5
* copy of this software and associated documentation files (the "Software"),
6
* to deal in the Software without restriction, including without limitation
7
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
8
* and/or sell copies of the Software, and to permit persons to whom the
9
* Software is furnished to do so, subject to the following conditions:
11
* The above copyright notice and this permission notice shall be included in
12
* all copies or substantial portions of the Software.
14
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17
* THE XFREE86 PROJECT BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
18
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
19
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
* Except as contained in this notice, the name of the XFree86 Project shall
23
* not be used in advertising or otherwise to promote the sale, use or other
24
* dealings in this Software without prior written authorization from the
27
* Author: Paulo César Pereira de Andrade
30
/* $XFree86: xc/programs/xedit/lisp/regex.c,v 1.10tsi Exp $ */
32
#include "lisp/regex.h"
33
#include "lisp/private.h"
34
#include "lisp/helper.h"
39
static re_cod *LispRecomp(LispBuiltin*, char*, int);
50
LispRecomp(LispBuiltin *builtin, char *pattern, int cflags)
53
re_cod *regex = LispMalloc(sizeof(re_cod));
55
if ((code = recomp(regex, pattern, cflags)) != 0) {
58
reerror(code, regex, buffer, sizeof(buffer));
61
LispDestroy("%s: recomp(\"%s\"): %s", STRFUN(builtin), pattern, buffer);
70
Knomatch = KEYWORD("NOMATCH");
74
Lisp_Recomp(LispBuiltin *builtin)
76
re-comp pattern &key nospec icase nosub newline
84
LispObj *pattern, *nospec, *icase, *nosub, *newline;
86
newline = ARGUMENT(4);
90
pattern = ARGUMENT(0);
92
/* Don't generate an error if it is already a compiled regex. */
96
CHECK_STRING(pattern);
98
if (nospec != UNSPEC && nospec != NIL)
100
if (icase != UNSPEC && icase != NIL)
102
if (nosub != UNSPEC && nosub != NIL)
104
if (newline != UNSPEC && newline != NIL)
105
cflags |= RE_NEWLINE;
107
regex = LispRecomp(builtin, THESTR(pattern), cflags);
108
result = LispNew(pattern, NIL);
109
result->type = LispRegex_t;
110
result->data.regex.regex = regex;
111
result->data.regex.pattern = pattern;
112
result->data.regex.options = cflags;
119
Lisp_Reexec(LispBuiltin *builtin)
121
re-exec regex string &key count start end notbol noteol
126
long start, end, length;
127
int code, cflags, eflags;
132
LispObj *regex, *ostring, *count, *ostart, *oend, *notbol, *noteol;
134
noteol = ARGUMENT(6);
135
notbol = ARGUMENT(5);
137
ostart = ARGUMENT(3);
139
ostring = ARGUMENT(1);
143
regexp = LispRecomp(builtin, THESTR(regex), cflags = 0);
146
regexp = regex->data.regex.regex;
147
cflags = regex->data.regex.options;
150
CHECK_STRING(ostring);
156
nmatch = FIXNUM_VALUE(count);
158
LispDestroy("%s: COUNT cannot be larger than 10", STRFUN(builtin));
160
if (nmatch && (cflags & RE_NOSUB))
163
eflags = RE_STARTEND;
164
if (notbol != UNSPEC && notbol != NIL)
166
if (noteol != UNSPEC && noteol != NIL)
169
string = THESTR(ostring);
170
LispCheckSequenceStartEnd(builtin, ostring, ostart, oend,
171
&start, &end, &length);
173
match[0].rm_so = start;
174
match[0].rm_eo = end;
175
code = reexec(regexp, string, nmatch, &match[0], eflags);
178
if (nmatch && match[0].rm_eo >= match[0].rm_so) {
179
result = CONS(CONS(FIXNUM(match[0].rm_so),
180
FIXNUM(match[0].rm_eo)), NIL);
181
if (nmatch > 1 && match[1].rm_eo >= match[1].rm_so) {
184
LispObj *cons = result;
188
i < nmatch && match[i].rm_eo >= match[i].rm_so;
190
RPLACD(cons, CONS(CONS(FIXNUM(match[i].rm_so),
191
FIXNUM(match[i].rm_eo)), NIL));
203
/* Maybe shoud cache compiled regex, but better the caller do it */
204
if (!XREGEXP(regex)) {
213
Lisp_Rep(LispBuiltin *builtin)
220
object = ARGUMENT(0);
222
return (REGEXP(object) ? T : NIL);