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