| 12 |
|
|
| 13 |
Written by: Philip Hazel <ph10@cam.ac.uk> |
Written by: Philip Hazel <ph10@cam.ac.uk> |
| 14 |
|
|
| 15 |
Copyright (c) 1998 University of Cambridge |
Copyright (c) 1997-2004 University of Cambridge |
| 16 |
|
|
| 17 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 18 |
Permission is granted to anyone to use this software for any purpose on any |
Redistribution and use in source and binary forms, with or without |
| 19 |
computer system, and to redistribute it freely, subject to the following |
modification, are permitted provided that the following conditions are met: |
|
restrictions: |
|
|
|
|
|
1. This software is distributed in the hope that it will be useful, |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
|
| 20 |
|
|
| 21 |
2. The origin of this software must not be misrepresented, either by |
* Redistributions of source code must retain the above copyright notice, |
| 22 |
explicit claim or by omission. |
this list of conditions and the following disclaimer. |
| 23 |
|
|
| 24 |
3. Altered versions must be plainly marked as such, and must not be |
* Redistributions in binary form must reproduce the above copyright |
| 25 |
misrepresented as being the original software. |
notice, this list of conditions and the following disclaimer in the |
| 26 |
|
documentation and/or other materials provided with the distribution. |
| 27 |
|
|
| 28 |
|
* Neither the name of the University of Cambridge nor the names of its |
| 29 |
|
contributors may be used to endorse or promote products derived from |
| 30 |
|
this software without specific prior written permission. |
| 31 |
|
|
| 32 |
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 33 |
|
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 34 |
|
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 35 |
|
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 36 |
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 37 |
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 38 |
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 39 |
|
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 40 |
|
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 41 |
|
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 42 |
|
POSSIBILITY OF SUCH DAMAGE. |
| 43 |
----------------------------------------------------------------------------- |
----------------------------------------------------------------------------- |
| 44 |
*/ |
*/ |
| 45 |
|
|
| 51 |
|
|
| 52 |
/* Corresponding tables of PCRE error messages and POSIX error codes. */ |
/* Corresponding tables of PCRE error messages and POSIX error codes. */ |
| 53 |
|
|
| 54 |
static const char *estring[] = { |
static const char *const estring[] = { |
| 55 |
ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10, |
ERR1, ERR2, ERR3, ERR4, ERR5, ERR6, ERR7, ERR8, ERR9, ERR10, |
| 56 |
ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20, |
ERR11, ERR12, ERR13, ERR14, ERR15, ERR16, ERR17, ERR18, ERR19, ERR20, |
| 57 |
ERR21, ERR22, ERR23, ERR24, ERR25 }; |
ERR21, ERR22, ERR23, ERR24, ERR25, ERR26, ERR27, ERR29, ERR29, ERR30, |
| 58 |
|
ERR31, ERR32, ERR33, ERR34, ERR35, ERR36, ERR37, ERR38, ERR39, ERR40, |
| 59 |
|
ERR41, ERR42, ERR43, ERR44, ERR45, ERR46, ERR47 }; |
| 60 |
|
|
| 61 |
static int eint[] = { |
static const int eint[] = { |
| 62 |
REG_EESCAPE, /* "\\ at end of pattern" */ |
REG_EESCAPE, /* "\\ at end of pattern" */ |
| 63 |
REG_EESCAPE, /* "\\c at end of pattern" */ |
REG_EESCAPE, /* "\\c at end of pattern" */ |
| 64 |
REG_EESCAPE, /* "unrecognized character follows \\" */ |
REG_EESCAPE, /* "unrecognized character follows \\" */ |
| 71 |
REG_BADRPT, /* "operand of unlimited repeat could match the empty string" */ |
REG_BADRPT, /* "operand of unlimited repeat could match the empty string" */ |
| 72 |
REG_ASSERT, /* "internal error: unexpected repeat" */ |
REG_ASSERT, /* "internal error: unexpected repeat" */ |
| 73 |
REG_BADPAT, /* "unrecognized character after (?" */ |
REG_BADPAT, /* "unrecognized character after (?" */ |
| 74 |
REG_ESIZE, /* "too many capturing parenthesized sub-patterns" */ |
REG_BADPAT, /* "POSIX named classes are supported only within a class" */ |
| 75 |
REG_EPAREN, /* "missing )" */ |
REG_EPAREN, /* "missing )" */ |
| 76 |
REG_ESUBREG, /* "back reference to non-existent subpattern" */ |
REG_ESUBREG, /* "reference to non-existent subpattern" */ |
| 77 |
REG_INVARG, /* "erroffset passed as NULL" */ |
REG_INVARG, /* "erroffset passed as NULL" */ |
| 78 |
REG_INVARG, /* "unknown option bit(s) set" */ |
REG_INVARG, /* "unknown option bit(s) set" */ |
| 79 |
REG_EPAREN, /* "missing ) after comment" */ |
REG_EPAREN, /* "missing ) after comment" */ |
| 80 |
REG_ESIZE, /* "too many sets of parentheses" */ |
REG_ESIZE, /* "parentheses nested too deeply" */ |
| 81 |
REG_ESIZE, /* "regular expression too large" */ |
REG_ESIZE, /* "regular expression too large" */ |
| 82 |
REG_ESPACE, /* "failed to get memory" */ |
REG_ESPACE, /* "failed to get memory" */ |
| 83 |
REG_EPAREN, /* "unmatched brackets" */ |
REG_EPAREN, /* "unmatched brackets" */ |
| 86 |
REG_BADPAT, /* "lookbehind assertion is not fixed length" */ |
REG_BADPAT, /* "lookbehind assertion is not fixed length" */ |
| 87 |
REG_BADPAT, /* "malformed number after (?(" */ |
REG_BADPAT, /* "malformed number after (?(" */ |
| 88 |
REG_BADPAT, /* "conditional group containe more than two branches" */ |
REG_BADPAT, /* "conditional group containe more than two branches" */ |
| 89 |
REG_BADPAT /* "assertion expected after (?(" */ |
REG_BADPAT, /* "assertion expected after (?(" */ |
| 90 |
|
REG_BADPAT, /* "(?R or (?digits must be followed by )" */ |
| 91 |
|
REG_ECTYPE, /* "unknown POSIX class name" */ |
| 92 |
|
REG_BADPAT, /* "POSIX collating elements are not supported" */ |
| 93 |
|
REG_INVARG, /* "this version of PCRE is not compiled with PCRE_UTF8 support" */ |
| 94 |
|
REG_BADPAT, /* "spare error" */ |
| 95 |
|
REG_BADPAT, /* "character value in \x{...} sequence is too large" */ |
| 96 |
|
REG_BADPAT, /* "invalid condition (?(0)" */ |
| 97 |
|
REG_BADPAT, /* "\\C not allowed in lookbehind assertion" */ |
| 98 |
|
REG_EESCAPE, /* "PCRE does not support \\L, \\l, \\N, \\U, or \\u" */ |
| 99 |
|
REG_BADPAT, /* "number after (?C is > 255" */ |
| 100 |
|
REG_BADPAT, /* "closing ) for (?C expected" */ |
| 101 |
|
REG_BADPAT, /* "recursive call could loop indefinitely" */ |
| 102 |
|
REG_BADPAT, /* "unrecognized character after (?P" */ |
| 103 |
|
REG_BADPAT, /* "syntax error after (?P" */ |
| 104 |
|
REG_BADPAT, /* "two named groups have the same name" */ |
| 105 |
|
REG_BADPAT, /* "invalid UTF-8 string" */ |
| 106 |
|
REG_BADPAT, /* "support for \\P, \\p, and \\X has not been compiled" */ |
| 107 |
|
REG_BADPAT, /* "malformed \\P or \\p sequence" */ |
| 108 |
|
REG_BADPAT /* "unknown property name after \\P or \\p" */ |
| 109 |
}; |
}; |
| 110 |
|
|
| 111 |
/* Table of texts corresponding to POSIX error codes */ |
/* Table of texts corresponding to POSIX error codes */ |
| 112 |
|
|
| 113 |
static const char *pstring[] = { |
static const char *const pstring[] = { |
| 114 |
"", /* Dummy for value 0 */ |
"", /* Dummy for value 0 */ |
| 115 |
"internal error", /* REG_ASSERT */ |
"internal error", /* REG_ASSERT */ |
| 116 |
"invalid repeat counts in {}", /* BADBR */ |
"invalid repeat counts in {}", /* BADBR */ |
| 156 |
* Translate error code to string * |
* Translate error code to string * |
| 157 |
*************************************************/ |
*************************************************/ |
| 158 |
|
|
| 159 |
size_t |
EXPORT size_t |
| 160 |
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) |
regerror(int errcode, const regex_t *preg, char *errbuf, size_t errbuf_size) |
| 161 |
{ |
{ |
| 162 |
const char *message, *addmessage; |
const char *message, *addmessage; |
| 191 |
* Free store held by a regex * |
* Free store held by a regex * |
| 192 |
*************************************************/ |
*************************************************/ |
| 193 |
|
|
| 194 |
void |
EXPORT void |
| 195 |
regfree(regex_t *preg) |
regfree(regex_t *preg) |
| 196 |
{ |
{ |
| 197 |
(pcre_free)(preg->re_pcre); |
(pcre_free)(preg->re_pcre); |
| 214 |
various non-zero codes on failure |
various non-zero codes on failure |
| 215 |
*/ |
*/ |
| 216 |
|
|
| 217 |
int |
EXPORT int |
| 218 |
regcomp(regex_t *preg, const char *pattern, int cflags) |
regcomp(regex_t *preg, const char *pattern, int cflags) |
| 219 |
{ |
{ |
| 220 |
const char *errorptr; |
const char *errorptr; |
| 224 |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
if ((cflags & REG_ICASE) != 0) options |= PCRE_CASELESS; |
| 225 |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
if ((cflags & REG_NEWLINE) != 0) options |= PCRE_MULTILINE; |
| 226 |
|
|
| 227 |
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset); |
preg->re_pcre = pcre_compile(pattern, options, &errorptr, &erroffset, NULL); |
| 228 |
preg->re_erroffset = erroffset; |
preg->re_erroffset = erroffset; |
| 229 |
|
|
| 230 |
if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr); |
if (preg->re_pcre == NULL) return pcre_posix_error_code(errorptr); |
| 231 |
|
|
| 232 |
preg->re_nsub = pcre_info(preg->re_pcre, NULL, NULL); |
preg->re_nsub = pcre_info((const pcre *)preg->re_pcre, NULL, NULL); |
| 233 |
return 0; |
return 0; |
| 234 |
} |
} |
| 235 |
|
|
| 240 |
* Match a regular expression * |
* Match a regular expression * |
| 241 |
*************************************************/ |
*************************************************/ |
| 242 |
|
|
| 243 |
int |
/* Unfortunately, PCRE requires 3 ints of working space for each captured |
| 244 |
regexec(regex_t *preg, const char *string, size_t nmatch, |
substring, so we have to get and release working store instead of just using |
| 245 |
|
the POSIX structures as was done in earlier releases when PCRE needed only 2 |
| 246 |
|
ints. However, if the number of possible capturing brackets is small, use a |
| 247 |
|
block of store on the stack, to reduce the use of malloc/free. The threshold is |
| 248 |
|
in a macro that can be changed at configure time. */ |
| 249 |
|
|
| 250 |
|
EXPORT int |
| 251 |
|
regexec(const regex_t *preg, const char *string, size_t nmatch, |
| 252 |
regmatch_t pmatch[], int eflags) |
regmatch_t pmatch[], int eflags) |
| 253 |
{ |
{ |
| 254 |
int rc; |
int rc; |
| 255 |
int options = 0; |
int options = 0; |
| 256 |
|
int *ovector = NULL; |
| 257 |
|
int small_ovector[POSIX_MALLOC_THRESHOLD * 3]; |
| 258 |
|
BOOL allocated_ovector = FALSE; |
| 259 |
|
|
| 260 |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
| 261 |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
| 262 |
|
|
| 263 |
preg->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
((regex_t *)preg)->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
| 264 |
|
|
| 265 |
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), options, |
if (nmatch > 0) |
| 266 |
(int *)pmatch, nmatch * 2); |
{ |
| 267 |
|
if (nmatch <= POSIX_MALLOC_THRESHOLD) |
| 268 |
|
{ |
| 269 |
|
ovector = &(small_ovector[0]); |
| 270 |
|
} |
| 271 |
|
else |
| 272 |
|
{ |
| 273 |
|
ovector = (int *)malloc(sizeof(int) * nmatch * 3); |
| 274 |
|
if (ovector == NULL) return REG_ESPACE; |
| 275 |
|
allocated_ovector = TRUE; |
| 276 |
|
} |
| 277 |
|
} |
| 278 |
|
|
| 279 |
|
rc = pcre_exec((const pcre *)preg->re_pcre, NULL, string, (int)strlen(string), |
| 280 |
|
0, options, ovector, nmatch * 3); |
| 281 |
|
|
| 282 |
if (rc == 0) return 0; /* All pmatch were filled in */ |
if (rc == 0) rc = nmatch; /* All captured slots were filled in */ |
| 283 |
|
|
| 284 |
if (rc > 0) |
if (rc >= 0) |
| 285 |
{ |
{ |
| 286 |
size_t i; |
size_t i; |
| 287 |
for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
for (i = 0; i < (size_t)rc; i++) |
| 288 |
|
{ |
| 289 |
|
pmatch[i].rm_so = ovector[i*2]; |
| 290 |
|
pmatch[i].rm_eo = ovector[i*2+1]; |
| 291 |
|
} |
| 292 |
|
if (allocated_ovector) free(ovector); |
| 293 |
|
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
| 294 |
return 0; |
return 0; |
| 295 |
} |
} |
| 296 |
|
|
| 297 |
else switch(rc) |
else |
| 298 |
{ |
{ |
| 299 |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
if (allocated_ovector) free(ovector); |
| 300 |
case PCRE_ERROR_NULL: return REG_INVARG; |
switch(rc) |
| 301 |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
{ |
| 302 |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
| 303 |
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
case PCRE_ERROR_NULL: return REG_INVARG; |
| 304 |
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
| 305 |
default: return REG_ASSERT; |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
| 306 |
|
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
| 307 |
|
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
| 308 |
|
case PCRE_ERROR_MATCHLIMIT: return REG_ESPACE; |
| 309 |
|
case PCRE_ERROR_BADUTF8: return REG_INVARG; |
| 310 |
|
case PCRE_ERROR_BADUTF8_OFFSET: return REG_INVARG; |
| 311 |
|
default: return REG_ASSERT; |
| 312 |
|
} |
| 313 |
} |
} |
| 314 |
} |
} |
| 315 |
|
|