12 |
|
|
13 |
Written by: Philip Hazel <ph10@cam.ac.uk> |
Written by: Philip Hazel <ph10@cam.ac.uk> |
14 |
|
|
15 |
Copyright (c) 1997 University of Cambridge |
Copyright (c) 1997-2000 University of Cambridge |
16 |
|
|
17 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
18 |
Permission is granted to anyone to use this software for any purpose on any |
Permission is granted to anyone to use this software for any purpose on any |
28 |
|
|
29 |
3. Altered versions must be plainly marked as such, and must not be |
3. Altered versions must be plainly marked as such, and must not be |
30 |
misrepresented as being the original software. |
misrepresented as being the original software. |
31 |
|
|
32 |
|
4. If PCRE is embedded in any software that is released under the GNU |
33 |
|
General Purpose Licence (GPL), then the terms of that licence shall |
34 |
|
supersede any condition above with which it is incompatible. |
35 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
36 |
*/ |
*/ |
37 |
|
|
46 |
static const char *estring[] = { |
static const char *estring[] = { |
47 |
ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10, |
ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10, |
48 |
ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20, |
ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20, |
49 |
ERR21, ERR22, ERR23 }; |
ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR29, ERR29, ERR30, |
50 |
|
ERR31 }; |
51 |
|
|
52 |
static int eint[] = { |
static int eint[] = { |
53 |
REG_EESCAPE, /* "\\ at end of pattern" */ |
REG_EESCAPE, /* "\\ at end of pattern" */ |
72 |
REG_ESIZE, /* "regular expression too large" */ |
REG_ESIZE, /* "regular expression too large" */ |
73 |
REG_ESPACE, /* "failed to get memory" */ |
REG_ESPACE, /* "failed to get memory" */ |
74 |
REG_EPAREN, /* "unmatched brackets" */ |
REG_EPAREN, /* "unmatched brackets" */ |
75 |
REG_ASSERT /* "internal error: code overflow" */ |
REG_ASSERT, /* "internal error: code overflow" */ |
76 |
|
REG_BADPAT, /* "unrecognized character after (?<" */ |
77 |
|
REG_BADPAT, /* "lookbehind assertion is not fixed length" */ |
78 |
|
REG_BADPAT, /* "malformed number after (?(" */ |
79 |
|
REG_BADPAT, /* "conditional group containe more than two branches" */ |
80 |
|
REG_BADPAT, /* "assertion expected after (?(" */ |
81 |
|
REG_BADPAT, /* "(?p must be followed by )" */ |
82 |
|
REG_ECTYPE, /* "unknown POSIX class name" */ |
83 |
|
REG_BADPAT, /* "POSIX collating elements are not supported" */ |
84 |
|
REG_INVARG, /* "this version of PCRE is not compiled with PCRE_UTF8 support" */ |
85 |
|
REG_BADPAT, /* "characters with values > 255 are not yet supported in classes" */ |
86 |
|
REG_BADPAT /* "character value in \x{...} sequence is too large" */ |
87 |
}; |
}; |
88 |
|
|
89 |
/* Table of texts corresponding to POSIX error codes */ |
/* Table of texts corresponding to POSIX error codes */ |
151 |
if (errbuf_size > 0) |
if (errbuf_size > 0) |
152 |
{ |
{ |
153 |
if (addlength > 0 && errbuf_size >= length + addlength) |
if (addlength > 0 && errbuf_size >= length + addlength) |
154 |
sprintf(errbuf, "%s%s%-6d", message, addmessage, preg->re_erroffset); |
sprintf(errbuf, "%s%s%-6d", message, addmessage, (int)preg->re_erroffset); |
155 |
else |
else |
156 |
{ |
{ |
157 |
strncpy(errbuf, message, errbuf_size - 1); |
strncpy(errbuf, message, errbuf_size - 1); |
202 |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
203 |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
204 |
|
|
205 |
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset); |
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL); |
206 |
preg->re_erroffset = erroffset; |
preg->re_erroffset = erroffset; |
207 |
|
|
208 |
if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr); |
if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr); |
218 |
* Match a regular expression * |
* Match a regular expression * |
219 |
*************************************************/ |
*************************************************/ |
220 |
|
|
221 |
|
/* Unfortunately, PCRE requires 3 ints of working space for each captured |
222 |
|
substring, so we have to get and release working store instead of just using |
223 |
|
the POSIX structures as was done in earlier releases when PCRE needed only 2 |
224 |
|
ints. */ |
225 |
|
|
226 |
int |
int |
227 |
regexec(regex_t *preg, const char *string, size_t nmatch, |
regexec(regex_t *preg, const char *string, size_t nmatch, |
228 |
regmatch_t pmatch[], int eflags) |
regmatch_t pmatch[], int eflags) |
229 |
{ |
{ |
230 |
int rc; |
int rc; |
231 |
int options = 0; |
int options = 0; |
232 |
|
int *ovector = NULL; |
233 |
|
|
234 |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
235 |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
236 |
|
|
237 |
preg->re_erroffset = -1; /* Only has meaning after compile */ |
preg->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
238 |
|
|
239 |
|
if (nmatch > 0) |
240 |
|
{ |
241 |
|
ovector = (int *)malloc(sizeof(int) * nmatch * 3); |
242 |
|
if (ovector == NULL) return REG_ESPACE; |
243 |
|
} |
244 |
|
|
245 |
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), options, |
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), 0, options, |
246 |
(int *)pmatch, nmatch * 2); |
ovector, nmatch * 3); |
247 |
|
|
248 |
if (rc == 0) return 0; /* All pmatch were filled in */ |
if (rc == 0) rc = nmatch; /* All captured slots were filled in */ |
249 |
|
|
250 |
if (rc > 0) |
if (rc >= 0) |
251 |
{ |
{ |
252 |
size_t i; |
size_t i; |
253 |
for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
for (i = 0; i < rc; i++) |
254 |
|
{ |
255 |
|
pmatch[i].rm_so = ovector[i*2]; |
256 |
|
pmatch[i].rm_eo = ovector[i*2+1]; |
257 |
|
} |
258 |
|
if (ovector != NULL) free(ovector); |
259 |
|
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
260 |
return 0; |
return 0; |
261 |
} |
} |
262 |
|
|
263 |
else switch(rc) |
else |
264 |
{ |
{ |
265 |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
if (ovector != NULL) free(ovector); |
266 |
case PCRE_ERROR_BADREF: return REG_ESUBREG; |
switch(rc) |
267 |
case PCRE_ERROR_NULL: return REG_INVARG; |
{ |
268 |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
269 |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
case PCRE_ERROR_NULL: return REG_INVARG; |
270 |
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
271 |
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
272 |
default: return REG_ASSERT; |
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
273 |
|
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
274 |
|
default: return REG_ASSERT; |
275 |
|
} |
276 |
} |
} |
277 |
} |
} |
278 |
|
|