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