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 |
|
|
211 |
* Match a regular expression * |
* Match a regular expression * |
212 |
*************************************************/ |
*************************************************/ |
213 |
|
|
214 |
|
/* Unfortunately, PCRE requires 3 ints of working space for each captured |
215 |
|
substring, so we have to get and release working store instead of just using |
216 |
|
the POSIX structures as was done in earlier releases when PCRE needed only 2 |
217 |
|
ints. */ |
218 |
|
|
219 |
int |
int |
220 |
regexec(regex_t *preg, const char *string, size_t nmatch, |
regexec(regex_t *preg, const char *string, size_t nmatch, |
221 |
regmatch_t pmatch[], int eflags) |
regmatch_t pmatch[], int eflags) |
222 |
{ |
{ |
223 |
int rc; |
int rc; |
224 |
int options = 0; |
int options = 0; |
225 |
|
int *ovector = NULL; |
226 |
|
|
227 |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
if ((eflags & REG_NOTBOL) != 0) options |= PCRE_NOTBOL; |
228 |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
if ((eflags & REG_NOTEOL) != 0) options |= PCRE_NOTEOL; |
229 |
|
|
230 |
preg->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
preg->re_erroffset = (size_t)(-1); /* Only has meaning after compile */ |
231 |
|
|
232 |
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), options, |
if (nmatch > 0) |
233 |
(int *)pmatch, nmatch * 2); |
{ |
234 |
|
ovector = malloc(sizeof(int) * nmatch * 3); |
235 |
|
if (ovector == NULL) return REG_ESPACE; |
236 |
|
} |
237 |
|
|
238 |
|
rc = pcre_exec(preg->re_pcre, NULL, string, (int)strlen(string), 0, options, |
239 |
|
ovector, nmatch * 3); |
240 |
|
|
241 |
if (rc == 0) return 0; /* All pmatch were filled in */ |
if (rc == 0) rc = nmatch; /* All captured slots were filled in */ |
242 |
|
|
243 |
if (rc > 0) |
if (rc >= 0) |
244 |
{ |
{ |
245 |
size_t i; |
size_t i; |
246 |
for (i = rc; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
for (i = 0; i < rc; i++) |
247 |
|
{ |
248 |
|
pmatch[i].rm_so = ovector[i*2]; |
249 |
|
pmatch[i].rm_eo = ovector[i*2+1]; |
250 |
|
} |
251 |
|
if (ovector != NULL) free(ovector); |
252 |
|
for (; i < nmatch; i++) pmatch[i].rm_so = pmatch[i].rm_eo = -1; |
253 |
return 0; |
return 0; |
254 |
} |
} |
255 |
|
|
256 |
else switch(rc) |
else |
257 |
{ |
{ |
258 |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
if (ovector != NULL) free(ovector); |
259 |
case PCRE_ERROR_NULL: return REG_INVARG; |
switch(rc) |
260 |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
{ |
261 |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
case PCRE_ERROR_NOMATCH: return REG_NOMATCH; |
262 |
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
case PCRE_ERROR_NULL: return REG_INVARG; |
263 |
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
case PCRE_ERROR_BADOPTION: return REG_INVARG; |
264 |
default: return REG_ASSERT; |
case PCRE_ERROR_BADMAGIC: return REG_INVARG; |
265 |
|
case PCRE_ERROR_UNKNOWN_NODE: return REG_ASSERT; |
266 |
|
case PCRE_ERROR_NOMEMORY: return REG_ESPACE; |
267 |
|
default: return REG_ASSERT; |
268 |
|
} |
269 |
} |
} |
270 |
} |
} |
271 |
|
|