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