[Patch for 5.005_54] re::debugcolors dumps core
[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
c712d376 779 PL_colors[i] = s = "";
d88dccdf 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{
11b8faa4 2107 dTHR;
620e46c5 2108 char *posixcc = 0;
2109
2110 if (value == '[' && PL_regcomp_parse + 1 < PL_regxend &&
2111 /* I smell either [: or [= or [. -- POSIX has been here, right? */
2112 (*PL_regcomp_parse == ':' ||
2113 *PL_regcomp_parse == '=' ||
2114 *PL_regcomp_parse == '.')) {
2115 char c = *PL_regcomp_parse;
2116 char* s = PL_regcomp_parse++;
2117
2118 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != c)
2119 PL_regcomp_parse++;
2120 if (PL_regcomp_parse == PL_regxend)
2121 /* Grandfather lone [:, [=, [. */
2122 PL_regcomp_parse = s;
2123 else {
2124 PL_regcomp_parse++; /* skip over the c */
2125 if (*PL_regcomp_parse == ']') {
2126 /* Not Implemented Yet.
2127 * (POSIX Extended Character Classes, that is)
2128 * The text between e.g. [: and :] would start
2129 * at s + 1 and stop at regcomp_parse - 2. */
2130 if (ckWARN(WARN_UNSAFE) && !SIZE_ONLY)
2131 warner(WARN_UNSAFE,
2132 "Character class syntax [%c %c] is reserved for future extensions", c, c);
2133 PL_regcomp_parse++; /* skip over the ending ] */
2134 posixcc = s + 1;
2135 }
2136 }
2137 }
2138
2139 return posixcc;
2140}
2141
76e3520e 2142STATIC regnode *
8ac85365 2143regclass(void)
a687059c 2144{
5c0ca799 2145 dTHR;
c277df42 2146 register char *opnd, *s;
a0ed51b3 2147 register I32 value;
2148 register I32 lastvalue = 1234;
a0d0e21e 2149 register I32 range = 0;
c277df42 2150 register regnode *ret;
a0d0e21e 2151 register I32 def;
2152 I32 numlen;
2153
3280af22 2154 s = opnd = (char *) OPERAND(PL_regcode);
c277df42 2155 ret = reg_node(ANYOF);
a0ed51b3 2156 for (value = 0; value < 33; value++)
c277df42 2157 regc(0, s++);
3280af22 2158 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2159 PL_regnaughty++;
2160 PL_regcomp_parse++;
c277df42 2161 if (!SIZE_ONLY)
bbce6d69 2162 *opnd |= ANYOF_INVERT;
2163 }
c277df42 2164 if (!SIZE_ONLY) {
3280af22 2165 PL_regcode += ANY_SKIP;
a0ed51b3 2166 if (FOLD)
bbce6d69 2167 *opnd |= ANYOF_FOLD;
a0ed51b3 2168 if (LOC)
bbce6d69 2169 *opnd |= ANYOF_LOCALE;
a0ed51b3 2170 }
2171 else {
3280af22 2172 PL_regsize += ANY_SKIP;
a0d0e21e 2173 }
3280af22 2174 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
a0d0e21e 2175 goto skipcond; /* allow 1st char to be ] or - */
3280af22 2176 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
a0d0e21e 2177 skipcond:
a0ed51b3 2178 value = UCHARAT(PL_regcomp_parse++);
620e46c5 2179 if (value == '[')
2180 (void)regpposixcc(value); /* ignore the return value for now */
2181 else if (value == '\\') {
a0ed51b3 2182 value = UCHARAT(PL_regcomp_parse++);
2183 switch (value) {
a0d0e21e 2184 case 'w':
ae5c130c 2185 if (!SIZE_ONLY) {
a0ed51b3 2186 if (LOC)
bbce6d69 2187 *opnd |= ANYOF_ALNUML;
ae5c130c 2188 else {
a0ed51b3 2189 for (value = 0; value < 256; value++)
2190 if (isALNUM(value))
2191 ANYOF_SET(opnd, value);
ae5c130c 2192 }
bbce6d69 2193 }
a0ed51b3 2194 lastvalue = 1234;
a0d0e21e 2195 continue;
2196 case 'W':
ae5c130c 2197 if (!SIZE_ONLY) {
a0ed51b3 2198 if (LOC)
bbce6d69 2199 *opnd |= ANYOF_NALNUML;
ae5c130c 2200 else {
a0ed51b3 2201 for (value = 0; value < 256; value++)
2202 if (!isALNUM(value))
2203 ANYOF_SET(opnd, value);
ae5c130c 2204 }
bbce6d69 2205 }
a0ed51b3 2206 lastvalue = 1234;
a0d0e21e 2207 continue;
2208 case 's':
ae5c130c 2209 if (!SIZE_ONLY) {
a0ed51b3 2210 if (LOC)
bbce6d69 2211 *opnd |= ANYOF_SPACEL;
ae5c130c 2212 else {
a0ed51b3 2213 for (value = 0; value < 256; value++)
2214 if (isSPACE(value))
2215 ANYOF_SET(opnd, value);
ae5c130c 2216 }
bbce6d69 2217 }
a0ed51b3 2218 lastvalue = 1234;
a0d0e21e 2219 continue;
2220 case 'S':
ae5c130c 2221 if (!SIZE_ONLY) {
a0ed51b3 2222 if (LOC)
bbce6d69 2223 *opnd |= ANYOF_NSPACEL;
ae5c130c 2224 else {
a0ed51b3 2225 for (value = 0; value < 256; value++)
2226 if (!isSPACE(value))
2227 ANYOF_SET(opnd, value);
ae5c130c 2228 }
bbce6d69 2229 }
a0ed51b3 2230 lastvalue = 1234;
a0d0e21e 2231 continue;
2232 case 'd':
ae5c130c 2233 if (!SIZE_ONLY) {
a0ed51b3 2234 for (value = '0'; value <= '9'; value++)
2235 ANYOF_SET(opnd, value);
ae5c130c 2236 }
a0ed51b3 2237 lastvalue = 1234;
a0d0e21e 2238 continue;
2239 case 'D':
ae5c130c 2240 if (!SIZE_ONLY) {
a0ed51b3 2241 for (value = 0; value < '0'; value++)
2242 ANYOF_SET(opnd, value);
2243 for (value = '9' + 1; value < 256; value++)
2244 ANYOF_SET(opnd, value);
ae5c130c 2245 }
a0ed51b3 2246 lastvalue = 1234;
a0d0e21e 2247 continue;
2248 case 'n':
a0ed51b3 2249 value = '\n';
a0d0e21e 2250 break;
2251 case 'r':
a0ed51b3 2252 value = '\r';
a0d0e21e 2253 break;
2254 case 't':
a0ed51b3 2255 value = '\t';
a0d0e21e 2256 break;
2257 case 'f':
a0ed51b3 2258 value = '\f';
a0d0e21e 2259 break;
2260 case 'b':
a0ed51b3 2261 value = '\b';
a0d0e21e 2262 break;
2263 case 'e':
a0ed51b3 2264 value = '\033';
a0d0e21e 2265 break;
2266 case 'a':
a0ed51b3 2267 value = '\007';
a0d0e21e 2268 break;
2269 case 'x':
a0ed51b3 2270 value = scan_hex(PL_regcomp_parse, 2, &numlen);
3280af22 2271 PL_regcomp_parse += numlen;
a0d0e21e 2272 break;
2273 case 'c':
a0ed51b3 2274 value = UCHARAT(PL_regcomp_parse++);
2275 value = toCTRL(value);
a0d0e21e 2276 break;
2277 case '0': case '1': case '2': case '3': case '4':
2278 case '5': case '6': case '7': case '8': case '9':
a0ed51b3 2279 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
3280af22 2280 PL_regcomp_parse += numlen;
a0d0e21e 2281 break;
2282 }
2283 }
2284 if (range) {
a0ed51b3 2285 if (lastvalue > value)
a0d0e21e 2286 FAIL("invalid [] range in regexp");
2287 range = 0;
2288 }
2289 else {
a0ed51b3 2290 lastvalue = value;
3280af22 2291 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2292 PL_regcomp_parse[1] != ']') {
2293 PL_regcomp_parse++;
a0d0e21e 2294 range = 1;
2295 continue; /* do it next time */
2296 }
a687059c 2297 }
ae5c130c 2298 if (!SIZE_ONLY) {
8ada0baa 2299#ifndef ASCIIish
2300 if ((isLOWER(lastvalue) && isLOWER(value)) ||
c8dba6f3 2301 (isUPPER(lastvalue) && isUPPER(value)))
2302 {
2303 I32 i;
8ada0baa 2304 if (isLOWER(lastvalue)) {
2305 for (i = lastvalue; i <= value; i++)
2306 if (isLOWER(i))
2307 ANYOF_SET(opnd, i);
2308 } else {
2309 for (i = lastvalue; i <= value; i++)
2310 if (isUPPER(i))
2311 ANYOF_SET(opnd, i);
2312 }
2313 }
2314 else
2315#endif
2316 for ( ; lastvalue <= value; lastvalue++)
2317 ANYOF_SET(opnd, lastvalue);
2318 }
a0ed51b3 2319 lastvalue = value;
a0d0e21e 2320 }
ae5c130c 2321 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2322 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
a0ed51b3 2323 for (value = 0; value < 256; ++value) {
2324 if (ANYOF_TEST(opnd, value)) {
22c35a8c 2325 I32 cf = PL_fold[value];
ae5c130c 2326 ANYOF_SET(opnd, cf);
2327 }
2328 }
2329 *opnd &= ~ANYOF_FOLD;
2330 }
2331 /* optimize inverted simple patterns (e.g. [^a-z]) */
2332 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
a0ed51b3 2333 for (value = 0; value < 32; ++value)
2334 opnd[1 + value] ^= 0xFF;
ae5c130c 2335 *opnd = 0;
2336 }
a0d0e21e 2337 return ret;
2338}
2339
a0ed51b3 2340STATIC regnode *
2341regclassutf8(void)
2342{
2343 register char *opnd, *e;
2344 register U32 value;
2345 register U32 lastvalue = 123456;
2346 register I32 range = 0;
2347 register regnode *ret;
2348 I32 numlen;
2349 I32 n;
2350 SV *listsv;
2351 U8 flags = 0;
c485e607 2352 dTHR;
a0ed51b3 2353
2354 if (*PL_regcomp_parse == '^') { /* Complement of range. */
2355 PL_regnaughty++;
2356 PL_regcomp_parse++;
2357 if (!SIZE_ONLY)
2358 flags |= ANYOF_INVERT;
2359 }
2360 if (!SIZE_ONLY) {
2361 if (FOLD)
2362 flags |= ANYOF_FOLD;
2363 if (LOC)
2364 flags |= ANYOF_LOCALE;
2365 listsv = newSVpv("# comment\n",0);
2366 }
2367
2368 if (*PL_regcomp_parse == ']' || *PL_regcomp_parse == '-')
2369 goto skipcond; /* allow 1st char to be ] or - */
2370
2371 while (PL_regcomp_parse < PL_regxend && *PL_regcomp_parse != ']') {
2372 skipcond:
dfe13c55 2373 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
a0ed51b3 2374 PL_regcomp_parse += numlen;
2375
620e46c5 2376 if (value == '[')
2377 (void)regpposixcc(value); /* ignore the return value for now */
2378 else if (value == '\\') {
dfe13c55 2379 value = utf8_to_uv((U8*)PL_regcomp_parse, &numlen);
a0ed51b3 2380 PL_regcomp_parse += numlen;
2381 switch (value) {
2382 case 'w':
2383 if (!SIZE_ONLY) {
2384 if (LOC)
2385 flags |= ANYOF_ALNUML;
2386
2387 sv_catpvf(listsv, "+utf8::IsAlnum\n");
2388 }
2389 lastvalue = 123456;
2390 continue;
2391 case 'W':
2392 if (!SIZE_ONLY) {
2393 if (LOC)
2394 flags |= ANYOF_NALNUML;
2395
2396 sv_catpvf(listsv,
2397 "-utf8::IsAlpha\n-utf8::IsDigit\n0000\t%04x\n%04x\tffff\n",
2398 '_' - 1,
2399 '_' + 1);
2400 }
2401 lastvalue = 123456;
2402 continue;
2403 case 's':
2404 if (!SIZE_ONLY) {
2405 if (LOC)
2406 flags |= ANYOF_SPACEL;
2407 sv_catpvf(listsv, "+utf8::IsSpace\n");
2408 if (!PL_utf8_space)
dfe13c55 2409 is_utf8_space((U8*)" ");
a0ed51b3 2410 }
2411 lastvalue = 123456;
2412 continue;
2413 case 'S':
2414 if (!SIZE_ONLY) {
2415 if (LOC)
2416 flags |= ANYOF_NSPACEL;
2417 sv_catpvf(listsv,
2418 "!utf8::IsSpace\n");
2419 if (!PL_utf8_space)
dfe13c55 2420 is_utf8_space((U8*)" ");
a0ed51b3 2421 }
2422 lastvalue = 123456;
2423 continue;
2424 case 'd':
2425 if (!SIZE_ONLY) {
2426 sv_catpvf(listsv, "+utf8::IsDigit\n");
2427 }
2428 lastvalue = 123456;
2429 continue;
2430 case 'D':
2431 if (!SIZE_ONLY) {
2432 sv_catpvf(listsv,
2433 "!utf8::IsDigit\n");
2434 }
2435 lastvalue = 123456;
2436 continue;
2437 case 'p':
2438 case 'P':
2439 if (*PL_regcomp_parse == '{') {
2440 e = strchr(PL_regcomp_parse++, '}');
2441 if (!e)
2442 FAIL("Missing right brace on \\p{}");
2443 n = e - PL_regcomp_parse;
2444 }
2445 else {
2446 e = PL_regcomp_parse;
2447 n = 1;
2448 }
2449 if (!SIZE_ONLY) {
2450 if (value == 'p')
2451 sv_catpvf(listsv, "+utf8::%.*s\n", n, PL_regcomp_parse);
2452 else
2453 sv_catpvf(listsv,
2454 "!utf8::%.*s\n", n, PL_regcomp_parse);
2455 }
2456 PL_regcomp_parse = e + 1;
2457 lastvalue = 123456;
2458 continue;
2459 case 'n':
2460 value = '\n';
2461 break;
2462 case 'r':
2463 value = '\r';
2464 break;
2465 case 't':
2466 value = '\t';
2467 break;
2468 case 'f':
2469 value = '\f';
2470 break;
2471 case 'b':
2472 value = '\b';
2473 break;
2474 case 'e':
2475 value = '\033';
2476 break;
2477 case 'a':
2478 value = '\007';
2479 break;
2480 case 'x':
2481 if (*PL_regcomp_parse == '{') {
2482 e = strchr(PL_regcomp_parse++, '}');
2483 if (!e)
2484 FAIL("Missing right brace on \\x{}");
2485 value = scan_hex(PL_regcomp_parse + 1, e - PL_regcomp_parse, &numlen);
2486 PL_regcomp_parse = e + 1;
2487 }
2488 else {
2489 value = scan_hex(PL_regcomp_parse, 2, &numlen);
2490 PL_regcomp_parse += numlen;
2491 }
2492 break;
2493 case 'c':
2494 value = UCHARAT(PL_regcomp_parse++);
2495 value = toCTRL(value);
2496 break;
2497 case '0': case '1': case '2': case '3': case '4':
2498 case '5': case '6': case '7': case '8': case '9':
2499 value = scan_oct(--PL_regcomp_parse, 3, &numlen);
2500 PL_regcomp_parse += numlen;
2501 break;
2502 }
2503 }
2504 if (range) {
2505 if (lastvalue > value)
2506 FAIL("invalid [] range in regexp");
2507 if (!SIZE_ONLY)
2508 sv_catpvf(listsv, "%04x\t%04x\n", lastvalue, value);
2509 lastvalue = value;
2510 range = 0;
2511 }
2512 else {
2513 lastvalue = value;
2514 if (*PL_regcomp_parse == '-' && PL_regcomp_parse+1 < PL_regxend &&
2515 PL_regcomp_parse[1] != ']') {
2516 PL_regcomp_parse++;
2517 range = 1;
2518 continue; /* do it next time */
2519 }
2520 if (!SIZE_ONLY)
2521 sv_catpvf(listsv, "%04x\n", value);
2522 }
2523 }
a0ed51b3 2524
2525 ret = reganode(ANYOFUTF8, 0);
2526
2527 if (!SIZE_ONLY) {
2528 SV *rv = swash_init("utf8", "", listsv, 1, 0);
2529 SvREFCNT_dec(listsv);
2530 n = add_data(1,"s");
2531 PL_regcomp_rx->data->data[n] = (void*)rv;
2532 ARG1_SET(ret, flags);
2533 ARG2_SET(ret, n);
2534 }
2535
2536 return ret;
2537}
2538
76e3520e 2539STATIC char*
8ac85365 2540nextchar(void)
a0d0e21e 2541{
5c0ca799 2542 dTHR;
3280af22 2543 char* retval = PL_regcomp_parse++;
a0d0e21e 2544
4633a7c4 2545 for (;;) {
3280af22 2546 if (*PL_regcomp_parse == '(' && PL_regcomp_parse[1] == '?' &&
2547 PL_regcomp_parse[2] == '#') {
2548 while (*PL_regcomp_parse && *PL_regcomp_parse != ')')
2549 PL_regcomp_parse++;
2550 PL_regcomp_parse++;
4633a7c4 2551 continue;
2552 }
3280af22 2553 if (PL_regflags & PMf_EXTENDED) {
2554 if (isSPACE(*PL_regcomp_parse)) {
2555 PL_regcomp_parse++;
748a9306 2556 continue;
2557 }
3280af22 2558 else if (*PL_regcomp_parse == '#') {
2559 while (*PL_regcomp_parse && *PL_regcomp_parse != '\n')
2560 PL_regcomp_parse++;
2561 PL_regcomp_parse++;
748a9306 2562 continue;
2563 }
748a9306 2564 }
4633a7c4 2565 return retval;
a0d0e21e 2566 }
a687059c 2567}
2568
2569/*
c277df42 2570- reg_node - emit a node
a0d0e21e 2571*/
76e3520e 2572STATIC regnode * /* Location. */
c277df42 2573reg_node(U8 op)
a687059c 2574{
5c0ca799 2575 dTHR;
c277df42 2576 register regnode *ret;
2577 register regnode *ptr;
a687059c 2578
3280af22 2579 ret = PL_regcode;
c277df42 2580 if (SIZE_ONLY) {
6b88bc9c 2581 SIZE_ALIGN(PL_regsize);
3280af22 2582 PL_regsize += 1;
a0d0e21e 2583 return(ret);
2584 }
a687059c 2585
c277df42 2586 NODE_ALIGN_FILL(ret);
a0d0e21e 2587 ptr = ret;
c277df42 2588 FILL_ADVANCE_NODE(ptr, op);
3280af22 2589 PL_regcode = ptr;
a687059c 2590
a0d0e21e 2591 return(ret);
a687059c 2592}
2593
2594/*
a0d0e21e 2595- reganode - emit a node with an argument
2596*/
76e3520e 2597STATIC regnode * /* Location. */
c277df42 2598reganode(U8 op, U32 arg)
fe14fcc3 2599{
5c0ca799 2600 dTHR;
c277df42 2601 register regnode *ret;
2602 register regnode *ptr;
fe14fcc3 2603
3280af22 2604 ret = PL_regcode;
c277df42 2605 if (SIZE_ONLY) {
6b88bc9c 2606 SIZE_ALIGN(PL_regsize);
3280af22 2607 PL_regsize += 2;
a0d0e21e 2608 return(ret);
2609 }
fe14fcc3 2610
c277df42 2611 NODE_ALIGN_FILL(ret);
a0d0e21e 2612 ptr = ret;
c277df42 2613 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
3280af22 2614 PL_regcode = ptr;
fe14fcc3 2615
a0d0e21e 2616 return(ret);
fe14fcc3 2617}
2618
2619/*
a0ed51b3 2620- regc - emit (if appropriate) a Unicode character
2621*/
2622STATIC void
2623reguni(UV uv, char* s, I32* lenp)
2624{
c485e607 2625 dTHR;
a0ed51b3 2626 if (SIZE_ONLY) {
dfe13c55 2627 U8 tmpbuf[10];
a0ed51b3 2628 *lenp = uv_to_utf8(tmpbuf, uv) - tmpbuf;
2629 }
2630 else
dfe13c55 2631 *lenp = uv_to_utf8((U8*)s, uv) - (U8*)s;
a0ed51b3 2632
2633}
2634
2635/*
a0d0e21e 2636- regc - emit (if appropriate) a byte of code
2637*/
76e3520e 2638STATIC void
c277df42 2639regc(U8 b, char* s)
a687059c 2640{
5c0ca799 2641 dTHR;
c277df42 2642 if (!SIZE_ONLY)
2643 *s = b;
a687059c 2644}
2645
2646/*
a0d0e21e 2647- reginsert - insert an operator in front of already-emitted operand
2648*
2649* Means relocating the operand.
2650*/
76e3520e 2651STATIC void
c277df42 2652reginsert(U8 op, regnode *opnd)
a687059c 2653{
5c0ca799 2654 dTHR;
c277df42 2655 register regnode *src;
2656 register regnode *dst;
2657 register regnode *place;
2658 register int offset = regarglen[(U8)op];
2659
22c35a8c 2660/* (PL_regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
c277df42 2661
2662 if (SIZE_ONLY) {
3280af22 2663 PL_regsize += NODE_STEP_REGNODE + offset;
a0d0e21e 2664 return;
2665 }
a687059c 2666
3280af22 2667 src = PL_regcode;
2668 PL_regcode += NODE_STEP_REGNODE + offset;
2669 dst = PL_regcode;
a0d0e21e 2670 while (src > opnd)
c277df42 2671 StructCopy(--src, --dst, regnode);
a0d0e21e 2672
2673 place = opnd; /* Op node, where operand used to be. */
c277df42 2674 src = NEXTOPER(place);
2675 FILL_ADVANCE_NODE(place, op);
2676 Zero(src, offset, regnode);
a687059c 2677}
2678
2679/*
c277df42 2680- regtail - set the next-pointer at the end of a node chain of p to val.
a0d0e21e 2681*/
76e3520e 2682STATIC void
c277df42 2683regtail(regnode *p, regnode *val)
a687059c 2684{
5c0ca799 2685 dTHR;
c277df42 2686 register regnode *scan;
2687 register regnode *temp;
a0d0e21e 2688 register I32 offset;
2689
c277df42 2690 if (SIZE_ONLY)
a0d0e21e 2691 return;
2692
2693 /* Find last node. */
2694 scan = p;
2695 for (;;) {
2696 temp = regnext(scan);
2697 if (temp == NULL)
2698 break;
2699 scan = temp;
2700 }
a687059c 2701
c277df42 2702 if (reg_off_by_arg[OP(scan)]) {
2703 ARG_SET(scan, val - scan);
a0ed51b3 2704 }
2705 else {
c277df42 2706 NEXT_OFF(scan) = val - scan;
2707 }
a687059c 2708}
2709
2710/*
a0d0e21e 2711- regoptail - regtail on operand of first argument; nop if operandless
2712*/
76e3520e 2713STATIC void
c277df42 2714regoptail(regnode *p, regnode *val)
a687059c 2715{
5c0ca799 2716 dTHR;
a0d0e21e 2717 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
c277df42 2718 if (p == NULL || SIZE_ONLY)
2719 return;
22c35a8c 2720 if (PL_regkind[(U8)OP(p)] == BRANCH) {
c277df42 2721 regtail(NEXTOPER(p), val);
a0ed51b3 2722 }
22c35a8c 2723 else if ( PL_regkind[(U8)OP(p)] == BRANCHJ) {
c277df42 2724 regtail(NEXTOPER(NEXTOPER(p)), val);
a0ed51b3 2725 }
2726 else
a0d0e21e 2727 return;
a687059c 2728}
2729
2730/*
2731 - regcurly - a little FSA that accepts {\d+,?\d*}
2732 */
79072805 2733STATIC I32
8ac85365 2734regcurly(register char *s)
a687059c 2735{
2736 if (*s++ != '{')
2737 return FALSE;
f0fcb552 2738 if (!isDIGIT(*s))
a687059c 2739 return FALSE;
f0fcb552 2740 while (isDIGIT(*s))
a687059c 2741 s++;
2742 if (*s == ',')
2743 s++;
f0fcb552 2744 while (isDIGIT(*s))
a687059c 2745 s++;
2746 if (*s != '}')
2747 return FALSE;
2748 return TRUE;
2749}
2750
a687059c 2751
76e3520e 2752STATIC regnode *
c277df42 2753dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2754{
35ff7856 2755#ifdef DEBUGGING
c277df42 2756 register char op = EXACT; /* Arbitrary non-END op. */
2757 register regnode *next, *onode;
2758
2759 while (op != END && (!last || node < last)) {
2760 /* While that wasn't END last time... */
2761
2762 NODE_ALIGN(node);
2763 op = OP(node);
2764 if (op == CLOSE)
2765 l--;
2766 next = regnext(node);
2767 /* Where, what. */
2768 if (OP(node) == OPTIMIZED)
2769 goto after_print;
2770 regprop(sv, node);
54dc92de 2771 PerlIO_printf(Perl_debug_log, "%4d:%*s%s", node - start,
c277df42 2772 2*l + 1, "", SvPVX(sv));
2773 if (next == NULL) /* Next ptr. */
2774 PerlIO_printf(Perl_debug_log, "(0)");
2775 else
2776 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2777 (void)PerlIO_putc(Perl_debug_log, '\n');
2778 after_print:
22c35a8c 2779 if (PL_regkind[(U8)op] == BRANCHJ) {
c277df42 2780 register regnode *nnode = (OP(next) == LONGJMP
2781 ? regnext(next)
2782 : next);
2783 if (last && nnode > last)
2784 nnode = last;
2785 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
a0ed51b3 2786 }
22c35a8c 2787 else if (PL_regkind[(U8)op] == BRANCH) {
c277df42 2788 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
a0ed51b3 2789 }
2790 else if ( op == CURLY) { /* `next' might be very big: optimizer */
c277df42 2791 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2792 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
a0ed51b3 2793 }
22c35a8c 2794 else if (PL_regkind[(U8)op] == CURLY && op != CURLYX) {
c277df42 2795 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2796 next, sv, l + 1);
a0ed51b3 2797 }
2798 else if ( op == PLUS || op == STAR) {
c277df42 2799 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
a0ed51b3 2800 }
2801 else if (op == ANYOF) {
c277df42 2802 node = NEXTOPER(node);
2803 node += ANY_SKIP;
a0ed51b3 2804 }
22c35a8c 2805 else if (PL_regkind[(U8)op] == EXACT) {
c277df42 2806 /* Literal string, where present. */
2807 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2808 node = NEXTOPER(node);
a0ed51b3 2809 }
2810 else {
c277df42 2811 node = NEXTOPER(node);
2812 node += regarglen[(U8)op];
2813 }
2814 if (op == CURLYX || op == OPEN)
2815 l++;
2816 else if (op == WHILEM)
2817 l--;
2818 }
17c3b450 2819#endif /* DEBUGGING */
c277df42 2820 return node;
2821}
2822
a687059c 2823/*
fd181c75 2824 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
a687059c 2825 */
2826void
8ac85365 2827regdump(regexp *r)
a687059c 2828{
35ff7856 2829#ifdef DEBUGGING
5c0ca799 2830 dTHR;
46fc3d4c 2831 SV *sv = sv_newmortal();
a687059c 2832
c277df42 2833 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
a0d0e21e 2834
2835 /* Header fields of interest. */
c277df42 2836 if (r->anchored_substr)
2837 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
3280af22 2838 PL_colors[0],
c277df42 2839 SvPVX(r->anchored_substr),
3280af22 2840 PL_colors[1],
c277df42 2841 SvTAIL(r->anchored_substr) ? "$" : "",
2842 r->anchored_offset);
2843 if (r->float_substr)
2844 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
3280af22 2845 PL_colors[0],
c277df42 2846 SvPVX(r->float_substr),
3280af22 2847 PL_colors[1],
c277df42 2848 SvTAIL(r->float_substr) ? "$" : "",
2849 r->float_min_offset, r->float_max_offset);
2850 if (r->check_substr)
2851 PerlIO_printf(Perl_debug_log,
2852 r->check_substr == r->float_substr
2853 ? "(checking floating" : "(checking anchored");
2854 if (r->reganch & ROPT_NOSCAN)
2855 PerlIO_printf(Perl_debug_log, " noscan");
2856 if (r->reganch & ROPT_CHECK_ALL)
2857 PerlIO_printf(Perl_debug_log, " isall");
2858 if (r->check_substr)
2859 PerlIO_printf(Perl_debug_log, ") ");
2860
46fc3d4c 2861 if (r->regstclass) {
2862 regprop(sv, r->regstclass);
2863 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2864 }
774d564b 2865 if (r->reganch & ROPT_ANCH) {
2866 PerlIO_printf(Perl_debug_log, "anchored");
2867 if (r->reganch & ROPT_ANCH_BOL)
2868 PerlIO_printf(Perl_debug_log, "(BOL)");
c277df42 2869 if (r->reganch & ROPT_ANCH_MBOL)
2870 PerlIO_printf(Perl_debug_log, "(MBOL)");
774d564b 2871 if (r->reganch & ROPT_ANCH_GPOS)
2872 PerlIO_printf(Perl_debug_log, "(GPOS)");
2873 PerlIO_putc(Perl_debug_log, ' ');
2874 }
c277df42 2875 if (r->reganch & ROPT_GPOS_SEEN)
2876 PerlIO_printf(Perl_debug_log, "GPOS ");
a0d0e21e 2877 if (r->reganch & ROPT_SKIP)
760ac839 2878 PerlIO_printf(Perl_debug_log, "plus ");
a0d0e21e 2879 if (r->reganch & ROPT_IMPLICIT)
760ac839 2880 PerlIO_printf(Perl_debug_log, "implicit ");
760ac839 2881 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
ce862d02 2882 if (r->reganch & ROPT_EVAL_SEEN)
2883 PerlIO_printf(Perl_debug_log, "with eval ");
760ac839 2884 PerlIO_printf(Perl_debug_log, "\n");
17c3b450 2885#endif /* DEBUGGING */
a687059c 2886}
2887
2888/*
a0d0e21e 2889- regprop - printable representation of opcode
2890*/
46fc3d4c 2891void
c277df42 2892regprop(SV *sv, regnode *o)
a687059c 2893{
35ff7856 2894#ifdef DEBUGGING
5c0ca799 2895 dTHR;
a0d0e21e 2896 register char *p = 0;
2897
54dc92de 2898 sv_setpvn(sv, "", 0);
11343788 2899 switch (OP(o)) {
a0d0e21e 2900 case BOL:
2901 p = "BOL";
2902 break;
2903 case MBOL:
2904 p = "MBOL";
2905 break;
2906 case SBOL:
2907 p = "SBOL";
2908 break;
2909 case EOL:
2910 p = "EOL";
2911 break;
b85d18e9 2912 case EOS:
2913 p = "EOS";
2914 break;
a0d0e21e 2915 case MEOL:
2916 p = "MEOL";
2917 break;
2918 case SEOL:
2919 p = "SEOL";
2920 break;
22c35a8c 2921 case REG_ANY:
a0d0e21e 2922 p = "ANY";
2923 break;
2924 case SANY:
2925 p = "SANY";
2926 break;
a0ed51b3 2927 case ANYUTF8:
2928 p = "ANYUTF8";
2929 break;
2930 case SANYUTF8:
2931 p = "SANYUTF8";
2932 break;
2933 case ANYOFUTF8:
2934 p = "ANYOFUTF8";
2935 break;
a0d0e21e 2936 case ANYOF:
2937 p = "ANYOF";
2938 break;
2939 case BRANCH:
2940 p = "BRANCH";
2941 break;
bbce6d69 2942 case EXACT:
3280af22 2943 sv_catpvf(sv, "EXACT <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
bbce6d69 2944 break;
2945 case EXACTF:
3280af22 2946 sv_catpvf(sv, "EXACTF <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
bbce6d69 2947 break;
2948 case EXACTFL:
3280af22 2949 sv_catpvf(sv, "EXACTFL <%s%s%s>", PL_colors[0], OPERAND(o) + 1, PL_colors[1]);
a0d0e21e 2950 break;
2951 case NOTHING:
2952 p = "NOTHING";
2953 break;
c277df42 2954 case TAIL:
2955 p = "TAIL";
2956 break;
a0d0e21e 2957 case BACK:
2958 p = "BACK";
2959 break;
2960 case END:
2961 p = "END";
2962 break;
a0d0e21e 2963 case BOUND:
2964 p = "BOUND";
2965 break;
bbce6d69 2966 case BOUNDL:
2967 p = "BOUNDL";
2968 break;
a0d0e21e 2969 case NBOUND:
2970 p = "NBOUND";
2971 break;
bbce6d69 2972 case NBOUNDL:
2973 p = "NBOUNDL";
a0d0e21e 2974 break;
2975 case CURLY:
5dc0d613 2976 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2977 break;
c277df42 2978 case CURLYM:
c277df42 2979 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2980 break;
2981 case CURLYN:
c277df42 2982 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2983 break;
a0d0e21e 2984 case CURLYX:
5dc0d613 2985 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2986 break;
2987 case REF:
c277df42 2988 sv_catpvf(sv, "REF%d", ARG(o));
a0d0e21e 2989 break;
c8756f30 2990 case REFF:
c277df42 2991 sv_catpvf(sv, "REFF%d", ARG(o));
c8756f30 2992 break;
2993 case REFFL:
c277df42 2994 sv_catpvf(sv, "REFFL%d", ARG(o));
c8756f30 2995 break;
a0d0e21e 2996 case OPEN:
c277df42 2997 sv_catpvf(sv, "OPEN%d", ARG(o));
a0d0e21e 2998 break;
2999 case CLOSE:
c277df42 3000 sv_catpvf(sv, "CLOSE%d", ARG(o));
a0d0e21e 3001 p = NULL;
3002 break;
3003 case STAR:
3004 p = "STAR";
3005 break;
3006 case PLUS:
3007 p = "PLUS";
3008 break;
3009 case MINMOD:
3010 p = "MINMOD";
3011 break;
774d564b 3012 case GPOS:
3013 p = "GPOS";
a0d0e21e 3014 break;
3015 case UNLESSM:
c277df42 3016 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
a0d0e21e 3017 break;
3018 case IFMATCH:
c277df42 3019 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
a0d0e21e 3020 break;
3021 case SUCCEED:
3022 p = "SUCCEED";
3023 break;
3024 case WHILEM:
3025 p = "WHILEM";
3026 break;
bbce6d69 3027 case DIGIT:
3028 p = "DIGIT";
3029 break;
3030 case NDIGIT:
3031 p = "NDIGIT";
3032 break;
3033 case ALNUM:
3034 p = "ALNUM";
3035 break;
3036 case NALNUM:
3037 p = "NALNUM";
3038 break;
3039 case SPACE:
3040 p = "SPACE";
3041 break;
3042 case NSPACE:
3043 p = "NSPACE";
3044 break;
3045 case ALNUML:
3046 p = "ALNUML";
3047 break;
3048 case NALNUML:
3049 p = "NALNUML";
3050 break;
3051 case SPACEL:
3052 p = "SPACEL";
3053 break;
3054 case NSPACEL:
3055 p = "NSPACEL";
3056 break;
c277df42 3057 case EVAL:
3058 p = "EVAL";
3059 break;
3060 case LONGJMP:
3061 p = "LONGJMP";
3062 break;
3063 case BRANCHJ:
3064 p = "BRANCHJ";
3065 break;
3066 case IFTHEN:
3067 p = "IFTHEN";
3068 break;
3069 case GROUPP:
3070 sv_catpvf(sv, "GROUPP%d", ARG(o));
3071 break;
3072 case LOGICAL:
0f5d15d6 3073 sv_catpvf(sv, "LOGICAL[%d]", o->flags);
c277df42 3074 break;
3075 case SUSPEND:
3076 p = "SUSPEND";
3077 break;
3078 case RENUM:
3079 p = "RENUM";
3080 break;
3081 case OPTIMIZED:
3082 p = "OPTIMIZED";
3083 break;
a0d0e21e 3084 default:
3085 FAIL("corrupted regexp opcode");
3086 }
46fc3d4c 3087 if (p)
3088 sv_catpv(sv, p);
17c3b450 3089#endif /* DEBUGGING */
35ff7856 3090}
a687059c 3091
2b69d0c2 3092void
8ac85365 3093pregfree(struct regexp *r)
a687059c 3094{
5c0ca799 3095 dTHR;
c277df42 3096 if (!r || (--r->refcnt > 0))
a0d0e21e 3097 return;
c277df42 3098 if (r->precomp)
a0d0e21e 3099 Safefree(r->precomp);
c277df42 3100 if (r->subbase)
a0d0e21e 3101 Safefree(r->subbase);
a193d654 3102 if (r->substrs) {
3103 if (r->anchored_substr)
3104 SvREFCNT_dec(r->anchored_substr);
3105 if (r->float_substr)
3106 SvREFCNT_dec(r->float_substr);
2779dcf1 3107 Safefree(r->substrs);
a193d654 3108 }
c277df42 3109 if (r->data) {
3110 int n = r->data->count;
3111 while (--n >= 0) {
3112 switch (r->data->what[n]) {
3113 case 's':
3114 SvREFCNT_dec((SV*)r->data->data[n]);
3115 break;
3116 case 'o':
3117 op_free((OP_4tree*)r->data->data[n]);
3118 break;
3119 case 'n':
3120 break;
3121 default:
3122 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
3123 }
3124 }
3125 Safefree(r->data->what);
3126 Safefree(r->data);
a0d0e21e 3127 }
3128 Safefree(r->startp);
3129 Safefree(r->endp);
3130 Safefree(r);
a687059c 3131}
c277df42 3132
3133/*
3134 - regnext - dig the "next" pointer out of a node
3135 *
3136 * [Note, when REGALIGN is defined there are two places in regmatch()
3137 * that bypass this code for speed.]
3138 */
3139regnode *
3140regnext(register regnode *p)
3141{
5c0ca799 3142 dTHR;
c277df42 3143 register I32 offset;
3144
3280af22 3145 if (p == &PL_regdummy)
c277df42 3146 return(NULL);
3147
3148 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
3149 if (offset == 0)
3150 return(NULL);
3151
c277df42 3152 return(p+offset);
c277df42 3153}
3154
01f988be 3155STATIC void
c277df42 3156re_croak2(const char* pat1,const char* pat2,...)
c277df42 3157{
3158 va_list args;
3159 STRLEN l1 = strlen(pat1);
3160 STRLEN l2 = strlen(pat2);
3161 char buf[512];
3162 char *message;
3163
3164 if (l1 > 510)
3165 l1 = 510;
3166 if (l1 + l2 > 510)
3167 l2 = 510 - l1;
3168 Copy(pat1, buf, l1 , char);
3169 Copy(pat2, buf + l1, l2 , char);
3b818b81 3170 buf[l1 + l2] = '\n';
3171 buf[l1 + l2 + 1] = '\0';
c277df42 3172 va_start(args, pat2);
c277df42 3173 message = mess(buf, &args);
3174 va_end(args);
3175 l1 = strlen(message);
3176 if (l1 > 512)
3177 l1 = 512;
3178 Copy(message, buf, l1 , char);
3179 buf[l1] = '\0'; /* Overwrite \n */
3180 croak("%s", buf);
3181}
a0ed51b3 3182
3183/* XXX Here's a total kludge. But we need to re-enter for swash routines. */
3184
3185void
3186save_re_context(void)
c485e607 3187{
3188 dTHR;
a0ed51b3 3189 SAVEPPTR(PL_bostr);
3190 SAVEPPTR(PL_regprecomp); /* uncompiled string. */
3191 SAVEI32(PL_regnpar); /* () count. */
3192 SAVEI32(PL_regsize); /* Code size. */
3193 SAVEI16(PL_regflags); /* are we folding, multilining? */
3194 SAVEPPTR(PL_reginput); /* String-input pointer. */
3195 SAVEPPTR(PL_regbol); /* Beginning of input, for ^ check. */
3196 SAVEPPTR(PL_regeol); /* End of input, for $ check. */
3197 SAVESPTR(PL_regstartp); /* Pointer to startp array. */
3198 SAVESPTR(PL_regendp); /* Ditto for endp. */
3199 SAVESPTR(PL_reglastparen); /* Similarly for lastparen. */
3200 SAVEPPTR(PL_regtill); /* How far we are required to go. */
3201 SAVEI32(PL_regprev); /* char before regbol, \n if none */
3202 SAVESPTR(PL_reg_start_tmp); /* from regexec.c */
3203 PL_reg_start_tmp = 0;
3204 SAVEFREEPV(PL_reg_start_tmp);
3205 SAVEI32(PL_reg_start_tmpl); /* from regexec.c */
3206 PL_reg_start_tmpl = 0;
3207 SAVESPTR(PL_regdata);
3208 SAVEI32(PL_reg_flags); /* from regexec.c */
3209 SAVEI32(PL_reg_eval_set); /* from regexec.c */
3210 SAVEI32(PL_regnarrate); /* from regexec.c */
3211 SAVESPTR(PL_regprogram); /* from regexec.c */
3212 SAVEINT(PL_regindent); /* from regexec.c */
3213 SAVESPTR(PL_regcc); /* from regexec.c */
3214 SAVESPTR(PL_curcop);
3215 SAVESPTR(PL_regcomp_rx); /* from regcomp.c */
3216 SAVEI32(PL_regseen); /* from regcomp.c */
3217 SAVEI32(PL_regsawback); /* Did we see \1, ...? */
3218 SAVEI32(PL_regnaughty); /* How bad is this pattern? */
3219 SAVESPTR(PL_regcode); /* Code-emit pointer; &regdummy = don't */
3220 SAVEPPTR(PL_regxend); /* End of input for compile */
3221 SAVEPPTR(PL_regcomp_parse); /* Input-scan pointer. */
54b6e2fa 3222 SAVESPTR(PL_reg_call_cc); /* from regexec.c */
3223 SAVESPTR(PL_reg_re); /* from regexec.c */
3224 SAVEPPTR(PL_reg_ganch); /* from regexec.c */
3225 SAVESPTR(PL_reg_sv); /* from regexec.c */
3226 SAVESPTR(PL_reg_magic); /* from regexec.c */
3227 SAVEI32(PL_reg_oldpos); /* from regexec.c */
3228 SAVESPTR(PL_reg_oldcurpm); /* from regexec.c */
3229 SAVESPTR(PL_reg_curpm); /* from regexec.c */
3230#ifdef DEBUGGING
3231 SAVEPPTR(PL_reg_starttry); /* from regexec.c */
3232#endif
a0ed51b3 3233}