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