add patch for C<use re 'debug'>
[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
56953603 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 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 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 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 65
66#define REG_COMP_C
a687059c 67#include "regcomp.h"
68
d4cce5f1 69#ifdef op
11343788 70#undef op
d4cce5f1 71#endif /* op */
11343788 72
fe14fcc3 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 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 89#ifdef atarist
90#define PERL_META "^$.[()|?+*\\"
91#else
a687059c 92#define META "^$.[()|?+*\\"
2b69d0c2 93#endif
a687059c 94
35c8bce7 95#ifdef SPSTART
96#undef SPSTART /* dratted cpp namespace... */
97#endif
a687059c 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 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 106
107/*
e50aee73 108 * Forward declarations for pregcomp()'s friends.
a687059c 109 */
a0d0e21e 110
76e3520e 111#ifndef PERL_OBJECT
c277df42 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 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 129/* Length of a variant. */
130
76e3520e 131#ifndef PERL_OBJECT
c277df42 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 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 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 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 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 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 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 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 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 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 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 434 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
435 && maxcount <= 10000) /* Complement check for big count */
c277df42 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 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 474 nogo:
475
476 /* Try optimization CURLYX => CURLYM. */
477 if ( OP(oscan) == CURLYX && data
c277df42 478 && !(data->flags & SF_HAS_PAR)
c277df42 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 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 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 577 data->flags |= SF_HAS_EVAL;
578 optimize_curly_tail:
c277df42 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 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 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 628 next = regnext(scan);
629
630 if ( next && (OP(next) != WHILEM) && next < last)
c277df42 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 658add_data(I32 n, char *s)
659{
77d41b28 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 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 669 New(1208, regcomp_rx->data->what, n, U8);
670 regcomp_rx->data->count = n;
c277df42 671 }
77d41b28 672 Copy(s, regcomp_rx->data->what + regcomp_rx->data->count - n, n, U8);
673 return regcomp_rx->data->count - n;
c277df42 674}
675
a687059c 676/*
e50aee73 677 - pregcomp - compile a regular expression into internal code
a687059c 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 695 regnode *scan;
696 SV **longest;
697 SV *longest_fixed;
698 SV *longest_float;
699 regnode *first;
a0d0e21e 700 I32 flags;
a0d0e21e 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 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 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 722 regnpar = 1;
723 regsize = 0L;
724 regcode = &regdummy;
c277df42 725 regc((U8)MAGIC, (char*)regcode);
a0d0e21e 726 if (reg(0, &flags) == NULL) {
727 Safefree(regprecomp);
728 regprecomp = Nullch;
729 return(NULL);
730 }
c277df42 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 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 758 /* Small enough for pointer-storage convention?
759 If extralen==0, this means that we will not need long jumps. */
c277df42 760 if (regsize >= 0x10000L && extralen)
761 regsize += extralen;
762 else
763 extralen = 0;
a0d0e21e 764
bbce6d69 765 /* Allocate space and initialize. */
c277df42 766 Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode),
767 char, regexp);
a0d0e21e 768 if (r == NULL)
769 FAIL("regexp out of space");
c277df42 770 r->refcnt = 1;
bbce6d69 771 r->prelen = xend - exp;
a0d0e21e 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 781 regnpar = 1;
782 regcode = r->program;
c277df42 783 regc((U8)MAGIC, (char*) regcode++);
784 r->data = 0;
a0d0e21e 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 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 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 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 817 }
818
a0d0e21e 819 /* Starting-point info. */
820 again:
c277df42 821 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
a0d0e21e 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 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 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 845 }
846 if (sawplus && (!sawopen || !regsawback))
847 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
848
c277df42 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 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 861 * earlier string may buy us something the later one won't.]
862 */
a0d0e21e 863 minlen = 0;
a687059c 864
c277df42 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 881 longest_float_length = SvCUR(data.longest_float);
882 if (longest_float_length
c277df42 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 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 906 longest_fixed_length = SvCUR(data.longest_fixed);
907 if (longest_fixed_length
c277df42 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 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 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 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 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 944 }
945
a0d0e21e 946 r->minlen = minlen;
c277df42 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 951 if (regseen & REG_SEEN_EVAL)
952 r->reganch |= ROPT_EVAL_SEEN;
a0d0e21e 953 Newz(1002, r->startp, regnpar, char*);
954 Newz(1002, r->endp, regnpar, char*);
955 DEBUG_r(regdump(r));
956 return(r);
a687059c 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 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 977 I32 flags, oregflags = regflags, have_branch = 0, open = 0;
978 char c;
a0d0e21e 979
821b33a5 980 *flagp = 0; /* Tentatively. */
a0d0e21e 981
982 /* Make an OPEN node, if parenthesized. */
983 if (paren) {
77d41b28 984 if (*regcomp_parse == '?') {
985 regcomp_parse++;
986 paren = *regcomp_parse++;
c277df42 987 ret = NULL; /* For look-ahead/behind. */
a0d0e21e 988 switch (paren) {
c277df42 989 case '<':
c277df42 990 regseen |= REG_SEEN_LOOKBEHIND;
77d41b28 991 if (*regcomp_parse == '!')
c277df42 992 paren = ',';
77d41b28 993 if (*regcomp_parse != '=' && *regcomp_parse != '!')
c277df42 994 goto unknown;
77d41b28 995 regcomp_parse++;
a0d0e21e 996 case '=':
997 case '!':
c277df42 998 seen_zerolen++;
999 case ':':
1000 case '>':
a0d0e21e 1001 break;
1002 case '$':
1003 case '@':
c277df42 1004 FAIL2("Sequence (?%c...) not implemented", (int)paren);
a0d0e21e 1005 break;
1006 case '#':
77d41b28 1007 while (*regcomp_parse && *regcomp_parse != ')')
1008 regcomp_parse++;
1009 if (*regcomp_parse != ')')
c277df42 1010 FAIL("Sequence (?#... not terminated");
a0d0e21e 1011 nextchar();
1012 *flagp = TRYAGAIN;
1013 return NULL;
c277df42 1014 case '{':
1015 {
1016 dTHR;
1017 I32 count = 1, n = 0;
1018 char c;
77d41b28 1019 char *s = regcomp_parse;
c277df42 1020 SV *sv;
1021 OP_4tree *sop, *rop;
1022
1023 seen_zerolen++;
ce862d02 1024 regseen |= REG_SEEN_EVAL;
77d41b28 1025 while (count && (c = *regcomp_parse)) {
1026 if (c == '\\' && regcomp_parse[1])
1027 regcomp_parse++;
c277df42 1028 else if (c == '{')
1029 count++;
1030 else if (c == '}')
1031 count--;
77d41b28 1032 regcomp_parse++;
c277df42 1033 }
77d41b28 1034 if (*regcomp_parse != ')')
c277df42 1035 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1036 if (!SIZE_ONLY) {
1037 AV *av;
1038
77d41b28 1039 if (regcomp_parse - 1 - s)
1040 sv = newSVpv(s, regcomp_parse - 1 - s);
c277df42 1041 else
1042 sv = newSVpv("", 0);
1043
1044 rop = sv_compile_2op(sv, &sop, "re", &av);
1045
1046 n = add_data(3, "nso");
77d41b28 1047 regcomp_rx->data->data[n] = (void*)rop;
1048 regcomp_rx->data->data[n+1] = (void*)av;
1049 regcomp_rx->data->data[n+2] = (void*)sop;
c277df42 1050 SvREFCNT_dec(sv);
cc6b7395 1051 } else { /* First pass */
e4d48cc9 1052 if (curcop == &compiling) {
1053 if (!(hints & HINT_RE_EVAL))
1054 FAIL("Eval-group not allowed, use re 'eval'");
1055 }
1056 else {
1057 FAIL("Eval-group not allowed at run time");
1058 }
cc6b7395 1059 if (tainted)
1060 FAIL("Eval-group in insecure regular expression");
c277df42 1061 }
1062
1063 nextchar();
c277df42 1064 return reganode(EVAL, n);
1065 }
1066 case '(':
1067 {
77d41b28 1068 if (regcomp_parse[0] == '?') {
1069 if (regcomp_parse[1] == '=' || regcomp_parse[1] == '!'
1070 || regcomp_parse[1] == '<'
1071 || regcomp_parse[1] == '{') { /* Lookahead or eval. */
c277df42 1072 I32 flag;
1073
1074 ret = reg_node(LOGICAL);
1075 regtail(ret, reg(1, &flag));
1076 goto insert_if;
1077 }
77d41b28 1078 } else if (regcomp_parse[0] >= '1' && regcomp_parse[0] <= '9' ) {
1079 parno = atoi(regcomp_parse++);
c277df42 1080
77d41b28 1081 while (isDIGIT(*regcomp_parse))
1082 regcomp_parse++;
c277df42 1083 ret = reganode(GROUPP, parno);
1084 if ((c = *nextchar()) != ')')
1085 FAIL2("Switch (?(number%c not recognized", c);
1086 insert_if:
1087 regtail(ret, reganode(IFTHEN, 0));
1088 br = regbranch(&flags, 1);
1089 if (br == NULL)
1090 br = reganode(LONGJMP, 0);
1091 else
1092 regtail(br, reganode(LONGJMP, 0));
1093 c = *nextchar();
1094 if (c == '|') {
1095 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1096 regbranch(&flags, 1);
1097 regtail(ret, lastbr);
1098 c = *nextchar();
1099 } else
1100 lastbr = NULL;
1101 if (c != ')')
1102 FAIL("Switch (?(condition)... contains too many branches");
1103 ender = reg_node(TAIL);
1104 regtail(br, ender);
1105 if (lastbr) {
1106 regtail(lastbr, ender);
1107 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1108 } else
1109 regtail(ret, ender);
1110 return ret;
1111 } else {
77d41b28 1112 FAIL2("Unknown condition for (?(%.2s", regcomp_parse);
c277df42 1113 }
1114 }
1b1626e4 1115 case 0:
c277df42 1116 FAIL("Sequence (? incomplete");
1b1626e4 1117 break;
a0d0e21e 1118 default:
77d41b28 1119 --regcomp_parse;
1120 while (*regcomp_parse && strchr("iogcmsx", *regcomp_parse)) {
1121 if (*regcomp_parse != 'o')
1122 pmflag(&regflags, *regcomp_parse);
1123 ++regcomp_parse;
48c036b1 1124 }
c277df42 1125 unknown:
77d41b28 1126 if (*regcomp_parse != ')')
1127 FAIL2("Sequence (?%c...) not recognized", *regcomp_parse);
a0d0e21e 1128 nextchar();
1129 *flagp = TRYAGAIN;
1130 return NULL;
1131 }
1132 }
1133 else {
1134 parno = regnpar;
1135 regnpar++;
1136 ret = reganode(OPEN, parno);
c277df42 1137 open = 1;
a0d0e21e 1138 }
1139 } else
1140 ret = NULL;
1141
1142 /* Pick up the branches, linking them together. */
c277df42 1143 br = regbranch(&flags, 1);
a0d0e21e 1144 if (br == NULL)
1145 return(NULL);
77d41b28 1146 if (*regcomp_parse == '|') {
c277df42 1147 if (!SIZE_ONLY && extralen) {
1148 reginsert(BRANCHJ, br);
1149 } else
1150 reginsert(BRANCH, br);
1151 have_branch = 1;
1152 if (SIZE_ONLY)
1153 extralen += 1; /* For BRANCHJ-BRANCH. */
1154 } else if (paren == ':') {
1155 *flagp |= flags&SIMPLE;
1156 }
1157 if (open) { /* Starts with OPEN. */
1158 regtail(ret, br); /* OPEN -> first. */
1159 } else if (paren != '?') /* Not Conditional */
a0d0e21e 1160 ret = br;
821b33a5 1161 if (flags&HASWIDTH)
1162 *flagp |= HASWIDTH;
a0d0e21e 1163 *flagp |= flags&SPSTART;
c277df42 1164 lastbr = br;
77d41b28 1165 while (*regcomp_parse == '|') {
c277df42 1166 if (!SIZE_ONLY && extralen) {
1167 ender = reganode(LONGJMP,0);
1168 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1169 }
1170 if (SIZE_ONLY)
1171 extralen += 2; /* Account for LONGJMP. */
a0d0e21e 1172 nextchar();
c277df42 1173 br = regbranch(&flags, 0);
a687059c 1174 if (br == NULL)
a0d0e21e 1175 return(NULL);
c277df42 1176 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1177 lastbr = br;
821b33a5 1178 if (flags&HASWIDTH)
1179 *flagp |= HASWIDTH;
a687059c 1180 *flagp |= flags&SPSTART;
a0d0e21e 1181 }
1182
c277df42 1183 if (have_branch || paren != ':') {
1184 /* Make a closing node, and hook it on the end. */
1185 switch (paren) {
1186 case ':':
1187 ender = reg_node(TAIL);
1188 break;
1189 case 1:
1190 ender = reganode(CLOSE, parno);
1191 break;
1192 case '<':
c277df42 1193 case ',':
1194 case '=':
1195 case '!':
c277df42 1196 *flagp &= ~HASWIDTH;
821b33a5 1197 /* FALL THROUGH */
1198 case '>':
1199 ender = reg_node(SUCCEED);
c277df42 1200 break;
1201 case 0:
1202 ender = reg_node(END);
1203 break;
1204 }
1205 regtail(lastbr, ender);
a0d0e21e 1206
c277df42 1207 if (have_branch) {
1208 /* Hook the tails of the branches to the closing node. */
1209 for (br = ret; br != NULL; br = regnext(br)) {
1210 regoptail(br, ender);
1211 }
1212 }
a0d0e21e 1213 }
c277df42 1214
1215 {
1216 char *p;
1217 static char parens[] = "=!<,>";
1218
1219 if (paren && (p = strchr(parens, paren))) {
1220 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1221 int flag = (p - parens) > 1;
1222
1223 if (paren == '>')
1224 node = SUSPEND, flag = 0;
1225 reginsert(node,ret);
c277df42 1226 ret->flags = flag;
c277df42 1227 regtail(ret, reg_node(TAIL));
1228 }
a0d0e21e 1229 }
1230
1231 /* Check for proper termination. */
77d41b28 1232 if (paren && (regcomp_parse >= regxend || *nextchar() != ')')) {
a0d0e21e 1233 FAIL("unmatched () in regexp");
77d41b28 1234 } else if (!paren && regcomp_parse < regxend) {
1235 if (*regcomp_parse == ')') {
a0d0e21e 1236 FAIL("unmatched () in regexp");
1237 } else
1238 FAIL("junk on end of regexp"); /* "Can't happen". */
1239 /* NOTREACHED */
1240 }
c277df42 1241 if (paren != 0) {
1242 regflags = oregflags;
1243 }
a687059c 1244
a0d0e21e 1245 return(ret);
a687059c 1246}
1247
1248/*
1249 - regbranch - one alternative of an | operator
1250 *
1251 * Implements the concatenation operator.
1252 */
76e3520e 1253STATIC regnode *
c277df42 1254regbranch(I32 *flagp, I32 first)
a687059c 1255{
c277df42 1256 register regnode *ret;
1257 register regnode *chain = NULL;
1258 register regnode *latest;
1259 I32 flags = 0, c = 0;
a0d0e21e 1260
c277df42 1261 if (first)
1262 ret = NULL;
1263 else {
1264 if (!SIZE_ONLY && extralen)
1265 ret = reganode(BRANCHJ,0);
1266 else
1267 ret = reg_node(BRANCH);
1268 }
1269
1270 if (!first && SIZE_ONLY)
1271 extralen += 1; /* BRANCHJ */
1272
1273 *flagp = WORST; /* Tentatively. */
a0d0e21e 1274
77d41b28 1275 regcomp_parse--;
a0d0e21e 1276 nextchar();
77d41b28 1277 while (regcomp_parse < regxend && *regcomp_parse != '|' && *regcomp_parse != ')') {
a0d0e21e 1278 flags &= ~TRYAGAIN;
1279 latest = regpiece(&flags);
1280 if (latest == NULL) {
1281 if (flags & TRYAGAIN)
1282 continue;
1283 return(NULL);
c277df42 1284 } else if (ret == NULL)
1285 ret = latest;
a0d0e21e 1286 *flagp |= flags&HASWIDTH;
c277df42 1287 if (chain == NULL) /* First piece. */
a0d0e21e 1288 *flagp |= flags&SPSTART;
1289 else {
1290 regnaughty++;
1291 regtail(chain, latest);
a687059c 1292 }
a0d0e21e 1293 chain = latest;
c277df42 1294 c++;
1295 }
1296 if (chain == NULL) { /* Loop ran zero times. */
1297 chain = reg_node(NOTHING);
1298 if (ret == NULL)
1299 ret = chain;
1300 }
1301 if (c == 1) {
1302 *flagp |= flags&SIMPLE;
a0d0e21e 1303 }
a687059c 1304
a0d0e21e 1305 return(ret);
a687059c 1306}
1307
1308/*
1309 - regpiece - something followed by possible [*+?]
1310 *
1311 * Note that the branching code sequences used for ? and the general cases
1312 * of * and + are somewhat optimized: they use the same NOTHING node as
1313 * both the endmarker for their branch list and the body of the last branch.
1314 * It might seem that this node could be dispensed with entirely, but the
1315 * endmarker role is not redundant.
1316 */
76e3520e 1317STATIC regnode *
8ac85365 1318regpiece(I32 *flagp)
a687059c 1319{
c277df42 1320 register regnode *ret;
a0d0e21e 1321 register char op;
1322 register char *next;
1323 I32 flags;
77d41b28 1324 char *origparse = regcomp_parse;
a0d0e21e 1325 char *maxpos;
1326 I32 min;
c277df42 1327 I32 max = REG_INFTY;
a0d0e21e 1328
1329 ret = regatom(&flags);
1330 if (ret == NULL) {
1331 if (flags & TRYAGAIN)
1332 *flagp |= TRYAGAIN;
1333 return(NULL);
1334 }
1335
77d41b28 1336 op = *regcomp_parse;
a0d0e21e 1337
77d41b28 1338 if (op == '{' && regcurly(regcomp_parse)) {
1339 next = regcomp_parse + 1;
a0d0e21e 1340 maxpos = Nullch;
1341 while (isDIGIT(*next) || *next == ',') {
1342 if (*next == ',') {
1343 if (maxpos)
1344 break;
1345 else
1346 maxpos = next;
a687059c 1347 }
a0d0e21e 1348 next++;
1349 }
1350 if (*next == '}') { /* got one */
1351 if (!maxpos)
1352 maxpos = next;
77d41b28 1353 regcomp_parse++;
1354 min = atoi(regcomp_parse);
a0d0e21e 1355 if (*maxpos == ',')
1356 maxpos++;
1357 else
77d41b28 1358 maxpos = regcomp_parse;
a0d0e21e 1359 max = atoi(maxpos);
1360 if (!max && *maxpos != '0')
c277df42 1361 max = REG_INFTY; /* meaning "infinity" */
1362 else if (max >= REG_INFTY)
1363 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
77d41b28 1364 regcomp_parse = next;
a0d0e21e 1365 nextchar();
1366
1367 do_curly:
1368 if ((flags&SIMPLE)) {
1369 regnaughty += 2 + regnaughty / 2;
1370 reginsert(CURLY, ret);
1371 }
1372 else {
1373 regnaughty += 4 + regnaughty; /* compound interest */
c277df42 1374 regtail(ret, reg_node(WHILEM));
1375 if (!SIZE_ONLY && extralen) {
1376 reginsert(LONGJMP,ret);
1377 reginsert(NOTHING,ret);
1378 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1379 }
a0d0e21e 1380 reginsert(CURLYX,ret);
c277df42 1381 if (!SIZE_ONLY && extralen)
1382 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1383 regtail(ret, reg_node(NOTHING));
1384 if (SIZE_ONLY)
1385 extralen += 3;
a0d0e21e 1386 }
c277df42 1387 ret->flags = 0;
a0d0e21e 1388
1389 if (min > 0)
821b33a5 1390 *flagp = WORST;
1391 if (max > 0)
1392 *flagp |= HASWIDTH;
a0d0e21e 1393 if (max && max < min)
c277df42 1394 FAIL("Can't do {n,m} with n > m");
1395 if (!SIZE_ONLY) {
1396 ARG1_SET(ret, min);
1397 ARG2_SET(ret, max);
a687059c 1398 }
a687059c 1399
a0d0e21e 1400 goto nest_check;
a687059c 1401 }
a0d0e21e 1402 }
a687059c 1403
a0d0e21e 1404 if (!ISMULT1(op)) {
1405 *flagp = flags;
a687059c 1406 return(ret);
a0d0e21e 1407 }
bb20fd44 1408
c277df42 1409#if 0 /* Now runtime fix should be reliable. */
bb20fd44 1410 if (!(flags&HASWIDTH) && op != '?')
c277df42 1411 FAIL("regexp *+ operand could be empty");
1412#endif
bb20fd44 1413
a0d0e21e 1414 nextchar();
1415
821b33a5 1416 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
a0d0e21e 1417
1418 if (op == '*' && (flags&SIMPLE)) {
1419 reginsert(STAR, ret);
c277df42 1420 ret->flags = 0;
a0d0e21e 1421 regnaughty += 4;
1422 }
1423 else if (op == '*') {
1424 min = 0;
1425 goto do_curly;
1426 } else if (op == '+' && (flags&SIMPLE)) {
1427 reginsert(PLUS, ret);
c277df42 1428 ret->flags = 0;
a0d0e21e 1429 regnaughty += 3;
1430 }
1431 else if (op == '+') {
1432 min = 1;
1433 goto do_curly;
1434 } else if (op == '?') {
1435 min = 0; max = 1;
1436 goto do_curly;
1437 }
1438 nest_check:
c277df42 1439 if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
a0d0e21e 1440 warn("%.*s matches null string many times",
77d41b28 1441 regcomp_parse - origparse, origparse);
a0d0e21e 1442 }
1443
77d41b28 1444 if (*regcomp_parse == '?') {
a0d0e21e 1445 nextchar();
1446 reginsert(MINMOD, ret);
c277df42 1447 regtail(ret, ret + NODE_STEP_REGNODE);
a0d0e21e 1448 }
77d41b28 1449 if (ISMULT2(regcomp_parse))
a0d0e21e 1450 FAIL("nested *?+ in regexp");
1451
1452 return(ret);
a687059c 1453}
1454
1455/*
1456 - regatom - the lowest level
1457 *
1458 * Optimization: gobbles an entire sequence of ordinary characters so that
1459 * it can turn them into a single node, which is smaller to store and
1460 * faster to run. Backslashed characters are exceptions, each becoming a
1461 * separate node; the code is simpler that way and it's not worth fixing.
1462 *
1463 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1464 */
76e3520e 1465STATIC regnode *
8ac85365 1466regatom(I32 *flagp)
a687059c 1467{
c277df42 1468 register regnode *ret = 0;
a0d0e21e 1469 I32 flags;
1470
1471 *flagp = WORST; /* Tentatively. */
1472
1473tryagain:
77d41b28 1474 switch (*regcomp_parse) {
a0d0e21e 1475 case '^':
c277df42 1476 seen_zerolen++;
a0d0e21e 1477 nextchar();
1478 if (regflags & PMf_MULTILINE)
c277df42 1479 ret = reg_node(MBOL);
a0d0e21e 1480 else if (regflags & PMf_SINGLELINE)
c277df42 1481 ret = reg_node(SBOL);
a0d0e21e 1482 else
c277df42 1483 ret = reg_node(BOL);
a0d0e21e 1484 break;
1485 case '$':
77d41b28 1486 if (regcomp_parse[1])
c277df42 1487 seen_zerolen++;
a0d0e21e 1488 nextchar();
1489 if (regflags & PMf_MULTILINE)
c277df42 1490 ret = reg_node(MEOL);
a0d0e21e 1491 else if (regflags & PMf_SINGLELINE)
c277df42 1492 ret = reg_node(SEOL);
a0d0e21e 1493 else
c277df42 1494 ret = reg_node(EOL);
a0d0e21e 1495 break;
1496 case '.':
1497 nextchar();
1498 if (regflags & PMf_SINGLELINE)
c277df42 1499 ret = reg_node(SANY);
a0d0e21e 1500 else
c277df42 1501 ret = reg_node(ANY);
a0d0e21e 1502 regnaughty++;
1503 *flagp |= HASWIDTH|SIMPLE;
1504 break;
1505 case '[':
77d41b28 1506 regcomp_parse++;
a0d0e21e 1507 ret = regclass();
1508 *flagp |= HASWIDTH|SIMPLE;
1509 break;
1510 case '(':
1511 nextchar();
1512 ret = reg(1, &flags);
1513 if (ret == NULL) {
1514 if (flags & TRYAGAIN)
1515 goto tryagain;
1516 return(NULL);
1517 }
c277df42 1518 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
a0d0e21e 1519 break;
1520 case '|':
1521 case ')':
1522 if (flags & TRYAGAIN) {
1523 *flagp |= TRYAGAIN;
1524 return NULL;
1525 }
77d41b28 1526 FAIL2("internal urp in regexp at /%s/", regcomp_parse);
a0d0e21e 1527 /* Supposed to be caught earlier. */
1528 break;
85afd4ae 1529 case '{':
77d41b28 1530 if (!regcurly(regcomp_parse)) {
1531 regcomp_parse++;
85afd4ae 1532 goto defchar;
1533 }
1534 /* FALL THROUGH */
a0d0e21e 1535 case '?':
1536 case '+':
1537 case '*':
3115e423 1538 FAIL("?+*{} follows nothing in regexp");
a0d0e21e 1539 break;
1540 case '\\':
77d41b28 1541 switch (*++regcomp_parse) {
a0d0e21e 1542 case 'A':
c277df42 1543 seen_zerolen++;
1544 ret = reg_node(SBOL);
a0d0e21e 1545 *flagp |= SIMPLE;
1546 nextchar();
1547 break;
1548 case 'G':
c277df42 1549 ret = reg_node(GPOS);
1550 regseen |= REG_SEEN_GPOS;
a0d0e21e 1551 *flagp |= SIMPLE;
1552 nextchar();
1553 break;
1554 case 'Z':
c277df42 1555 ret = reg_node(SEOL);
a0d0e21e 1556 *flagp |= SIMPLE;
1557 nextchar();
1558 break;
b85d18e9 1559 case 'z':
1560 ret = reg_node(EOS);
1561 *flagp |= SIMPLE;
1562 seen_zerolen++; /* Do not optimize RE away */
1563 nextchar();
1564 break;
a0d0e21e 1565 case 'w':
c277df42 1566 ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
a0d0e21e 1567 *flagp |= HASWIDTH|SIMPLE;
1568 nextchar();
1569 break;
1570 case 'W':
c277df42 1571 ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
a0d0e21e 1572 *flagp |= HASWIDTH|SIMPLE;
1573 nextchar();
1574 break;
1575 case 'b':
c277df42 1576 seen_zerolen++;
1577 ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
a0d0e21e 1578 *flagp |= SIMPLE;
1579 nextchar();
1580 break;
1581 case 'B':
c277df42 1582 seen_zerolen++;
1583 ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
a0d0e21e 1584 *flagp |= SIMPLE;
1585 nextchar();
1586 break;
1587 case 's':
c277df42 1588 ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE);
a0d0e21e 1589 *flagp |= HASWIDTH|SIMPLE;
1590 nextchar();
1591 break;
1592 case 'S':
c277df42 1593 ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
a0d0e21e 1594 *flagp |= HASWIDTH|SIMPLE;
1595 nextchar();
1596 break;
1597 case 'd':
c277df42 1598 ret = reg_node(DIGIT);
a0d0e21e 1599 *flagp |= HASWIDTH|SIMPLE;
1600 nextchar();
1601 break;
1602 case 'D':
c277df42 1603 ret = reg_node(NDIGIT);
a0d0e21e 1604 *flagp |= HASWIDTH|SIMPLE;
1605 nextchar();
1606 break;
1607 case 'n':
1608 case 'r':
1609 case 't':
1610 case 'f':
1611 case 'e':
1612 case 'a':
1613 case 'x':
1614 case 'c':
1615 case '0':
1616 goto defchar;
1617 case '1': case '2': case '3': case '4':
1618 case '5': case '6': case '7': case '8': case '9':
1619 {
77d41b28 1620 I32 num = atoi(regcomp_parse);
a0d0e21e 1621
1622 if (num > 9 && num >= regnpar)
1623 goto defchar;
1624 else {
ef64f398 1625 if (!SIZE_ONLY && num > regcomp_rx->nparens)
1626 FAIL("reference to nonexistent group");
a0d0e21e 1627 regsawback = 1;
c8756f30 1628 ret = reganode((regflags & PMf_FOLD)
1629 ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
1630 : REF, num);
a0d0e21e 1631 *flagp |= HASWIDTH;
77d41b28 1632 while (isDIGIT(*regcomp_parse))
1633 regcomp_parse++;
1634 regcomp_parse--;
a0d0e21e 1635 nextchar();
1636 }
1637 }
1638 break;
1639 case '\0':
77d41b28 1640 if (regcomp_parse >= regxend)
a0d0e21e 1641 FAIL("trailing \\ in regexp");
1642 /* FALL THROUGH */
1643 default:
1644 goto defchar;
1645 }
1646 break;
4633a7c4 1647
1648 case '#':
1649 if (regflags & PMf_EXTENDED) {
77d41b28 1650 while (regcomp_parse < regxend && *regcomp_parse != '\n') regcomp_parse++;
1651 if (regcomp_parse < regxend)
4633a7c4 1652 goto tryagain;
1653 }
1654 /* FALL THROUGH */
1655
a0d0e21e 1656 default: {
1657 register I32 len;
c277df42 1658 register U8 ender;
a0d0e21e 1659 register char *p;
c277df42 1660 char *oldp, *s;
a0d0e21e 1661 I32 numlen;
1662
77d41b28 1663 regcomp_parse++;
a0d0e21e 1664
1665 defchar:
c277df42 1666 ret = reg_node((regflags & PMf_FOLD)
bbce6d69 1667 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
1668 : EXACT);
161b471a 1669 s = (char *) OPERAND(ret);
c277df42 1670 regc(0, s++); /* save spot for len */
77d41b28 1671 for (len = 0, p = regcomp_parse - 1;
a0d0e21e 1672 len < 127 && p < regxend;
1673 len++)
1674 {
1675 oldp = p;
5b5a24f7 1676
1677 if (regflags & PMf_EXTENDED)
1678 p = regwhite(p, regxend);
a0d0e21e 1679 switch (*p) {
1680 case '^':
1681 case '$':
1682 case '.':
1683 case '[':
1684 case '(':
1685 case ')':
1686 case '|':
1687 goto loopdone;
1688 case '\\':
1689 switch (*++p) {
1690 case 'A':
1691 case 'G':
1692 case 'Z':
b85d18e9 1693 case 'z':
a0d0e21e 1694 case 'w':
1695 case 'W':
1696 case 'b':
1697 case 'B':
1698 case 's':
1699 case 'S':
1700 case 'd':
1701 case 'D':
1702 --p;
1703 goto loopdone;
1704 case 'n':
1705 ender = '\n';
1706 p++;
a687059c 1707 break;
a0d0e21e 1708 case 'r':
1709 ender = '\r';
1710 p++;
a687059c 1711 break;
a0d0e21e 1712 case 't':
1713 ender = '\t';
1714 p++;
a687059c 1715 break;
a0d0e21e 1716 case 'f':
1717 ender = '\f';
1718 p++;
a687059c 1719 break;
a0d0e21e 1720 case 'e':
1721 ender = '\033';
1722 p++;
a687059c 1723 break;
a0d0e21e 1724 case 'a':
1725 ender = '\007';
1726 p++;
a687059c 1727 break;
a0d0e21e 1728 case 'x':
1729 ender = scan_hex(++p, 2, &numlen);
1730 p += numlen;
a687059c 1731 break;
a0d0e21e 1732 case 'c':
1733 p++;
bbce6d69 1734 ender = UCHARAT(p++);
1735 ender = toCTRL(ender);
a687059c 1736 break;
a0d0e21e 1737 case '0': case '1': case '2': case '3':case '4':
1738 case '5': case '6': case '7': case '8':case '9':
1739 if (*p == '0' ||
1740 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1741 ender = scan_oct(p, 3, &numlen);
1742 p += numlen;
1743 }
1744 else {
1745 --p;
1746 goto loopdone;
a687059c 1747 }
1748 break;
a0d0e21e 1749 case '\0':
1750 if (p >= regxend)
a687059c 1751 FAIL("trailing \\ in regexp");
1752 /* FALL THROUGH */
a0d0e21e 1753 default:
1754 ender = *p++;
1755 break;
1756 }
1757 break;
a687059c 1758 default:
a0d0e21e 1759 ender = *p++;
1760 break;
a687059c 1761 }
5b5a24f7 1762 if (regflags & PMf_EXTENDED)
1763 p = regwhite(p, regxend);
a0d0e21e 1764 if (ISMULT2(p)) { /* Back off on ?+*. */
1765 if (len)
1766 p = oldp;
1767 else {
1768 len++;
c277df42 1769 regc(ender, s++);
a0d0e21e 1770 }
1771 break;
a687059c 1772 }
c277df42 1773 regc(ender, s++);
a0d0e21e 1774 }
1775 loopdone:
77d41b28 1776 regcomp_parse = p - 1;
a0d0e21e 1777 nextchar();
1778 if (len < 0)
1779 FAIL("internal disaster in regexp");
1780 if (len > 0)
1781 *flagp |= HASWIDTH;
1782 if (len == 1)
1783 *flagp |= SIMPLE;
c277df42 1784 if (!SIZE_ONLY)
a0d0e21e 1785 *OPERAND(ret) = len;
c277df42 1786 regc('\0', s++);
1787 if (SIZE_ONLY) {
c277df42 1788 regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
c277df42 1789 } else {
1790 regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1791 }
a687059c 1792 }
a0d0e21e 1793 break;
1794 }
a687059c 1795
a0d0e21e 1796 return(ret);
a687059c 1797}
1798
873ef191 1799STATIC char *
8ac85365 1800regwhite(char *p, char *e)
5b5a24f7 1801{
1802 while (p < e) {
1803 if (isSPACE(*p))
1804 ++p;
1805 else if (*p == '#') {
1806 do {
1807 p++;
1808 } while (p < e && *p != '\n');
1809 }
1810 else
1811 break;
1812 }
1813 return p;
1814}
1815
76e3520e 1816STATIC regnode *
8ac85365 1817regclass(void)
a687059c 1818{
c277df42 1819 register char *opnd, *s;
8ac85365 1820 register I32 Class;
a0d0e21e 1821 register I32 lastclass = 1234;
1822 register I32 range = 0;
c277df42 1823 register regnode *ret;
a0d0e21e 1824 register I32 def;
1825 I32 numlen;
1826
161b471a 1827 s = opnd = (char *) OPERAND(regcode);
c277df42 1828 ret = reg_node(ANYOF);
8ac85365 1829 for (Class = 0; Class < 33; Class++)
c277df42 1830 regc(0, s++);
77d41b28 1831 if (*regcomp_parse == '^') { /* Complement of range. */
a0d0e21e 1832 regnaughty++;
77d41b28 1833 regcomp_parse++;
c277df42 1834 if (!SIZE_ONLY)
bbce6d69 1835 *opnd |= ANYOF_INVERT;
1836 }
c277df42 1837 if (!SIZE_ONLY) {
1838 regcode += ANY_SKIP;
bbce6d69 1839 if (regflags & PMf_FOLD)
1840 *opnd |= ANYOF_FOLD;
1841 if (regflags & PMf_LOCALE)
1842 *opnd |= ANYOF_LOCALE;
c277df42 1843 } else {
1844 regsize += ANY_SKIP;
a0d0e21e 1845 }
77d41b28 1846 if (*regcomp_parse == ']' || *regcomp_parse == '-')
a0d0e21e 1847 goto skipcond; /* allow 1st char to be ] or - */
77d41b28 1848 while (regcomp_parse < regxend && *regcomp_parse != ']') {
a0d0e21e 1849 skipcond:
77d41b28 1850 Class = UCHARAT(regcomp_parse++);
1851 if (Class == '[' && regcomp_parse + 1 < regxend &&
4599a1de 1852 /* I smell either [: or [= or [. -- POSIX has been here, right? */
77d41b28 1853 (*regcomp_parse == ':' || *regcomp_parse == '=' || *regcomp_parse == '.')) {
1854 char posixccc = *regcomp_parse;
1855 char* posixccs = regcomp_parse++;
4599a1de 1856
77d41b28 1857 while (regcomp_parse < regxend && *regcomp_parse != posixccc)
1858 regcomp_parse++;
1859 if (regcomp_parse == regxend)
4599a1de 1860 /* Grandfather lone [:, [=, [. */
77d41b28 1861 regcomp_parse = posixccs;
4599a1de 1862 else {
77d41b28 1863 regcomp_parse++; /* skip over the posixccc */
1864 if (*regcomp_parse == ']') {
4599a1de 1865 /* Not Implemented Yet.
1866 * (POSIX Extended Character Classes, that is)
1867 * The text between e.g. [: and :] would start
77d41b28 1868 * at posixccs + 1 and stop at regcomp_parse - 2. */
4599a1de 1869 if (dowarn && !SIZE_ONLY)
1870 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
77d41b28 1871 regcomp_parse++; /* skip over the ending ] */
4599a1de 1872 }
1873 }
1874 }
8ac85365 1875 if (Class == '\\') {
77d41b28 1876 Class = UCHARAT(regcomp_parse++);
8ac85365 1877 switch (Class) {
a0d0e21e 1878 case 'w':
ae5c130c 1879 if (!SIZE_ONLY) {
1880 if (regflags & PMf_LOCALE)
bbce6d69 1881 *opnd |= ANYOF_ALNUML;
ae5c130c 1882 else {
1883 for (Class = 0; Class < 256; Class++)
1884 if (isALNUM(Class))
1885 ANYOF_SET(opnd, Class);
1886 }
bbce6d69 1887 }
a0d0e21e 1888 lastclass = 1234;
1889 continue;
1890 case 'W':
ae5c130c 1891 if (!SIZE_ONLY) {
1892 if (regflags & PMf_LOCALE)
bbce6d69 1893 *opnd |= ANYOF_NALNUML;
ae5c130c 1894 else {
1895 for (Class = 0; Class < 256; Class++)
1896 if (!isALNUM(Class))
1897 ANYOF_SET(opnd, Class);
1898 }
bbce6d69 1899 }
a0d0e21e 1900 lastclass = 1234;
1901 continue;
1902 case 's':
ae5c130c 1903 if (!SIZE_ONLY) {
1904 if (regflags & PMf_LOCALE)
bbce6d69 1905 *opnd |= ANYOF_SPACEL;
ae5c130c 1906 else {
1907 for (Class = 0; Class < 256; Class++)
1908 if (isSPACE(Class))
1909 ANYOF_SET(opnd, Class);
1910 }
bbce6d69 1911 }
a0d0e21e 1912 lastclass = 1234;
1913 continue;
1914 case 'S':
ae5c130c 1915 if (!SIZE_ONLY) {
1916 if (regflags & PMf_LOCALE)
bbce6d69 1917 *opnd |= ANYOF_NSPACEL;
ae5c130c 1918 else {
1919 for (Class = 0; Class < 256; Class++)
1920 if (!isSPACE(Class))
1921 ANYOF_SET(opnd, Class);
1922 }
bbce6d69 1923 }
a0d0e21e 1924 lastclass = 1234;
1925 continue;
1926 case 'd':
ae5c130c 1927 if (!SIZE_ONLY) {
1928 for (Class = '0'; Class <= '9'; Class++)
1929 ANYOF_SET(opnd, Class);
1930 }
a0d0e21e 1931 lastclass = 1234;
1932 continue;
1933 case 'D':
ae5c130c 1934 if (!SIZE_ONLY) {
1935 for (Class = 0; Class < '0'; Class++)
1936 ANYOF_SET(opnd, Class);
1937 for (Class = '9' + 1; Class < 256; Class++)
1938 ANYOF_SET(opnd, Class);
1939 }
a0d0e21e 1940 lastclass = 1234;
1941 continue;
1942 case 'n':
8ac85365 1943 Class = '\n';
a0d0e21e 1944 break;
1945 case 'r':
8ac85365 1946 Class = '\r';
a0d0e21e 1947 break;
1948 case 't':
8ac85365 1949 Class = '\t';
a0d0e21e 1950 break;
1951 case 'f':
8ac85365 1952 Class = '\f';
a0d0e21e 1953 break;
1954 case 'b':
8ac85365 1955 Class = '\b';
a0d0e21e 1956 break;
1957 case 'e':
8ac85365 1958 Class = '\033';
a0d0e21e 1959 break;
1960 case 'a':
8ac85365 1961 Class = '\007';
a0d0e21e 1962 break;
1963 case 'x':
77d41b28 1964 Class = scan_hex(regcomp_parse, 2, &numlen);
1965 regcomp_parse += numlen;
a0d0e21e 1966 break;
1967 case 'c':
77d41b28 1968 Class = UCHARAT(regcomp_parse++);
8ac85365 1969 Class = toCTRL(Class);
a0d0e21e 1970 break;
1971 case '0': case '1': case '2': case '3': case '4':
1972 case '5': case '6': case '7': case '8': case '9':
77d41b28 1973 Class = scan_oct(--regcomp_parse, 3, &numlen);
1974 regcomp_parse += numlen;
a0d0e21e 1975 break;
1976 }
1977 }
1978 if (range) {
8ac85365 1979 if (lastclass > Class)
a0d0e21e 1980 FAIL("invalid [] range in regexp");
1981 range = 0;
1982 }
1983 else {
8ac85365 1984 lastclass = Class;
77d41b28 1985 if (*regcomp_parse == '-' && regcomp_parse+1 < regxend &&
1986 regcomp_parse[1] != ']') {
1987 regcomp_parse++;
a0d0e21e 1988 range = 1;
1989 continue; /* do it next time */
1990 }
a687059c 1991 }
ae5c130c 1992 if (!SIZE_ONLY) {
1993 for ( ; lastclass <= Class; lastclass++)
1994 ANYOF_SET(opnd, lastclass);
1995 }
8ac85365 1996 lastclass = Class;
a0d0e21e 1997 }
77d41b28 1998 if (*regcomp_parse != ']')
a0d0e21e 1999 FAIL("unmatched [] in regexp");
2000 nextchar();
ae5c130c 2001 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
2002 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
2003 for (Class = 0; Class < 256; ++Class) {
2004 if (ANYOF_TEST(opnd, Class)) {
2005 I32 cf = fold[Class];
2006 ANYOF_SET(opnd, cf);
2007 }
2008 }
2009 *opnd &= ~ANYOF_FOLD;
2010 }
2011 /* optimize inverted simple patterns (e.g. [^a-z]) */
2012 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2013 for (Class = 0; Class < 32; ++Class)
2014 opnd[1 + Class] ^= 0xFF;
2015 *opnd = 0;
2016 }
a0d0e21e 2017 return ret;
2018}
2019
76e3520e 2020STATIC char*
8ac85365 2021nextchar(void)
a0d0e21e 2022{
77d41b28 2023 char* retval = regcomp_parse++;
a0d0e21e 2024
4633a7c4 2025 for (;;) {
77d41b28 2026 if (*regcomp_parse == '(' && regcomp_parse[1] == '?' &&
2027 regcomp_parse[2] == '#') {
2028 while (*regcomp_parse && *regcomp_parse != ')')
2029 regcomp_parse++;
2030 regcomp_parse++;
4633a7c4 2031 continue;
2032 }
2033 if (regflags & PMf_EXTENDED) {
77d41b28 2034 if (isSPACE(*regcomp_parse)) {
2035 regcomp_parse++;
748a9306 2036 continue;
2037 }
77d41b28 2038 else if (*regcomp_parse == '#') {
2039 while (*regcomp_parse && *regcomp_parse != '\n')
2040 regcomp_parse++;
2041 regcomp_parse++;
748a9306 2042 continue;
2043 }
748a9306 2044 }
4633a7c4 2045 return retval;
a0d0e21e 2046 }
a687059c 2047}
2048
2049/*
c277df42 2050- reg_node - emit a node
a0d0e21e 2051*/
76e3520e 2052STATIC regnode * /* Location. */
c277df42 2053reg_node(U8 op)
a687059c 2054{
c277df42 2055 register regnode *ret;
2056 register regnode *ptr;
a687059c 2057
a0d0e21e 2058 ret = regcode;
c277df42 2059 if (SIZE_ONLY) {
2060 SIZE_ALIGN(regsize);
c277df42 2061 regsize += 1;
a0d0e21e 2062 return(ret);
2063 }
a687059c 2064
c277df42 2065 NODE_ALIGN_FILL(ret);
a0d0e21e 2066 ptr = ret;
c277df42 2067 FILL_ADVANCE_NODE(ptr, op);
a0d0e21e 2068 regcode = ptr;
a687059c 2069
a0d0e21e 2070 return(ret);
a687059c 2071}
2072
2073/*
a0d0e21e 2074- reganode - emit a node with an argument
2075*/
76e3520e 2076STATIC regnode * /* Location. */
c277df42 2077reganode(U8 op, U32 arg)
fe14fcc3 2078{
c277df42 2079 register regnode *ret;
2080 register regnode *ptr;
fe14fcc3 2081
a0d0e21e 2082 ret = regcode;
c277df42 2083 if (SIZE_ONLY) {
2084 SIZE_ALIGN(regsize);
c277df42 2085 regsize += 2;
a0d0e21e 2086 return(ret);
2087 }
fe14fcc3 2088
c277df42 2089 NODE_ALIGN_FILL(ret);
a0d0e21e 2090 ptr = ret;
c277df42 2091 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
a0d0e21e 2092 regcode = ptr;
fe14fcc3 2093
a0d0e21e 2094 return(ret);
fe14fcc3 2095}
2096
2097/*
a0d0e21e 2098- regc - emit (if appropriate) a byte of code
2099*/
76e3520e 2100STATIC void
c277df42 2101regc(U8 b, char* s)
a687059c 2102{
c277df42 2103 if (!SIZE_ONLY)
2104 *s = b;
a687059c 2105}
2106
2107/*
a0d0e21e 2108- reginsert - insert an operator in front of already-emitted operand
2109*
2110* Means relocating the operand.
2111*/
76e3520e 2112STATIC void
c277df42 2113reginsert(U8 op, regnode *opnd)
a687059c 2114{
c277df42 2115 register regnode *src;
2116 register regnode *dst;
2117 register regnode *place;
2118 register int offset = regarglen[(U8)op];
2119
2120/* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2121
2122 if (SIZE_ONLY) {
2123 regsize += NODE_STEP_REGNODE + offset;
a0d0e21e 2124 return;
2125 }
a687059c 2126
a0d0e21e 2127 src = regcode;
c277df42 2128 regcode += NODE_STEP_REGNODE + offset;
a0d0e21e 2129 dst = regcode;
2130 while (src > opnd)
c277df42 2131 StructCopy(--src, --dst, regnode);
a0d0e21e 2132
2133 place = opnd; /* Op node, where operand used to be. */
c277df42 2134 src = NEXTOPER(place);
2135 FILL_ADVANCE_NODE(place, op);
2136 Zero(src, offset, regnode);
a687059c 2137}
2138
2139/*
c277df42 2140- regtail - set the next-pointer at the end of a node chain of p to val.
a0d0e21e 2141*/
76e3520e 2142STATIC void
c277df42 2143regtail(regnode *p, regnode *val)
a687059c 2144{
c277df42 2145 register regnode *scan;
2146 register regnode *temp;
a0d0e21e 2147 register I32 offset;
2148
c277df42 2149 if (SIZE_ONLY)
a0d0e21e 2150 return;
2151
2152 /* Find last node. */
2153 scan = p;
2154 for (;;) {
2155 temp = regnext(scan);
2156 if (temp == NULL)
2157 break;
2158 scan = temp;
2159 }
a687059c 2160
c277df42 2161 if (reg_off_by_arg[OP(scan)]) {
2162 ARG_SET(scan, val - scan);
2163 } else {
2164 NEXT_OFF(scan) = val - scan;
2165 }
a687059c 2166}
2167
2168/*
a0d0e21e 2169- regoptail - regtail on operand of first argument; nop if operandless
2170*/
76e3520e 2171STATIC void
c277df42 2172regoptail(regnode *p, regnode *val)
a687059c 2173{
a0d0e21e 2174 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
c277df42 2175 if (p == NULL || SIZE_ONLY)
2176 return;
2177 if (regkind[(U8)OP(p)] == BRANCH) {
2178 regtail(NEXTOPER(p), val);
2179 } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2180 regtail(NEXTOPER(NEXTOPER(p)), val);
2181 } else
a0d0e21e 2182 return;
a687059c 2183}
2184
2185/*
2186 - regcurly - a little FSA that accepts {\d+,?\d*}
2187 */
79072805 2188STATIC I32
8ac85365 2189regcurly(register char *s)
a687059c 2190{
2191 if (*s++ != '{')
2192 return FALSE;
f0fcb552 2193 if (!isDIGIT(*s))
a687059c 2194 return FALSE;
f0fcb552 2195 while (isDIGIT(*s))
a687059c 2196 s++;
2197 if (*s == ',')
2198 s++;
f0fcb552 2199 while (isDIGIT(*s))
a687059c 2200 s++;
2201 if (*s != '}')
2202 return FALSE;
2203 return TRUE;
2204}
2205
a687059c 2206
76e3520e 2207STATIC regnode *
c277df42 2208dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2209{
35ff7856 2210#ifdef DEBUGGING
c277df42 2211 register char op = EXACT; /* Arbitrary non-END op. */
2212 register regnode *next, *onode;
2213
2214 while (op != END && (!last || node < last)) {
2215 /* While that wasn't END last time... */
2216
2217 NODE_ALIGN(node);
2218 op = OP(node);
2219 if (op == CLOSE)
2220 l--;
2221 next = regnext(node);
2222 /* Where, what. */
2223 if (OP(node) == OPTIMIZED)
2224 goto after_print;
2225 regprop(sv, node);
2226 PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start,
2227 2*l + 1, "", SvPVX(sv));
2228 if (next == NULL) /* Next ptr. */
2229 PerlIO_printf(Perl_debug_log, "(0)");
2230 else
2231 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2232 (void)PerlIO_putc(Perl_debug_log, '\n');
2233 after_print:
2234 if (regkind[(U8)op] == BRANCHJ) {
2235 register regnode *nnode = (OP(next) == LONGJMP
2236 ? regnext(next)
2237 : next);
2238 if (last && nnode > last)
2239 nnode = last;
2240 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2241 } else if (regkind[(U8)op] == BRANCH) {
2242 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2243 } else if ( op == CURLY) { /* `next' might be very big: optimizer */
2244 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2245 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2246 } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2247 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2248 next, sv, l + 1);
2249 } else if ( op == PLUS || op == STAR) {
2250 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2251 } else if (op == ANYOF) {
2252 node = NEXTOPER(node);
2253 node += ANY_SKIP;
2254 } else if (regkind[(U8)op] == EXACT) {
2255 /* Literal string, where present. */
2256 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2257 node = NEXTOPER(node);
2258 } else {
2259 node = NEXTOPER(node);
2260 node += regarglen[(U8)op];
2261 }
2262 if (op == CURLYX || op == OPEN)
2263 l++;
2264 else if (op == WHILEM)
2265 l--;
2266 }
17c3b450 2267#endif /* DEBUGGING */
c277df42 2268 return node;
2269}
2270
a687059c 2271/*
fd181c75 2272 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
a687059c 2273 */
2274void
8ac85365 2275regdump(regexp *r)
a687059c 2276{
35ff7856 2277#ifdef DEBUGGING
46fc3d4c 2278 SV *sv = sv_newmortal();
a687059c 2279
c277df42 2280 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
a0d0e21e 2281
2282 /* Header fields of interest. */
c277df42 2283 if (r->anchored_substr)
2284 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2285 colors[0],
2286 SvPVX(r->anchored_substr),
2287 colors[1],
2288 SvTAIL(r->anchored_substr) ? "$" : "",
2289 r->anchored_offset);
2290 if (r->float_substr)
2291 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2292 colors[0],
2293 SvPVX(r->float_substr),
2294 colors[1],
2295 SvTAIL(r->float_substr) ? "$" : "",
2296 r->float_min_offset, r->float_max_offset);
2297 if (r->check_substr)
2298 PerlIO_printf(Perl_debug_log,
2299 r->check_substr == r->float_substr
2300 ? "(checking floating" : "(checking anchored");
2301 if (r->reganch & ROPT_NOSCAN)
2302 PerlIO_printf(Perl_debug_log, " noscan");
2303 if (r->reganch & ROPT_CHECK_ALL)
2304 PerlIO_printf(Perl_debug_log, " isall");
2305 if (r->check_substr)
2306 PerlIO_printf(Perl_debug_log, ") ");
2307
46fc3d4c 2308 if (r->regstclass) {
2309 regprop(sv, r->regstclass);
2310 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2311 }
774d564b 2312 if (r->reganch & ROPT_ANCH) {
2313 PerlIO_printf(Perl_debug_log, "anchored");
2314 if (r->reganch & ROPT_ANCH_BOL)
2315 PerlIO_printf(Perl_debug_log, "(BOL)");
c277df42 2316 if (r->reganch & ROPT_ANCH_MBOL)
2317 PerlIO_printf(Perl_debug_log, "(MBOL)");
774d564b 2318 if (r->reganch & ROPT_ANCH_GPOS)
2319 PerlIO_printf(Perl_debug_log, "(GPOS)");
2320 PerlIO_putc(Perl_debug_log, ' ');
2321 }
c277df42 2322 if (r->reganch & ROPT_GPOS_SEEN)
2323 PerlIO_printf(Perl_debug_log, "GPOS ");
a0d0e21e 2324 if (r->reganch & ROPT_SKIP)
760ac839 2325 PerlIO_printf(Perl_debug_log, "plus ");
a0d0e21e 2326 if (r->reganch & ROPT_IMPLICIT)
760ac839 2327 PerlIO_printf(Perl_debug_log, "implicit ");
760ac839 2328 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
ce862d02 2329 if (r->reganch & ROPT_EVAL_SEEN)
2330 PerlIO_printf(Perl_debug_log, "with eval ");
760ac839 2331 PerlIO_printf(Perl_debug_log, "\n");
17c3b450 2332#endif /* DEBUGGING */
a687059c 2333}
2334
2335/*
a0d0e21e 2336- regprop - printable representation of opcode
2337*/
46fc3d4c 2338void
c277df42 2339regprop(SV *sv, regnode *o)
a687059c 2340{
35ff7856 2341#ifdef DEBUGGING
a0d0e21e 2342 register char *p = 0;
2343
46fc3d4c 2344 sv_setpv(sv, ":");
11343788 2345 switch (OP(o)) {
a0d0e21e 2346 case BOL:
2347 p = "BOL";
2348 break;
2349 case MBOL:
2350 p = "MBOL";
2351 break;
2352 case SBOL:
2353 p = "SBOL";
2354 break;
2355 case EOL:
2356 p = "EOL";
2357 break;
b85d18e9 2358 case EOS:
2359 p = "EOS";
2360 break;
a0d0e21e 2361 case MEOL:
2362 p = "MEOL";
2363 break;
2364 case SEOL:
2365 p = "SEOL";
2366 break;
2367 case ANY:
2368 p = "ANY";
2369 break;
2370 case SANY:
2371 p = "SANY";
2372 break;
2373 case ANYOF:
2374 p = "ANYOF";
2375 break;
2376 case BRANCH:
2377 p = "BRANCH";
2378 break;
bbce6d69 2379 case EXACT:
c277df42 2380 sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
bbce6d69 2381 break;
2382 case EXACTF:
c277df42 2383 sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
bbce6d69 2384 break;
2385 case EXACTFL:
c277df42 2386 sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
a0d0e21e 2387 break;
2388 case NOTHING:
2389 p = "NOTHING";
2390 break;
c277df42 2391 case TAIL:
2392 p = "TAIL";
2393 break;
a0d0e21e 2394 case BACK:
2395 p = "BACK";
2396 break;
2397 case END:
2398 p = "END";
2399 break;
a0d0e21e 2400 case BOUND:
2401 p = "BOUND";
2402 break;
bbce6d69 2403 case BOUNDL:
2404 p = "BOUNDL";
2405 break;
a0d0e21e 2406 case NBOUND:
2407 p = "NBOUND";
2408 break;
bbce6d69 2409 case NBOUNDL:
2410 p = "NBOUNDL";
a0d0e21e 2411 break;
2412 case CURLY:
5dc0d613 2413 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2414 break;
c277df42 2415 case CURLYM:
c277df42 2416 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2417 break;
2418 case CURLYN:
c277df42 2419 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2420 break;
a0d0e21e 2421 case CURLYX:
5dc0d613 2422 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2423 break;
2424 case REF:
c277df42 2425 sv_catpvf(sv, "REF%d", ARG(o));
a0d0e21e 2426 break;
c8756f30 2427 case REFF:
c277df42 2428 sv_catpvf(sv, "REFF%d", ARG(o));
c8756f30 2429 break;
2430 case REFFL:
c277df42 2431 sv_catpvf(sv, "REFFL%d", ARG(o));
c8756f30 2432 break;
a0d0e21e 2433 case OPEN:
c277df42 2434 sv_catpvf(sv, "OPEN%d", ARG(o));
a0d0e21e 2435 break;
2436 case CLOSE:
c277df42 2437 sv_catpvf(sv, "CLOSE%d", ARG(o));
a0d0e21e 2438 p = NULL;
2439 break;
2440 case STAR:
2441 p = "STAR";
2442 break;
2443 case PLUS:
2444 p = "PLUS";
2445 break;
2446 case MINMOD:
2447 p = "MINMOD";
2448 break;
774d564b 2449 case GPOS:
2450 p = "GPOS";
a0d0e21e 2451 break;
2452 case UNLESSM:
c277df42 2453 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
a0d0e21e 2454 break;
2455 case IFMATCH:
c277df42 2456 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
a0d0e21e 2457 break;
2458 case SUCCEED:
2459 p = "SUCCEED";
2460 break;
2461 case WHILEM:
2462 p = "WHILEM";
2463 break;
bbce6d69 2464 case DIGIT:
2465 p = "DIGIT";
2466 break;
2467 case NDIGIT:
2468 p = "NDIGIT";
2469 break;
2470 case ALNUM:
2471 p = "ALNUM";
2472 break;
2473 case NALNUM:
2474 p = "NALNUM";
2475 break;
2476 case SPACE:
2477 p = "SPACE";
2478 break;
2479 case NSPACE:
2480 p = "NSPACE";
2481 break;
2482 case ALNUML:
2483 p = "ALNUML";
2484 break;
2485 case NALNUML:
2486 p = "NALNUML";
2487 break;
2488 case SPACEL:
2489 p = "SPACEL";
2490 break;
2491 case NSPACEL:
2492 p = "NSPACEL";
2493 break;
c277df42 2494 case EVAL:
2495 p = "EVAL";
2496 break;
2497 case LONGJMP:
2498 p = "LONGJMP";
2499 break;
2500 case BRANCHJ:
2501 p = "BRANCHJ";
2502 break;
2503 case IFTHEN:
2504 p = "IFTHEN";
2505 break;
2506 case GROUPP:
2507 sv_catpvf(sv, "GROUPP%d", ARG(o));
2508 break;
2509 case LOGICAL:
2510 p = "LOGICAL";
2511 break;
2512 case SUSPEND:
2513 p = "SUSPEND";
2514 break;
2515 case RENUM:
2516 p = "RENUM";
2517 break;
2518 case OPTIMIZED:
2519 p = "OPTIMIZED";
2520 break;
a0d0e21e 2521 default:
2522 FAIL("corrupted regexp opcode");
2523 }
46fc3d4c 2524 if (p)
2525 sv_catpv(sv, p);
17c3b450 2526#endif /* DEBUGGING */
35ff7856 2527}
a687059c 2528
2b69d0c2 2529void
8ac85365 2530pregfree(struct regexp *r)
a687059c 2531{
c277df42 2532 if (!r || (--r->refcnt > 0))
a0d0e21e 2533 return;
c277df42 2534 if (r->precomp)
a0d0e21e 2535 Safefree(r->precomp);
c277df42 2536 if (r->subbase)
a0d0e21e 2537 Safefree(r->subbase);
a193d654 2538 if (r->substrs) {
2539 if (r->anchored_substr)
2540 SvREFCNT_dec(r->anchored_substr);
2541 if (r->float_substr)
2542 SvREFCNT_dec(r->float_substr);
2779dcf1 2543 Safefree(r->substrs);
a193d654 2544 }
c277df42 2545 if (r->data) {
2546 int n = r->data->count;
2547 while (--n >= 0) {
2548 switch (r->data->what[n]) {
2549 case 's':
2550 SvREFCNT_dec((SV*)r->data->data[n]);
2551 break;
2552 case 'o':
2553 op_free((OP_4tree*)r->data->data[n]);
2554 break;
2555 case 'n':
2556 break;
2557 default:
2558 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2559 }
2560 }
2561 Safefree(r->data->what);
2562 Safefree(r->data);
a0d0e21e 2563 }
2564 Safefree(r->startp);
2565 Safefree(r->endp);
2566 Safefree(r);
a687059c 2567}
c277df42 2568
2569/*
2570 - regnext - dig the "next" pointer out of a node
2571 *
2572 * [Note, when REGALIGN is defined there are two places in regmatch()
2573 * that bypass this code for speed.]
2574 */
2575regnode *
2576regnext(register regnode *p)
2577{
2578 register I32 offset;
2579
2580 if (p == &regdummy)
2581 return(NULL);
2582
2583 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2584 if (offset == 0)
2585 return(NULL);
2586
c277df42 2587 return(p+offset);
c277df42 2588}
2589
01f988be 2590STATIC void
c277df42 2591re_croak2(const char* pat1,const char* pat2,...)
c277df42 2592{
2593 va_list args;
2594 STRLEN l1 = strlen(pat1);
2595 STRLEN l2 = strlen(pat2);
2596 char buf[512];
2597 char *message;
2598
2599 if (l1 > 510)
2600 l1 = 510;
2601 if (l1 + l2 > 510)
2602 l2 = 510 - l1;
2603 Copy(pat1, buf, l1 , char);
2604 Copy(pat2, buf + l1, l2 , char);
3b818b81 2605 buf[l1 + l2] = '\n';
2606 buf[l1 + l2 + 1] = '\0';
c277df42 2607 va_start(args, pat2);
c277df42 2608 message = mess(buf, &args);
2609 va_end(args);
2610 l1 = strlen(message);
2611 if (l1 > 512)
2612 l1 = 512;
2613 Copy(message, buf, l1 , char);
2614 buf[l1] = '\0'; /* Overwrite \n */
2615 croak("%s", buf);
2616}