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