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