This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
integarte malloc.c changes from mainline change#1807,2112,2133
[perl5.git] / regcomp.c
CommitLineData
a0d0e21e
LW
1/* regcomp.c
2 */
3
4/*
5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
6 */
7
a687059c
LW
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
AD
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
b9d5759e
AD
22#ifdef PERL_EXT_RE_BUILD
23/* need to replace pregcomp et al, so enable that */
24# ifndef PERL_IN_XSUB_RE
25# define PERL_IN_XSUB_RE
26# endif
27/* need access to debugger hooks */
28# ifndef DEBUGGING
29# define DEBUGGING
30# endif
31#endif
32
33#ifdef PERL_IN_XSUB_RE
d06ea78c 34/* We *really* need to overwrite these symbols: */
56953603
IZ
35# define Perl_pregcomp my_regcomp
36# define Perl_regdump my_regdump
37# define Perl_regprop my_regprop
d06ea78c
GS
38/* *These* symbols are masked to allow static link. */
39# define Perl_pregfree my_regfree
40# define Perl_regnext my_regnext
56953603
IZ
41#endif
42
f0fcb552 43/*SUPPRESS 112*/
a687059c 44/*
e50aee73 45 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c
LW
46 *
47 * Copyright (c) 1986 by University of Toronto.
48 * Written by Henry Spencer. Not derived from licensed software.
49 *
50 * Permission is granted to anyone to use this software for any
51 * purpose on any computer system, and to redistribute it freely,
52 * subject to the following restrictions:
53 *
54 * 1. The author is not responsible for the consequences of use of
55 * this software, no matter how awful, even if they arise
56 * from defects in it.
57 *
58 * 2. The origin of this software must not be misrepresented, either
59 * by explicit claim or by omission.
60 *
61 * 3. Altered versions must be plainly marked as such, and must not
62 * be misrepresented as being the original software.
63 *
64 *
65 **** Alterations to Henry's code are...
66 ****
9607fc9c 67 **** Copyright (c) 1991-1997, Larry Wall
a687059c 68 ****
9ef589d8
LW
69 **** You may distribute under the terms of either the GNU General Public
70 **** License or the Artistic License, as specified in the README file.
71
a687059c
LW
72 *
73 * Beware that some of this code is subtly aware of the way operator
74 * precedence is structured in regular expressions. Serious changes in
75 * regular-expression syntax might require a total rethink.
76 */
77#include "EXTERN.h"
78#include "perl.h"
d06ea78c 79
b9d5759e 80#ifndef PERL_IN_XSUB_RE
d06ea78c
GS
81# include "INTERN.h"
82#endif
c277df42
IZ
83
84#define REG_COMP_C
a687059c
LW
85#include "regcomp.h"
86
d4cce5f1 87#ifdef op
11343788 88#undef op
d4cce5f1 89#endif /* op */
11343788 90
fe14fcc3
LW
91#ifdef MSDOS
92# if defined(BUGGY_MSC6)
93 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
94 # pragma optimize("a",off)
95 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
96 # pragma optimize("w",on )
97# endif /* BUGGY_MSC6 */
98#endif /* MSDOS */
99
a687059c
LW
100#ifndef STATIC
101#define STATIC static
102#endif
103
104#define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
105#define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
106 ((*s) == '{' && regcurly(s)))
2b69d0c2
LW
107#ifdef atarist
108#define PERL_META "^$.[()|?+*\\"
109#else
a687059c 110#define META "^$.[()|?+*\\"
2b69d0c2 111#endif
a687059c 112
35c8bce7
LW
113#ifdef SPSTART
114#undef SPSTART /* dratted cpp namespace... */
115#endif
a687059c
LW
116/*
117 * Flags to be passed up and down.
118 */
a687059c 119#define WORST 0 /* Worst case. */
821b33a5 120#define HASWIDTH 0x1 /* Known to match non-null strings. */
a0d0e21e
LW
121#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
122#define SPSTART 0x4 /* Starts with * or +. */
123#define TRYAGAIN 0x8 /* Weeded out a declaration. */
a687059c
LW
124
125/*
e50aee73 126 * Forward declarations for pregcomp()'s friends.
a687059c 127 */
a0d0e21e 128
76e3520e 129#ifndef PERL_OBJECT
c277df42
IZ
130static regnode *reg _((I32, I32 *));
131static regnode *reganode _((U8, U32));
132static regnode *regatom _((I32 *));
133static regnode *regbranch _((I32 *, I32));
134static void regc _((U8, char *));
135static regnode *regclass _((void));
a0d0e21e 136STATIC I32 regcurly _((char *));
c277df42
IZ
137static regnode *reg_node _((U8));
138static regnode *regpiece _((I32 *));
139static void reginsert _((U8, regnode *));
140static void regoptail _((regnode *, regnode *));
c277df42 141static void regtail _((regnode *, regnode *));
873ef191 142static char* regwhite _((char *, char *));
a0d0e21e 143static char* nextchar _((void));
3bd495df 144static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
76e3520e 145#endif
a687059c 146
c277df42
IZ
147/* Length of a variant. */
148
76e3520e 149#ifndef PERL_OBJECT
c277df42
IZ
150typedef struct {
151 I32 len_min;
152 I32 len_delta;
153 I32 pos_min;
154 I32 pos_delta;
155 SV *last_found;
156 I32 last_end; /* min value, <0 unless valid. */
157 I32 last_start_min;
158 I32 last_start_max;
159 SV **longest; /* Either &l_fixed, or &l_float. */
160 SV *longest_fixed;
161 I32 offset_fixed;
162 SV *longest_float;
163 I32 offset_float_min;
164 I32 offset_float_max;
165 I32 flags;
166} scan_data_t;
76e3520e 167#endif
c277df42
IZ
168
169static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
170
171#define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
172#define SF_BEFORE_SEOL 0x1
173#define SF_BEFORE_MEOL 0x2
174#define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
175#define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
176
09b7f37c
CB
177#ifdef NO_UNARY_PLUS
178# define SF_FIX_SHIFT_EOL (0+2)
179# define SF_FL_SHIFT_EOL (0+4)
180#else
181# define SF_FIX_SHIFT_EOL (+2)
182# define SF_FL_SHIFT_EOL (+4)
183#endif
c277df42
IZ
184
185#define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
186#define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
187
188#define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
189#define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
190#define SF_IS_INF 0x40
191#define SF_HAS_PAR 0x80
192#define SF_IN_PAR 0x100
193#define SF_HAS_EVAL 0x200
4bfe0158 194#define SCF_DO_SUBSTR 0x400
c277df42 195
76e3520e 196STATIC void
c277df42
IZ
197scan_commit(scan_data_t *data)
198{
199 STRLEN l = SvCUR(data->last_found);
200 STRLEN old_l = SvCUR(*data->longest);
201
202 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
203 sv_setsv(*data->longest, data->last_found);
204 if (*data->longest == data->longest_fixed) {
205 data->offset_fixed = l ? data->last_start_min : data->pos_min;
206 if (data->flags & SF_BEFORE_EOL)
207 data->flags
208 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
209 else
210 data->flags &= ~SF_FIX_BEFORE_EOL;
211 } else {
212 data->offset_float_min = l ? data->last_start_min : data->pos_min;
213 data->offset_float_max = (l
214 ? data->last_start_max
215 : data->pos_min + data->pos_delta);
216 if (data->flags & SF_BEFORE_EOL)
217 data->flags
218 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
219 else
220 data->flags &= ~SF_FL_BEFORE_EOL;
221 }
222 }
223 SvCUR_set(data->last_found, 0);
224 data->last_end = -1;
225 data->flags &= ~SF_BEFORE_EOL;
226}
227
c277df42
IZ
228/* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
229 to the position after last scanned or to NULL. */
230
76e3520e 231STATIC I32
c277df42
IZ
232study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
233 /* scanp: Start here (read-write). */
234 /* deltap: Write maxlen-minlen here. */
235 /* last: Stop before this one. */
236{
5c0ca799 237 dTHR;
c277df42
IZ
238 I32 min = 0, pars = 0, code;
239 regnode *scan = *scanp, *next;
240 I32 delta = 0;
241 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
3da4a404 242 int is_inf_internal = 0; /* The studied chunk is infinite */
c277df42
IZ
243 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
244 scan_data_t data_fake;
245
246 while (scan && OP(scan) != END && scan < last) {
247 /* Peephole optimizer: */
248
249 if (regkind[(U8)OP(scan)] == EXACT) {
250 regnode *n = regnext(scan);
251 U32 stringok = 1;
252#ifdef DEBUGGING
253 regnode *stop = scan;
254#endif
255
256 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
257 /* Skip NOTHING, merge EXACT*. */
258 while (n &&
259 ( regkind[(U8)OP(n)] == NOTHING ||
260 (stringok && (OP(n) == OP(scan))))
261 && NEXT_OFF(n)
262 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
263 if (OP(n) == TAIL || n > next)
264 stringok = 0;
265 if (regkind[(U8)OP(n)] == NOTHING) {
266 NEXT_OFF(scan) += NEXT_OFF(n);
267 next = n + NODE_STEP_REGNODE;
268#ifdef DEBUGGING
269 if (stringok)
270 stop = n;
271#endif
272 n = regnext(n);
273 } else {
274 int oldl = *OPERAND(scan);
275 regnode *nnext = regnext(n);
276
277 if (oldl + *OPERAND(n) > U8_MAX)
278 break;
279 NEXT_OFF(scan) += NEXT_OFF(n);
280 *OPERAND(scan) += *OPERAND(n);
281 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
282 /* Now we can overwrite *n : */
283 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
284 *OPERAND(n) + 1, char);
285#ifdef DEBUGGING
286 if (stringok)
287 stop = next - 1;
288#endif
289 n = nnext;
290 }
291 }
292#ifdef DEBUGGING
293 /* Allow dumping */
294 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
295 while (n <= stop) {
ca04da08
GS
296 /* Purify reports a benign UMR here sometimes, because we
297 * don't initialize the OP() slot of a node when that node
298 * is occupied by just the trailing null of the string in
299 * an EXACT node */
c277df42
IZ
300 if (regkind[(U8)OP(n)] != NOTHING || OP(n) == NOTHING) {
301 OP(n) = OPTIMIZED;
302 NEXT_OFF(n) = 0;
303 }
304 n++;
305 }
306#endif
307
308 }
309 if (OP(scan) != CURLYX) {
048cfca1
GS
310 int max = (reg_off_by_arg[OP(scan)]
311 ? I32_MAX
312 /* I32 may be smaller than U16 on CRAYs! */
313 : (I32_MAX < U16_MAX ? I32_MAX : U16_MAX));
c277df42
IZ
314 int off = (reg_off_by_arg[OP(scan)] ? ARG(scan) : NEXT_OFF(scan));
315 int noff;
316 regnode *n = scan;
317
318 /* Skip NOTHING and LONGJMP. */
319 while ((n = regnext(n))
320 && ((regkind[(U8)OP(n)] == NOTHING && (noff = NEXT_OFF(n)))
321 || ((OP(n) == LONGJMP) && (noff = ARG(n))))
322 && off + noff < max)
323 off += noff;
324 if (reg_off_by_arg[OP(scan)])
325 ARG(scan) = off;
326 else
327 NEXT_OFF(scan) = off;
328 }
329 if (OP(scan) == BRANCH || OP(scan) == BRANCHJ
330 || OP(scan) == IFTHEN || OP(scan) == SUSPEND) {
331 next = regnext(scan);
332 code = OP(scan);
333
334 if (OP(next) == code || code == IFTHEN || code == SUSPEND) {
335 I32 max1 = 0, min1 = I32_MAX, num = 0;
336
337 if (flags & SCF_DO_SUBSTR)
338 scan_commit(data);
339 while (OP(scan) == code) {
340 I32 deltanext, minnext;
341
342 num++;
343 data_fake.flags = 0;
344 next = regnext(scan);
345 scan = NEXTOPER(scan);
346 if (code != BRANCH)
347 scan = NEXTOPER(scan);
348 /* We suppose the run is continuous, last=next...*/
349 minnext = study_chunk(&scan, &deltanext, next,
350 &data_fake, 0);
351 if (min1 > minnext)
352 min1 = minnext;
353 if (max1 < minnext + deltanext)
354 max1 = minnext + deltanext;
355 if (deltanext == I32_MAX)
3da4a404 356 is_inf = is_inf_internal = 1;
c277df42
IZ
357 scan = next;
358 if (data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
359 pars++;
405ff068 360 if (data && (data_fake.flags & SF_HAS_EVAL))
c277df42
IZ
361 data->flags |= SF_HAS_EVAL;
362 if (code == SUSPEND)
363 break;
364 }
365 if (code == IFTHEN && num < 2) /* Empty ELSE branch */
366 min1 = 0;
367 if (flags & SCF_DO_SUBSTR) {
368 data->pos_min += min1;
369 data->pos_delta += max1 - min1;
370 if (max1 != min1 || is_inf)
371 data->longest = &(data->longest_float);
372 }
373 min += min1;
374 delta += max1 - min1;
375 } else if (code == BRANCHJ) /* single branch is optimized. */
376 scan = NEXTOPER(NEXTOPER(scan));
377 else /* single branch is optimized. */
378 scan = NEXTOPER(scan);
379 continue;
380 } else if (OP(scan) == EXACT) {
381 min += *OPERAND(scan);
382 if (flags & SCF_DO_SUBSTR) { /* Update longest substr. */
383 I32 l = *OPERAND(scan);
384
385 /* The code below prefers earlier match for fixed
386 offset, later match for variable offset. */
387 if (data->last_end == -1) { /* Update the start info. */
388 data->last_start_min = data->pos_min;
389 data->last_start_max = is_inf
390 ? I32_MAX : data->pos_min + data->pos_delta;
391 }
161b471a 392 sv_catpvn(data->last_found, (char *)(OPERAND(scan)+1), l);
c277df42
IZ
393 data->last_end = data->pos_min + l;
394 data->pos_min += l; /* As in the first entry. */
395 data->flags &= ~SF_BEFORE_EOL;
396 }
397 } else if (regkind[(U8)OP(scan)] == EXACT) {
398 if (flags & SCF_DO_SUBSTR)
399 scan_commit(data);
400 min += *OPERAND(scan);
401 if (data && (flags & SCF_DO_SUBSTR))
402 data->pos_min += *OPERAND(scan);
403 } else if (strchr(varies,OP(scan))) {
404 I32 mincount, maxcount, minnext, deltanext, pos_before, fl;
405 regnode *oscan = scan;
406
407 switch (regkind[(U8)OP(scan)]) {
408 case WHILEM:
409 scan = NEXTOPER(scan);
410 goto finish;
411 case PLUS:
412 if (flags & SCF_DO_SUBSTR) {
413 next = NEXTOPER(scan);
414 if (OP(next) == EXACT) {
415 mincount = 1;
416 maxcount = REG_INFTY;
417 next = regnext(scan);
418 scan = NEXTOPER(scan);
419 goto do_curly;
420 }
421 }
422 if (flags & SCF_DO_SUBSTR)
423 data->pos_min++;
424 min++;
425 /* Fall through. */
426 case STAR:
3da4a404 427 is_inf = is_inf_internal = 1;
c277df42
IZ
428 scan = regnext(scan);
429 if (flags & SCF_DO_SUBSTR) {
430 scan_commit(data);
431 data->longest = &(data->longest_float);
432 }
433 goto optimize_curly_tail;
434 case CURLY:
435 mincount = ARG1(scan);
436 maxcount = ARG2(scan);
437 next = regnext(scan);
438 scan = NEXTOPER(scan) + EXTRA_STEP_2ARGS;
439 do_curly:
440 if (flags & SCF_DO_SUBSTR) {
441 if (mincount == 0) scan_commit(data);
442 pos_before = data->pos_min;
443 }
444 if (data) {
445 fl = data->flags;
446 data->flags &= ~(SF_HAS_PAR|SF_IN_PAR|SF_HAS_EVAL);
447 if (is_inf)
448 data->flags |= SF_IS_INF;
449 }
450 /* This will finish on WHILEM, setting scan, or on NULL: */
451 minnext = study_chunk(&scan, &deltanext, last, data,
452 mincount == 0
453 ? (flags & ~SCF_DO_SUBSTR) : flags);
454 if (!scan) /* It was not CURLYX, but CURLY. */
455 scan = next;
3280af22 456 if (PL_dowarn && (minnext + deltanext == 0)
821b33a5
IZ
457 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
458 && maxcount <= 10000) /* Complement check for big count */
c277df42
IZ
459 warn("Strange *+?{} on zero-length expression");
460 min += minnext * mincount;
3da4a404
IZ
461 is_inf_internal |= (maxcount == REG_INFTY
462 && (minnext + deltanext) > 0
463 || deltanext == I32_MAX);
464 is_inf |= is_inf_internal;
c277df42
IZ
465 delta += (minnext + deltanext) * maxcount - minnext * mincount;
466
467 /* Try powerful optimization CURLYX => CURLYN. */
c277df42
IZ
468 if ( OP(oscan) == CURLYX && data
469 && data->flags & SF_IN_PAR
470 && !(data->flags & SF_HAS_EVAL)
471 && !deltanext && minnext == 1 ) {
472 /* Try to optimize to CURLYN. */
473 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS;
474 regnode *nxt1 = nxt, *nxt2;
475
476 /* Skip open. */
477 nxt = regnext(nxt);
478 if (!strchr(simple,OP(nxt))
479 && !(regkind[(U8)OP(nxt)] == EXACT
480 && *OPERAND(nxt) == 1))
481 goto nogo;
482 nxt2 = nxt;
483 nxt = regnext(nxt);
484 if (OP(nxt) != CLOSE)
485 goto nogo;
486 /* Now we know that nxt2 is the only contents: */
487 oscan->flags = ARG(nxt);
488 OP(oscan) = CURLYN;
489 OP(nxt1) = NOTHING; /* was OPEN. */
490#ifdef DEBUGGING
491 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
492 NEXT_OFF(nxt1+ 1) = 0; /* just for consistancy. */
493 NEXT_OFF(nxt2) = 0; /* just for consistancy with CURLY. */
494 OP(nxt) = OPTIMIZED; /* was CLOSE. */
495 OP(nxt + 1) = OPTIMIZED; /* was count. */
496 NEXT_OFF(nxt+ 1) = 0; /* just for consistancy. */
497#endif
498 }
c277df42
IZ
499 nogo:
500
501 /* Try optimization CURLYX => CURLYM. */
502 if ( OP(oscan) == CURLYX && data
c277df42 503 && !(data->flags & SF_HAS_PAR)
c277df42
IZ
504 && !(data->flags & SF_HAS_EVAL)
505 && !deltanext ) {
506 /* XXXX How to optimize if data == 0? */
507 /* Optimize to a simpler form. */
508 regnode *nxt = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN */
509 regnode *nxt2;
510
511 OP(oscan) = CURLYM;
512 while ( (nxt2 = regnext(nxt)) /* skip over embedded stuff*/
513 && (OP(nxt2) != WHILEM))
514 nxt = nxt2;
515 OP(nxt2) = SUCCEED; /* Whas WHILEM */
c277df42
IZ
516 /* Need to optimize away parenths. */
517 if (data->flags & SF_IN_PAR) {
518 /* Set the parenth number. */
519 regnode *nxt1 = NEXTOPER(oscan) + EXTRA_STEP_2ARGS; /* OPEN*/
520
521 if (OP(nxt) != CLOSE)
522 FAIL("panic opt close");
523 oscan->flags = ARG(nxt);
524 OP(nxt1) = OPTIMIZED; /* was OPEN. */
525 OP(nxt) = OPTIMIZED; /* was CLOSE. */
526#ifdef DEBUGGING
527 OP(nxt1 + 1) = OPTIMIZED; /* was count. */
528 OP(nxt + 1) = OPTIMIZED; /* was count. */
529 NEXT_OFF(nxt1 + 1) = 0; /* just for consistancy. */
530 NEXT_OFF(nxt + 1) = 0; /* just for consistancy. */
531#endif
532#if 0
533 while ( nxt1 && (OP(nxt1) != WHILEM)) {
534 regnode *nnxt = regnext(nxt1);
535
536 if (nnxt == nxt) {
537 if (reg_off_by_arg[OP(nxt1)])
538 ARG_SET(nxt1, nxt2 - nxt1);
539 else if (nxt2 - nxt1 < U16_MAX)
540 NEXT_OFF(nxt1) = nxt2 - nxt1;
541 else
542 OP(nxt) = NOTHING; /* Cannot beautify */
543 }
544 nxt1 = nnxt;
545 }
546#endif
547 /* Optimize again: */
548 study_chunk(&nxt1, &deltanext, nxt, NULL, 0);
549 } else
550 oscan->flags = 0;
c277df42
IZ
551 }
552 if (data && fl & (SF_HAS_PAR|SF_IN_PAR))
553 pars++;
554 if (flags & SCF_DO_SUBSTR) {
555 SV *last_str = Nullsv;
556 int counted = mincount != 0;
557
558 if (data->last_end > 0 && mincount != 0) { /* Ends with a string. */
559 I32 b = pos_before >= data->last_start_min
560 ? pos_before : data->last_start_min;
561 STRLEN l;
562 char *s = SvPV(data->last_found, l);
563
564 l -= b - data->last_start_min;
565 /* Get the added string: */
566 last_str = newSVpv(s + b - data->last_start_min, l);
567 if (deltanext == 0 && pos_before == b) {
568 /* What was added is a constant string */
569 if (mincount > 1) {
570 SvGROW(last_str, (mincount * l) + 1);
571 repeatcpy(SvPVX(last_str) + l,
572 SvPVX(last_str), l, mincount - 1);
573 SvCUR(last_str) *= mincount;
574 /* Add additional parts. */
575 SvCUR_set(data->last_found,
576 SvCUR(data->last_found) - l);
577 sv_catsv(data->last_found, last_str);
578 data->last_end += l * (mincount - 1);
579 }
580 }
581 }
582 /* It is counted once already... */
583 data->pos_min += minnext * (mincount - counted);
584 data->pos_delta += - counted * deltanext +
585 (minnext + deltanext) * maxcount - minnext * mincount;
586 if (mincount != maxcount) {
587 scan_commit(data);
588 if (mincount && last_str) {
589 sv_setsv(data->last_found, last_str);
590 data->last_end = data->pos_min;
591 data->last_start_min =
592 data->pos_min - SvCUR(last_str);
593 data->last_start_max = is_inf
594 ? I32_MAX
595 : data->pos_min + data->pos_delta
596 - SvCUR(last_str);
597 }
598 data->longest = &(data->longest_float);
599 }
3da4a404 600 SvREFCNT_dec(last_str);
c277df42 601 }
405ff068 602 if (data && (fl & SF_HAS_EVAL))
c277df42
IZ
603 data->flags |= SF_HAS_EVAL;
604 optimize_curly_tail:
c277df42
IZ
605 if (OP(oscan) != CURLYX) {
606 while (regkind[(U8)OP(next = regnext(oscan))] == NOTHING
607 && NEXT_OFF(next))
608 NEXT_OFF(oscan) += NEXT_OFF(next);
609 }
c277df42
IZ
610 continue;
611 default: /* REF only? */
612 if (flags & SCF_DO_SUBSTR) {
613 scan_commit(data);
614 data->longest = &(data->longest_float);
615 }
3da4a404 616 is_inf = is_inf_internal = 1;
c277df42
IZ
617 break;
618 }
619 } else if (strchr(simple,OP(scan))) {
620 if (flags & SCF_DO_SUBSTR) {
621 scan_commit(data);
622 data->pos_min++;
623 }
624 min++;
625 } else if (regkind[(U8)OP(scan)] == EOL && flags & SCF_DO_SUBSTR) {
626 data->flags |= (OP(scan) == MEOL
627 ? SF_BEFORE_MEOL
628 : SF_BEFORE_SEOL);
629 } else if (regkind[(U8)OP(scan)] == BRANCHJ
630 && (scan->flags || data)
631 && (OP(scan) == IFMATCH || OP(scan) == UNLESSM)) {
632 I32 deltanext, minnext;
633 regnode *nscan;
634
635 data_fake.flags = 0;
636 next = regnext(scan);
637 nscan = NEXTOPER(NEXTOPER(scan));
638 minnext = study_chunk(&nscan, &deltanext, last, &data_fake, 0);
639 if (scan->flags) {
640 if (deltanext) {
641 FAIL("variable length lookbehind not implemented");
642 } else if (minnext > U8_MAX) {
643 FAIL2("lookbehind longer than %d not implemented", U8_MAX);
644 }
645 scan->flags = minnext;
646 }
647 if (data && data_fake.flags & (SF_HAS_PAR|SF_IN_PAR))
648 pars++;
405ff068 649 if (data && (data_fake.flags & SF_HAS_EVAL))
c277df42
IZ
650 data->flags |= SF_HAS_EVAL;
651 } else if (OP(scan) == OPEN) {
652 pars++;
653 } else if (OP(scan) == CLOSE && ARG(scan) == is_par) {
c277df42
IZ
654 next = regnext(scan);
655
656 if ( next && (OP(next) != WHILEM) && next < last)
c277df42
IZ
657 is_par = 0; /* Disable optimization */
658 } else if (OP(scan) == EVAL) {
659 if (data)
660 data->flags |= SF_HAS_EVAL;
661 }
662 /* Else: zero-length, ignore. */
663 scan = regnext(scan);
664 }
665
666 finish:
667 *scanp = scan;
3da4a404 668 *deltap = is_inf_internal ? I32_MAX : delta;
c277df42
IZ
669 if (flags & SCF_DO_SUBSTR && is_inf)
670 data->pos_delta = I32_MAX - data->pos_min;
671 if (is_par > U8_MAX)
672 is_par = 0;
673 if (is_par && pars==1 && data) {
674 data->flags |= SF_IN_PAR;
675 data->flags &= ~SF_HAS_PAR;
676 } else if (pars && data) {
677 data->flags |= SF_HAS_PAR;
678 data->flags &= ~SF_IN_PAR;
679 }
680 return min;
681}
682
76e3520e 683STATIC I32
c277df42
IZ
684add_data(I32 n, char *s)
685{
5c0ca799 686 dTHR;
3280af22
NIS
687 if (PL_regcomp_rx->data) {
688 Renewc(PL_regcomp_rx->data,
689 sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (PL_regcomp_rx->data->count + n - 1),
c277df42 690 char, struct reg_data);
3280af22
NIS
691 Renew(PL_regcomp_rx->data->what, PL_regcomp_rx->data->count + n, U8);
692 PL_regcomp_rx->data->count += n;
c277df42 693 } else {
3280af22 694 Newc(1207, PL_regcomp_rx->data, sizeof(*PL_regcomp_rx->data) + sizeof(void*) * (n - 1),
c277df42 695 char, struct reg_data);
3280af22
NIS
696 New(1208, PL_regcomp_rx->data->what, n, U8);
697 PL_regcomp_rx->data->count = n;
c277df42 698 }
3280af22
NIS
699 Copy(s, PL_regcomp_rx->data->what + PL_regcomp_rx->data->count - n, n, U8);
700 return PL_regcomp_rx->data->count - n;
c277df42
IZ
701}
702
a687059c 703/*
e50aee73 704 - pregcomp - compile a regular expression into internal code
a687059c
LW
705 *
706 * We can't allocate space until we know how big the compiled form will be,
707 * but we can't compile it (and thus know how big it is) until we've got a
708 * place to put the code. So we cheat: we compile it twice, once with code
709 * generation turned off and size counting turned on, and once "for real".
710 * This also means that we don't allocate space until we are sure that the
711 * thing really will compile successfully, and we never have to move the
712 * code and thus invalidate pointers into it. (Note that it has to be in
713 * one piece because free() must be able to free it all.) [NB: not true in perl]
714 *
715 * Beware that the optimization-preparation code in here knows about some
716 * of the structure of the compiled regexp. [I'll say.]
717 */
718regexp *
8ac85365 719pregcomp(char *exp, char *xend, PMOP *pm)
a687059c 720{
5c0ca799 721 dTHR;
a0d0e21e 722 register regexp *r;
c277df42
IZ
723 regnode *scan;
724 SV **longest;
725 SV *longest_fixed;
726 SV *longest_float;
727 regnode *first;
a0d0e21e 728 I32 flags;
a0d0e21e
LW
729 I32 minlen = 0;
730 I32 sawplus = 0;
731 I32 sawopen = 0;
732
733 if (exp == NULL)
c277df42 734 FAIL("NULL regexp argument");
a0d0e21e 735
3280af22 736 PL_regprecomp = savepvn(exp, xend - exp);
afe2cf94
GS
737 DEBUG_r(PerlIO_printf(Perl_debug_log, "compiling RE `%*s'\n",
738 xend - exp, PL_regprecomp));
3280af22
NIS
739 PL_regflags = pm->op_pmflags;
740 PL_regsawback = 0;
bbce6d69 741
3280af22
NIS
742 PL_regseen = 0;
743 PL_seen_zerolen = *exp == '^' ? -1 : 0;
744 PL_seen_evals = 0;
745 PL_extralen = 0;
c277df42 746
bbce6d69 747 /* First pass: determine size, legality. */
3280af22
NIS
748 PL_regcomp_parse = exp;
749 PL_regxend = xend;
750 PL_regnaughty = 0;
751 PL_regnpar = 1;
752 PL_regsize = 0L;
753 PL_regcode = &PL_regdummy;
754 regc((U8)MAGIC, (char*)PL_regcode);
a0d0e21e 755 if (reg(0, &flags) == NULL) {
3280af22
NIS
756 Safefree(PL_regprecomp);
757 PL_regprecomp = Nullch;
a0d0e21e
LW
758 return(NULL);
759 }
3280af22 760 DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", PL_regsize));
c277df42 761
afe2cf94
GS
762 DEBUG_r(
763 if (!PL_colorset) {
764 int i = 0;
765 char *s = PerlEnv_getenv("TERMCAP_COLORS");
766
767 PL_colorset = 1;
768 if (s) {
769 PL_colors[0] = s = savepv(s);
770 while (++i < 4) {
771 s = strchr(s, '\t');
772 if (!s)
773 FAIL("Not enough TABs in TERMCAP_COLORS");
774 *s = '\0';
775 PL_colors[i] = ++s;
776 }
777 } else {
778 while (i < 4)
779 PL_colors[i++] = "";
780 }
781 /* Reset colors: */
782 PerlIO_printf(Perl_debug_log, "%s%s%s%s",
783 PL_colors[0],PL_colors[1],PL_colors[2],PL_colors[3]);
784 }
785 );
786
c277df42
IZ
787 /* Small enough for pointer-storage convention?
788 If extralen==0, this means that we will not need long jumps. */
3280af22
NIS
789 if (PL_regsize >= 0x10000L && PL_extralen)
790 PL_regsize += PL_extralen;
c277df42 791 else
3280af22 792 PL_extralen = 0;
a0d0e21e 793
bbce6d69 794 /* Allocate space and initialize. */
3280af22 795 Newc(1001, r, sizeof(regexp) + (unsigned)PL_regsize * sizeof(regnode),
c277df42 796 char, regexp);
a0d0e21e
LW
797 if (r == NULL)
798 FAIL("regexp out of space");
c277df42 799 r->refcnt = 1;
bbce6d69 800 r->prelen = xend - exp;
3280af22 801 r->precomp = PL_regprecomp;
a0d0e21e 802 r->subbeg = r->subbase = NULL;
3280af22
NIS
803 r->nparens = PL_regnpar - 1; /* set early to validate backrefs */
804 PL_regcomp_rx = r;
bbce6d69 805
806 /* Second pass: emit code. */
3280af22
NIS
807 PL_regcomp_parse = exp;
808 PL_regxend = xend;
809 PL_regnaughty = 0;
810 PL_regnpar = 1;
811 PL_regcode = r->program;
2cd61cdb 812 /* Store the count of eval-groups for security checks: */
3280af22
NIS
813 PL_regcode->next_off = ((PL_seen_evals > U16_MAX) ? U16_MAX : PL_seen_evals);
814 regc((U8)MAGIC, (char*) PL_regcode++);
c277df42 815 r->data = 0;
a0d0e21e
LW
816 if (reg(0, &flags) == NULL)
817 return(NULL);
818
819 /* Dig out information for optimizations. */
8782bef2 820 r->reganch = pm->op_pmflags & PMf_COMPILETIME;
3280af22 821 pm->op_pmflags = PL_regflags;
c277df42 822 r->regstclass = NULL;
3280af22 823 r->naughty = PL_regnaughty >= 10; /* Probably an expensive pattern. */
c277df42 824 scan = r->program + 1; /* First BRANCH. */
2779dcf1
IZ
825
826 /* XXXX To minimize changes to RE engine we always allocate
827 3-units-long substrs field. */
828 Newz(1004, r->substrs, 1, struct reg_substr_data);
829
c277df42
IZ
830 if (OP(scan) != BRANCH) { /* Only one top-level choice. */
831 scan_data_t data;
832 I32 fake;
c5254dd6 833 STRLEN longest_float_length, longest_fixed_length;
a0d0e21e 834
c277df42 835 StructCopy(&zero_scan_data, &data, scan_data_t);
a0d0e21e 836 first = scan;
c277df42 837 /* Skip introductions and multiplicators >= 1. */
a0d0e21e
LW
838 while ((OP(first) == OPEN && (sawopen = 1)) ||
839 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
840 (OP(first) == PLUS) ||
841 (OP(first) == MINMOD) ||
842 (regkind[(U8)OP(first)] == CURLY && ARG1(first) > 0) ) {
843 if (OP(first) == PLUS)
844 sawplus = 1;
845 else
846 first += regarglen[(U8)OP(first)];
847 first = NEXTOPER(first);
a687059c
LW
848 }
849
a0d0e21e
LW
850 /* Starting-point info. */
851 again:
c277df42 852 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
a0d0e21e
LW
853 else if (strchr(simple+2,OP(first)))
854 r->regstclass = first;
bbce6d69 855 else if (regkind[(U8)OP(first)] == BOUND ||
856 regkind[(U8)OP(first)] == NBOUND)
a0d0e21e
LW
857 r->regstclass = first;
858 else if (regkind[(U8)OP(first)] == BOL) {
c277df42 859 r->reganch |= (OP(first) == MBOL ? ROPT_ANCH_MBOL: ROPT_ANCH_BOL);
a0d0e21e 860 first = NEXTOPER(first);
774d564b 861 goto again;
862 }
863 else if (OP(first) == GPOS) {
864 r->reganch |= ROPT_ANCH_GPOS;
865 first = NEXTOPER(first);
866 goto again;
a0d0e21e
LW
867 }
868 else if ((OP(first) == STAR &&
869 regkind[(U8)OP(NEXTOPER(first))] == ANY) &&
870 !(r->reganch & ROPT_ANCH) )
871 {
872 /* turn .* into ^.* with an implied $*=1 */
774d564b 873 r->reganch |= ROPT_ANCH_BOL | ROPT_IMPLICIT;
a0d0e21e 874 first = NEXTOPER(first);
774d564b 875 goto again;
a0d0e21e 876 }
3280af22 877 if (sawplus && (!sawopen || !PL_regsawback))
a0d0e21e
LW
878 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
879
c277df42
IZ
880 /* Scan is after the zeroth branch, first is atomic matcher. */
881 DEBUG_r(PerlIO_printf(Perl_debug_log, "first at %d\n",
882 first - scan + 1));
a0d0e21e
LW
883 /*
884 * If there's something expensive in the r.e., find the
885 * longest literal string that must appear and make it the
886 * regmust. Resolve ties in favor of later strings, since
887 * the regstart check works with the beginning of the r.e.
888 * and avoiding duplication strengthens checking. Not a
889 * strong reason, but sufficient in the absence of others.
890 * [Now we resolve ties in favor of the earlier string if
c277df42 891 * it happens that c_offset_min has been invalidated, since the
a0d0e21e
LW
892 * earlier string may buy us something the later one won't.]
893 */
a0d0e21e 894 minlen = 0;
a687059c 895
c277df42
IZ
896 data.longest_fixed = newSVpv("",0);
897 data.longest_float = newSVpv("",0);
898 data.last_found = newSVpv("",0);
899 data.longest = &(data.longest_fixed);
900 first = scan;
901
3280af22 902 minlen = study_chunk(&first, &fake, scan + PL_regsize, /* Up to end */
c277df42 903 &data, SCF_DO_SUBSTR);
3280af22 904 if ( PL_regnpar == 1 && data.longest == &(data.longest_fixed)
c277df42 905 && data.last_start_min == 0 && data.last_end > 0
3280af22
NIS
906 && !PL_seen_zerolen
907 && (!(PL_regseen & REG_SEEN_GPOS) || (r->reganch & ROPT_ANCH_GPOS)))
c277df42
IZ
908 r->reganch |= ROPT_CHECK_ALL;
909 scan_commit(&data);
910 SvREFCNT_dec(data.last_found);
911
c5254dd6
MB
912 longest_float_length = SvCUR(data.longest_float);
913 if (longest_float_length
c277df42
IZ
914 || (data.flags & SF_FL_BEFORE_EOL
915 && (!(data.flags & SF_FL_BEFORE_MEOL)
3280af22 916 || (PL_regflags & PMf_MULTILINE)))) {
c277df42 917 if (SvCUR(data.longest_fixed)
3da4a404
IZ
918 && data.offset_fixed == data.offset_float_min
919 && SvCUR(data.longest_fixed) == SvCUR(data.longest_float))
920 goto remove_float; /* Like in (a)+. */
c277df42
IZ
921
922 r->float_substr = data.longest_float;
923 r->float_min_offset = data.offset_float_min;
924 r->float_max_offset = data.offset_float_max;
2779dcf1 925 fbm_compile(r->float_substr, 0);
c277df42
IZ
926 BmUSEFUL(r->float_substr) = 100;
927 if (data.flags & SF_FL_BEFORE_EOL /* Cannot have SEOL and MULTI */
928 && (!(data.flags & SF_FL_BEFORE_MEOL)
3280af22 929 || (PL_regflags & PMf_MULTILINE)))
c277df42
IZ
930 SvTAIL_on(r->float_substr);
931 } else {
3da4a404 932 remove_float:
c277df42
IZ
933 r->float_substr = Nullsv;
934 SvREFCNT_dec(data.longest_float);
c5254dd6 935 longest_float_length = 0;
a0d0e21e 936 }
c277df42 937
c5254dd6
MB
938 longest_fixed_length = SvCUR(data.longest_fixed);
939 if (longest_fixed_length
c277df42
IZ
940 || (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
941 && (!(data.flags & SF_FIX_BEFORE_MEOL)
3280af22 942 || (PL_regflags & PMf_MULTILINE)))) {
c277df42
IZ
943 r->anchored_substr = data.longest_fixed;
944 r->anchored_offset = data.offset_fixed;
2779dcf1 945 fbm_compile(r->anchored_substr, 0);
c277df42
IZ
946 BmUSEFUL(r->anchored_substr) = 100;
947 if (data.flags & SF_FIX_BEFORE_EOL /* Cannot have SEOL and MULTI */
948 && (!(data.flags & SF_FIX_BEFORE_MEOL)
3280af22 949 || (PL_regflags & PMf_MULTILINE)))
c277df42
IZ
950 SvTAIL_on(r->anchored_substr);
951 } else {
952 r->anchored_substr = Nullsv;
953 SvREFCNT_dec(data.longest_fixed);
c5254dd6 954 longest_fixed_length = 0;
a0d0e21e 955 }
c277df42
IZ
956
957 /* A temporary algorithm prefers floated substr to fixed one to dig more info. */
c5254dd6 958 if (longest_fixed_length > longest_float_length) {
c277df42
IZ
959 r->check_substr = r->anchored_substr;
960 r->check_offset_min = r->check_offset_max = r->anchored_offset;
961 if (r->reganch & ROPT_ANCH_SINGLE)
962 r->reganch |= ROPT_NOSCAN;
963 } else {
964 r->check_substr = r->float_substr;
965 r->check_offset_min = data.offset_float_min;
966 r->check_offset_max = data.offset_float_max;
a0d0e21e 967 }
c277df42
IZ
968 } else {
969 /* Several toplevels. Best we can is to set minlen. */
970 I32 fake;
971
972 DEBUG_r(PerlIO_printf(Perl_debug_log, "\n"));
973 scan = r->program + 1;
3280af22 974 minlen = study_chunk(&scan, &fake, scan + PL_regsize, NULL, 0);
c277df42 975 r->check_substr = r->anchored_substr = r->float_substr = Nullsv;
a0d0e21e
LW
976 }
977
a0d0e21e 978 r->minlen = minlen;
3280af22 979 if (PL_regseen & REG_SEEN_GPOS)
c277df42 980 r->reganch |= ROPT_GPOS_SEEN;
3280af22 981 if (PL_regseen & REG_SEEN_LOOKBEHIND)
c277df42 982 r->reganch |= ROPT_LOOKBEHIND_SEEN;
3280af22 983 if (PL_regseen & REG_SEEN_EVAL)
ce862d02 984 r->reganch |= ROPT_EVAL_SEEN;
3280af22
NIS
985 Newz(1002, r->startp, PL_regnpar, char*);
986 Newz(1002, r->endp, PL_regnpar, char*);
a0d0e21e
LW
987 DEBUG_r(regdump(r));
988 return(r);
a687059c
LW
989}
990
991/*
992 - reg - regular expression, i.e. main body or parenthesized thing
993 *
994 * Caller must absorb opening parenthesis.
995 *
996 * Combining parenthesis handling with the base level of regular expression
997 * is a trifle forced, but the need to tie the tails of the branches to what
998 * follows makes it hard to avoid.
999 */
76e3520e 1000STATIC regnode *
8ac85365 1001reg(I32 paren, I32 *flagp)
c277df42 1002 /* paren: Parenthesized? 0=top, 1=(, inside: changed to letter. */
a687059c 1003{
5c0ca799 1004 dTHR;
c277df42
IZ
1005 register regnode *ret; /* Will be the head of the group. */
1006 register regnode *br;
1007 register regnode *lastbr;
1008 register regnode *ender = 0;
a0d0e21e 1009 register I32 parno = 0;
3280af22 1010 I32 flags, oregflags = PL_regflags, have_branch = 0, open = 0;
c277df42 1011 char c;
a0d0e21e 1012
821b33a5 1013 *flagp = 0; /* Tentatively. */
a0d0e21e
LW
1014
1015 /* Make an OPEN node, if parenthesized. */
1016 if (paren) {
3280af22 1017 if (*PL_regcomp_parse == '?') {
ca9dfc88
IZ
1018 U16 posflags = 0, negflags = 0;
1019 U16 *flagsp = &posflags;
1020
3280af22
NIS
1021 PL_regcomp_parse++;
1022 paren = *PL_regcomp_parse++;
c277df42 1023 ret = NULL; /* For look-ahead/behind. */
a0d0e21e 1024 switch (paren) {
c277df42 1025 case '<':
3280af22
NIS
1026 PL_regseen |= REG_SEEN_LOOKBEHIND;
1027 if (*PL_regcomp_parse == '!')
c277df42 1028 paren = ',';
3280af22 1029 if (*PL_regcomp_parse != '=' && *PL_regcomp_parse != '!')
c277df42 1030 goto unknown;
3280af22 1031 PL_regcomp_parse++;
a0d0e21e
LW
1032 case '=':
1033 case '!':
3280af22 1034 PL_seen_zerolen++;
c277df42
IZ
1035 case ':':
1036 case '>':
a0d0e21e
LW
1037 break;
1038 case '$':
1039 case '@':
c277df42 1040 FAIL2("Sequence (?%c...) not implemented", (int)paren);
a0d0e21e
LW
1041 break;
1042 case '#':
3280af22
NIS
1043 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
1044 PL_regcomp_parse++;
1045 if (*PL_regcomp_parse != ')')
c277df42 1046 FAIL("Sequence (?#... not terminated");
a0d0e21e
LW
1047 nextchar();
1048 *flagp = TRYAGAIN;
1049 return NULL;
c277df42
IZ
1050 case '{':
1051 {
1052 dTHR;
1053 I32 count = 1, n = 0;
1054 char c;
3280af22 1055 char *s = PL_regcomp_parse;
c277df42
IZ
1056 SV *sv;
1057 OP_4tree *sop, *rop;
1058
3280af22
NIS
1059 PL_seen_zerolen++;
1060 PL_regseen |= REG_SEEN_EVAL;
1061 while (count && (c = *PL_regcomp_parse)) {
1062 if (c == '\\' && PL_regcomp_parse[1])
1063 PL_regcomp_parse++;
c277df42
IZ
1064 else if (c == '{')
1065 count++;
1066 else if (c == '}')
1067 count--;
3280af22 1068 PL_regcomp_parse++;
c277df42 1069 }
3280af22 1070 if (*PL_regcomp_parse != ')')
c277df42
IZ
1071 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1072 if (!SIZE_ONLY) {
1073 AV *av;
1074
3280af22
NIS
1075 if (PL_regcomp_parse - 1 - s)
1076 sv = newSVpv(s, PL_regcomp_parse - 1 - s);
c277df42
IZ
1077 else
1078 sv = newSVpv("", 0);
1079
1080 rop = sv_compile_2op(sv, &sop, "re", &av);
1081
1082 n = add_data(3, "nso");
3280af22
NIS
1083 PL_regcomp_rx->data->data[n] = (void*)rop;
1084 PL_regcomp_rx->data->data[n+1] = (void*)av;
1085 PL_regcomp_rx->data->data[n+2] = (void*)sop;
c277df42 1086 SvREFCNT_dec(sv);
cc6b7395 1087 } else { /* First pass */
3280af22 1088 if (PL_reginterp_cnt < ++PL_seen_evals && PL_curcop != &PL_compiling)
2cd61cdb
IZ
1089 /* No compiled RE interpolated, has runtime
1090 components ===> unsafe. */
1091 FAIL("Eval-group not allowed at runtime, use re 'eval'");
3280af22 1092 if (PL_tainted)
cc6b7395 1093 FAIL("Eval-group in insecure regular expression");
c277df42
IZ
1094 }
1095
1096 nextchar();
c277df42
IZ
1097 return reganode(EVAL, n);
1098 }
1099 case '(':
1100 {
3280af22
NIS
1101 if (PL_regcomp_parse[0] == '?') {
1102 if (PL_regcomp_parse[1] == '=' || PL_regcomp_parse[1] == '!'
1103 || PL_regcomp_parse[1] == '<'
1104 || PL_regcomp_parse[1] == '{') { /* Lookahead or eval. */
c277df42
IZ
1105 I32 flag;
1106
1107 ret = reg_node(LOGICAL);
1108 regtail(ret, reg(1, &flag));
1109 goto insert_if;
1110 }
3280af22
NIS
1111 } else if (PL_regcomp_parse[0] >= '1' && PL_regcomp_parse[0] <= '9' ) {
1112 parno = atoi(PL_regcomp_parse++);
c277df42 1113
3280af22
NIS
1114 while (isDIGIT(*PL_regcomp_parse))
1115 PL_regcomp_parse++;
c277df42
IZ
1116 ret = reganode(GROUPP, parno);
1117 if ((c = *nextchar()) != ')')
1118 FAIL2("Switch (?(number%c not recognized", c);
1119 insert_if:
1120 regtail(ret, reganode(IFTHEN, 0));
1121 br = regbranch(&flags, 1);
1122 if (br == NULL)
1123 br = reganode(LONGJMP, 0);
1124 else
1125 regtail(br, reganode(LONGJMP, 0));
1126 c = *nextchar();
a1d8a0e6
IZ
1127 if (flags&HASWIDTH)
1128 *flagp |= HASWIDTH;
c277df42
IZ
1129 if (c == '|') {
1130 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1131 regbranch(&flags, 1);
1132 regtail(ret, lastbr);
a1d8a0e6
IZ
1133 if (flags&HASWIDTH)
1134 *flagp |= HASWIDTH;
c277df42
IZ
1135 c = *nextchar();
1136 } else
1137 lastbr = NULL;
1138 if (c != ')')
1139 FAIL("Switch (?(condition)... contains too many branches");
1140 ender = reg_node(TAIL);
1141 regtail(br, ender);
1142 if (lastbr) {
1143 regtail(lastbr, ender);
1144 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1145 } else
1146 regtail(ret, ender);
1147 return ret;
1148 } else {
3280af22 1149 FAIL2("Unknown condition for (?(%.2s", PL_regcomp_parse);
c277df42
IZ
1150 }
1151 }
1b1626e4 1152 case 0:
c277df42 1153 FAIL("Sequence (? incomplete");
1b1626e4 1154 break;
a0d0e21e 1155 default:
3280af22 1156 --PL_regcomp_parse;
ca9dfc88 1157 parse_flags:
3280af22
NIS
1158 while (*PL_regcomp_parse && strchr("iogcmsx", *PL_regcomp_parse)) {
1159 if (*PL_regcomp_parse != 'o')
1160 pmflag(flagsp, *PL_regcomp_parse);
1161 ++PL_regcomp_parse;
ca9dfc88 1162 }
3280af22 1163 if (*PL_regcomp_parse == '-') {
ca9dfc88 1164 flagsp = &negflags;
3280af22 1165 ++PL_regcomp_parse;
ca9dfc88 1166 goto parse_flags;
48c036b1 1167 }
3280af22
NIS
1168 PL_regflags |= posflags;
1169 PL_regflags &= ~negflags;
1170 if (*PL_regcomp_parse == ':') {
1171 PL_regcomp_parse++;
ca9dfc88
IZ
1172 paren = ':';
1173 break;
1174 }
c277df42 1175 unknown:
3280af22
NIS
1176 if (*PL_regcomp_parse != ')')
1177 FAIL2("Sequence (?%c...) not recognized", *PL_regcomp_parse);
a0d0e21e
LW
1178 nextchar();
1179 *flagp = TRYAGAIN;
1180 return NULL;
1181 }
1182 }
1183 else {
3280af22
NIS
1184 parno = PL_regnpar;
1185 PL_regnpar++;
a0d0e21e 1186 ret = reganode(OPEN, parno);
c277df42 1187 open = 1;
a0d0e21e
LW
1188 }
1189 } else
1190 ret = NULL;
1191
1192 /* Pick up the branches, linking them together. */
c277df42 1193 br = regbranch(&flags, 1);
a0d0e21e
LW
1194 if (br == NULL)
1195 return(NULL);
3280af22
NIS
1196 if (*PL_regcomp_parse == '|') {
1197 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1198 reginsert(BRANCHJ, br);
1199 } else
1200 reginsert(BRANCH, br);
1201 have_branch = 1;
1202 if (SIZE_ONLY)
3280af22 1203 PL_extralen += 1; /* For BRANCHJ-BRANCH. */
c277df42
IZ
1204 } else if (paren == ':') {
1205 *flagp |= flags&SIMPLE;
1206 }
1207 if (open) { /* Starts with OPEN. */
1208 regtail(ret, br); /* OPEN -> first. */
1209 } else if (paren != '?') /* Not Conditional */
a0d0e21e 1210 ret = br;
821b33a5
IZ
1211 if (flags&HASWIDTH)
1212 *flagp |= HASWIDTH;
a0d0e21e 1213 *flagp |= flags&SPSTART;
c277df42 1214 lastbr = br;
3280af22
NIS
1215 while (*PL_regcomp_parse == '|') {
1216 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1217 ender = reganode(LONGJMP,0);
1218 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1219 }
1220 if (SIZE_ONLY)
3280af22 1221 PL_extralen += 2; /* Account for LONGJMP. */
a0d0e21e 1222 nextchar();
c277df42 1223 br = regbranch(&flags, 0);
a687059c 1224 if (br == NULL)
a0d0e21e 1225 return(NULL);
c277df42
IZ
1226 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1227 lastbr = br;
821b33a5
IZ
1228 if (flags&HASWIDTH)
1229 *flagp |= HASWIDTH;
a687059c 1230 *flagp |= flags&SPSTART;
a0d0e21e
LW
1231 }
1232
c277df42
IZ
1233 if (have_branch || paren != ':') {
1234 /* Make a closing node, and hook it on the end. */
1235 switch (paren) {
1236 case ':':
1237 ender = reg_node(TAIL);
1238 break;
1239 case 1:
1240 ender = reganode(CLOSE, parno);
1241 break;
1242 case '<':
c277df42
IZ
1243 case ',':
1244 case '=':
1245 case '!':
c277df42 1246 *flagp &= ~HASWIDTH;
821b33a5
IZ
1247 /* FALL THROUGH */
1248 case '>':
1249 ender = reg_node(SUCCEED);
c277df42
IZ
1250 break;
1251 case 0:
1252 ender = reg_node(END);
1253 break;
1254 }
1255 regtail(lastbr, ender);
a0d0e21e 1256
c277df42
IZ
1257 if (have_branch) {
1258 /* Hook the tails of the branches to the closing node. */
1259 for (br = ret; br != NULL; br = regnext(br)) {
1260 regoptail(br, ender);
1261 }
1262 }
a0d0e21e 1263 }
c277df42
IZ
1264
1265 {
1266 char *p;
1267 static char parens[] = "=!<,>";
1268
1269 if (paren && (p = strchr(parens, paren))) {
1270 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1271 int flag = (p - parens) > 1;
1272
1273 if (paren == '>')
1274 node = SUSPEND, flag = 0;
1275 reginsert(node,ret);
c277df42 1276 ret->flags = flag;
c277df42
IZ
1277 regtail(ret, reg_node(TAIL));
1278 }
a0d0e21e
LW
1279 }
1280
1281 /* Check for proper termination. */
3280af22 1282 if (paren && (PL_regcomp_parse >= PL_regxend || *nextchar() != ')')) {
a0d0e21e 1283 FAIL("unmatched () in regexp");
3280af22
NIS
1284 } else if (!paren && PL_regcomp_parse < PL_regxend) {
1285 if (*PL_regcomp_parse == ')') {
a0d0e21e
LW
1286 FAIL("unmatched () in regexp");
1287 } else
1288 FAIL("junk on end of regexp"); /* "Can't happen". */
1289 /* NOTREACHED */
1290 }
c277df42 1291 if (paren != 0) {
3280af22 1292 PL_regflags = oregflags;
c277df42 1293 }
a687059c 1294
a0d0e21e 1295 return(ret);
a687059c
LW
1296}
1297
1298/*
1299 - regbranch - one alternative of an | operator
1300 *
1301 * Implements the concatenation operator.
1302 */
76e3520e 1303STATIC regnode *
c277df42 1304regbranch(I32 *flagp, I32 first)
a687059c 1305{
5c0ca799 1306 dTHR;
c277df42
IZ
1307 register regnode *ret;
1308 register regnode *chain = NULL;
1309 register regnode *latest;
1310 I32 flags = 0, c = 0;
a0d0e21e 1311
c277df42
IZ
1312 if (first)
1313 ret = NULL;
1314 else {
3280af22 1315 if (!SIZE_ONLY && PL_extralen)
c277df42
IZ
1316 ret = reganode(BRANCHJ,0);
1317 else
1318 ret = reg_node(BRANCH);
1319 }
1320
1321 if (!first && SIZE_ONLY)
3280af22 1322 PL_extralen += 1; /* BRANCHJ */
c277df42
IZ
1323
1324 *flagp = WORST; /* Tentatively. */
a0d0e21e 1325
3280af22 1326 PL_regcomp_parse--;
a0d0e21e 1327 nextchar();
3280af22 1328 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '|' && *PL_regcomp_parse != ')') {
a0d0e21e
LW
1329 flags &= ~TRYAGAIN;
1330 latest = regpiece(&flags);
1331 if (latest == NULL) {
1332 if (flags & TRYAGAIN)
1333 continue;
1334 return(NULL);
c277df42
IZ
1335 } else if (ret == NULL)
1336 ret = latest;
a0d0e21e 1337 *flagp |= flags&HASWIDTH;
c277df42 1338 if (chain == NULL) /* First piece. */
a0d0e21e
LW
1339 *flagp |= flags&SPSTART;
1340 else {
3280af22 1341 PL_regnaughty++;
a0d0e21e 1342 regtail(chain, latest);
a687059c 1343 }
a0d0e21e 1344 chain = latest;
c277df42
IZ
1345 c++;
1346 }
1347 if (chain == NULL) { /* Loop ran zero times. */
1348 chain = reg_node(NOTHING);
1349 if (ret == NULL)
1350 ret = chain;
1351 }
1352 if (c == 1) {
1353 *flagp |= flags&SIMPLE;
a0d0e21e 1354 }
a687059c 1355
a0d0e21e 1356 return(ret);
a687059c
LW
1357}
1358
1359/*
1360 - regpiece - something followed by possible [*+?]
1361 *
1362 * Note that the branching code sequences used for ? and the general cases
1363 * of * and + are somewhat optimized: they use the same NOTHING node as
1364 * both the endmarker for their branch list and the body of the last branch.
1365 * It might seem that this node could be dispensed with entirely, but the
1366 * endmarker role is not redundant.
1367 */
76e3520e 1368STATIC regnode *
8ac85365 1369regpiece(I32 *flagp)
a687059c 1370{
5c0ca799 1371 dTHR;
c277df42 1372 register regnode *ret;
a0d0e21e
LW
1373 register char op;
1374 register char *next;
1375 I32 flags;
3280af22 1376 char *origparse = PL_regcomp_parse;
a0d0e21e
LW
1377 char *maxpos;
1378 I32 min;
c277df42 1379 I32 max = REG_INFTY;
a0d0e21e
LW
1380
1381 ret = regatom(&flags);
1382 if (ret == NULL) {
1383 if (flags & TRYAGAIN)
1384 *flagp |= TRYAGAIN;
1385 return(NULL);
1386 }
1387
3280af22 1388 op = *PL_regcomp_parse;
a0d0e21e 1389
3280af22
NIS
1390 if (op == '{' && regcurly(PL_regcomp_parse)) {
1391 next = PL_regcomp_parse + 1;
a0d0e21e
LW
1392 maxpos = Nullch;
1393 while (isDIGIT(*next) || *next == ',') {
1394 if (*next == ',') {
1395 if (maxpos)
1396 break;
1397 else
1398 maxpos = next;
a687059c 1399 }
a0d0e21e
LW
1400 next++;
1401 }
1402 if (*next == '}') { /* got one */
1403 if (!maxpos)
1404 maxpos = next;
3280af22
NIS
1405 PL_regcomp_parse++;
1406 min = atoi(PL_regcomp_parse);
a0d0e21e
LW
1407 if (*maxpos == ',')
1408 maxpos++;
1409 else
3280af22 1410 maxpos = PL_regcomp_parse;
a0d0e21e
LW
1411 max = atoi(maxpos);
1412 if (!max && *maxpos != '0')
c277df42
IZ
1413 max = REG_INFTY; /* meaning "infinity" */
1414 else if (max >= REG_INFTY)
1415 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
3280af22 1416 PL_regcomp_parse = next;
a0d0e21e
LW
1417 nextchar();
1418
1419 do_curly:
1420 if ((flags&SIMPLE)) {
3280af22 1421 PL_regnaughty += 2 + PL_regnaughty / 2;
a0d0e21e
LW
1422 reginsert(CURLY, ret);
1423 }
1424 else {
3280af22 1425 PL_regnaughty += 4 + PL_regnaughty; /* compound interest */
c277df42 1426 regtail(ret, reg_node(WHILEM));
3280af22 1427 if (!SIZE_ONLY && PL_extralen) {
c277df42
IZ
1428 reginsert(LONGJMP,ret);
1429 reginsert(NOTHING,ret);
1430 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1431 }
a0d0e21e 1432 reginsert(CURLYX,ret);
3280af22 1433 if (!SIZE_ONLY && PL_extralen)
c277df42
IZ
1434 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1435 regtail(ret, reg_node(NOTHING));
1436 if (SIZE_ONLY)
3280af22 1437 PL_extralen += 3;
a0d0e21e 1438 }
c277df42 1439 ret->flags = 0;
a0d0e21e
LW
1440
1441 if (min > 0)
821b33a5
IZ
1442 *flagp = WORST;
1443 if (max > 0)
1444 *flagp |= HASWIDTH;
a0d0e21e 1445 if (max && max < min)
c277df42
IZ
1446 FAIL("Can't do {n,m} with n > m");
1447 if (!SIZE_ONLY) {
1448 ARG1_SET(ret, min);
1449 ARG2_SET(ret, max);
a687059c 1450 }
a687059c 1451
a0d0e21e 1452 goto nest_check;
a687059c 1453 }
a0d0e21e 1454 }
a687059c 1455
a0d0e21e
LW
1456 if (!ISMULT1(op)) {
1457 *flagp = flags;
a687059c 1458 return(ret);
a0d0e21e 1459 }
bb20fd44 1460
c277df42 1461#if 0 /* Now runtime fix should be reliable. */
bb20fd44 1462 if (!(flags&HASWIDTH) && op != '?')
c277df42
IZ
1463 FAIL("regexp *+ operand could be empty");
1464#endif
bb20fd44 1465
a0d0e21e
LW
1466 nextchar();
1467
821b33a5 1468 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
a0d0e21e
LW
1469
1470 if (op == '*' && (flags&SIMPLE)) {
1471 reginsert(STAR, ret);
c277df42 1472 ret->flags = 0;
3280af22 1473 PL_regnaughty += 4;
a0d0e21e
LW
1474 }
1475 else if (op == '*') {
1476 min = 0;
1477 goto do_curly;
1478 } else if (op == '+' && (flags&SIMPLE)) {
1479 reginsert(PLUS, ret);
c277df42 1480 ret->flags = 0;
3280af22 1481 PL_regnaughty += 3;
a0d0e21e
LW
1482 }
1483 else if (op == '+') {
1484 min = 1;
1485 goto do_curly;
1486 } else if (op == '?') {
1487 min = 0; max = 1;
1488 goto do_curly;
1489 }
1490 nest_check:
3280af22 1491 if (PL_dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
a0d0e21e 1492 warn("%.*s matches null string many times",
3280af22 1493 PL_regcomp_parse - origparse, origparse);
a0d0e21e
LW
1494 }
1495
3280af22 1496 if (*PL_regcomp_parse == '?') {
a0d0e21e
LW
1497 nextchar();
1498 reginsert(MINMOD, ret);
c277df42 1499 regtail(ret, ret + NODE_STEP_REGNODE);
a0d0e21e 1500 }
3280af22 1501 if (ISMULT2(PL_regcomp_parse))
a0d0e21e
LW
1502 FAIL("nested *?+ in regexp");
1503
1504 return(ret);
a687059c
LW
1505}
1506
1507/*
1508 - regatom - the lowest level
1509 *
1510 * Optimization: gobbles an entire sequence of ordinary characters so that
1511 * it can turn them into a single node, which is smaller to store and
1512 * faster to run. Backslashed characters are exceptions, each becoming a
1513 * separate node; the code is simpler that way and it's not worth fixing.
1514 *
1515 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1516 */
76e3520e 1517STATIC regnode *
8ac85365 1518regatom(I32 *flagp)
a687059c 1519{
5c0ca799 1520 dTHR;
c277df42 1521 register regnode *ret = 0;
a0d0e21e
LW
1522 I32 flags;
1523
1524 *flagp = WORST; /* Tentatively. */
1525
1526tryagain:
3280af22 1527 switch (*PL_regcomp_parse) {
a0d0e21e 1528 case '^':
3280af22 1529 PL_seen_zerolen++;
a0d0e21e 1530 nextchar();
3280af22 1531 if (PL_regflags & PMf_MULTILINE)
c277df42 1532 ret = reg_node(MBOL);
3280af22 1533 else if (PL_regflags & PMf_SINGLELINE)
c277df42 1534 ret = reg_node(SBOL);
a0d0e21e 1535 else
c277df42 1536 ret = reg_node(BOL);
a0d0e21e
LW
1537 break;
1538 case '$':
3280af22
NIS
1539 if (PL_regcomp_parse[1])
1540 PL_seen_zerolen++;
a0d0e21e 1541 nextchar();
3280af22 1542 if (PL_regflags & PMf_MULTILINE)
c277df42 1543 ret = reg_node(MEOL);
3280af22 1544 else if (PL_regflags & PMf_SINGLELINE)
c277df42 1545 ret = reg_node(SEOL);
a0d0e21e 1546 else
c277df42 1547 ret = reg_node(EOL);
a0d0e21e
LW
1548 break;
1549 case '.':
1550 nextchar();
3280af22 1551 if (PL_regflags & PMf_SINGLELINE)
c277df42 1552 ret = reg_node(SANY);
a0d0e21e 1553 else
c277df42 1554 ret = reg_node(ANY);
3280af22 1555 PL_regnaughty++;
a0d0e21e
LW
1556 *flagp |= HASWIDTH|SIMPLE;
1557 break;
1558 case '[':
3280af22 1559 PL_regcomp_parse++;
a0d0e21e
LW
1560 ret = regclass();
1561 *flagp |= HASWIDTH|SIMPLE;
1562 break;
1563 case '(':
1564 nextchar();
1565 ret = reg(1, &flags);
1566 if (ret == NULL) {
1567 if (flags & TRYAGAIN)
1568 goto tryagain;
1569 return(NULL);
1570 }
c277df42 1571 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
a0d0e21e
LW
1572 break;
1573 case '|':
1574 case ')':
1575 if (flags & TRYAGAIN) {
1576 *flagp |= TRYAGAIN;
1577 return NULL;
1578 }
3280af22 1579 FAIL2("internal urp in regexp at /%s/", PL_regcomp_parse);
a0d0e21e
LW
1580 /* Supposed to be caught earlier. */
1581 break;
85afd4ae 1582 case '{':
3280af22
NIS
1583 if (!regcurly(PL_regcomp_parse)) {
1584 PL_regcomp_parse++;
85afd4ae
CS
1585 goto defchar;
1586 }
1587 /* FALL THROUGH */
a0d0e21e
LW
1588 case '?':
1589 case '+':
1590 case '*':
3115e423 1591 FAIL("?+*{} follows nothing in regexp");
a0d0e21e
LW
1592 break;
1593 case '\\':
3280af22 1594 switch (*++PL_regcomp_parse) {
a0d0e21e 1595 case 'A':
3280af22 1596 PL_seen_zerolen++;
c277df42 1597 ret = reg_node(SBOL);
a0d0e21e
LW
1598 *flagp |= SIMPLE;
1599 nextchar();
1600 break;
1601 case 'G':
c277df42 1602 ret = reg_node(GPOS);
3280af22 1603 PL_regseen |= REG_SEEN_GPOS;
a0d0e21e
LW
1604 *flagp |= SIMPLE;
1605 nextchar();
1606 break;
1607 case 'Z':
c277df42 1608 ret = reg_node(SEOL);
a0d0e21e
LW
1609 *flagp |= SIMPLE;
1610 nextchar();
1611 break;
b85d18e9
IZ
1612 case 'z':
1613 ret = reg_node(EOS);
1614 *flagp |= SIMPLE;
3280af22 1615 PL_seen_zerolen++; /* Do not optimize RE away */
b85d18e9
IZ
1616 nextchar();
1617 break;
a0d0e21e 1618 case 'w':
3280af22 1619 ret = reg_node((PL_regflags & PMf_LOCALE) ? ALNUML : ALNUM);
a0d0e21e
LW
1620 *flagp |= HASWIDTH|SIMPLE;
1621 nextchar();
1622 break;
1623 case 'W':
3280af22 1624 ret = reg_node((PL_regflags & PMf_LOCALE) ? NALNUML : NALNUM);
a0d0e21e
LW
1625 *flagp |= HASWIDTH|SIMPLE;
1626 nextchar();
1627 break;
1628 case 'b':
3280af22
NIS
1629 PL_seen_zerolen++;
1630 ret = reg_node((PL_regflags & PMf_LOCALE) ? BOUNDL : BOUND);
a0d0e21e
LW
1631 *flagp |= SIMPLE;
1632 nextchar();
1633 break;
1634 case 'B':
3280af22
NIS
1635 PL_seen_zerolen++;
1636 ret = reg_node((PL_regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
a0d0e21e
LW
1637 *flagp |= SIMPLE;
1638 nextchar();
1639 break;
1640 case 's':
3280af22 1641 ret = reg_node((PL_regflags & PMf_LOCALE) ? SPACEL : SPACE);
a0d0e21e
LW
1642 *flagp |= HASWIDTH|SIMPLE;
1643 nextchar();
1644 break;
1645 case 'S':
3280af22 1646 ret = reg_node((PL_regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
a0d0e21e
LW
1647 *flagp |= HASWIDTH|SIMPLE;
1648 nextchar();
1649 break;
1650 case 'd':
c277df42 1651 ret = reg_node(DIGIT);
a0d0e21e
LW
1652 *flagp |= HASWIDTH|SIMPLE;
1653 nextchar();
1654 break;
1655 case 'D':
c277df42 1656 ret = reg_node(NDIGIT);
a0d0e21e
LW
1657 *flagp |= HASWIDTH|SIMPLE;
1658 nextchar();
1659 break;
1660 case 'n':
1661 case 'r':
1662 case 't':
1663 case 'f':
1664 case 'e':
1665 case 'a':
1666 case 'x':
1667 case 'c':
1668 case '0':
1669 goto defchar;
1670 case '1': case '2': case '3': case '4':
1671 case '5': case '6': case '7': case '8': case '9':
1672 {
3280af22 1673 I32 num = atoi(PL_regcomp_parse);
a0d0e21e 1674
3280af22 1675 if (num > 9 && num >= PL_regnpar)
a0d0e21e
LW
1676 goto defchar;
1677 else {
3280af22 1678 if (!SIZE_ONLY && num > PL_regcomp_rx->nparens)
ef64f398 1679 FAIL("reference to nonexistent group");
3280af22
NIS
1680 PL_regsawback = 1;
1681 ret = reganode((PL_regflags & PMf_FOLD)
1682 ? ((PL_regflags & PMf_LOCALE) ? REFFL : REFF)
c8756f30 1683 : REF, num);
a0d0e21e 1684 *flagp |= HASWIDTH;
3280af22
NIS
1685 while (isDIGIT(*PL_regcomp_parse))
1686 PL_regcomp_parse++;
1687 PL_regcomp_parse--;
a0d0e21e
LW
1688 nextchar();
1689 }
1690 }
1691 break;
1692 case '\0':
3280af22 1693 if (PL_regcomp_parse >= PL_regxend)
a0d0e21e
LW
1694 FAIL("trailing \\ in regexp");
1695 /* FALL THROUGH */
1696 default:
1697 goto defchar;
1698 }
1699 break;
4633a7c4
LW
1700
1701 case '#':
3280af22
NIS
1702 if (PL_regflags & PMf_EXTENDED) {
1703 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != '\n') PL_regcomp_parse++;
1704 if (PL_regcomp_parse < PL_regxend)
4633a7c4
LW
1705 goto tryagain;
1706 }
1707 /* FALL THROUGH */
1708
a0d0e21e
LW
1709 default: {
1710 register I32 len;
c277df42 1711 register U8 ender;
a0d0e21e 1712 register char *p;
c277df42 1713 char *oldp, *s;
a0d0e21e
LW
1714 I32 numlen;
1715
3280af22 1716 PL_regcomp_parse++;
a0d0e21e
LW
1717
1718 defchar:
3280af22
NIS
1719 ret = reg_node((PL_regflags & PMf_FOLD)
1720 ? ((PL_regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
bbce6d69 1721 : EXACT);
161b471a 1722 s = (char *) OPERAND(ret);
c277df42 1723 regc(0, s++); /* save spot for len */
3280af22
NIS
1724 for (len = 0, p = PL_regcomp_parse - 1;
1725 len < 127 && p < PL_regxend;
a0d0e21e
LW
1726 len++)
1727 {
1728 oldp = p;
5b5a24f7 1729
3280af22
NIS
1730 if (PL_regflags & PMf_EXTENDED)
1731 p = regwhite(p, PL_regxend);
a0d0e21e
LW
1732 switch (*p) {
1733 case '^':
1734 case '$':
1735 case '.':
1736 case '[':
1737 case '(':
1738 case ')':
1739 case '|':
1740 goto loopdone;
1741 case '\\':
1742 switch (*++p) {
1743 case 'A':
1744 case 'G':
1745 case 'Z':
b85d18e9 1746 case 'z':
a0d0e21e
LW
1747 case 'w':
1748 case 'W':
1749 case 'b':
1750 case 'B':
1751 case 's':
1752 case 'S':
1753 case 'd':
1754 case 'D':
1755 --p;
1756 goto loopdone;
1757 case 'n':
1758 ender = '\n';
1759 p++;
a687059c 1760 break;
a0d0e21e
LW
1761 case 'r':
1762 ender = '\r';
1763 p++;
a687059c 1764 break;
a0d0e21e
LW
1765 case 't':
1766 ender = '\t';
1767 p++;
a687059c 1768 break;
a0d0e21e
LW
1769 case 'f':
1770 ender = '\f';
1771 p++;
a687059c 1772 break;
a0d0e21e
LW
1773 case 'e':
1774 ender = '\033';
1775 p++;
a687059c 1776 break;
a0d0e21e
LW
1777 case 'a':
1778 ender = '\007';
1779 p++;
a687059c 1780 break;
a0d0e21e
LW
1781 case 'x':
1782 ender = scan_hex(++p, 2, &numlen);
1783 p += numlen;
a687059c 1784 break;
a0d0e21e
LW
1785 case 'c':
1786 p++;
bbce6d69 1787 ender = UCHARAT(p++);
1788 ender = toCTRL(ender);
a687059c 1789 break;
a0d0e21e
LW
1790 case '0': case '1': case '2': case '3':case '4':
1791 case '5': case '6': case '7': case '8':case '9':
1792 if (*p == '0' ||
3280af22 1793 (isDIGIT(p[1]) && atoi(p) >= PL_regnpar) ) {
a0d0e21e
LW
1794 ender = scan_oct(p, 3, &numlen);
1795 p += numlen;
1796 }
1797 else {
1798 --p;
1799 goto loopdone;
a687059c
LW
1800 }
1801 break;
a0d0e21e 1802 case '\0':
3280af22 1803 if (p >= PL_regxend)
a687059c
LW
1804 FAIL("trailing \\ in regexp");
1805 /* FALL THROUGH */
a0d0e21e
LW
1806 default:
1807 ender = *p++;
1808 break;
1809 }
1810 break;
a687059c 1811 default:
a0d0e21e
LW
1812 ender = *p++;
1813 break;
a687059c 1814 }
3280af22
NIS
1815 if (PL_regflags & PMf_EXTENDED)
1816 p = regwhite(p, PL_regxend);
a0d0e21e
LW
1817 if (ISMULT2(p)) { /* Back off on ?+*. */
1818 if (len)
1819 p = oldp;
1820 else {
1821 len++;
c277df42 1822 regc(ender, s++);
a0d0e21e
LW
1823 }
1824 break;
a687059c 1825 }
c277df42 1826 regc(ender, s++);
a0d0e21e
LW
1827 }
1828 loopdone:
3280af22 1829 PL_regcomp_parse = p - 1;
a0d0e21e
LW
1830 nextchar();
1831 if (len < 0)
1832 FAIL("internal disaster in regexp");
1833 if (len > 0)
1834 *flagp |= HASWIDTH;
1835 if (len == 1)
1836 *flagp |= SIMPLE;
c277df42 1837 if (!SIZE_ONLY)
a0d0e21e 1838 *OPERAND(ret) = len;
c277df42
IZ
1839 regc('\0', s++);
1840 if (SIZE_ONLY) {
3280af22 1841 PL_regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
c277df42 1842 } else {
3280af22 1843 PL_regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
c277df42 1844 }
a687059c 1845 }
a0d0e21e
LW
1846 break;
1847 }
a687059c 1848
a0d0e21e 1849 return(ret);
a687059c
LW
1850}
1851
873ef191 1852STATIC char *
8ac85365 1853regwhite(char *p, char *e)
5b5a24f7
CS
1854{
1855 while (p < e) {
1856 if (isSPACE(*p))
1857 ++p;
1858 else if (*p == '#') {
1859 do {
1860 p++;
1861 } while (p < e && *p != '\n');
1862 }
1863 else
1864 break;
1865 }
1866 return p;
1867}
1868
76e3520e 1869STATIC regnode *
8ac85365 1870regclass(void)
a687059c 1871{
5c0ca799 1872 dTHR;
c277df42 1873 register char *opnd, *s;
8ac85365 1874 register I32 Class;
a0d0e21e
LW
1875 register I32 lastclass = 1234;
1876 register I32 range = 0;
c277df42 1877 register regnode *ret;
a0d0e21e
LW
1878 register I32 def;
1879 I32 numlen;
1880
3280af22 1881 s = opnd = (char *) OPERAND(PL_regcode);
c277df42 1882 ret = reg_node(ANYOF);
8ac85365 1883 for (Class = 0; Class < 33; Class++)
c277df42 1884 regc(0, s++);
3280af22
NIS
1885 if (*PL_regcomp_parse == '^') { /* Complement of range. */
1886 PL_regnaughty++;
1887 PL_regcomp_parse++;
c277df42 1888 if (!SIZE_ONLY)
bbce6d69 1889 *opnd |= ANYOF_INVERT;
1890 }
c277df42 1891 if (!SIZE_ONLY) {
3280af22
NIS
1892 PL_regcode += ANY_SKIP;
1893 if (PL_regflags & PMf_FOLD)
bbce6d69 1894 *opnd |= ANYOF_FOLD;
3280af22 1895 if (PL_regflags & PMf_LOCALE)
bbce6d69 1896 *opnd |= ANYOF_LOCALE;
c277df42 1897 } else {
3280af22 1898 PL_regsize += ANY_SKIP;
a0d0e21e 1899 }
3280af22 1900 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
a0d0e21e 1901 goto skipcond; /* allow 1st char to be ] or - */
3280af22 1902 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
a0d0e21e 1903 skipcond:
3280af22
NIS
1904 Class = UCHARAT(PL_regcomp_parse++);
1905 if (Class == '[' && PL_regcomp_parse + 1 < PL_regxend &&
4599a1de 1906 /* I smell either [: or [= or [. -- POSIX has been here, right? */
3280af22
NIS
1907 (*PL_regcomp_parse == ':' || *PL_regcomp_parse == '=' || *PL_regcomp_parse == '.')) {
1908 char posixccc = *PL_regcomp_parse;
1909 char* posixccs = PL_regcomp_parse++;
4599a1de 1910
3280af22
NIS
1911 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != posixccc)
1912 PL_regcomp_parse++;
1913 if (PL_regcomp_parse == PL_regxend)
4599a1de 1914 /* Grandfather lone [:, [=, [. */
3280af22 1915 PL_regcomp_parse = posixccs;
4599a1de 1916 else {
3280af22
NIS
1917 PL_regcomp_parse++; /* skip over the posixccc */
1918 if (*PL_regcomp_parse == ']') {
4599a1de
JH
1919 /* Not Implemented Yet.
1920 * (POSIX Extended Character Classes, that is)
1921 * The text between e.g. [: and :] would start
77d41b28 1922 * at posixccs + 1 and stop at regcomp_parse - 2. */
3280af22 1923 if (PL_dowarn && !SIZE_ONLY)
4599a1de 1924 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
3280af22 1925 PL_regcomp_parse++; /* skip over the ending ] */
4599a1de
JH
1926 }
1927 }
1928 }
8ac85365 1929 if (Class == '\\') {
3280af22 1930 Class = UCHARAT(PL_regcomp_parse++);
8ac85365 1931 switch (Class) {
a0d0e21e 1932 case 'w':
ae5c130c 1933 if (!SIZE_ONLY) {
3280af22 1934 if (PL_regflags & PMf_LOCALE)
bbce6d69 1935 *opnd |= ANYOF_ALNUML;
ae5c130c
GS
1936 else {
1937 for (Class = 0; Class < 256; Class++)
1938 if (isALNUM(Class))
1939 ANYOF_SET(opnd, Class);
1940 }
bbce6d69 1941 }
a0d0e21e
LW
1942 lastclass = 1234;
1943 continue;
1944 case 'W':
ae5c130c 1945 if (!SIZE_ONLY) {
3280af22 1946 if (PL_regflags & PMf_LOCALE)
bbce6d69 1947 *opnd |= ANYOF_NALNUML;
ae5c130c
GS
1948 else {
1949 for (Class = 0; Class < 256; Class++)
1950 if (!isALNUM(Class))
1951 ANYOF_SET(opnd, Class);
1952 }
bbce6d69 1953 }
a0d0e21e
LW
1954 lastclass = 1234;
1955 continue;
1956 case 's':
ae5c130c 1957 if (!SIZE_ONLY) {
3280af22 1958 if (PL_regflags & PMf_LOCALE)
bbce6d69 1959 *opnd |= ANYOF_SPACEL;
ae5c130c
GS
1960 else {
1961 for (Class = 0; Class < 256; Class++)
1962 if (isSPACE(Class))
1963 ANYOF_SET(opnd, Class);
1964 }
bbce6d69 1965 }
a0d0e21e
LW
1966 lastclass = 1234;
1967 continue;
1968 case 'S':
ae5c130c 1969 if (!SIZE_ONLY) {
3280af22 1970 if (PL_regflags & PMf_LOCALE)
bbce6d69 1971 *opnd |= ANYOF_NSPACEL;
ae5c130c
GS
1972 else {
1973 for (Class = 0; Class < 256; Class++)
1974 if (!isSPACE(Class))
1975 ANYOF_SET(opnd, Class);
1976 }
bbce6d69 1977 }
a0d0e21e
LW
1978 lastclass = 1234;
1979 continue;
1980 case 'd':
ae5c130c
GS
1981 if (!SIZE_ONLY) {
1982 for (Class = '0'; Class <= '9'; Class++)
1983 ANYOF_SET(opnd, Class);
1984 }
a0d0e21e
LW
1985 lastclass = 1234;
1986 continue;
1987 case 'D':
ae5c130c
GS
1988 if (!SIZE_ONLY) {
1989 for (Class = 0; Class < '0'; Class++)
1990 ANYOF_SET(opnd, Class);
1991 for (Class = '9' + 1; Class < 256; Class++)
1992 ANYOF_SET(opnd, Class);
1993 }
a0d0e21e
LW
1994 lastclass = 1234;
1995 continue;
1996 case 'n':
8ac85365 1997 Class = '\n';
a0d0e21e
LW
1998 break;
1999 case 'r':
8ac85365 2000 Class = '\r';
a0d0e21e
LW
2001 break;
2002 case 't':
8ac85365 2003 Class = '\t';
a0d0e21e
LW
2004 break;
2005 case 'f':
8ac85365 2006 Class = '\f';
a0d0e21e
LW
2007 break;
2008 case 'b':
8ac85365 2009 Class = '\b';
a0d0e21e
LW
2010 break;
2011 case 'e':
8ac85365 2012 Class = '\033';
a0d0e21e
LW
2013 break;
2014 case 'a':
8ac85365 2015 Class = '\007';
a0d0e21e
LW
2016 break;
2017 case 'x':
3280af22
NIS
2018 Class = scan_hex(PL_regcomp_parse, 2, &numlen);
2019 PL_regcomp_parse += numlen;
a0d0e21e
LW
2020 break;
2021 case 'c':
3280af22 2022 Class = UCHARAT(PL_regcomp_parse++);
8ac85365 2023 Class = toCTRL(Class);
a0d0e21e
LW
2024 break;
2025 case '0': case '1': case '2': case '3': case '4':
2026 case '5': case '6': case '7': case '8': case '9':
3280af22
NIS
2027 Class = scan_oct(--PL_regcomp_parse, 3, &numlen);
2028 PL_regcomp_parse += numlen;
a0d0e21e
LW
2029 break;
2030 }
2031 }
2032 if (range) {
8ac85365 2033 if (lastclass > Class)
a0d0e21e
LW
2034 FAIL("invalid [] range in regexp");
2035 range = 0;
2036 }
2037 else {
8ac85365 2038 lastclass = Class;
3280af22
NIS
2039 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2040 PL_regcomp_parse[1] != ']') {
2041 PL_regcomp_parse++;
a0d0e21e
LW
2042 range = 1;
2043 continue; /* do it next time */
2044 }
a687059c 2045 }
ae5c130c 2046 if (!SIZE_ONLY) {
b1e066e5
JH
2047#ifndef ASCIIish
2048 if ((isLOWER(lastvalue) && isLOWER(value)) ||
2049 (isUPPER(lastvalue) && isUPPER(value))) {
2050 if (isLOWER(lastvalue)) {
2051 for (i = lastvalue; i <= value; i++)
2052 if (isLOWER(i))
2053 ANYOF_SET(opnd, i);
2054 } else {
2055 for (i = lastvalue; i <= value; i++)
2056 if (isUPPER(i))
2057 ANYOF_SET(opnd, i);
2058 }
2059 }
2060 else
2061#endif
2062 for ( ; lastclass <= Class; lastclass++)
2063 ANYOF_SET(opnd, lastclass);
ae5c130c 2064 }
8ac85365 2065 lastclass = Class;
a0d0e21e 2066 }
3280af22 2067 if (*PL_regcomp_parse != ']')
a0d0e21e
LW
2068 FAIL("unmatched [] in regexp");
2069 nextchar();
ae5c130c
GS
2070 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2071 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2072 for (Class = 0; Class < 256; ++Class) {
2073 if (ANYOF_TEST(opnd, Class)) {
2074 I32 cf = fold[Class];
2075 ANYOF_SET(opnd, cf);
2076 }
2077 }
2078 *opnd &= ~ANYOF_FOLD;
2079 }
2080 /* optimize inverted simple patterns (e.g. [^a-z]) */
2081 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2082 for (Class = 0; Class < 32; ++Class)
2083 opnd[1 + Class] ^= 0xFF;
2084 *opnd = 0;
2085 }
a0d0e21e
LW
2086 return ret;
2087}
2088
76e3520e 2089STATIC char*
8ac85365 2090nextchar(void)
a0d0e21e 2091{
5c0ca799 2092 dTHR;
3280af22 2093 char* retval = PL_regcomp_parse++;
a0d0e21e 2094
4633a7c4 2095 for (;;) {
3280af22
NIS
2096 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2097 PL_regcomp_parse[2] == '#') {
2098 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2099 PL_regcomp_parse++;
2100 PL_regcomp_parse++;
4633a7c4
LW
2101 continue;
2102 }
3280af22
NIS
2103 if (PL_regflags & PMf_EXTENDED) {
2104 if (isSPACE(*PL_regcomp_parse)) {
2105 PL_regcomp_parse++;
748a9306
LW
2106 continue;
2107 }
3280af22
NIS
2108 else if (*PL_regcomp_parse == '#') {
2109 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2110 PL_regcomp_parse++;
2111 PL_regcomp_parse++;
748a9306
LW
2112 continue;
2113 }
748a9306 2114 }
4633a7c4 2115 return retval;
a0d0e21e 2116 }
a687059c
LW
2117}
2118
2119/*
c277df42 2120- reg_node - emit a node
a0d0e21e 2121*/
76e3520e 2122STATIC regnode * /* Location. */
c277df42 2123reg_node(U8 op)
a687059c 2124{
5c0ca799 2125 dTHR;
c277df42
IZ
2126 register regnode *ret;
2127 register regnode *ptr;
a687059c 2128
3280af22 2129 ret = PL_regcode;
c277df42 2130 if (SIZE_ONLY) {
6b88bc9c 2131 SIZE_ALIGN(PL_regsize);
3280af22 2132 PL_regsize += 1;
a0d0e21e
LW
2133 return(ret);
2134 }
a687059c 2135
c277df42 2136 NODE_ALIGN_FILL(ret);
a0d0e21e 2137 ptr = ret;
c277df42 2138 FILL_ADVANCE_NODE(ptr, op);
3280af22 2139 PL_regcode = ptr;
a687059c 2140
a0d0e21e 2141 return(ret);
a687059c
LW
2142}
2143
2144/*
a0d0e21e
LW
2145- reganode - emit a node with an argument
2146*/
76e3520e 2147STATIC regnode * /* Location. */
c277df42 2148reganode(U8 op, U32 arg)
fe14fcc3 2149{
5c0ca799 2150 dTHR;
c277df42
IZ
2151 register regnode *ret;
2152 register regnode *ptr;
fe14fcc3 2153
3280af22 2154 ret = PL_regcode;
c277df42 2155 if (SIZE_ONLY) {
6b88bc9c 2156 SIZE_ALIGN(PL_regsize);
3280af22 2157 PL_regsize += 2;
a0d0e21e
LW
2158 return(ret);
2159 }
fe14fcc3 2160
c277df42 2161 NODE_ALIGN_FILL(ret);
a0d0e21e 2162 ptr = ret;
c277df42 2163 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3280af22 2164 PL_regcode = ptr;
fe14fcc3 2165
a0d0e21e 2166 return(ret);
fe14fcc3
LW
2167}
2168
2169/*
a0d0e21e
LW
2170- regc - emit (if appropriate) a byte of code
2171*/
76e3520e 2172STATIC void
c277df42 2173regc(U8 b, char* s)
a687059c 2174{
5c0ca799 2175 dTHR;
c277df42
IZ
2176 if (!SIZE_ONLY)
2177 *s = b;
a687059c
LW
2178}
2179
2180/*
a0d0e21e
LW
2181- reginsert - insert an operator in front of already-emitted operand
2182*
2183* Means relocating the operand.
2184*/
76e3520e 2185STATIC void
c277df42 2186reginsert(U8 op, regnode *opnd)
a687059c 2187{
5c0ca799 2188 dTHR;
c277df42
IZ
2189 register regnode *src;
2190 register regnode *dst;
2191 register regnode *place;
2192 register int offset = regarglen[(U8)op];
2193
2194/* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2195
2196 if (SIZE_ONLY) {
3280af22 2197 PL_regsize += NODE_STEP_REGNODE + offset;
a0d0e21e
LW
2198 return;
2199 }
a687059c 2200
3280af22
NIS
2201 src = PL_regcode;
2202 PL_regcode += NODE_STEP_REGNODE + offset;
2203 dst = PL_regcode;
a0d0e21e 2204 while (src > opnd)
c277df42 2205 StructCopy(--src, --dst, regnode);
a0d0e21e
LW
2206
2207 place = opnd; /* Op node, where operand used to be. */
c277df42
IZ
2208 src = NEXTOPER(place);
2209 FILL_ADVANCE_NODE(place, op);
2210 Zero(src, offset, regnode);
a687059c
LW
2211}
2212
2213/*
c277df42 2214- regtail - set the next-pointer at the end of a node chain of p to val.
a0d0e21e 2215*/
76e3520e 2216STATIC void
c277df42 2217regtail(regnode *p, regnode *val)
a687059c 2218{
5c0ca799 2219 dTHR;
c277df42
IZ
2220 register regnode *scan;
2221 register regnode *temp;
a0d0e21e
LW
2222 register I32 offset;
2223
c277df42 2224 if (SIZE_ONLY)
a0d0e21e
LW
2225 return;
2226
2227 /* Find last node. */
2228 scan = p;
2229 for (;;) {
2230 temp = regnext(scan);
2231 if (temp == NULL)
2232 break;
2233 scan = temp;
2234 }
a687059c 2235
c277df42
IZ
2236 if (reg_off_by_arg[OP(scan)]) {
2237 ARG_SET(scan, val - scan);
2238 } else {
2239 NEXT_OFF(scan) = val - scan;
2240 }
a687059c
LW
2241}
2242
2243/*
a0d0e21e
LW
2244- regoptail - regtail on operand of first argument; nop if operandless
2245*/
76e3520e 2246STATIC void
c277df42 2247regoptail(regnode *p, regnode *val)
a687059c 2248{
5c0ca799 2249 dTHR;
a0d0e21e 2250 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
c277df42
IZ
2251 if (p == NULL || SIZE_ONLY)
2252 return;
2253 if (regkind[(U8)OP(p)] == BRANCH) {
2254 regtail(NEXTOPER(p), val);
2255 } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2256 regtail(NEXTOPER(NEXTOPER(p)), val);
2257 } else
a0d0e21e 2258 return;
a687059c
LW
2259}
2260
2261/*
2262 - regcurly - a little FSA that accepts {\d+,?\d*}
2263 */
79072805 2264STATIC I32
8ac85365 2265regcurly(register char *s)
a687059c
LW
2266{
2267 if (*s++ != '{')
2268 return FALSE;
f0fcb552 2269 if (!isDIGIT(*s))
a687059c 2270 return FALSE;
f0fcb552 2271 while (isDIGIT(*s))
a687059c
LW
2272 s++;
2273 if (*s == ',')
2274 s++;
f0fcb552 2275 while (isDIGIT(*s))
a687059c
LW
2276 s++;
2277 if (*s != '}')
2278 return FALSE;
2279 return TRUE;
2280}
2281
a687059c 2282
76e3520e 2283STATIC regnode *
c277df42
IZ
2284dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2285{
35ff7856 2286#ifdef DEBUGGING
c277df42
IZ
2287 register char op = EXACT; /* Arbitrary non-END op. */
2288 register regnode *next, *onode;
2289
2290 while (op != END && (!last || node < last)) {
2291 /* While that wasn't END last time... */
2292
2293 NODE_ALIGN(node);
2294 op = OP(node);
2295 if (op == CLOSE)
2296 l--;
2297 next = regnext(node);
2298 /* Where, what. */
2299 if (OP(node) == OPTIMIZED)
2300 goto after_print;
2301 regprop(sv, node);
54dc92de 2302 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
c277df42
IZ
2303 2*l + 1, "", SvPVX(sv));
2304 if (next == NULL) /* Next ptr. */
2305 PerlIO_printf(Perl_debug_log, "(0)");
2306 else
2307 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2308 (void)PerlIO_putc(Perl_debug_log, '\n');
2309 after_print:
2310 if (regkind[(U8)op] == BRANCHJ) {
2311 register regnode *nnode = (OP(next) == LONGJMP
2312 ? regnext(next)
2313 : next);
2314 if (last && nnode > last)
2315 nnode = last;
2316 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2317 } else if (regkind[(U8)op] == BRANCH) {
2318 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2319 } else if ( op == CURLY) { /* `next' might be very big: optimizer */
2320 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2321 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2322 } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2323 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2324 next, sv, l + 1);
2325 } else if ( op == PLUS || op == STAR) {
2326 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2327 } else if (op == ANYOF) {
2328 node = NEXTOPER(node);
2329 node += ANY_SKIP;
2330 } else if (regkind[(U8)op] == EXACT) {
2331 /* Literal string, where present. */
2332 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2333 node = NEXTOPER(node);
2334 } else {
2335 node = NEXTOPER(node);
2336 node += regarglen[(U8)op];
2337 }
2338 if (op == CURLYX || op == OPEN)
2339 l++;
2340 else if (op == WHILEM)
2341 l--;
2342 }
17c3b450 2343#endif /* DEBUGGING */
c277df42
IZ
2344 return node;
2345}
2346
a687059c 2347/*
fd181c75 2348 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
a687059c
LW
2349 */
2350void
8ac85365 2351regdump(regexp *r)
a687059c 2352{
35ff7856 2353#ifdef DEBUGGING
5c0ca799 2354 dTHR;
46fc3d4c 2355 SV *sv = sv_newmortal();
a687059c 2356
c277df42 2357 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
a0d0e21e
LW
2358
2359 /* Header fields of interest. */
c277df42
IZ
2360 if (r->anchored_substr)
2361 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
3280af22 2362 PL_colors[0],
c277df42 2363 SvPVX(r->anchored_substr),
3280af22 2364 PL_colors[1],
c277df42
IZ
2365 SvTAIL(r->anchored_substr) ? "$" : "",
2366 r->anchored_offset);
2367 if (r->float_substr)
2368 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
3280af22 2369 PL_colors[0],
c277df42 2370 SvPVX(r->float_substr),
3280af22 2371 PL_colors[1],
c277df42
IZ
2372 SvTAIL(r->float_substr) ? "$" : "",
2373 r->float_min_offset, r->float_max_offset);
2374 if (r->check_substr)
2375 PerlIO_printf(Perl_debug_log,
2376 r->check_substr == r->float_substr
2377 ? "(checking floating" : "(checking anchored");
2378 if (r->reganch & ROPT_NOSCAN)
2379 PerlIO_printf(Perl_debug_log, " noscan");
2380 if (r->reganch & ROPT_CHECK_ALL)
2381 PerlIO_printf(Perl_debug_log, " isall");
2382 if (r->check_substr)
2383 PerlIO_printf(Perl_debug_log, ") ");
2384
46fc3d4c 2385 if (r->regstclass) {
2386 regprop(sv, r->regstclass);
2387 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2388 }
774d564b 2389 if (r->reganch & ROPT_ANCH) {
2390 PerlIO_printf(Perl_debug_log, "anchored");
2391 if (r->reganch & ROPT_ANCH_BOL)
2392 PerlIO_printf(Perl_debug_log, "(BOL)");
c277df42
IZ
2393 if (r->reganch & ROPT_ANCH_MBOL)
2394 PerlIO_printf(Perl_debug_log, "(MBOL)");
774d564b 2395 if (r->reganch & ROPT_ANCH_GPOS)
2396 PerlIO_printf(Perl_debug_log, "(GPOS)");
2397 PerlIO_putc(Perl_debug_log, ' ');
2398 }
c277df42
IZ
2399 if (r->reganch & ROPT_GPOS_SEEN)
2400 PerlIO_printf(Perl_debug_log, "GPOS ");
a0d0e21e 2401 if (r->reganch & ROPT_SKIP)
760ac839 2402 PerlIO_printf(Perl_debug_log, "plus ");
a0d0e21e 2403 if (r->reganch & ROPT_IMPLICIT)
760ac839 2404 PerlIO_printf(Perl_debug_log, "implicit ");
760ac839 2405 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
ce862d02
IZ
2406 if (r->reganch & ROPT_EVAL_SEEN)
2407 PerlIO_printf(Perl_debug_log, "with eval ");
760ac839 2408 PerlIO_printf(Perl_debug_log, "\n");
17c3b450 2409#endif /* DEBUGGING */
a687059c
LW
2410}
2411
2412/*
a0d0e21e
LW
2413- regprop - printable representation of opcode
2414*/
46fc3d4c 2415void
c277df42 2416regprop(SV *sv, regnode *o)
a687059c 2417{
35ff7856 2418#ifdef DEBUGGING
5c0ca799 2419 dTHR;
a0d0e21e
LW
2420 register char *p = 0;
2421
54dc92de 2422 sv_setpvn(sv, "", 0);
11343788 2423 switch (OP(o)) {
a0d0e21e
LW
2424 case BOL:
2425 p = "BOL";
2426 break;
2427 case MBOL:
2428 p = "MBOL";
2429 break;
2430 case SBOL:
2431 p = "SBOL";
2432 break;
2433 case EOL:
2434 p = "EOL";
2435 break;
b85d18e9
IZ
2436 case EOS:
2437 p = "EOS";
2438 break;
a0d0e21e
LW
2439 case MEOL:
2440 p = "MEOL";
2441 break;
2442 case SEOL:
2443 p = "SEOL";
2444 break;
2445 case ANY:
2446 p = "ANY";
2447 break;
2448 case SANY:
2449 p = "SANY";
2450 break;
2451 case ANYOF:
2452 p = "ANYOF";
2453 break;
2454 case BRANCH:
2455 p = "BRANCH";
2456 break;
bbce6d69 2457 case EXACT:
3280af22 2458 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
bbce6d69 2459 break;
2460 case EXACTF:
3280af22 2461 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
bbce6d69 2462 break;
2463 case EXACTFL:
3280af22 2464 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
a0d0e21e
LW
2465 break;
2466 case NOTHING:
2467 p = "NOTHING";
2468 break;
c277df42
IZ
2469 case TAIL:
2470 p = "TAIL";
2471 break;
a0d0e21e
LW
2472 case BACK:
2473 p = "BACK";
2474 break;
2475 case END:
2476 p = "END";
2477 break;
a0d0e21e
LW
2478 case BOUND:
2479 p = "BOUND";
2480 break;
bbce6d69 2481 case BOUNDL:
2482 p = "BOUNDL";
2483 break;
a0d0e21e
LW
2484 case NBOUND:
2485 p = "NBOUND";
2486 break;
bbce6d69 2487 case NBOUNDL:
2488 p = "NBOUNDL";
a0d0e21e
LW
2489 break;
2490 case CURLY:
5dc0d613 2491 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2492 break;
c277df42 2493 case CURLYM:
c277df42 2494 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42
IZ
2495 break;
2496 case CURLYN:
c277df42 2497 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2498 break;
a0d0e21e 2499 case CURLYX:
5dc0d613 2500 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e
LW
2501 break;
2502 case REF:
c277df42 2503 sv_catpvf(sv, "REF%d", ARG(o));
a0d0e21e 2504 break;
c8756f30 2505 case REFF:
c277df42 2506 sv_catpvf(sv, "REFF%d", ARG(o));
c8756f30
AK
2507 break;
2508 case REFFL:
c277df42 2509 sv_catpvf(sv, "REFFL%d", ARG(o));
c8756f30 2510 break;
a0d0e21e 2511 case OPEN:
c277df42 2512 sv_catpvf(sv, "OPEN%d", ARG(o));
a0d0e21e
LW
2513 break;
2514 case CLOSE:
c277df42 2515 sv_catpvf(sv, "CLOSE%d", ARG(o));
a0d0e21e
LW
2516 p = NULL;
2517 break;
2518 case STAR:
2519 p = "STAR";
2520 break;
2521 case PLUS:
2522 p = "PLUS";
2523 break;
2524 case MINMOD:
2525 p = "MINMOD";
2526 break;
774d564b 2527 case GPOS:
2528 p = "GPOS";
a0d0e21e
LW
2529 break;
2530 case UNLESSM:
c277df42 2531 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
a0d0e21e
LW
2532 break;
2533 case IFMATCH:
c277df42 2534 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
a0d0e21e
LW
2535 break;
2536 case SUCCEED:
2537 p = "SUCCEED";
2538 break;
2539 case WHILEM:
2540 p = "WHILEM";
2541 break;
bbce6d69 2542 case DIGIT:
2543 p = "DIGIT";
2544 break;
2545 case NDIGIT:
2546 p = "NDIGIT";
2547 break;
2548 case ALNUM:
2549 p = "ALNUM";
2550 break;
2551 case NALNUM:
2552 p = "NALNUM";
2553 break;
2554 case SPACE:
2555 p = "SPACE";
2556 break;
2557 case NSPACE:
2558 p = "NSPACE";
2559 break;
2560 case ALNUML:
2561 p = "ALNUML";
2562 break;
2563 case NALNUML:
2564 p = "NALNUML";
2565 break;
2566 case SPACEL:
2567 p = "SPACEL";
2568 break;
2569 case NSPACEL:
2570 p = "NSPACEL";
2571 break;
c277df42
IZ
2572 case EVAL:
2573 p = "EVAL";
2574 break;
2575 case LONGJMP:
2576 p = "LONGJMP";
2577 break;
2578 case BRANCHJ:
2579 p = "BRANCHJ";
2580 break;
2581 case IFTHEN:
2582 p = "IFTHEN";
2583 break;
2584 case GROUPP:
2585 sv_catpvf(sv, "GROUPP%d", ARG(o));
2586 break;
2587 case LOGICAL:
2588 p = "LOGICAL";
2589 break;
2590 case SUSPEND:
2591 p = "SUSPEND";
2592 break;
2593 case RENUM:
2594 p = "RENUM";
2595 break;
2596 case OPTIMIZED:
2597 p = "OPTIMIZED";
2598 break;
a0d0e21e
LW
2599 default:
2600 FAIL("corrupted regexp opcode");
2601 }
46fc3d4c 2602 if (p)
2603 sv_catpv(sv, p);
17c3b450 2604#endif /* DEBUGGING */
35ff7856 2605}
a687059c 2606
2b69d0c2 2607void
8ac85365 2608pregfree(struct regexp *r)
a687059c 2609{
5c0ca799 2610 dTHR;
c277df42 2611 if (!r || (--r->refcnt > 0))
a0d0e21e 2612 return;
c277df42 2613 if (r->precomp)
a0d0e21e 2614 Safefree(r->precomp);
c277df42 2615 if (r->subbase)
a0d0e21e 2616 Safefree(r->subbase);
a193d654
GS
2617 if (r->substrs) {
2618 if (r->anchored_substr)
2619 SvREFCNT_dec(r->anchored_substr);
2620 if (r->float_substr)
2621 SvREFCNT_dec(r->float_substr);
2779dcf1 2622 Safefree(r->substrs);
a193d654 2623 }
c277df42
IZ
2624 if (r->data) {
2625 int n = r->data->count;
2626 while (--n >= 0) {
2627 switch (r->data->what[n]) {
2628 case 's':
2629 SvREFCNT_dec((SV*)r->data->data[n]);
2630 break;
2631 case 'o':
2632 op_free((OP_4tree*)r->data->data[n]);
2633 break;
2634 case 'n':
2635 break;
2636 default:
2637 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2638 }
2639 }
2640 Safefree(r->data->what);
2641 Safefree(r->data);
a0d0e21e
LW
2642 }
2643 Safefree(r->startp);
2644 Safefree(r->endp);
2645 Safefree(r);
a687059c 2646}
c277df42
IZ
2647
2648/*
2649 - regnext - dig the "next" pointer out of a node
2650 *
2651 * [Note, when REGALIGN is defined there are two places in regmatch()
2652 * that bypass this code for speed.]
2653 */
2654regnode *
2655regnext(register regnode *p)
2656{
5c0ca799 2657 dTHR;
c277df42
IZ
2658 register I32 offset;
2659
3280af22 2660 if (p == &PL_regdummy)
c277df42
IZ
2661 return(NULL);
2662
2663 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2664 if (offset == 0)
2665 return(NULL);
2666
c277df42 2667 return(p+offset);
c277df42
IZ
2668}
2669
01f988be 2670STATIC void
c277df42 2671re_croak2(const char* pat1,const char* pat2,...)
c277df42
IZ
2672{
2673 va_list args;
2674 STRLEN l1 = strlen(pat1);
2675 STRLEN l2 = strlen(pat2);
2676 char buf[512];
2677 char *message;
2678
2679 if (l1 > 510)
2680 l1 = 510;
2681 if (l1 + l2 > 510)
2682 l2 = 510 - l1;
2683 Copy(pat1, buf, l1 , char);
2684 Copy(pat2, buf + l1, l2 , char);
3b818b81
GS
2685 buf[l1 + l2] = '\n';
2686 buf[l1 + l2 + 1] = '\0';
c277df42 2687 va_start(args, pat2);
c277df42
IZ
2688 message = mess(buf, &args);
2689 va_end(args);
2690 l1 = strlen(message);
2691 if (l1 > 512)
2692 l1 = 512;
2693 Copy(message, buf, l1 , char);
2694 buf[l1] = '\0'; /* Overwrite \n */
2695 croak("%s", buf);
2696}