-
Notifications
You must be signed in to change notification settings - Fork 38
/
regexp.c
359 lines (306 loc) · 9.78 KB
/
regexp.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
* regexp.c: Implementation of the EXSLT -- Regular Expressions module
*
* References:
* http://exslt.org/regexp/index.html
*
* See Copyright for the status of this software.
*
* Authors:
* Joel W. Reed <[email protected]>
* Some modification by Kyle Maxwell
*
* TODO:
* functions:
* regexp:match
* regexp:replace
* regexp:test
*/
#include "regexp.h"
static void
exsltRegexpFlagsFromString(const xmlChar* flagstr,
int* global, int* flags)
{
const xmlChar* i = flagstr;
/* defaults */
(*flags) = PCRE_UTF8;
(*global) = 0;
while (*i != '\0')
{
if (*i == 'i') (*flags) |= PCRE_CASELESS;
else if (*i == 'g') (*global)= 1;
/* TODO: support other flags? */
i++;
}
}
static int
exsltRegexpExecute(xmlXPathParserContextPtr ctxt,
const xmlChar* haystack, const xmlChar* regexp,
int flags, int ovector[], int ovector_len)
{
int haystack_len = 0;
pcre *compiled_regexp = NULL;
int rc = 0, erroffset = 0;
const char *error = 0;
compiled_regexp = pcre_compile(regexp, /* the pattern */
flags, /* default options */
&error, /* for error message */
&erroffset, /* for error offset */
NULL); /* use default character tables */
if (compiled_regexp == NULL) {
xsltTransformError (xsltXPathGetTransformContext (ctxt), NULL, NULL,
"exslt:regexp failed to compile %s (char: %d). %s", regexp, erroffset, error);
return -1;
}
haystack_len = xmlUTF8Strlen (haystack);
rc = pcre_exec(compiled_regexp, /* result of pcre_compile() */
NULL, /* we didn't study the pattern */
haystack, /* the subject string */
haystack_len, /* the length of the subject string */
0, /* start at offset 0 in the subject */
0, /* default options */
(int*)ovector, /* vector of integers for substring information */
ovector_len); /* number of elements in the vector (NOT size in bytes) */
if (rc < -1) {
xsltTransformError (xsltXPathGetTransformContext (ctxt), NULL, NULL,
"exslt:regexp failed to execute %s for %s", regexp, haystack);
rc = 0;
}
if (compiled_regexp != NULL)
pcre_free(compiled_regexp);
return rc;
}
/**
* exsltRegexpMatchFunction:
* @ns:
*
* Returns a node set of string matches
*/
static void
exsltRegexpMatchFunction (xmlXPathParserContextPtr ctxt, int nargs)
{
xsltTransformContextPtr tctxt;
xmlNodePtr node;
xmlDocPtr container;
xmlXPathObjectPtr ret = NULL;
xmlChar *haystack, *regexp, *flagstr, *working, *match;
int rc, x, flags, global, ovector[30];
if ((nargs < 1) || (nargs > 3)) {
xmlXPathSetArityError(ctxt);
return;
}
if (nargs > 2) {
flagstr = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (flagstr == NULL)) {
return;
}
} else {
flagstr = xmlStrdup("");
}
regexp = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (regexp == NULL)) {
xmlFree(flagstr);
return;
}
haystack = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (haystack == NULL)) {
xmlFree(regexp);
xmlFree(flagstr);
return;
}
/* Return a result tree fragment */
tctxt = xsltXPathGetTransformContext(ctxt);
if (tctxt == NULL) {
xsltTransformError(xsltXPathGetTransformContext(ctxt), NULL, NULL,
"exslt:regexp : internal error tctxt == NULL\n");
goto fail;
}
container = xsltCreateRVT(tctxt);
if (container != NULL) {
xsltRegisterTmpRVT(tctxt, container);
ret = xmlXPathNewNodeSet(NULL);
if (ret != NULL) {
ret->boolval = 0;
exsltRegexpFlagsFromString(flagstr, &global, &flags);
working = haystack;
rc = exsltRegexpExecute(ctxt, working, regexp, flags,
ovector, sizeof(ovector)/sizeof(int));
while (rc > 0) {
for(int group = 0; group < rc; group++) {
match = xmlStrsub(working, ovector[group*2], ovector[group*2+1]-ovector[group*2]);
if (NULL == match) goto fail;
node = xmlNewDocRawNode(container, NULL, "match", match);
xmlFree(match);
xmlAddChild((xmlNodePtr) container, node);
xmlXPathNodeSetAddUnique(ret->nodesetval, node);
}
if (!global) break;
working = working + ovector[1];
rc = exsltRegexpExecute(ctxt, working, regexp, flags,
ovector, sizeof(ovector)/sizeof(int));
}
}
}
fail:
if (flagstr != NULL)
xmlFree(flagstr);
if (regexp != NULL)
xmlFree(regexp);
if (haystack != NULL)
xmlFree(haystack);
if (ret != NULL)
valuePush(ctxt, ret);
else
valuePush(ctxt, xmlXPathNewNodeSet(NULL));
}
/**
* exsltRegexpReplaceFunction:
* @ns:
*
* Returns a node set of string matches
*/
static void
exsltRegexpReplaceFunction (xmlXPathParserContextPtr ctxt, int nargs)
{
xmlChar *haystack, *regexp, *flagstr, *replace, *tmp;
xmlChar *result = NULL, *working, *end;
int rc, x, flags, global, ovector[3];
if ((nargs < 1) || (nargs > 4)) {
xmlXPathSetArityError(ctxt);
return;
}
replace = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (replace == NULL)) {
return;
}
flagstr = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (flagstr == NULL)) {
xmlFree(replace);
return;
}
regexp = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (regexp == NULL)) {
xmlFree(flagstr);
xmlFree(replace);
return;
}
haystack = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (haystack == NULL)) {
xmlFree(regexp);
xmlFree(flagstr);
xmlFree(replace);
return;
}
exsltRegexpFlagsFromString(flagstr, &global, &flags);
working = haystack;
rc = exsltRegexpExecute(ctxt, working, regexp, flags,
ovector, sizeof(ovector)/sizeof(int));
while (rc > 0 ) {
if (0==ovector[0]) {
if (NULL==result) result = xmlStrdup(replace);
else result = xmlStrcat(result, replace);
}
else {
tmp = xmlStrsub(working, 0, ovector[0]);
if (NULL==result) result = tmp;
else {
result = xmlStrcat(result, tmp);
xmlFree(tmp);
}
result = xmlStrcat(result, replace);
}
working = working + ovector[1];
if (!global) break;
rc = exsltRegexpExecute(ctxt, working, regexp, flags,
ovector, sizeof(ovector)/sizeof(int));
}
end = haystack + xmlUTF8Strlen(haystack);
if (working < end ) {
if (NULL==result) result = xmlStrdup(working);
else {
result = xmlStrcat(result, working);
}
}
fail:
if (replace != NULL)
xmlFree(replace);
if (flagstr != NULL)
xmlFree(flagstr);
if (regexp != NULL)
xmlFree(regexp);
if (haystack != NULL)
xmlFree(haystack);
xmlXPathReturnString(ctxt, result);
}
/**
* exsltRegexpTestFunction:
* @ns:
*
* returns true if the string given as the first argument
* matches the regular expression given as the second argument
*
*/
static void
exsltRegexpTestFunction (xmlXPathParserContextPtr ctxt, int nargs)
{
xmlChar *haystack, *regexp_middle, *regexp, *flagstr;
int rc = 0, flags, global, ovector[3];
if ((nargs < 1) || (nargs > 3)) {
xmlXPathSetArityError(ctxt);
return;
}
if(nargs > 2) {
flagstr = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (flagstr == NULL)) {
return;
}
} else {
flagstr = xmlStrdup("");
}
regexp_middle = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (regexp_middle == NULL)) {
xmlFree(flagstr);
return;
}
haystack = xmlXPathPopString(ctxt);
if (xmlXPathCheckError(ctxt) || (haystack == NULL)) {
xmlFree(regexp_middle);
xmlFree(flagstr);
return;
}
/* build the regexp */
regexp = xmlStrdup("\\A");
regexp = xmlStrcat(regexp, regexp_middle);
regexp = xmlStrcat(regexp, "\\Z");
exsltRegexpFlagsFromString(flagstr, &global, &flags);
rc = exsltRegexpExecute(ctxt, haystack, regexp, flags,
ovector, sizeof(ovector)/sizeof(int));
fail:
if (flagstr != NULL)
xmlFree(flagstr);
if (regexp != NULL)
xmlFree(regexp);
if (regexp_middle != NULL)
xmlFree(regexp_middle);
if (haystack != NULL)
xmlFree(haystack);
xmlXPathReturnBoolean(ctxt, (rc > 0));
}
/**
* exsltRegexpRegister:
*
* Registers the EXSLT - Regexp module
*/
void
PLUGINPUBFUN exslt_org_regular_expressions_init (void)
{
xsltRegisterExtModuleFunction ((const xmlChar *) "match",
(const xmlChar *) EXSLT_REGEXP_NAMESPACE,
exsltRegexpMatchFunction);
xsltRegisterExtModuleFunction ((const xmlChar *) "replace",
(const xmlChar *) EXSLT_REGEXP_NAMESPACE,
exsltRegexpReplaceFunction);
xsltRegisterExtModuleFunction ((const xmlChar *) "test",
(const xmlChar *) EXSLT_REGEXP_NAMESPACE,
exsltRegexpTestFunction);
}