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