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