Re: Exceptions in IPC::Open2
[p5sagit/p5-mst-13.2.git] / regexec.c
CommitLineData
a0d0e21e 1/* regexec.c
2 */
3
4/*
5 * "One Ring to rule them all, One Ring to find them..."
6 */
7
a687059c 8/* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
10 */
11
12/* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
15 */
16
e50aee73 17/* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
20*/
21
f0fcb552 22/*SUPPRESS 112*/
a687059c 23/*
e50aee73 24 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c 25 *
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
28 *
29 * Permission is granted to anyone to use this software for any
30 * purpose on any computer system, and to redistribute it freely,
31 * subject to the following restrictions:
32 *
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
35 * from defects in it.
36 *
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
39 *
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
42 *
43 **** Alterations to Henry's code are...
44 ****
9607fc9c 45 **** Copyright (c) 1991-1997, Larry Wall
a687059c 46 ****
9ef589d8 47 **** You may distribute under the terms of either the GNU General Public
48 **** License or the Artistic License, as specified in the README file.
a687059c 49 *
50 * Beware that some of this code is subtly aware of the way operator
51 * precedence is structured in regular expressions. Serious changes in
52 * regular-expression syntax might require a total rethink.
53 */
54#include "EXTERN.h"
55#include "perl.h"
56#include "regcomp.h"
57
c277df42 58#define RF_tainted 1 /* tainted information used? */
59#define RF_warned 2 /* warned about big count? */
ce862d02 60#define RF_evaled 4 /* Did an EVAL with setting? */
61
62#define RS_init 1 /* eval environment created */
63#define RS_set 2 /* replsv value is set */
c277df42 64
a687059c 65#ifndef STATIC
66#define STATIC static
67#endif
68
76e3520e 69#ifndef PERL_OBJECT
a0d0e21e 70typedef I32 CHECKPOINT;
71
c277df42 72/*
73 * Forwards.
74 */
75
76static I32 regmatch _((regnode *prog));
77static I32 regrepeat _((regnode *p, I32 max));
78static I32 regrepeat_hard _((regnode *p, I32 max, I32 *lp));
79static I32 regtry _((regexp *prog, char *startpos));
ae5c130c 80
c277df42 81static bool reginclass _((char *p, I32 c));
55497cff 82static CHECKPOINT regcppush _((I32 parenfloor));
83static char * regcppop _((void));
76e3520e 84#endif
ae5c130c 85#define REGINCLASS(p,c) (*(p) ? reginclass(p,c) : ANYOF_TEST(p,c))
a0d0e21e 86
76e3520e 87STATIC CHECKPOINT
8ac85365 88regcppush(I32 parenfloor)
a0d0e21e 89{
11343788 90 dTHR;
a0d0e21e 91 int retval = savestack_ix;
c277df42 92 int i = (regsize - parenfloor) * 4;
a0d0e21e 93 int p;
94
95 SSCHECK(i + 5);
96 for (p = regsize; p > parenfloor; p--) {
97 SSPUSHPTR(regendp[p]);
98 SSPUSHPTR(regstartp[p]);
c5254dd6 99 SSPUSHPTR(reg_start_tmp[p]);
a0d0e21e 100 SSPUSHINT(p);
101 }
102 SSPUSHINT(regsize);
103 SSPUSHINT(*reglastparen);
104 SSPUSHPTR(reginput);
105 SSPUSHINT(i + 3);
106 SSPUSHINT(SAVEt_REGCONTEXT);
107 return retval;
108}
109
c277df42 110/* These are needed since we do not localize EVAL nodes: */
111# define REGCP_SET DEBUG_r(PerlIO_printf(Perl_debug_log, " Setting an EVAL scope, savestack=%i\n", savestack_ix)); lastcp = savestack_ix
112# define REGCP_UNWIND DEBUG_r(lastcp != savestack_ix ? PerlIO_printf(Perl_debug_log," Clearing an EVAL scope, savestack=%i..%i\n", lastcp, savestack_ix) : 0); regcpblow(lastcp)
113
76e3520e 114STATIC char *
8ac85365 115regcppop(void)
a0d0e21e 116{
11343788 117 dTHR;
a0d0e21e 118 I32 i = SSPOPINT;
119 U32 paren = 0;
120 char *input;
121 char *tmps;
122 assert(i == SAVEt_REGCONTEXT);
123 i = SSPOPINT;
124 input = (char *) SSPOPPTR;
125 *reglastparen = SSPOPINT;
126 regsize = SSPOPINT;
c277df42 127 for (i -= 3; i > 0; i -= 4) {
a0d0e21e 128 paren = (U32)SSPOPINT;
c277df42 129 reg_start_tmp[paren] = (char *) SSPOPPTR;
a0d0e21e 130 regstartp[paren] = (char *) SSPOPPTR;
131 tmps = (char*)SSPOPPTR;
132 if (paren <= *reglastparen)
133 regendp[paren] = tmps;
c277df42 134 DEBUG_r(
135 PerlIO_printf(Perl_debug_log, " restoring \\%d to %d(%d)..%d%s\n",
136 paren, regstartp[paren] - regbol,
137 reg_start_tmp[paren] - regbol,
138 regendp[paren] - regbol,
139 (paren > *reglastparen ? "(no)" : ""));
140 );
a0d0e21e 141 }
c277df42 142 DEBUG_r(
143 if (*reglastparen + 1 <= regnpar) {
144 PerlIO_printf(Perl_debug_log, " restoring \\%d..\\%d to undef\n",
145 *reglastparen + 1, regnpar);
146 }
147 );
a0d0e21e 148 for (paren = *reglastparen + 1; paren <= regnpar; paren++) {
149 if (paren > regsize)
150 regstartp[paren] = Nullch;
151 regendp[paren] = Nullch;
152 }
153 return input;
154}
155
c277df42 156#define regcpblow(cp) LEAVE_SCOPE(cp)
a0d0e21e 157
a687059c 158/*
e50aee73 159 * pregexec and friends
a687059c 160 */
161
162/*
c277df42 163 - pregexec - match a regexp against a string
a687059c 164 */
c277df42 165I32
166pregexec(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, U32 nosave)
167/* strend: pointer to null at end of string */
168/* strbeg: real beginning of string */
169/* minend: end of match must be >=minend after stringarg. */
170/* nosave: For optimizations. */
171{
172 return
173 regexec_flags(prog, stringarg, strend, strbeg, minend, screamer, NULL,
174 nosave ? 0 : REXEC_COPY_STR);
175}
176
a687059c 177/*
c277df42 178 - regexec_flags - match a regexp against a string
a687059c 179 */
79072805 180I32
c277df42 181regexec_flags(register regexp *prog, char *stringarg, register char *strend, char *strbeg, I32 minend, SV *screamer, void *data, U32 flags)
182/* strend: pointer to null at end of string */
183/* strbeg: real beginning of string */
184/* minend: end of match must be >=minend after stringarg. */
185/* data: May be used for some additional optimizations. */
186/* nosave: For optimizations. */
a687059c 187{
a0d0e21e 188 register char *s;
c277df42 189 register regnode *c;
a0d0e21e 190 register char *startpos = stringarg;
191 register I32 tmp;
c277df42 192 I32 minlen; /* must match at least this many chars */
a0d0e21e 193 I32 dontbother = 0; /* how many characters not to try at end */
194 CURCUR cc;
c277df42 195 I32 start_shift = 0; /* Offset of the start to find
196 constant substr. */
197 I32 end_shift = 0; /* Same for the end. */
198 I32 scream_pos = -1; /* Internal iterator of scream. */
199 char *scream_olds;
ce862d02 200 SV* oreplsv = GvSV(replgv);
a687059c 201
a0d0e21e 202 cc.cur = 0;
4633a7c4 203 cc.oldcc = 0;
a0d0e21e 204 regcc = &cc;
205
c277df42 206 regprecomp = prog->precomp; /* Needed for error messages. */
a0d0e21e 207#ifdef DEBUGGING
208 regnarrate = debug & 512;
209 regprogram = prog->program;
210#endif
211
212 /* Be paranoid... */
213 if (prog == NULL || startpos == NULL) {
214 croak("NULL regexp parameter");
215 return 0;
216 }
217
c277df42 218 minlen = prog->minlen;
219 if (strend - startpos < minlen) goto phooey;
220
a0d0e21e 221 if (startpos == strbeg) /* is ^ valid at stringarg? */
222 regprev = '\n';
223 else {
224 regprev = stringarg[-1];
225 if (!multiline && regprev == '\n')
226 regprev = '\0'; /* force ^ to NOT match */
227 }
bbce6d69 228
a0d0e21e 229 /* Check validity of program. */
230 if (UCHARAT(prog->program) != MAGIC) {
231 FAIL("corrupted regexp program");
232 }
233
bbce6d69 234 regnpar = prog->nparens;
c277df42 235 reg_flags = 0;
236 reg_eval_set = 0;
a0d0e21e 237
238 /* If there is a "must appear" string, look for it. */
239 s = startpos;
c277df42 240 if (!(flags & REXEC_CHECKED)
241 && prog->check_substr != Nullsv &&
774d564b 242 !(prog->reganch & ROPT_ANCH_GPOS) &&
c277df42 243 (!(prog->reganch & (ROPT_ANCH_BOL | ROPT_ANCH_MBOL))
244 || (multiline && prog->check_substr == prog->anchored_substr)) )
a0d0e21e 245 {
c277df42 246 start_shift = prog->check_offset_min;
247 /* Should be nonnegative! */
248 end_shift = minlen - start_shift - SvCUR(prog->check_substr);
249 if (screamer) {
250 if (screamfirst[BmRARE(prog->check_substr)] >= 0)
251 s = screaminstr(screamer, prog->check_substr,
252 start_shift + (stringarg - strbeg),
253 end_shift, &scream_pos, 0);
a0d0e21e 254 else
255 s = Nullch;
c277df42 256 scream_olds = s;
0a12ae7d 257 }
a0d0e21e 258 else
c277df42 259 s = fbm_instr((unsigned char*)s + start_shift,
260 (unsigned char*)strend - end_shift,
261 prog->check_substr);
a0d0e21e 262 if (!s) {
c277df42 263 ++BmUSEFUL(prog->check_substr); /* hooray */
a0d0e21e 264 goto phooey; /* not present */
c277df42 265 } else if ((s - stringarg) > prog->check_offset_max) {
266 ++BmUSEFUL(prog->check_substr); /* hooray/2 */
267 s -= prog->check_offset_max;
268 } else if (!prog->naughty
269 && --BmUSEFUL(prog->check_substr) < 0
270 && prog->check_substr == prog->float_substr) { /* boo */
271 SvREFCNT_dec(prog->check_substr);
272 prog->check_substr = Nullsv; /* disable */
273 prog->float_substr = Nullsv; /* clear */
a0d0e21e 274 s = startpos;
c277df42 275 } else s = startpos;
a0d0e21e 276 }
a687059c 277
c277df42 278 /* Mark beginning of line for ^ and lookbehind. */
a0d0e21e 279 regbol = startpos;
c277df42 280 bostr = strbeg;
a687059c 281
a0d0e21e 282 /* Mark end of line for $ (and such) */
283 regeol = strend;
a687059c 284
a0d0e21e 285 /* see how far we have to get to not match where we matched before */
286 regtill = startpos+minend;
a687059c 287
c277df42 288 DEBUG_r(
289 PerlIO_printf(Perl_debug_log,
290 "Matching `%.60s%s' against `%.*s%s'\n",
291 prog->precomp,
292 (strlen(prog->precomp) > 60 ? "..." : ""),
293 (strend - startpos > 60 ? 60 : strend - startpos),
294 startpos,
295 (strend - startpos > 60 ? "..." : ""))
296 );
297
a0d0e21e 298 /* Simplest case: anchored match need be tried only once. */
774d564b 299 /* [unless only anchor is BOL and multiline is set] */
a0d0e21e 300 if (prog->reganch & ROPT_ANCH) {
301 if (regtry(prog, startpos))
302 goto got_it;
774d564b 303 else if (!(prog->reganch & ROPT_ANCH_GPOS) &&
c277df42 304 (multiline || (prog->reganch & ROPT_IMPLICIT)
305 || (prog->reganch & ROPT_ANCH_MBOL)))
774d564b 306 {
a0d0e21e 307 if (minlen)
308 dontbother = minlen - 1;
309 strend -= dontbother;
310 /* for multiline we only have to try after newlines */
311 if (s > startpos)
312 s--;
313 while (s < strend) {
314 if (*s++ == '\n') {
315 if (s < strend && regtry(prog, s))
316 goto got_it;
317 }
35c8bce7 318 }
35c8bce7 319 }
a0d0e21e 320 goto phooey;
321 }
35c8bce7 322
a0d0e21e 323 /* Messy cases: unanchored match. */
c277df42 324 if (prog->anchored_substr && prog->reganch & ROPT_SKIP) {
325 /* we have /x+whatever/ */
326 /* it must be a one character string */
327 char ch = SvPVX(prog->anchored_substr)[0];
328 while (s < strend) {
329 if (*s == ch) {
330 if (regtry(prog, s)) goto got_it;
a0d0e21e 331 s++;
c277df42 332 while (s < strend && *s == ch)
333 s++;
a0d0e21e 334 }
c277df42 335 s++;
a687059c 336 }
c277df42 337 }
338 /*SUPPRESS 560*/
339 else if (prog->anchored_substr != Nullsv
340 || (prog->float_substr != Nullsv
341 && prog->float_max_offset < strend - s)) {
342 SV *must = prog->anchored_substr
343 ? prog->anchored_substr : prog->float_substr;
344 I32 back_max =
345 prog->anchored_substr ? prog->anchored_offset : prog->float_max_offset;
346 I32 back_min =
347 prog->anchored_substr ? prog->anchored_offset : prog->float_min_offset;
348 I32 delta = back_max - back_min;
349 char *last = strend - SvCUR(must) - back_min; /* Cannot start after this */
350 char *last1 = s - 1; /* Last position checked before */
351
352 /* XXXX check_substr already used to find `s', can optimize if
353 check_substr==must. */
354 scream_pos = -1;
355 dontbother = end_shift;
356 strend -= dontbother;
357 while ( (s <= last) &&
358 (screamer
359 ? (s = screaminstr(screamer, must, s + back_min - strbeg,
360 end_shift, &scream_pos, 0))
361 : (s = fbm_instr((unsigned char*)s + back_min,
362 (unsigned char*)strend, must))) ) {
363 if (s - back_max > last1) {
364 last1 = s - back_min;
365 s = s - back_max;
366 } else {
367 char *t = last1 + 1;
368
369 last1 = s - back_min;
370 s = t;
a0d0e21e 371 }
c277df42 372 while (s <= last1) {
a0d0e21e 373 if (regtry(prog, s))
374 goto got_it;
375 s++;
376 }
377 }
378 goto phooey;
c277df42 379 } else if (c = prog->regstclass) {
a0d0e21e 380 I32 doevery = (prog->reganch & ROPT_SKIP) == 0;
161b471a 381 char *Class;
a687059c 382
a0d0e21e 383 if (minlen)
384 dontbother = minlen - 1;
385 strend -= dontbother; /* don't bother with what can't match */
386 tmp = 1;
387 /* We know what class it must start with. */
388 switch (OP(c)) {
389 case ANYOF:
161b471a 390 Class = (char *) OPERAND(c);
a0d0e21e 391 while (s < strend) {
ae5c130c 392 if (REGINCLASS(Class, *s)) {
a0d0e21e 393 if (tmp && regtry(prog, s))
394 goto got_it;
395 else
396 tmp = doevery;
a687059c 397 }
a0d0e21e 398 else
399 tmp = 1;
400 s++;
401 }
402 break;
bbce6d69 403 case BOUNDL:
c277df42 404 reg_flags |= RF_tainted;
bbce6d69 405 /* FALL THROUGH */
a0d0e21e 406 case BOUND:
407 if (minlen)
408 dontbother++,strend--;
bbce6d69 409 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
95bac841 410 tmp = ((OP(c) == BOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
a0d0e21e 411 while (s < strend) {
95bac841 412 if (tmp == !(OP(c) == BOUND ? isALNUM(*s) : isALNUM_LC(*s))) {
a0d0e21e 413 tmp = !tmp;
414 if (regtry(prog, s))
415 goto got_it;
a687059c 416 }
a0d0e21e 417 s++;
418 }
419 if ((minlen || tmp) && regtry(prog,s))
420 goto got_it;
421 break;
bbce6d69 422 case NBOUNDL:
c277df42 423 reg_flags |= RF_tainted;
bbce6d69 424 /* FALL THROUGH */
a0d0e21e 425 case NBOUND:
426 if (minlen)
427 dontbother++,strend--;
bbce6d69 428 tmp = (s != startpos) ? UCHARAT(s - 1) : regprev;
95bac841 429 tmp = ((OP(c) == NBOUND ? isALNUM(tmp) : isALNUM_LC(tmp)) != 0);
a0d0e21e 430 while (s < strend) {
95bac841 431 if (tmp == !(OP(c) == NBOUND ? isALNUM(*s) : isALNUM_LC(*s)))
a0d0e21e 432 tmp = !tmp;
433 else if (regtry(prog, s))
434 goto got_it;
435 s++;
436 }
437 if ((minlen || !tmp) && regtry(prog,s))
438 goto got_it;
439 break;
440 case ALNUM:
441 while (s < strend) {
bbce6d69 442 if (isALNUM(*s)) {
443 if (tmp && regtry(prog, s))
444 goto got_it;
445 else
446 tmp = doevery;
447 }
448 else
449 tmp = 1;
450 s++;
451 }
452 break;
453 case ALNUML:
c277df42 454 reg_flags |= RF_tainted;
bbce6d69 455 while (s < strend) {
456 if (isALNUM_LC(*s)) {
a0d0e21e 457 if (tmp && regtry(prog, s))
458 goto got_it;
a687059c 459 else
a0d0e21e 460 tmp = doevery;
461 }
462 else
463 tmp = 1;
464 s++;
465 }
466 break;
467 case NALNUM:
468 while (s < strend) {
bbce6d69 469 if (!isALNUM(*s)) {
470 if (tmp && regtry(prog, s))
471 goto got_it;
472 else
473 tmp = doevery;
474 }
475 else
476 tmp = 1;
477 s++;
478 }
479 break;
480 case NALNUML:
c277df42 481 reg_flags |= RF_tainted;
bbce6d69 482 while (s < strend) {
483 if (!isALNUM_LC(*s)) {
a0d0e21e 484 if (tmp && regtry(prog, s))
485 goto got_it;
a687059c 486 else
a0d0e21e 487 tmp = doevery;
a687059c 488 }
a0d0e21e 489 else
490 tmp = 1;
491 s++;
492 }
493 break;
494 case SPACE:
495 while (s < strend) {
496 if (isSPACE(*s)) {
497 if (tmp && regtry(prog, s))
498 goto got_it;
499 else
500 tmp = doevery;
2304df62 501 }
a0d0e21e 502 else
503 tmp = 1;
504 s++;
505 }
506 break;
bbce6d69 507 case SPACEL:
c277df42 508 reg_flags |= RF_tainted;
bbce6d69 509 while (s < strend) {
510 if (isSPACE_LC(*s)) {
511 if (tmp && regtry(prog, s))
512 goto got_it;
513 else
514 tmp = doevery;
515 }
516 else
517 tmp = 1;
518 s++;
519 }
520 break;
a0d0e21e 521 case NSPACE:
522 while (s < strend) {
523 if (!isSPACE(*s)) {
524 if (tmp && regtry(prog, s))
525 goto got_it;
526 else
527 tmp = doevery;
a687059c 528 }
a0d0e21e 529 else
530 tmp = 1;
531 s++;
532 }
533 break;
bbce6d69 534 case NSPACEL:
c277df42 535 reg_flags |= RF_tainted;
bbce6d69 536 while (s < strend) {
537 if (!isSPACE_LC(*s)) {
538 if (tmp && regtry(prog, s))
539 goto got_it;
540 else
541 tmp = doevery;
542 }
543 else
544 tmp = 1;
545 s++;
546 }
547 break;
a0d0e21e 548 case DIGIT:
549 while (s < strend) {
550 if (isDIGIT(*s)) {
551 if (tmp && regtry(prog, s))
552 goto got_it;
553 else
554 tmp = doevery;
2b69d0c2 555 }
a0d0e21e 556 else
557 tmp = 1;
558 s++;
559 }
560 break;
561 case NDIGIT:
562 while (s < strend) {
563 if (!isDIGIT(*s)) {
564 if (tmp && regtry(prog, s))
565 goto got_it;
566 else
567 tmp = doevery;
a687059c 568 }
a0d0e21e 569 else
570 tmp = 1;
571 s++;
572 }
573 break;
a687059c 574 }
a0d0e21e 575 }
576 else {
c277df42 577 dontbother = 0;
578 if (prog->float_substr != Nullsv) { /* Trim the end. */
579 char *last;
580 I32 oldpos = scream_pos;
581
582 if (screamer) {
583 last = screaminstr(screamer, prog->float_substr, s - strbeg,
584 end_shift, &scream_pos, 1); /* last one */
585 if (!last) {
586 last = scream_olds; /* Only one occurence. */
587 }
588 } else {
589 STRLEN len;
590 char *little = SvPV(prog->float_substr, len);
591 last = rninstr(s, strend, little, little + len);
592 }
593 if (last == NULL) goto phooey; /* Should not happen! */
594 dontbother = strend - last - 1;
595 }
596 if (minlen && (dontbother < minlen))
a0d0e21e 597 dontbother = minlen - 1;
598 strend -= dontbother;
599 /* We don't know much -- general case. */
600 do {
601 if (regtry(prog, s))
602 goto got_it;
603 } while (s++ < strend);
604 }
605
606 /* Failure. */
607 goto phooey;
a687059c 608
a0d0e21e 609got_it:
420218e7 610 strend += dontbother; /* uncheat */
a0d0e21e 611 prog->subbeg = strbeg;
612 prog->subend = strend;
c277df42 613 RX_MATCH_TAINTED_SET(prog, reg_flags & RF_tainted);
5f05dabc 614
615 /* make sure $`, $&, $', and $digit will work later */
c277df42 616 if (strbeg != prog->subbase) { /* second+ //g match. */
617 if (!(flags & REXEC_COPY_STR)) {
137443ea 618 if (prog->subbase) {
619 Safefree(prog->subbase);
620 prog->subbase = Nullch;
621 }
622 }
623 else {
624 I32 i = strend - startpos + (stringarg - strbeg);
625 s = savepvn(strbeg, i);
626 Safefree(prog->subbase);
627 prog->subbase = s;
628 prog->subbeg = prog->subbase;
629 prog->subend = prog->subbase + i;
630 s = prog->subbase + (stringarg - strbeg);
631 for (i = 0; i <= prog->nparens; i++) {
632 if (prog->endp[i]) {
633 prog->startp[i] = s + (prog->startp[i] - startpos);
634 prog->endp[i] = s + (prog->endp[i] - startpos);
635 }
a0d0e21e 636 }
637 }
a0d0e21e 638 }
ce862d02 639 /* Preserve the current value of $^R */
640 if (oreplsv != GvSV(replgv)) {
641 sv_setsv(oreplsv, GvSV(replgv));/* So that when GvSV(replgv) is
642 restored, the value remains
643 the same. */
644 }
a0d0e21e 645 return 1;
646
647phooey:
a0d0e21e 648 return 0;
a687059c 649}
650
651/*
652 - regtry - try match at specific point
653 */
76e3520e 654STATIC I32 /* 0 failure, 1 success */
8ac85365 655regtry(regexp *prog, char *startpos)
a687059c 656{
c277df42 657 dTHR;
a0d0e21e 658 register I32 i;
659 register char **sp;
660 register char **ep;
c277df42 661 CHECKPOINT lastcp;
a0d0e21e 662
ce862d02 663 if ((prog->reganch & ROPT_EVAL_SEEN) && !reg_eval_set) {
664 reg_eval_set = RS_init;
665 DEBUG_r(DEBUG_s(
666 PerlIO_printf(Perl_debug_log, " setting stack tmpbase at %i\n", stack_sp - stack_base);
667 ));
668 SAVEINT(cxstack[cxstack_ix].blk_oldsp);
669 cxstack[cxstack_ix].blk_oldsp = stack_sp - stack_base;
670 /* Otherwise OP_NEXTSTATE will free whatever on stack now. */
671 SAVETMPS;
672 /* Apparently this is not needed, judging by wantarray. */
673 /* SAVEINT(cxstack[cxstack_ix].blk_gimme);
674 cxstack[cxstack_ix].blk_gimme = G_SCALAR; */
675 }
a0d0e21e 676 reginput = startpos;
677 regstartp = prog->startp;
678 regendp = prog->endp;
679 reglastparen = &prog->lastparen;
680 prog->lastparen = 0;
681 regsize = 0;
c277df42 682 if (reg_start_tmpl <= prog->nparens) {
683 reg_start_tmpl = prog->nparens*3/2 + 3;
684 if(reg_start_tmp)
685 Renew(reg_start_tmp, reg_start_tmpl, char*);
686 else
687 New(22,reg_start_tmp, reg_start_tmpl, char*);
688 }
a0d0e21e 689
690 sp = prog->startp;
691 ep = prog->endp;
7fae4e64 692 regdata = prog->data;
a0d0e21e 693 if (prog->nparens) {
694 for (i = prog->nparens; i >= 0; i--) {
695 *sp++ = NULL;
696 *ep++ = NULL;
a687059c 697 }
a0d0e21e 698 }
c277df42 699 REGCP_SET;
a0d0e21e 700 if (regmatch(prog->program + 1) && reginput >= regtill) {
701 prog->startp[0] = startpos;
702 prog->endp[0] = reginput;
703 return 1;
704 }
c277df42 705 REGCP_UNWIND;
706 return 0;
a687059c 707}
708
709/*
710 - regmatch - main matching routine
711 *
712 * Conceptually the strategy is simple: check to see whether the current
713 * node matches, call self recursively to see whether the rest matches,
714 * and then act accordingly. In practice we make some effort to avoid
715 * recursion, in particular by going through "ordinary" nodes (that don't
716 * need to know whether the rest of the match failed) by a loop instead of
717 * by recursion.
718 */
719/* [lwall] I've hoisted the register declarations to the outer block in order to
720 * maybe save a little bit of pushing and popping on the stack. It also takes
721 * advantage of machines that use a register save mask on subroutine entry.
722 */
76e3520e 723STATIC I32 /* 0 failure, 1 success */
c277df42 724regmatch(regnode *prog)
a687059c 725{
c277df42 726 dTHR;
727 register regnode *scan; /* Current node. */
728 regnode *next; /* Next node. */
729 regnode *inner; /* Next node in internal branch. */
76e3520e 730 register I32 nextchr; /* renamed nextchr - nextchar colides with function of same name */
a0d0e21e 731 register I32 n; /* no or next */
732 register I32 ln; /* len or last */
733 register char *s; /* operand or save */
734 register char *locinput = reginput;
c277df42 735 register I32 c1, c2, paren; /* case fold search, parenth */
736 int minmod = 0, sw = 0, logical = 0;
4633a7c4 737#ifdef DEBUGGING
4633a7c4 738 regindent++;
739#endif
a0d0e21e 740
76e3520e 741 nextchr = UCHARAT(locinput);
a0d0e21e 742 scan = prog;
743 while (scan != NULL) {
c277df42 744#define sayNO_L (logical ? (logical = 0, sw = 0, goto cont) : sayNO)
a687059c 745#ifdef DEBUGGING
c277df42 746# define sayYES goto yes
747# define sayNO goto no
748# define saySAME(x) if (x) goto yes; else goto no
749# define REPORT_CODE_OFF 24
4633a7c4 750#else
c277df42 751# define sayYES return 1
752# define sayNO return 0
753# define saySAME(x) return x
a687059c 754#endif
c277df42 755 DEBUG_r( {
756 SV *prop = sv_newmortal();
757 int docolor = *colors[0];
758 int taill = (docolor ? 10 : 7); /* 3 chars for "> <" */
759 int l = (regeol - locinput > taill ? taill : regeol - locinput);
760 int pref_len = (locinput - bostr > (5 + taill) - l
761 ? (5 + taill) - l : locinput - bostr);
762
763 if (l + pref_len < (5 + taill) && l < regeol - locinput)
764 l = ( regeol - locinput > (5 + taill) - pref_len
765 ? (5 + taill) - pref_len : regeol - locinput);
766 regprop(prop, scan);
767 PerlIO_printf(Perl_debug_log,
768 "%4i <%s%.*s%s%s%s%.*s%s>%*s|%*s%2d%s\n",
769 locinput - bostr,
770 colors[2], pref_len, locinput - pref_len, colors[3],
771 (docolor ? "" : "> <"),
772 colors[0], l, locinput, colors[1],
773 15 - l - pref_len + 1,
774 "",
775 regindent*2, "", scan - regprogram,
776 SvPVX(prop));
777 } );
a687059c 778
c277df42 779 next = scan + NEXT_OFF(scan);
a0d0e21e 780 if (next == scan)
781 next = NULL;
a687059c 782
a0d0e21e 783 switch (OP(scan)) {
784 case BOL:
785 if (locinput == regbol
786 ? regprev == '\n'
c277df42 787 : (multiline &&
76e3520e 788 (nextchr || locinput < regeol) && locinput[-1] == '\n') )
a0d0e21e 789 {
790 /* regtill = regbol; */
791 break;
792 }
4633a7c4 793 sayNO;
a0d0e21e 794 case MBOL:
795 if (locinput == regbol
796 ? regprev == '\n'
76e3520e 797 : ((nextchr || locinput < regeol) && locinput[-1] == '\n') )
a0d0e21e 798 {
799 break;
800 }
4633a7c4 801 sayNO;
a0d0e21e 802 case SBOL:
803 if (locinput == regbol && regprev == '\n')
804 break;
4633a7c4 805 sayNO;
774d564b 806 case GPOS:
a0d0e21e 807 if (locinput == regbol)
808 break;
4633a7c4 809 sayNO;
a0d0e21e 810 case EOL:
811 if (multiline)
812 goto meol;
813 else
814 goto seol;
815 case MEOL:
816 meol:
76e3520e 817 if ((nextchr || locinput < regeol) && nextchr != '\n')
4633a7c4 818 sayNO;
a0d0e21e 819 break;
820 case SEOL:
821 seol:
76e3520e 822 if ((nextchr || locinput < regeol) && nextchr != '\n')
4633a7c4 823 sayNO;
a0d0e21e 824 if (regeol - locinput > 1)
4633a7c4 825 sayNO;
a0d0e21e 826 break;
827 case SANY:
76e3520e 828 if (!nextchr && locinput >= regeol)
4633a7c4 829 sayNO;
76e3520e 830 nextchr = UCHARAT(++locinput);
a0d0e21e 831 break;
832 case ANY:
76e3520e 833 if (!nextchr && locinput >= regeol || nextchr == '\n')
4633a7c4 834 sayNO;
76e3520e 835 nextchr = UCHARAT(++locinput);
a0d0e21e 836 break;
bbce6d69 837 case EXACT:
161b471a 838 s = (char *) OPERAND(scan);
c277df42 839 ln = UCHARAT(s++);
a0d0e21e 840 /* Inline the first character, for speed. */
76e3520e 841 if (UCHARAT(s) != nextchr)
4633a7c4 842 sayNO;
a0d0e21e 843 if (regeol - locinput < ln)
4633a7c4 844 sayNO;
36477c24 845 if (ln > 1 && memNE(s, locinput, ln))
4633a7c4 846 sayNO;
a0d0e21e 847 locinput += ln;
76e3520e 848 nextchr = UCHARAT(locinput);
bbce6d69 849 break;
850 case EXACTFL:
c277df42 851 reg_flags |= RF_tainted;
bbce6d69 852 /* FALL THROUGH */
853 case EXACTF:
161b471a 854 s = (char *) OPERAND(scan);
c277df42 855 ln = UCHARAT(s++);
bbce6d69 856 /* Inline the first character, for speed. */
76e3520e 857 if (UCHARAT(s) != nextchr &&
bbce6d69 858 UCHARAT(s) != ((OP(scan) == EXACTF)
76e3520e 859 ? fold : fold_locale)[nextchr])
bbce6d69 860 sayNO;
861 if (regeol - locinput < ln)
862 sayNO;
5f05dabc 863 if (ln > 1 && (OP(scan) == EXACTF
864 ? ibcmp(s, locinput, ln)
865 : ibcmp_locale(s, locinput, ln)))
bbce6d69 866 sayNO;
867 locinput += ln;
76e3520e 868 nextchr = UCHARAT(locinput);
a0d0e21e 869 break;
870 case ANYOF:
161b471a 871 s = (char *) OPERAND(scan);
76e3520e 872 if (nextchr < 0)
873 nextchr = UCHARAT(locinput);
873ef191 874 if (!REGINCLASS(s, nextchr))
4633a7c4 875 sayNO;
76e3520e 876 if (!nextchr && locinput >= regeol)
4633a7c4 877 sayNO;
76e3520e 878 nextchr = UCHARAT(++locinput);
a0d0e21e 879 break;
bbce6d69 880 case ALNUML:
c277df42 881 reg_flags |= RF_tainted;
bbce6d69 882 /* FALL THROUGH */
a0d0e21e 883 case ALNUM:
76e3520e 884 if (!nextchr)
4633a7c4 885 sayNO;
bbce6d69 886 if (!(OP(scan) == ALNUM
76e3520e 887 ? isALNUM(nextchr) : isALNUM_LC(nextchr)))
4633a7c4 888 sayNO;
76e3520e 889 nextchr = UCHARAT(++locinput);
a0d0e21e 890 break;
bbce6d69 891 case NALNUML:
c277df42 892 reg_flags |= RF_tainted;
bbce6d69 893 /* FALL THROUGH */
a0d0e21e 894 case NALNUM:
76e3520e 895 if (!nextchr && locinput >= regeol)
4633a7c4 896 sayNO;
bbce6d69 897 if (OP(scan) == NALNUM
76e3520e 898 ? isALNUM(nextchr) : isALNUM_LC(nextchr))
4633a7c4 899 sayNO;
76e3520e 900 nextchr = UCHARAT(++locinput);
a0d0e21e 901 break;
bbce6d69 902 case BOUNDL:
903 case NBOUNDL:
c277df42 904 reg_flags |= RF_tainted;
bbce6d69 905 /* FALL THROUGH */
a0d0e21e 906 case BOUND:
bbce6d69 907 case NBOUND:
908 /* was last char in word? */
909 ln = (locinput != regbol) ? UCHARAT(locinput - 1) : regprev;
910 if (OP(scan) == BOUND || OP(scan) == NBOUND) {
911 ln = isALNUM(ln);
76e3520e 912 n = isALNUM(nextchr);
bbce6d69 913 }
914 else {
915 ln = isALNUM_LC(ln);
76e3520e 916 n = isALNUM_LC(nextchr);
bbce6d69 917 }
95bac841 918 if (((!ln) == (!n)) == (OP(scan) == BOUND || OP(scan) == BOUNDL))
4633a7c4 919 sayNO;
a0d0e21e 920 break;
bbce6d69 921 case SPACEL:
c277df42 922 reg_flags |= RF_tainted;
bbce6d69 923 /* FALL THROUGH */
a0d0e21e 924 case SPACE:
76e3520e 925 if (!nextchr && locinput >= regeol)
4633a7c4 926 sayNO;
bbce6d69 927 if (!(OP(scan) == SPACE
76e3520e 928 ? isSPACE(nextchr) : isSPACE_LC(nextchr)))
4633a7c4 929 sayNO;
76e3520e 930 nextchr = UCHARAT(++locinput);
a0d0e21e 931 break;
bbce6d69 932 case NSPACEL:
c277df42 933 reg_flags |= RF_tainted;
bbce6d69 934 /* FALL THROUGH */
a0d0e21e 935 case NSPACE:
76e3520e 936 if (!nextchr)
4633a7c4 937 sayNO;
bbce6d69 938 if (OP(scan) == SPACE
76e3520e 939 ? isSPACE(nextchr) : isSPACE_LC(nextchr))
4633a7c4 940 sayNO;
76e3520e 941 nextchr = UCHARAT(++locinput);
a0d0e21e 942 break;
943 case DIGIT:
76e3520e 944 if (!isDIGIT(nextchr))
4633a7c4 945 sayNO;
76e3520e 946 nextchr = UCHARAT(++locinput);
a0d0e21e 947 break;
948 case NDIGIT:
76e3520e 949 if (!nextchr && locinput >= regeol)
4633a7c4 950 sayNO;
76e3520e 951 if (isDIGIT(nextchr))
4633a7c4 952 sayNO;
76e3520e 953 nextchr = UCHARAT(++locinput);
a0d0e21e 954 break;
c8756f30 955 case REFFL:
c277df42 956 reg_flags |= RF_tainted;
c8756f30 957 /* FALL THROUGH */
c277df42 958 case REF:
c8756f30 959 case REFF:
c277df42 960 n = ARG(scan); /* which paren pair */
a0d0e21e 961 s = regstartp[n];
c277df42 962 if (*reglastparen < n || !s)
af3f8c16 963 sayNO; /* Do not match unless seen CLOSEn. */
a0d0e21e 964 if (s == regendp[n])
965 break;
966 /* Inline the first character, for speed. */
76e3520e 967 if (UCHARAT(s) != nextchr &&
c8756f30 968 (OP(scan) == REF ||
969 (UCHARAT(s) != ((OP(scan) == REFF
76e3520e 970 ? fold : fold_locale)[nextchr]))))
4633a7c4 971 sayNO;
a0d0e21e 972 ln = regendp[n] - s;
973 if (locinput + ln > regeol)
4633a7c4 974 sayNO;
c8756f30 975 if (ln > 1 && (OP(scan) == REF
976 ? memNE(s, locinput, ln)
977 : (OP(scan) == REFF
978 ? ibcmp(s, locinput, ln)
979 : ibcmp_locale(s, locinput, ln))))
4633a7c4 980 sayNO;
a0d0e21e 981 locinput += ln;
76e3520e 982 nextchr = UCHARAT(locinput);
a0d0e21e 983 break;
984
985 case NOTHING:
c277df42 986 case TAIL:
a0d0e21e 987 break;
988 case BACK:
989 break;
c277df42 990 case EVAL:
991 {
992 dSP;
993 OP_4tree *oop = op;
994 COP *ocurcop = curcop;
995 SV **ocurpad = curpad;
996 SV *ret;
997
998 n = ARG(scan);
7fae4e64 999 op = (OP_4tree*)regdata->data[n];
c277df42 1000 DEBUG_r( PerlIO_printf(Perl_debug_log, " re_eval 0x%x\n", op) );
7fae4e64 1001 curpad = AvARRAY((AV*)regdata->data[n + 1]);
c277df42 1002
76e3520e 1003 CALLRUNOPS(); /* Scalar context. */
c277df42 1004 SPAGAIN;
1005 ret = POPs;
1006 PUTBACK;
1007
1008 if (logical) {
1009 logical = 0;
1010 sw = SvTRUE(ret);
ce862d02 1011 } else
1012 sv_setsv(save_scalar(replgv), ret);
c277df42 1013 op = oop;
1014 curpad = ocurpad;
1015 curcop = ocurcop;
1016 break;
1017 }
a0d0e21e 1018 case OPEN:
c277df42 1019 n = ARG(scan); /* which paren pair */
1020 reg_start_tmp[n] = locinput;
a0d0e21e 1021 if (n > regsize)
1022 regsize = n;
1023 break;
1024 case CLOSE:
c277df42 1025 n = ARG(scan); /* which paren pair */
1026 regstartp[n] = reg_start_tmp[n];
a0d0e21e 1027 regendp[n] = locinput;
1028 if (n > *reglastparen)
1029 *reglastparen = n;
1030 break;
c277df42 1031 case GROUPP:
1032 n = ARG(scan); /* which paren pair */
1033 sw = (*reglastparen >= n && regendp[n] != NULL);
1034 break;
1035 case IFTHEN:
1036 if (sw)
1037 next = NEXTOPER(NEXTOPER(scan));
1038 else {
1039 next = scan + ARG(scan);
1040 if (OP(next) == IFTHEN) /* Fake one. */
1041 next = NEXTOPER(NEXTOPER(next));
1042 }
1043 break;
1044 case LOGICAL:
1045 logical = 1;
1046 break;
a0d0e21e 1047 case CURLYX: {
1048 CURCUR cc;
1049 CHECKPOINT cp = savestack_ix;
c277df42 1050
1051 if (OP(PREVOPER(next)) == NOTHING) /* LONGJMP */
1052 next += ARG(next);
a0d0e21e 1053 cc.oldcc = regcc;
1054 regcc = &cc;
1055 cc.parenfloor = *reglastparen;
1056 cc.cur = -1;
1057 cc.min = ARG1(scan);
1058 cc.max = ARG2(scan);
c277df42 1059 cc.scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
a0d0e21e 1060 cc.next = next;
1061 cc.minmod = minmod;
1062 cc.lastloc = 0;
1063 reginput = locinput;
1064 n = regmatch(PREVOPER(next)); /* start on the WHILEM */
1065 regcpblow(cp);
1066 regcc = cc.oldcc;
4633a7c4 1067 saySAME(n);
a0d0e21e 1068 }
1069 /* NOT REACHED */
1070 case WHILEM: {
1071 /*
1072 * This is really hard to understand, because after we match
1073 * what we're trying to match, we must make sure the rest of
1074 * the RE is going to match for sure, and to do that we have
1075 * to go back UP the parse tree by recursing ever deeper. And
1076 * if it fails, we have to reset our parent's current state
1077 * that we can try again after backing off.
1078 */
1079
c277df42 1080 CHECKPOINT cp, lastcp;
a0d0e21e 1081 CURCUR* cc = regcc;
c277df42 1082 char *lastloc = cc->lastloc; /* Detection of 0-len. */
1083
4633a7c4 1084 n = cc->cur + 1; /* how many we know we matched */
a0d0e21e 1085 reginput = locinput;
1086
c277df42 1087 DEBUG_r(
1088 PerlIO_printf(Perl_debug_log,
1089 "%*s %ld out of %ld..%ld cc=%lx\n",
1090 REPORT_CODE_OFF+regindent*2, "",
1091 (long)n, (long)cc->min,
1092 (long)cc->max, (long)cc)
1093 );
4633a7c4 1094
a0d0e21e 1095 /* If degenerate scan matches "", assume scan done. */
1096
579cf2c3 1097 if (locinput == cc->lastloc && n >= cc->min) {
a0d0e21e 1098 regcc = cc->oldcc;
1099 ln = regcc->cur;
c277df42 1100 DEBUG_r(
1101 PerlIO_printf(Perl_debug_log, "%*s empty match detected, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1102 );
a0d0e21e 1103 if (regmatch(cc->next))
4633a7c4 1104 sayYES;
c277df42 1105 DEBUG_r(
1106 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1107 );
a0d0e21e 1108 regcc->cur = ln;
1109 regcc = cc;
4633a7c4 1110 sayNO;
a0d0e21e 1111 }
1112
1113 /* First just match a string of min scans. */
1114
1115 if (n < cc->min) {
1116 cc->cur = n;
1117 cc->lastloc = locinput;
4633a7c4 1118 if (regmatch(cc->scan))
1119 sayYES;
1120 cc->cur = n - 1;
c277df42 1121 cc->lastloc = lastloc;
1122 DEBUG_r(
1123 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1124 );
4633a7c4 1125 sayNO;
a0d0e21e 1126 }
1127
1128 /* Prefer next over scan for minimal matching. */
1129
1130 if (cc->minmod) {
1131 regcc = cc->oldcc;
1132 ln = regcc->cur;
5f05dabc 1133 cp = regcppush(cc->parenfloor);
c277df42 1134 REGCP_SET;
5f05dabc 1135 if (regmatch(cc->next)) {
c277df42 1136 regcpblow(cp);
4633a7c4 1137 sayYES; /* All done. */
5f05dabc 1138 }
c277df42 1139 REGCP_UNWIND;
5f05dabc 1140 regcppop();
a0d0e21e 1141 regcc->cur = ln;
1142 regcc = cc;
1143
c277df42 1144 if (n >= cc->max) { /* Maximum greed exceeded? */
1145 if (dowarn && n >= REG_INFTY
1146 && !(reg_flags & RF_warned)) {
1147 reg_flags |= RF_warned;
1148 warn("count exceeded %d", REG_INFTY - 1);
1149 }
4633a7c4 1150 sayNO;
c277df42 1151 }
a687059c 1152
c277df42 1153 DEBUG_r(
1154 PerlIO_printf(Perl_debug_log, "%*s trying longer...\n", REPORT_CODE_OFF+regindent*2, "")
1155 );
a0d0e21e 1156 /* Try scanning more and see if it helps. */
1157 reginput = locinput;
1158 cc->cur = n;
1159 cc->lastloc = locinput;
5f05dabc 1160 cp = regcppush(cc->parenfloor);
c277df42 1161 REGCP_SET;
5f05dabc 1162 if (regmatch(cc->scan)) {
c277df42 1163 regcpblow(cp);
4633a7c4 1164 sayYES;
5f05dabc 1165 }
c277df42 1166 DEBUG_r(
1167 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1168 );
1169 REGCP_UNWIND;
5f05dabc 1170 regcppop();
4633a7c4 1171 cc->cur = n - 1;
c277df42 1172 cc->lastloc = lastloc;
4633a7c4 1173 sayNO;
a0d0e21e 1174 }
1175
1176 /* Prefer scan over next for maximal matching. */
1177
1178 if (n < cc->max) { /* More greed allowed? */
5f05dabc 1179 cp = regcppush(cc->parenfloor);
a0d0e21e 1180 cc->cur = n;
1181 cc->lastloc = locinput;
c277df42 1182 REGCP_SET;
5f05dabc 1183 if (regmatch(cc->scan)) {
c277df42 1184 regcpblow(cp);
4633a7c4 1185 sayYES;
5f05dabc 1186 }
c277df42 1187 REGCP_UNWIND;
a0d0e21e 1188 regcppop(); /* Restore some previous $<digit>s? */
1189 reginput = locinput;
c277df42 1190 DEBUG_r(
1191 PerlIO_printf(Perl_debug_log, "%*s failed, try continuation...\n", REPORT_CODE_OFF+regindent*2, "")
1192 );
1193 }
1194 if (dowarn && n >= REG_INFTY && !(reg_flags & RF_warned)) {
1195 reg_flags |= RF_warned;
1196 warn("count exceeded %d", REG_INFTY - 1);
a0d0e21e 1197 }
1198
1199 /* Failed deeper matches of scan, so see if this one works. */
1200 regcc = cc->oldcc;
1201 ln = regcc->cur;
1202 if (regmatch(cc->next))
4633a7c4 1203 sayYES;
c277df42 1204 DEBUG_r(
1205 PerlIO_printf(Perl_debug_log, "%*s failed...\n", REPORT_CODE_OFF+regindent*2, "")
1206 );
a0d0e21e 1207 regcc->cur = ln;
1208 regcc = cc;
4633a7c4 1209 cc->cur = n - 1;
c277df42 1210 cc->lastloc = lastloc;
4633a7c4 1211 sayNO;
a0d0e21e 1212 }
1213 /* NOT REACHED */
c277df42 1214 case BRANCHJ:
1215 next = scan + ARG(scan);
1216 if (next == scan)
1217 next = NULL;
1218 inner = NEXTOPER(NEXTOPER(scan));
1219 goto do_branch;
1220 case BRANCH:
1221 inner = NEXTOPER(scan);
1222 do_branch:
1223 {
1224 CHECKPOINT lastcp;
1225 c1 = OP(scan);
1226 if (OP(next) != c1) /* No choice. */
1227 next = inner; /* Avoid recursion. */
a0d0e21e 1228 else {
748a9306 1229 int lastparen = *reglastparen;
c277df42 1230
1231 REGCP_SET;
a0d0e21e 1232 do {
1233 reginput = locinput;
c277df42 1234 if (regmatch(inner))
4633a7c4 1235 sayYES;
c277df42 1236 REGCP_UNWIND;
748a9306 1237 for (n = *reglastparen; n > lastparen; n--)
1238 regendp[n] = 0;
1239 *reglastparen = n;
c277df42 1240 scan = next;
a0d0e21e 1241 /*SUPPRESS 560*/
c277df42 1242 if (n = (c1 == BRANCH ? NEXT_OFF(next) : ARG(next)))
1243 next += n;
a0d0e21e 1244 else
c277df42 1245 next = NULL;
c277df42 1246 inner = NEXTOPER(scan);
1247 if (c1 == BRANCHJ) {
1248 inner = NEXTOPER(inner);
1249 }
1250 } while (scan != NULL && OP(scan) == c1);
4633a7c4 1251 sayNO;
a0d0e21e 1252 /* NOTREACHED */
a687059c 1253 }
a0d0e21e 1254 }
1255 break;
1256 case MINMOD:
1257 minmod = 1;
1258 break;
c277df42 1259 case CURLYM:
1260 {
1261 I32 l;
1262 CHECKPOINT lastcp;
1263
1264 /* We suppose that the next guy does not need
1265 backtracking: in particular, it is of constant length,
1266 and has no parenths to influence future backrefs. */
1267 ln = ARG1(scan); /* min to match */
1268 n = ARG2(scan); /* max to match */
c277df42 1269 paren = scan->flags;
1270 if (paren) {
1271 if (paren > regsize)
1272 regsize = paren;
1273 if (paren > *reglastparen)
1274 *reglastparen = paren;
1275 }
dc45a647 1276 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
c277df42 1277 if (paren)
1278 scan += NEXT_OFF(scan); /* Skip former OPEN. */
1279 reginput = locinput;
1280 if (minmod) {
1281 minmod = 0;
1282 if (ln && regrepeat_hard(scan, ln, &l) < ln)
1283 sayNO;
5f4b28b2 1284 if (ln && l == 0 && n >= ln
c277df42 1285 /* In fact, this is tricky. If paren, then the
1286 fact that we did/didnot match may influence
1287 future execution. */
1288 && !(paren && ln == 0))
1289 ln = n;
1290 locinput = reginput;
1291 if (regkind[(U8)OP(next)] == EXACT) {
1292 c1 = UCHARAT(OPERAND(next) + 1);
1293 if (OP(next) == EXACTF)
1294 c2 = fold[c1];
1295 else if (OP(next) == EXACTFL)
1296 c2 = fold_locale[c1];
1297 else
1298 c2 = c1;
1299 } else
1300 c1 = c2 = -1000;
1301 REGCP_SET;
5f4b28b2 1302 /* This may be improved if l == 0. */
c277df42 1303 while (n >= ln || (n == REG_INFTY && ln > 0 && l)) { /* ln overflow ? */
1304 /* If it could work, try it. */
1305 if (c1 == -1000 ||
1306 UCHARAT(reginput) == c1 ||
1307 UCHARAT(reginput) == c2)
1308 {
1309 if (paren) {
1310 if (n) {
1311 regstartp[paren] = reginput - l;
1312 regendp[paren] = reginput;
1313 } else
1314 regendp[paren] = NULL;
1315 }
1316 if (regmatch(next))
1317 sayYES;
1318 REGCP_UNWIND;
1319 }
1320 /* Couldn't or didn't -- move forward. */
1321 reginput = locinput;
1322 if (regrepeat_hard(scan, 1, &l)) {
1323 ln++;
1324 locinput = reginput;
1325 }
1326 else
1327 sayNO;
1328 }
1329 } else {
1330 n = regrepeat_hard(scan, n, &l);
1331 if (n != 0 && l == 0
1332 /* In fact, this is tricky. If paren, then the
1333 fact that we did/didnot match may influence
1334 future execution. */
1335 && !(paren && ln == 0))
1336 ln = n;
1337 locinput = reginput;
1338 DEBUG_r(
1339 PerlIO_printf(Perl_debug_log, "%*s matched %ld times, len=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n, l)
1340 );
1341 if (n >= ln) {
1342 if (regkind[(U8)OP(next)] == EXACT) {
1343 c1 = UCHARAT(OPERAND(next) + 1);
1344 if (OP(next) == EXACTF)
1345 c2 = fold[c1];
1346 else if (OP(next) == EXACTFL)
1347 c2 = fold_locale[c1];
1348 else
1349 c2 = c1;
1350 } else
1351 c1 = c2 = -1000;
1352 }
1353 REGCP_SET;
1354 while (n >= ln) {
1355 /* If it could work, try it. */
1356 if (c1 == -1000 ||
1357 UCHARAT(reginput) == c1 ||
1358 UCHARAT(reginput) == c2)
1359 {
1360 DEBUG_r(
1361 PerlIO_printf(Perl_debug_log, "%*s trying tail with n=%ld...\n", REPORT_CODE_OFF+regindent*2, "", n)
1362 );
1363 if (paren) {
1364 if (n) {
1365 regstartp[paren] = reginput - l;
1366 regendp[paren] = reginput;
1367 } else
1368 regendp[paren] = NULL;
1369 }
1370 if (regmatch(next))
1371 sayYES;
1372 REGCP_UNWIND;
1373 }
1374 /* Couldn't or didn't -- back up. */
1375 n--;
1376 locinput -= l;
1377 reginput = locinput;
1378 }
1379 }
1380 sayNO;
1381 break;
1382 }
1383 case CURLYN:
1384 paren = scan->flags; /* Which paren to set */
1385 if (paren > regsize)
1386 regsize = paren;
1387 if (paren > *reglastparen)
1388 *reglastparen = paren;
1389 ln = ARG1(scan); /* min to match */
1390 n = ARG2(scan); /* max to match */
dc45a647 1391 scan = regnext(NEXTOPER(scan) + NODE_STEP_REGNODE);
c277df42 1392 goto repeat;
a0d0e21e 1393 case CURLY:
c277df42 1394 paren = 0;
a0d0e21e 1395 ln = ARG1(scan); /* min to match */
1396 n = ARG2(scan); /* max to match */
dc45a647 1397 scan = NEXTOPER(scan) + NODE_STEP_REGNODE;
a0d0e21e 1398 goto repeat;
1399 case STAR:
1400 ln = 0;
c277df42 1401 n = REG_INFTY;
a0d0e21e 1402 scan = NEXTOPER(scan);
c277df42 1403 paren = 0;
a0d0e21e 1404 goto repeat;
1405 case PLUS:
c277df42 1406 ln = 1;
1407 n = REG_INFTY;
1408 scan = NEXTOPER(scan);
1409 paren = 0;
1410 repeat:
a0d0e21e 1411 /*
1412 * Lookahead to avoid useless match attempts
1413 * when we know what character comes next.
1414 */
bbce6d69 1415 if (regkind[(U8)OP(next)] == EXACT) {
1416 c1 = UCHARAT(OPERAND(next) + 1);
1417 if (OP(next) == EXACTF)
1418 c2 = fold[c1];
1419 else if (OP(next) == EXACTFL)
1420 c2 = fold_locale[c1];
1421 else
1422 c2 = c1;
1423 }
a0d0e21e 1424 else
bbce6d69 1425 c1 = c2 = -1000;
a0d0e21e 1426 reginput = locinput;
1427 if (minmod) {
c277df42 1428 CHECKPOINT lastcp;
a0d0e21e 1429 minmod = 0;
1430 if (ln && regrepeat(scan, ln) < ln)
4633a7c4 1431 sayNO;
c277df42 1432 REGCP_SET;
1433 while (n >= ln || (n == REG_INFTY && ln > 0)) { /* ln overflow ? */
a0d0e21e 1434 /* If it could work, try it. */
bbce6d69 1435 if (c1 == -1000 ||
1436 UCHARAT(reginput) == c1 ||
1437 UCHARAT(reginput) == c2)
1438 {
c277df42 1439 if (paren) {
1440 if (n) {
1441 regstartp[paren] = reginput - 1;
1442 regendp[paren] = reginput;
1443 } else
1444 regendp[paren] = NULL;
1445 }
a0d0e21e 1446 if (regmatch(next))
4633a7c4 1447 sayYES;
c277df42 1448 REGCP_UNWIND;
bbce6d69 1449 }
c277df42 1450 /* Couldn't or didn't -- move forward. */
748a9306 1451 reginput = locinput + ln;
a0d0e21e 1452 if (regrepeat(scan, 1)) {
1453 ln++;
1454 reginput = locinput + ln;
c277df42 1455 } else
4633a7c4 1456 sayNO;
a0d0e21e 1457 }
1458 }
1459 else {
c277df42 1460 CHECKPOINT lastcp;
a0d0e21e 1461 n = regrepeat(scan, n);
1462 if (ln < n && regkind[(U8)OP(next)] == EOL &&
c277df42 1463 (!multiline || OP(next) == SEOL))
a0d0e21e 1464 ln = n; /* why back off? */
c277df42 1465 REGCP_SET;
1466 if (paren) {
1467 while (n >= ln) {
1468 /* If it could work, try it. */
1469 if (c1 == -1000 ||
1470 UCHARAT(reginput) == c1 ||
1471 UCHARAT(reginput) == c2)
1472 {
1473 if (paren && n) {
1474 if (n) {
1475 regstartp[paren] = reginput - 1;
1476 regendp[paren] = reginput;
1477 } else
1478 regendp[paren] = NULL;
1479 }
1480 if (regmatch(next))
1481 sayYES;
1482 REGCP_UNWIND;
1483 }
1484 /* Couldn't or didn't -- back up. */
1485 n--;
1486 reginput = locinput + n;
1487 }
1488 } else {
1489 while (n >= ln) {
1490 /* If it could work, try it. */
1491 if (c1 == -1000 ||
1492 UCHARAT(reginput) == c1 ||
1493 UCHARAT(reginput) == c2)
1494 {
1495 if (regmatch(next))
1496 sayYES;
1497 REGCP_UNWIND;
1498 }
1499 /* Couldn't or didn't -- back up. */
1500 n--;
1501 reginput = locinput + n;
bbce6d69 1502 }
a0d0e21e 1503 }
1504 }
4633a7c4 1505 sayNO;
c277df42 1506 break;
a0d0e21e 1507 case SUCCEED:
1508 case END:
1509 reginput = locinput; /* put where regtry can find it */
4633a7c4 1510 sayYES; /* Success! */
c277df42 1511 case SUSPEND:
1512 n = 1;
1513 goto do_ifmatch;
a0d0e21e 1514 case UNLESSM:
c277df42 1515 n = 0;
1516 if (locinput < bostr + scan->flags)
1517 goto say_yes;
1518 goto do_ifmatch;
1519 case IFMATCH:
1520 n = 1;
1521 if (locinput < bostr + scan->flags)
1522 goto say_no;
1523 do_ifmatch:
1524 reginput = locinput - scan->flags;
1525 inner = NEXTOPER(NEXTOPER(scan));
1526 if (regmatch(inner) != n) {
1527 say_no:
1528 if (logical) {
1529 logical = 0;
1530 sw = 0;
1531 goto do_longjump;
1532 } else
1533 sayNO;
1534 }
1535 say_yes:
1536 if (logical) {
1537 logical = 0;
1538 sw = 1;
1539 }
fe44a5e8 1540 if (OP(scan) == SUSPEND) {
c277df42 1541 locinput = reginput;
565764a8 1542 nextchr = UCHARAT(locinput);
fe44a5e8 1543 }
c277df42 1544 /* FALL THROUGH. */
1545 case LONGJMP:
1546 do_longjump:
1547 next = scan + ARG(scan);
1548 if (next == scan)
1549 next = NULL;
a0d0e21e 1550 break;
1551 default:
c030ccd9 1552 PerlIO_printf(PerlIO_stderr(), "%lx %d\n",
c277df42 1553 (unsigned long)scan, OP(scan));
a0d0e21e 1554 FAIL("regexp memory corruption");
a687059c 1555 }
a0d0e21e 1556 scan = next;
1557 }
a687059c 1558
a0d0e21e 1559 /*
1560 * We get here only if there's trouble -- normally "case END" is
1561 * the terminating point.
1562 */
1563 FAIL("corrupted regexp pointers");
1564 /*NOTREACHED*/
4633a7c4 1565 sayNO;
1566
1567yes:
1568#ifdef DEBUGGING
1569 regindent--;
1570#endif
1571 return 1;
1572
1573no:
1574#ifdef DEBUGGING
1575 regindent--;
1576#endif
a0d0e21e 1577 return 0;
a687059c 1578}
1579
1580/*
1581 - regrepeat - repeatedly match something simple, report how many
1582 */
1583/*
1584 * [This routine now assumes that it will only match on things of length 1.
1585 * That was true before, but now we assume scan - reginput is the count,
1586 * rather than incrementing count on every character.]
1587 */
76e3520e 1588STATIC I32
c277df42 1589regrepeat(regnode *p, I32 max)
a687059c 1590{
a0d0e21e 1591 register char *scan;
1592 register char *opnd;
1593 register I32 c;
1594 register char *loceol = regeol;
1595
1596 scan = reginput;
c277df42 1597 if (max != REG_INFTY && max < loceol - scan)
a0d0e21e 1598 loceol = scan + max;
161b471a 1599 opnd = (char *) OPERAND(p);
a0d0e21e 1600 switch (OP(p)) {
1601 case ANY:
1602 while (scan < loceol && *scan != '\n')
1603 scan++;
1604 break;
1605 case SANY:
1606 scan = loceol;
1607 break;
bbce6d69 1608 case EXACT: /* length of string is 1 */
1609 c = UCHARAT(++opnd);
1610 while (scan < loceol && UCHARAT(scan) == c)
1611 scan++;
1612 break;
1613 case EXACTF: /* length of string is 1 */
1614 c = UCHARAT(++opnd);
1615 while (scan < loceol &&
1616 (UCHARAT(scan) == c || UCHARAT(scan) == fold[c]))
1617 scan++;
1618 break;
1619 case EXACTFL: /* length of string is 1 */
c277df42 1620 reg_flags |= RF_tainted;
bbce6d69 1621 c = UCHARAT(++opnd);
1622 while (scan < loceol &&
1623 (UCHARAT(scan) == c || UCHARAT(scan) == fold_locale[c]))
a0d0e21e 1624 scan++;
1625 break;
1626 case ANYOF:
ae5c130c 1627 while (scan < loceol && REGINCLASS(opnd, *scan))
a0d0e21e 1628 scan++;
a0d0e21e 1629 break;
1630 case ALNUM:
1631 while (scan < loceol && isALNUM(*scan))
1632 scan++;
1633 break;
bbce6d69 1634 case ALNUML:
c277df42 1635 reg_flags |= RF_tainted;
bbce6d69 1636 while (scan < loceol && isALNUM_LC(*scan))
1637 scan++;
1638 break;
a0d0e21e 1639 case NALNUM:
1640 while (scan < loceol && !isALNUM(*scan))
1641 scan++;
1642 break;
bbce6d69 1643 case NALNUML:
c277df42 1644 reg_flags |= RF_tainted;
bbce6d69 1645 while (scan < loceol && !isALNUM_LC(*scan))
1646 scan++;
1647 break;
a0d0e21e 1648 case SPACE:
1649 while (scan < loceol && isSPACE(*scan))
1650 scan++;
1651 break;
bbce6d69 1652 case SPACEL:
c277df42 1653 reg_flags |= RF_tainted;
bbce6d69 1654 while (scan < loceol && isSPACE_LC(*scan))
1655 scan++;
1656 break;
a0d0e21e 1657 case NSPACE:
1658 while (scan < loceol && !isSPACE(*scan))
1659 scan++;
1660 break;
bbce6d69 1661 case NSPACEL:
c277df42 1662 reg_flags |= RF_tainted;
bbce6d69 1663 while (scan < loceol && !isSPACE_LC(*scan))
1664 scan++;
1665 break;
a0d0e21e 1666 case DIGIT:
1667 while (scan < loceol && isDIGIT(*scan))
1668 scan++;
1669 break;
1670 case NDIGIT:
1671 while (scan < loceol && !isDIGIT(*scan))
1672 scan++;
1673 break;
1674 default: /* Called on something of 0 width. */
1675 break; /* So match right here or not at all. */
1676 }
a687059c 1677
a0d0e21e 1678 c = scan - reginput;
1679 reginput = scan;
a687059c 1680
c277df42 1681 DEBUG_r(
1682 {
1683 SV *prop = sv_newmortal();
1684
1685 regprop(prop, p);
1686 PerlIO_printf(Perl_debug_log,
1687 "%*s %s can match %ld times out of %ld...\n",
1688 REPORT_CODE_OFF+1, "", SvPVX(prop),c,max);
1689 });
1690
a0d0e21e 1691 return(c);
a687059c 1692}
1693
1694/*
c277df42 1695 - regrepeat_hard - repeatedly match something, report total lenth and length
1696 *
1697 * The repeater is supposed to have constant length.
1698 */
1699
76e3520e 1700STATIC I32
c277df42 1701regrepeat_hard(regnode *p, I32 max, I32 *lp)
1702{
1703 register char *scan;
1704 register char *start;
1705 register char *loceol = regeol;
1706 I32 l = -1;
1707
1708 start = reginput;
1709 while (reginput < loceol && (scan = reginput, regmatch(p))) {
1710 if (l == -1) {
1711 *lp = l = reginput - start;
1712 if (max != REG_INFTY && l*max < loceol - scan)
1713 loceol = scan + l*max;
1714 if (l == 0) {
1715 return max;
1716 }
1717 }
1718 }
1719 if (reginput < loceol)
1720 reginput = scan;
1721 else
1722 scan = reginput;
1723
1724 return (scan - start)/l;
1725}
1726
1727/*
bbce6d69 1728 - regclass - determine if a character falls into a character class
1729 */
1730
76e3520e 1731STATIC bool
8ac85365 1732reginclass(register char *p, register I32 c)
bbce6d69 1733{
1734 char flags = *p;
1735 bool match = FALSE;
1736
1737 c &= 0xFF;
ae5c130c 1738 if (ANYOF_TEST(p, c))
bbce6d69 1739 match = TRUE;
1740 else if (flags & ANYOF_FOLD) {
1741 I32 cf;
1742 if (flags & ANYOF_LOCALE) {
c277df42 1743 reg_flags |= RF_tainted;
bbce6d69 1744 cf = fold_locale[c];
1745 }
1746 else
1747 cf = fold[c];
ae5c130c 1748 if (ANYOF_TEST(p, cf))
bbce6d69 1749 match = TRUE;
1750 }
1751
1752 if (!match && (flags & ANYOF_ISA)) {
c277df42 1753 reg_flags |= RF_tainted;
bbce6d69 1754
1755 if (((flags & ANYOF_ALNUML) && isALNUM_LC(c)) ||
1756 ((flags & ANYOF_NALNUML) && !isALNUM_LC(c)) ||
1757 ((flags & ANYOF_SPACEL) && isSPACE_LC(c)) ||
1758 ((flags & ANYOF_NSPACEL) && !isSPACE_LC(c)))
1759 {
1760 match = TRUE;
1761 }
1762 }
1763
ae5c130c 1764 return (flags & ANYOF_INVERT) ? !match : match;
bbce6d69 1765}
1766
161b471a 1767
1768