newer emacs/cperl-mode.el (via private mail)
[p5sagit/p5-mst-13.2.git] / regcomp.c
CommitLineData
a0d0e21e 1/* regcomp.c
2 */
3
4/*
5 * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee
6 */
7
a687059c 8/* NOTE: this is derived from Henry Spencer's regexp code, and should not
9 * confused with the original package (see point 3 below). Thanks, Henry!
10 */
11
12/* Additional note: this code is very heavily munged from Henry's version
13 * in places. In some spots I've traded clarity for efficiency, so don't
14 * blame Henry for some of the lack of readability.
15 */
16
e50aee73 17/* The names of the functions have been changed from regcomp and
18 * regexec to pregcomp and pregexec in order to avoid conflicts
19 * with the POSIX routines of the same names.
20*/
21
f0fcb552 22/*SUPPRESS 112*/
a687059c 23/*
e50aee73 24 * pregcomp and pregexec -- regsub and regerror are not used in perl
a687059c 25 *
26 * Copyright (c) 1986 by University of Toronto.
27 * Written by Henry Spencer. Not derived from licensed software.
28 *
29 * Permission is granted to anyone to use this software for any
30 * purpose on any computer system, and to redistribute it freely,
31 * subject to the following restrictions:
32 *
33 * 1. The author is not responsible for the consequences of use of
34 * this software, no matter how awful, even if they arise
35 * from defects in it.
36 *
37 * 2. The origin of this software must not be misrepresented, either
38 * by explicit claim or by omission.
39 *
40 * 3. Altered versions must be plainly marked as such, and must not
41 * be misrepresented as being the original software.
42 *
43 *
44 **** Alterations to Henry's code are...
45 ****
9607fc9c 46 **** Copyright (c) 1991-1997, Larry Wall
a687059c 47 ****
9ef589d8 48 **** You may distribute under the terms of either the GNU General Public
49 **** License or the Artistic License, as specified in the README file.
50
a687059c 51 *
52 * Beware that some of this code is subtly aware of the way operator
53 * precedence is structured in regular expressions. Serious changes in
54 * regular-expression syntax might require a total rethink.
55 */
56#include "EXTERN.h"
57#include "perl.h"
58#include "INTERN.h"
c277df42 59
60#define REG_COMP_C
a687059c 61#include "regcomp.h"
62
d4cce5f1 63#ifdef op
11343788 64#undef op
d4cce5f1 65#endif /* op */
11343788 66
fe14fcc3 67#ifdef MSDOS
68# if defined(BUGGY_MSC6)
69 /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */
70 # pragma optimize("a",off)
71 /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/
72 # pragma optimize("w",on )
73# endif /* BUGGY_MSC6 */
74#endif /* MSDOS */
75
a687059c 76#ifndef STATIC
77#define STATIC static
78#endif
79
80#define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
81#define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
82 ((*s) == '{' && regcurly(s)))
2b69d0c2 83#ifdef atarist
84#define PERL_META "^$.[()|?+*\\"
85#else
a687059c 86#define META "^$.[()|?+*\\"
2b69d0c2 87#endif
a687059c 88
35c8bce7 89#ifdef SPSTART
90#undef SPSTART /* dratted cpp namespace... */
91#endif
a687059c 92/*
93 * Flags to be passed up and down.
94 */
a687059c 95#define WORST 0 /* Worst case. */
821b33a5 96#define HASWIDTH 0x1 /* Known to match non-null strings. */
a0d0e21e 97#define SIMPLE 0x2 /* Simple enough to be STAR/PLUS operand. */
98#define SPSTART 0x4 /* Starts with * or +. */
99#define TRYAGAIN 0x8 /* Weeded out a declaration. */
a687059c 100
101/*
e50aee73 102 * Forward declarations for pregcomp()'s friends.
a687059c 103 */
a0d0e21e 104
76e3520e 105#ifndef PERL_OBJECT
c277df42 106static regnode *reg _((I32, I32 *));
107static regnode *reganode _((U8, U32));
108static regnode *regatom _((I32 *));
109static regnode *regbranch _((I32 *, I32));
110static void regc _((U8, char *));
111static regnode *regclass _((void));
a0d0e21e 112STATIC I32 regcurly _((char *));
c277df42 113static regnode *reg_node _((U8));
114static regnode *regpiece _((I32 *));
115static void reginsert _((U8, regnode *));
116static void regoptail _((regnode *, regnode *));
c277df42 117static void regtail _((regnode *, regnode *));
873ef191 118static char* regwhite _((char *, char *));
a0d0e21e 119static char* nextchar _((void));
3bd495df 120static void re_croak2 _((const char* pat1,const char* pat2,...)) __attribute__((noreturn));
76e3520e 121#endif
a687059c 122
c277df42 123/* Length of a variant. */
124
76e3520e 125#ifndef PERL_OBJECT
c277df42 126typedef struct {
127 I32 len_min;
128 I32 len_delta;
129 I32 pos_min;
130 I32 pos_delta;
131 SV *last_found;
132 I32 last_end; /* min value, <0 unless valid. */
133 I32 last_start_min;
134 I32 last_start_max;
135 SV **longest; /* Either &l_fixed, or &l_float. */
136 SV *longest_fixed;
137 I32 offset_fixed;
138 SV *longest_float;
139 I32 offset_float_min;
140 I32 offset_float_max;
141 I32 flags;
142} scan_data_t;
76e3520e 143#endif
c277df42 144
145static scan_data_t zero_scan_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
146
147#define SF_BEFORE_EOL (SF_BEFORE_SEOL|SF_BEFORE_MEOL)
148#define SF_BEFORE_SEOL 0x1
149#define SF_BEFORE_MEOL 0x2
150#define SF_FIX_BEFORE_EOL (SF_FIX_BEFORE_SEOL|SF_FIX_BEFORE_MEOL)
151#define SF_FL_BEFORE_EOL (SF_FL_BEFORE_SEOL|SF_FL_BEFORE_MEOL)
152
09b7f37c 153#ifdef NO_UNARY_PLUS
154# define SF_FIX_SHIFT_EOL (0+2)
155# define SF_FL_SHIFT_EOL (0+4)
156#else
157# define SF_FIX_SHIFT_EOL (+2)
158# define SF_FL_SHIFT_EOL (+4)
159#endif
c277df42 160
161#define SF_FIX_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FIX_SHIFT_EOL)
162#define SF_FIX_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FIX_SHIFT_EOL)
163
164#define SF_FL_BEFORE_SEOL (SF_BEFORE_SEOL << SF_FL_SHIFT_EOL)
165#define SF_FL_BEFORE_MEOL (SF_BEFORE_MEOL << SF_FL_SHIFT_EOL) /* 0x20 */
166#define SF_IS_INF 0x40
167#define SF_HAS_PAR 0x80
168#define SF_IN_PAR 0x100
169#define SF_HAS_EVAL 0x200
4bfe0158 170#define SCF_DO_SUBSTR 0x400
c277df42 171
76e3520e 172STATIC void
c277df42 173scan_commit(scan_data_t *data)
174{
175 STRLEN l = SvCUR(data->last_found);
176 STRLEN old_l = SvCUR(*data->longest);
177
178 if ((l >= old_l) && ((l > old_l) || (data->flags & SF_BEFORE_EOL))) {
179 sv_setsv(*data->longest, data->last_found);
180 if (*data->longest == data->longest_fixed) {
181 data->offset_fixed = l ? data->last_start_min : data->pos_min;
182 if (data->flags & SF_BEFORE_EOL)
183 data->flags
184 |= ((data->flags & SF_BEFORE_EOL) << SF_FIX_SHIFT_EOL);
185 else
186 data->flags &= ~SF_FIX_BEFORE_EOL;
187 } else {
188 data->offset_float_min = l ? data->last_start_min : data->pos_min;
189 data->offset_float_max = (l
190 ? data->last_start_max
191 : data->pos_min + data->pos_delta);
192 if (data->flags & SF_BEFORE_EOL)
193 data->flags
194 |= ((data->flags & SF_BEFORE_EOL) << SF_FL_SHIFT_EOL);
195 else
196 data->flags &= ~SF_FL_BEFORE_EOL;
197 }
198 }
199 SvCUR_set(data->last_found, 0);
200 data->last_end = -1;
201 data->flags &= ~SF_BEFORE_EOL;
202}
203
c277df42 204/* Stops at toplevel WHILEM as well as at `last'. At end *scanp is set
205 to the position after last scanned or to NULL. */
206
76e3520e 207STATIC I32
c277df42 208study_chunk(regnode **scanp, I32 *deltap, regnode *last, scan_data_t *data, U32 flags)
209 /* scanp: Start here (read-write). */
210 /* deltap: Write maxlen-minlen here. */
211 /* last: Stop before this one. */
212{
213 I32 min = 0, pars = 0, code;
214 regnode *scan = *scanp, *next;
215 I32 delta = 0;
216 int is_inf = (flags & SCF_DO_SUBSTR) && (data->flags & SF_IS_INF);
217 I32 is_par = OP(scan) == OPEN ? ARG(scan) : 0;
218 scan_data_t data_fake;
219
220 while (scan && OP(scan) != END && scan < last) {
221 /* Peephole optimizer: */
222
223 if (regkind[(U8)OP(scan)] == EXACT) {
224 regnode *n = regnext(scan);
225 U32 stringok = 1;
226#ifdef DEBUGGING
227 regnode *stop = scan;
228#endif
229
230 next = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
231 /* Skip NOTHING, merge EXACT*. */
232 while (n &&
233 ( regkind[(U8)OP(n)] == NOTHING ||
234 (stringok && (OP(n) == OP(scan))))
235 && NEXT_OFF(n)
236 && NEXT_OFF(scan) + NEXT_OFF(n) < I16_MAX) {
237 if (OP(n) == TAIL || n > next)
238 stringok = 0;
239 if (regkind[(U8)OP(n)] == NOTHING) {
240 NEXT_OFF(scan) += NEXT_OFF(n);
241 next = n + NODE_STEP_REGNODE;
242#ifdef DEBUGGING
243 if (stringok)
244 stop = n;
245#endif
246 n = regnext(n);
247 } else {
248 int oldl = *OPERAND(scan);
249 regnode *nnext = regnext(n);
250
251 if (oldl + *OPERAND(n) > U8_MAX)
252 break;
253 NEXT_OFF(scan) += NEXT_OFF(n);
254 *OPERAND(scan) += *OPERAND(n);
255 next = n + (*OPERAND(n) + 2 - 1)/sizeof(regnode) + 2;
256 /* Now we can overwrite *n : */
257 Move(OPERAND(n) + 1, OPERAND(scan) + oldl + 1,
258 *OPERAND(n) + 1, char);
259#ifdef DEBUGGING
260 if (stringok)
261 stop = next - 1;
262#endif
263 n = nnext;
264 }
265 }
266#ifdef DEBUGGING
267 /* Allow dumping */
268 n = scan + (*OPERAND(scan) + 2 - 1)/sizeof(regnode) + 2;
269 while (n <= stop) {
ca04da08 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 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 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 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 428 && !(data->flags & (SF_HAS_PAR|SF_IN_PAR))
429 && maxcount <= 10000) /* Complement check for big count */
c277df42 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 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 468 nogo:
469
470 /* Try optimization CURLYX => CURLYM. */
471 if ( OP(oscan) == CURLYX && data
c277df42 472 && !(data->flags & SF_HAS_PAR)
c277df42 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 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 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 571 data->flags |= SF_HAS_EVAL;
572 optimize_curly_tail:
c277df42 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 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 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 622 next = regnext(scan);
623
624 if ( next && (OP(next) != WHILEM) && next < last)
c277df42 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 652add_data(I32 n, char *s)
653{
77d41b28 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 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 663 New(1208, regcomp_rx->data->what, n, U8);
664 regcomp_rx->data->count = n;
c277df42 665 }
77d41b28 666 Copy(s, regcomp_rx->data->what + regcomp_rx->data->count - n, n, U8);
667 return regcomp_rx->data->count - n;
c277df42 668}
669
a687059c 670/*
e50aee73 671 - pregcomp - compile a regular expression into internal code
a687059c 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 689 regnode *scan;
690 SV **longest;
691 SV *longest_fixed;
692 SV *longest_float;
693 regnode *first;
a0d0e21e 694 I32 flags;
a0d0e21e 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 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 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 716 regnpar = 1;
717 regsize = 0L;
718 regcode = &regdummy;
c277df42 719 regc((U8)MAGIC, (char*)regcode);
a0d0e21e 720 if (reg(0, &flags) == NULL) {
721 Safefree(regprecomp);
722 regprecomp = Nullch;
723 return(NULL);
724 }
c277df42 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 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 752 /* Small enough for pointer-storage convention?
753 If extralen==0, this means that we will not need long jumps. */
c277df42 754 if (regsize >= 0x10000L && extralen)
755 regsize += extralen;
756 else
757 extralen = 0;
a0d0e21e 758
bbce6d69 759 /* Allocate space and initialize. */
c277df42 760 Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode),
761 char, regexp);
a0d0e21e 762 if (r == NULL)
763 FAIL("regexp out of space");
c277df42 764 r->refcnt = 1;
bbce6d69 765 r->prelen = xend - exp;
a0d0e21e 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 775 regnpar = 1;
776 regcode = r->program;
c277df42 777 regc((U8)MAGIC, (char*) regcode++);
778 r->data = 0;
a0d0e21e 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 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 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 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 811 }
812
a0d0e21e 813 /* Starting-point info. */
814 again:
c277df42 815 if (OP(first) == EXACT); /* Empty, get anchored substr later. */
a0d0e21e 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 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 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 839 }
840 if (sawplus && (!sawopen || !regsawback))
841 r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */
842
c277df42 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 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 855 * earlier string may buy us something the later one won't.]
856 */
a0d0e21e 857 minlen = 0;
a687059c 858
c277df42 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 875 longest_float_length = SvCUR(data.longest_float);
876 if (longest_float_length
c277df42 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 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 900 longest_fixed_length = SvCUR(data.longest_fixed);
901 if (longest_fixed_length
c277df42 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 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 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 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 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 938 }
939
a0d0e21e 940 r->minlen = minlen;
c277df42 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 945 if (regseen & REG_SEEN_EVAL)
946 r->reganch |= ROPT_EVAL_SEEN;
a0d0e21e 947 Newz(1002, r->startp, regnpar, char*);
948 Newz(1002, r->endp, regnpar, char*);
949 DEBUG_r(regdump(r));
950 return(r);
a687059c 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 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 971 I32 flags, oregflags = regflags, have_branch = 0, open = 0;
972 char c;
a0d0e21e 973
821b33a5 974 *flagp = 0; /* Tentatively. */
a0d0e21e 975
976 /* Make an OPEN node, if parenthesized. */
977 if (paren) {
77d41b28 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 990 case '=':
991 case '!':
c277df42 992 seen_zerolen++;
993 case ':':
994 case '>':
a0d0e21e 995 break;
996 case '$':
997 case '@':
c277df42 998 FAIL2("Sequence (?%c...) not implemented", (int)paren);
a0d0e21e 999 break;
1000 case '#':
77d41b28 1001 while (*regcomp_parse && *regcomp_parse != ')')
1002 regcomp_parse++;
1003 if (*regcomp_parse != ')')
c277df42 1004 FAIL("Sequence (?#... not terminated");
a0d0e21e 1005 nextchar();
1006 *flagp = TRYAGAIN;
1007 return NULL;
c277df42 1008 case '{':
1009 {
1010 dTHR;
1011 I32 count = 1, n = 0;
1012 char c;
77d41b28 1013 char *s = regcomp_parse;
c277df42 1014 SV *sv;
1015 OP_4tree *sop, *rop;
1016
1017 seen_zerolen++;
ce862d02 1018 regseen |= REG_SEEN_EVAL;
77d41b28 1019 while (count && (c = *regcomp_parse)) {
1020 if (c == '\\' && regcomp_parse[1])
1021 regcomp_parse++;
c277df42 1022 else if (c == '{')
1023 count++;
1024 else if (c == '}')
1025 count--;
77d41b28 1026 regcomp_parse++;
c277df42 1027 }
77d41b28 1028 if (*regcomp_parse != ')')
c277df42 1029 FAIL("Sequence (?{...}) not terminated or not {}-balanced");
1030 if (!SIZE_ONLY) {
1031 AV *av;
1032
77d41b28 1033 if (regcomp_parse - 1 - s)
1034 sv = newSVpv(s, regcomp_parse - 1 - s);
c277df42 1035 else
1036 sv = newSVpv("", 0);
1037
1038 rop = sv_compile_2op(sv, &sop, "re", &av);
1039
1040 n = add_data(3, "nso");
77d41b28 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 */
1046 if (tainted)
1047 FAIL("Eval-group in insecure regular expression");
c277df42 1048 }
1049
1050 nextchar();
c277df42 1051 return reganode(EVAL, n);
1052 }
1053 case '(':
1054 {
77d41b28 1055 if (regcomp_parse[0] == '?') {
1056 if (regcomp_parse[1] == '=' || regcomp_parse[1] == '!'
1057 || regcomp_parse[1] == '<'
1058 || regcomp_parse[1] == '{') { /* Lookahead or eval. */
c277df42 1059 I32 flag;
1060
1061 ret = reg_node(LOGICAL);
1062 regtail(ret, reg(1, &flag));
1063 goto insert_if;
1064 }
77d41b28 1065 } else if (regcomp_parse[0] >= '1' && regcomp_parse[0] <= '9' ) {
1066 parno = atoi(regcomp_parse++);
c277df42 1067
77d41b28 1068 while (isDIGIT(*regcomp_parse))
1069 regcomp_parse++;
c277df42 1070 ret = reganode(GROUPP, parno);
1071 if ((c = *nextchar()) != ')')
1072 FAIL2("Switch (?(number%c not recognized", c);
1073 insert_if:
1074 regtail(ret, reganode(IFTHEN, 0));
1075 br = regbranch(&flags, 1);
1076 if (br == NULL)
1077 br = reganode(LONGJMP, 0);
1078 else
1079 regtail(br, reganode(LONGJMP, 0));
1080 c = *nextchar();
1081 if (c == '|') {
1082 lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */
1083 regbranch(&flags, 1);
1084 regtail(ret, lastbr);
1085 c = *nextchar();
1086 } else
1087 lastbr = NULL;
1088 if (c != ')')
1089 FAIL("Switch (?(condition)... contains too many branches");
1090 ender = reg_node(TAIL);
1091 regtail(br, ender);
1092 if (lastbr) {
1093 regtail(lastbr, ender);
1094 regtail(NEXTOPER(NEXTOPER(lastbr)), ender);
1095 } else
1096 regtail(ret, ender);
1097 return ret;
1098 } else {
77d41b28 1099 FAIL2("Unknown condition for (?(%.2s", regcomp_parse);
c277df42 1100 }
1101 }
1b1626e4 1102 case 0:
c277df42 1103 FAIL("Sequence (? incomplete");
1b1626e4 1104 break;
a0d0e21e 1105 default:
77d41b28 1106 --regcomp_parse;
1107 while (*regcomp_parse && strchr("iogcmsx", *regcomp_parse)) {
1108 if (*regcomp_parse != 'o')
1109 pmflag(&regflags, *regcomp_parse);
1110 ++regcomp_parse;
48c036b1 1111 }
c277df42 1112 unknown:
77d41b28 1113 if (*regcomp_parse != ')')
1114 FAIL2("Sequence (?%c...) not recognized", *regcomp_parse);
a0d0e21e 1115 nextchar();
1116 *flagp = TRYAGAIN;
1117 return NULL;
1118 }
1119 }
1120 else {
1121 parno = regnpar;
1122 regnpar++;
1123 ret = reganode(OPEN, parno);
c277df42 1124 open = 1;
a0d0e21e 1125 }
1126 } else
1127 ret = NULL;
1128
1129 /* Pick up the branches, linking them together. */
c277df42 1130 br = regbranch(&flags, 1);
a0d0e21e 1131 if (br == NULL)
1132 return(NULL);
77d41b28 1133 if (*regcomp_parse == '|') {
c277df42 1134 if (!SIZE_ONLY && extralen) {
1135 reginsert(BRANCHJ, br);
1136 } else
1137 reginsert(BRANCH, br);
1138 have_branch = 1;
1139 if (SIZE_ONLY)
1140 extralen += 1; /* For BRANCHJ-BRANCH. */
1141 } else if (paren == ':') {
1142 *flagp |= flags&SIMPLE;
1143 }
1144 if (open) { /* Starts with OPEN. */
1145 regtail(ret, br); /* OPEN -> first. */
1146 } else if (paren != '?') /* Not Conditional */
a0d0e21e 1147 ret = br;
821b33a5 1148 if (flags&HASWIDTH)
1149 *flagp |= HASWIDTH;
a0d0e21e 1150 *flagp |= flags&SPSTART;
c277df42 1151 lastbr = br;
77d41b28 1152 while (*regcomp_parse == '|') {
c277df42 1153 if (!SIZE_ONLY && extralen) {
1154 ender = reganode(LONGJMP,0);
1155 regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */
1156 }
1157 if (SIZE_ONLY)
1158 extralen += 2; /* Account for LONGJMP. */
a0d0e21e 1159 nextchar();
c277df42 1160 br = regbranch(&flags, 0);
a687059c 1161 if (br == NULL)
a0d0e21e 1162 return(NULL);
c277df42 1163 regtail(lastbr, br); /* BRANCH -> BRANCH. */
1164 lastbr = br;
821b33a5 1165 if (flags&HASWIDTH)
1166 *flagp |= HASWIDTH;
a687059c 1167 *flagp |= flags&SPSTART;
a0d0e21e 1168 }
1169
c277df42 1170 if (have_branch || paren != ':') {
1171 /* Make a closing node, and hook it on the end. */
1172 switch (paren) {
1173 case ':':
1174 ender = reg_node(TAIL);
1175 break;
1176 case 1:
1177 ender = reganode(CLOSE, parno);
1178 break;
1179 case '<':
c277df42 1180 case ',':
1181 case '=':
1182 case '!':
c277df42 1183 *flagp &= ~HASWIDTH;
821b33a5 1184 /* FALL THROUGH */
1185 case '>':
1186 ender = reg_node(SUCCEED);
c277df42 1187 break;
1188 case 0:
1189 ender = reg_node(END);
1190 break;
1191 }
1192 regtail(lastbr, ender);
a0d0e21e 1193
c277df42 1194 if (have_branch) {
1195 /* Hook the tails of the branches to the closing node. */
1196 for (br = ret; br != NULL; br = regnext(br)) {
1197 regoptail(br, ender);
1198 }
1199 }
a0d0e21e 1200 }
c277df42 1201
1202 {
1203 char *p;
1204 static char parens[] = "=!<,>";
1205
1206 if (paren && (p = strchr(parens, paren))) {
1207 int node = ((p - parens) % 2) ? UNLESSM : IFMATCH;
1208 int flag = (p - parens) > 1;
1209
1210 if (paren == '>')
1211 node = SUSPEND, flag = 0;
1212 reginsert(node,ret);
c277df42 1213 ret->flags = flag;
c277df42 1214 regtail(ret, reg_node(TAIL));
1215 }
a0d0e21e 1216 }
1217
1218 /* Check for proper termination. */
77d41b28 1219 if (paren && (regcomp_parse >= regxend || *nextchar() != ')')) {
a0d0e21e 1220 FAIL("unmatched () in regexp");
77d41b28 1221 } else if (!paren && regcomp_parse < regxend) {
1222 if (*regcomp_parse == ')') {
a0d0e21e 1223 FAIL("unmatched () in regexp");
1224 } else
1225 FAIL("junk on end of regexp"); /* "Can't happen". */
1226 /* NOTREACHED */
1227 }
c277df42 1228 if (paren != 0) {
1229 regflags = oregflags;
1230 }
a687059c 1231
a0d0e21e 1232 return(ret);
a687059c 1233}
1234
1235/*
1236 - regbranch - one alternative of an | operator
1237 *
1238 * Implements the concatenation operator.
1239 */
76e3520e 1240STATIC regnode *
c277df42 1241regbranch(I32 *flagp, I32 first)
a687059c 1242{
c277df42 1243 register regnode *ret;
1244 register regnode *chain = NULL;
1245 register regnode *latest;
1246 I32 flags = 0, c = 0;
a0d0e21e 1247
c277df42 1248 if (first)
1249 ret = NULL;
1250 else {
1251 if (!SIZE_ONLY && extralen)
1252 ret = reganode(BRANCHJ,0);
1253 else
1254 ret = reg_node(BRANCH);
1255 }
1256
1257 if (!first && SIZE_ONLY)
1258 extralen += 1; /* BRANCHJ */
1259
1260 *flagp = WORST; /* Tentatively. */
a0d0e21e 1261
77d41b28 1262 regcomp_parse--;
a0d0e21e 1263 nextchar();
77d41b28 1264 while (regcomp_parse < regxend && *regcomp_parse != '|' && *regcomp_parse != ')') {
a0d0e21e 1265 flags &= ~TRYAGAIN;
1266 latest = regpiece(&flags);
1267 if (latest == NULL) {
1268 if (flags & TRYAGAIN)
1269 continue;
1270 return(NULL);
c277df42 1271 } else if (ret == NULL)
1272 ret = latest;
a0d0e21e 1273 *flagp |= flags&HASWIDTH;
c277df42 1274 if (chain == NULL) /* First piece. */
a0d0e21e 1275 *flagp |= flags&SPSTART;
1276 else {
1277 regnaughty++;
1278 regtail(chain, latest);
a687059c 1279 }
a0d0e21e 1280 chain = latest;
c277df42 1281 c++;
1282 }
1283 if (chain == NULL) { /* Loop ran zero times. */
1284 chain = reg_node(NOTHING);
1285 if (ret == NULL)
1286 ret = chain;
1287 }
1288 if (c == 1) {
1289 *flagp |= flags&SIMPLE;
a0d0e21e 1290 }
a687059c 1291
a0d0e21e 1292 return(ret);
a687059c 1293}
1294
1295/*
1296 - regpiece - something followed by possible [*+?]
1297 *
1298 * Note that the branching code sequences used for ? and the general cases
1299 * of * and + are somewhat optimized: they use the same NOTHING node as
1300 * both the endmarker for their branch list and the body of the last branch.
1301 * It might seem that this node could be dispensed with entirely, but the
1302 * endmarker role is not redundant.
1303 */
76e3520e 1304STATIC regnode *
8ac85365 1305regpiece(I32 *flagp)
a687059c 1306{
c277df42 1307 register regnode *ret;
a0d0e21e 1308 register char op;
1309 register char *next;
1310 I32 flags;
77d41b28 1311 char *origparse = regcomp_parse;
a0d0e21e 1312 char *maxpos;
1313 I32 min;
c277df42 1314 I32 max = REG_INFTY;
a0d0e21e 1315
1316 ret = regatom(&flags);
1317 if (ret == NULL) {
1318 if (flags & TRYAGAIN)
1319 *flagp |= TRYAGAIN;
1320 return(NULL);
1321 }
1322
77d41b28 1323 op = *regcomp_parse;
a0d0e21e 1324
77d41b28 1325 if (op == '{' && regcurly(regcomp_parse)) {
1326 next = regcomp_parse + 1;
a0d0e21e 1327 maxpos = Nullch;
1328 while (isDIGIT(*next) || *next == ',') {
1329 if (*next == ',') {
1330 if (maxpos)
1331 break;
1332 else
1333 maxpos = next;
a687059c 1334 }
a0d0e21e 1335 next++;
1336 }
1337 if (*next == '}') { /* got one */
1338 if (!maxpos)
1339 maxpos = next;
77d41b28 1340 regcomp_parse++;
1341 min = atoi(regcomp_parse);
a0d0e21e 1342 if (*maxpos == ',')
1343 maxpos++;
1344 else
77d41b28 1345 maxpos = regcomp_parse;
a0d0e21e 1346 max = atoi(maxpos);
1347 if (!max && *maxpos != '0')
c277df42 1348 max = REG_INFTY; /* meaning "infinity" */
1349 else if (max >= REG_INFTY)
1350 FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1);
77d41b28 1351 regcomp_parse = next;
a0d0e21e 1352 nextchar();
1353
1354 do_curly:
1355 if ((flags&SIMPLE)) {
1356 regnaughty += 2 + regnaughty / 2;
1357 reginsert(CURLY, ret);
1358 }
1359 else {
1360 regnaughty += 4 + regnaughty; /* compound interest */
c277df42 1361 regtail(ret, reg_node(WHILEM));
1362 if (!SIZE_ONLY && extralen) {
1363 reginsert(LONGJMP,ret);
1364 reginsert(NOTHING,ret);
1365 NEXT_OFF(ret) = 3; /* Go over LONGJMP. */
1366 }
a0d0e21e 1367 reginsert(CURLYX,ret);
c277df42 1368 if (!SIZE_ONLY && extralen)
1369 NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */
1370 regtail(ret, reg_node(NOTHING));
1371 if (SIZE_ONLY)
1372 extralen += 3;
a0d0e21e 1373 }
c277df42 1374 ret->flags = 0;
a0d0e21e 1375
1376 if (min > 0)
821b33a5 1377 *flagp = WORST;
1378 if (max > 0)
1379 *flagp |= HASWIDTH;
a0d0e21e 1380 if (max && max < min)
c277df42 1381 FAIL("Can't do {n,m} with n > m");
1382 if (!SIZE_ONLY) {
1383 ARG1_SET(ret, min);
1384 ARG2_SET(ret, max);
a687059c 1385 }
a687059c 1386
a0d0e21e 1387 goto nest_check;
a687059c 1388 }
a0d0e21e 1389 }
a687059c 1390
a0d0e21e 1391 if (!ISMULT1(op)) {
1392 *flagp = flags;
a687059c 1393 return(ret);
a0d0e21e 1394 }
bb20fd44 1395
c277df42 1396#if 0 /* Now runtime fix should be reliable. */
bb20fd44 1397 if (!(flags&HASWIDTH) && op != '?')
c277df42 1398 FAIL("regexp *+ operand could be empty");
1399#endif
bb20fd44 1400
a0d0e21e 1401 nextchar();
1402
821b33a5 1403 *flagp = (op != '+') ? (WORST|SPSTART|HASWIDTH) : (WORST|HASWIDTH);
a0d0e21e 1404
1405 if (op == '*' && (flags&SIMPLE)) {
1406 reginsert(STAR, ret);
c277df42 1407 ret->flags = 0;
a0d0e21e 1408 regnaughty += 4;
1409 }
1410 else if (op == '*') {
1411 min = 0;
1412 goto do_curly;
1413 } else if (op == '+' && (flags&SIMPLE)) {
1414 reginsert(PLUS, ret);
c277df42 1415 ret->flags = 0;
a0d0e21e 1416 regnaughty += 3;
1417 }
1418 else if (op == '+') {
1419 min = 1;
1420 goto do_curly;
1421 } else if (op == '?') {
1422 min = 0; max = 1;
1423 goto do_curly;
1424 }
1425 nest_check:
c277df42 1426 if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) {
a0d0e21e 1427 warn("%.*s matches null string many times",
77d41b28 1428 regcomp_parse - origparse, origparse);
a0d0e21e 1429 }
1430
77d41b28 1431 if (*regcomp_parse == '?') {
a0d0e21e 1432 nextchar();
1433 reginsert(MINMOD, ret);
c277df42 1434 regtail(ret, ret + NODE_STEP_REGNODE);
a0d0e21e 1435 }
77d41b28 1436 if (ISMULT2(regcomp_parse))
a0d0e21e 1437 FAIL("nested *?+ in regexp");
1438
1439 return(ret);
a687059c 1440}
1441
1442/*
1443 - regatom - the lowest level
1444 *
1445 * Optimization: gobbles an entire sequence of ordinary characters so that
1446 * it can turn them into a single node, which is smaller to store and
1447 * faster to run. Backslashed characters are exceptions, each becoming a
1448 * separate node; the code is simpler that way and it's not worth fixing.
1449 *
1450 * [Yes, it is worth fixing, some scripts can run twice the speed.]
1451 */
76e3520e 1452STATIC regnode *
8ac85365 1453regatom(I32 *flagp)
a687059c 1454{
c277df42 1455 register regnode *ret = 0;
a0d0e21e 1456 I32 flags;
1457
1458 *flagp = WORST; /* Tentatively. */
1459
1460tryagain:
77d41b28 1461 switch (*regcomp_parse) {
a0d0e21e 1462 case '^':
c277df42 1463 seen_zerolen++;
a0d0e21e 1464 nextchar();
1465 if (regflags & PMf_MULTILINE)
c277df42 1466 ret = reg_node(MBOL);
a0d0e21e 1467 else if (regflags & PMf_SINGLELINE)
c277df42 1468 ret = reg_node(SBOL);
a0d0e21e 1469 else
c277df42 1470 ret = reg_node(BOL);
a0d0e21e 1471 break;
1472 case '$':
77d41b28 1473 if (regcomp_parse[1])
c277df42 1474 seen_zerolen++;
a0d0e21e 1475 nextchar();
1476 if (regflags & PMf_MULTILINE)
c277df42 1477 ret = reg_node(MEOL);
a0d0e21e 1478 else if (regflags & PMf_SINGLELINE)
c277df42 1479 ret = reg_node(SEOL);
a0d0e21e 1480 else
c277df42 1481 ret = reg_node(EOL);
a0d0e21e 1482 break;
1483 case '.':
1484 nextchar();
1485 if (regflags & PMf_SINGLELINE)
c277df42 1486 ret = reg_node(SANY);
a0d0e21e 1487 else
c277df42 1488 ret = reg_node(ANY);
a0d0e21e 1489 regnaughty++;
1490 *flagp |= HASWIDTH|SIMPLE;
1491 break;
1492 case '[':
77d41b28 1493 regcomp_parse++;
a0d0e21e 1494 ret = regclass();
1495 *flagp |= HASWIDTH|SIMPLE;
1496 break;
1497 case '(':
1498 nextchar();
1499 ret = reg(1, &flags);
1500 if (ret == NULL) {
1501 if (flags & TRYAGAIN)
1502 goto tryagain;
1503 return(NULL);
1504 }
c277df42 1505 *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE);
a0d0e21e 1506 break;
1507 case '|':
1508 case ')':
1509 if (flags & TRYAGAIN) {
1510 *flagp |= TRYAGAIN;
1511 return NULL;
1512 }
77d41b28 1513 FAIL2("internal urp in regexp at /%s/", regcomp_parse);
a0d0e21e 1514 /* Supposed to be caught earlier. */
1515 break;
85afd4ae 1516 case '{':
77d41b28 1517 if (!regcurly(regcomp_parse)) {
1518 regcomp_parse++;
85afd4ae 1519 goto defchar;
1520 }
1521 /* FALL THROUGH */
a0d0e21e 1522 case '?':
1523 case '+':
1524 case '*':
3115e423 1525 FAIL("?+*{} follows nothing in regexp");
a0d0e21e 1526 break;
1527 case '\\':
77d41b28 1528 switch (*++regcomp_parse) {
a0d0e21e 1529 case 'A':
c277df42 1530 seen_zerolen++;
1531 ret = reg_node(SBOL);
a0d0e21e 1532 *flagp |= SIMPLE;
1533 nextchar();
1534 break;
1535 case 'G':
c277df42 1536 ret = reg_node(GPOS);
1537 regseen |= REG_SEEN_GPOS;
a0d0e21e 1538 *flagp |= SIMPLE;
1539 nextchar();
1540 break;
1541 case 'Z':
c277df42 1542 ret = reg_node(SEOL);
a0d0e21e 1543 *flagp |= SIMPLE;
1544 nextchar();
1545 break;
b85d18e9 1546 case 'z':
1547 ret = reg_node(EOS);
1548 *flagp |= SIMPLE;
1549 seen_zerolen++; /* Do not optimize RE away */
1550 nextchar();
1551 break;
a0d0e21e 1552 case 'w':
c277df42 1553 ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM);
a0d0e21e 1554 *flagp |= HASWIDTH|SIMPLE;
1555 nextchar();
1556 break;
1557 case 'W':
c277df42 1558 ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM);
a0d0e21e 1559 *flagp |= HASWIDTH|SIMPLE;
1560 nextchar();
1561 break;
1562 case 'b':
c277df42 1563 seen_zerolen++;
1564 ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND);
a0d0e21e 1565 *flagp |= SIMPLE;
1566 nextchar();
1567 break;
1568 case 'B':
c277df42 1569 seen_zerolen++;
1570 ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND);
a0d0e21e 1571 *flagp |= SIMPLE;
1572 nextchar();
1573 break;
1574 case 's':
c277df42 1575 ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE);
a0d0e21e 1576 *flagp |= HASWIDTH|SIMPLE;
1577 nextchar();
1578 break;
1579 case 'S':
c277df42 1580 ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE);
a0d0e21e 1581 *flagp |= HASWIDTH|SIMPLE;
1582 nextchar();
1583 break;
1584 case 'd':
c277df42 1585 ret = reg_node(DIGIT);
a0d0e21e 1586 *flagp |= HASWIDTH|SIMPLE;
1587 nextchar();
1588 break;
1589 case 'D':
c277df42 1590 ret = reg_node(NDIGIT);
a0d0e21e 1591 *flagp |= HASWIDTH|SIMPLE;
1592 nextchar();
1593 break;
1594 case 'n':
1595 case 'r':
1596 case 't':
1597 case 'f':
1598 case 'e':
1599 case 'a':
1600 case 'x':
1601 case 'c':
1602 case '0':
1603 goto defchar;
1604 case '1': case '2': case '3': case '4':
1605 case '5': case '6': case '7': case '8': case '9':
1606 {
77d41b28 1607 I32 num = atoi(regcomp_parse);
a0d0e21e 1608
1609 if (num > 9 && num >= regnpar)
1610 goto defchar;
1611 else {
ef64f398 1612 if (!SIZE_ONLY && num > regcomp_rx->nparens)
1613 FAIL("reference to nonexistent group");
a0d0e21e 1614 regsawback = 1;
c8756f30 1615 ret = reganode((regflags & PMf_FOLD)
1616 ? ((regflags & PMf_LOCALE) ? REFFL : REFF)
1617 : REF, num);
a0d0e21e 1618 *flagp |= HASWIDTH;
77d41b28 1619 while (isDIGIT(*regcomp_parse))
1620 regcomp_parse++;
1621 regcomp_parse--;
a0d0e21e 1622 nextchar();
1623 }
1624 }
1625 break;
1626 case '\0':
77d41b28 1627 if (regcomp_parse >= regxend)
a0d0e21e 1628 FAIL("trailing \\ in regexp");
1629 /* FALL THROUGH */
1630 default:
1631 goto defchar;
1632 }
1633 break;
4633a7c4 1634
1635 case '#':
1636 if (regflags & PMf_EXTENDED) {
77d41b28 1637 while (regcomp_parse < regxend && *regcomp_parse != '\n') regcomp_parse++;
1638 if (regcomp_parse < regxend)
4633a7c4 1639 goto tryagain;
1640 }
1641 /* FALL THROUGH */
1642
a0d0e21e 1643 default: {
1644 register I32 len;
c277df42 1645 register U8 ender;
a0d0e21e 1646 register char *p;
c277df42 1647 char *oldp, *s;
a0d0e21e 1648 I32 numlen;
1649
77d41b28 1650 regcomp_parse++;
a0d0e21e 1651
1652 defchar:
c277df42 1653 ret = reg_node((regflags & PMf_FOLD)
bbce6d69 1654 ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF)
1655 : EXACT);
161b471a 1656 s = (char *) OPERAND(ret);
c277df42 1657 regc(0, s++); /* save spot for len */
77d41b28 1658 for (len = 0, p = regcomp_parse - 1;
a0d0e21e 1659 len < 127 && p < regxend;
1660 len++)
1661 {
1662 oldp = p;
5b5a24f7 1663
1664 if (regflags & PMf_EXTENDED)
1665 p = regwhite(p, regxend);
a0d0e21e 1666 switch (*p) {
1667 case '^':
1668 case '$':
1669 case '.':
1670 case '[':
1671 case '(':
1672 case ')':
1673 case '|':
1674 goto loopdone;
1675 case '\\':
1676 switch (*++p) {
1677 case 'A':
1678 case 'G':
1679 case 'Z':
b85d18e9 1680 case 'z':
a0d0e21e 1681 case 'w':
1682 case 'W':
1683 case 'b':
1684 case 'B':
1685 case 's':
1686 case 'S':
1687 case 'd':
1688 case 'D':
1689 --p;
1690 goto loopdone;
1691 case 'n':
1692 ender = '\n';
1693 p++;
a687059c 1694 break;
a0d0e21e 1695 case 'r':
1696 ender = '\r';
1697 p++;
a687059c 1698 break;
a0d0e21e 1699 case 't':
1700 ender = '\t';
1701 p++;
a687059c 1702 break;
a0d0e21e 1703 case 'f':
1704 ender = '\f';
1705 p++;
a687059c 1706 break;
a0d0e21e 1707 case 'e':
1708 ender = '\033';
1709 p++;
a687059c 1710 break;
a0d0e21e 1711 case 'a':
1712 ender = '\007';
1713 p++;
a687059c 1714 break;
a0d0e21e 1715 case 'x':
1716 ender = scan_hex(++p, 2, &numlen);
1717 p += numlen;
a687059c 1718 break;
a0d0e21e 1719 case 'c':
1720 p++;
bbce6d69 1721 ender = UCHARAT(p++);
1722 ender = toCTRL(ender);
a687059c 1723 break;
a0d0e21e 1724 case '0': case '1': case '2': case '3':case '4':
1725 case '5': case '6': case '7': case '8':case '9':
1726 if (*p == '0' ||
1727 (isDIGIT(p[1]) && atoi(p) >= regnpar) ) {
1728 ender = scan_oct(p, 3, &numlen);
1729 p += numlen;
1730 }
1731 else {
1732 --p;
1733 goto loopdone;
a687059c 1734 }
1735 break;
a0d0e21e 1736 case '\0':
1737 if (p >= regxend)
a687059c 1738 FAIL("trailing \\ in regexp");
1739 /* FALL THROUGH */
a0d0e21e 1740 default:
1741 ender = *p++;
1742 break;
1743 }
1744 break;
a687059c 1745 default:
a0d0e21e 1746 ender = *p++;
1747 break;
a687059c 1748 }
5b5a24f7 1749 if (regflags & PMf_EXTENDED)
1750 p = regwhite(p, regxend);
a0d0e21e 1751 if (ISMULT2(p)) { /* Back off on ?+*. */
1752 if (len)
1753 p = oldp;
1754 else {
1755 len++;
c277df42 1756 regc(ender, s++);
a0d0e21e 1757 }
1758 break;
a687059c 1759 }
c277df42 1760 regc(ender, s++);
a0d0e21e 1761 }
1762 loopdone:
77d41b28 1763 regcomp_parse = p - 1;
a0d0e21e 1764 nextchar();
1765 if (len < 0)
1766 FAIL("internal disaster in regexp");
1767 if (len > 0)
1768 *flagp |= HASWIDTH;
1769 if (len == 1)
1770 *flagp |= SIMPLE;
c277df42 1771 if (!SIZE_ONLY)
a0d0e21e 1772 *OPERAND(ret) = len;
c277df42 1773 regc('\0', s++);
1774 if (SIZE_ONLY) {
c277df42 1775 regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
c277df42 1776 } else {
1777 regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode);
1778 }
a687059c 1779 }
a0d0e21e 1780 break;
1781 }
a687059c 1782
a0d0e21e 1783 return(ret);
a687059c 1784}
1785
873ef191 1786STATIC char *
8ac85365 1787regwhite(char *p, char *e)
5b5a24f7 1788{
1789 while (p < e) {
1790 if (isSPACE(*p))
1791 ++p;
1792 else if (*p == '#') {
1793 do {
1794 p++;
1795 } while (p < e && *p != '\n');
1796 }
1797 else
1798 break;
1799 }
1800 return p;
1801}
1802
76e3520e 1803STATIC regnode *
8ac85365 1804regclass(void)
a687059c 1805{
c277df42 1806 register char *opnd, *s;
8ac85365 1807 register I32 Class;
a0d0e21e 1808 register I32 lastclass = 1234;
1809 register I32 range = 0;
c277df42 1810 register regnode *ret;
a0d0e21e 1811 register I32 def;
1812 I32 numlen;
1813
161b471a 1814 s = opnd = (char *) OPERAND(regcode);
c277df42 1815 ret = reg_node(ANYOF);
8ac85365 1816 for (Class = 0; Class < 33; Class++)
c277df42 1817 regc(0, s++);
77d41b28 1818 if (*regcomp_parse == '^') { /* Complement of range. */
a0d0e21e 1819 regnaughty++;
77d41b28 1820 regcomp_parse++;
c277df42 1821 if (!SIZE_ONLY)
bbce6d69 1822 *opnd |= ANYOF_INVERT;
1823 }
c277df42 1824 if (!SIZE_ONLY) {
1825 regcode += ANY_SKIP;
bbce6d69 1826 if (regflags & PMf_FOLD)
1827 *opnd |= ANYOF_FOLD;
1828 if (regflags & PMf_LOCALE)
1829 *opnd |= ANYOF_LOCALE;
c277df42 1830 } else {
1831 regsize += ANY_SKIP;
a0d0e21e 1832 }
77d41b28 1833 if (*regcomp_parse == ']' || *regcomp_parse == '-')
a0d0e21e 1834 goto skipcond; /* allow 1st char to be ] or - */
77d41b28 1835 while (regcomp_parse < regxend && *regcomp_parse != ']') {
a0d0e21e 1836 skipcond:
77d41b28 1837 Class = UCHARAT(regcomp_parse++);
1838 if (Class == '[' && regcomp_parse + 1 < regxend &&
4599a1de 1839 /* I smell either [: or [= or [. -- POSIX has been here, right? */
77d41b28 1840 (*regcomp_parse == ':' || *regcomp_parse == '=' || *regcomp_parse == '.')) {
1841 char posixccc = *regcomp_parse;
1842 char* posixccs = regcomp_parse++;
4599a1de 1843
77d41b28 1844 while (regcomp_parse < regxend && *regcomp_parse != posixccc)
1845 regcomp_parse++;
1846 if (regcomp_parse == regxend)
4599a1de 1847 /* Grandfather lone [:, [=, [. */
77d41b28 1848 regcomp_parse = posixccs;
4599a1de 1849 else {
77d41b28 1850 regcomp_parse++; /* skip over the posixccc */
1851 if (*regcomp_parse == ']') {
4599a1de 1852 /* Not Implemented Yet.
1853 * (POSIX Extended Character Classes, that is)
1854 * The text between e.g. [: and :] would start
77d41b28 1855 * at posixccs + 1 and stop at regcomp_parse - 2. */
4599a1de 1856 if (dowarn && !SIZE_ONLY)
1857 warn("Character class syntax [%c %c] is reserved for future extensions", posixccc, posixccc);
77d41b28 1858 regcomp_parse++; /* skip over the ending ] */
4599a1de 1859 }
1860 }
1861 }
8ac85365 1862 if (Class == '\\') {
77d41b28 1863 Class = UCHARAT(regcomp_parse++);
8ac85365 1864 switch (Class) {
a0d0e21e 1865 case 'w':
ae5c130c 1866 if (!SIZE_ONLY) {
1867 if (regflags & PMf_LOCALE)
bbce6d69 1868 *opnd |= ANYOF_ALNUML;
ae5c130c 1869 else {
1870 for (Class = 0; Class < 256; Class++)
1871 if (isALNUM(Class))
1872 ANYOF_SET(opnd, Class);
1873 }
bbce6d69 1874 }
a0d0e21e 1875 lastclass = 1234;
1876 continue;
1877 case 'W':
ae5c130c 1878 if (!SIZE_ONLY) {
1879 if (regflags & PMf_LOCALE)
bbce6d69 1880 *opnd |= ANYOF_NALNUML;
ae5c130c 1881 else {
1882 for (Class = 0; Class < 256; Class++)
1883 if (!isALNUM(Class))
1884 ANYOF_SET(opnd, Class);
1885 }
bbce6d69 1886 }
a0d0e21e 1887 lastclass = 1234;
1888 continue;
1889 case 's':
ae5c130c 1890 if (!SIZE_ONLY) {
1891 if (regflags & PMf_LOCALE)
bbce6d69 1892 *opnd |= ANYOF_SPACEL;
ae5c130c 1893 else {
1894 for (Class = 0; Class < 256; Class++)
1895 if (isSPACE(Class))
1896 ANYOF_SET(opnd, Class);
1897 }
bbce6d69 1898 }
a0d0e21e 1899 lastclass = 1234;
1900 continue;
1901 case 'S':
ae5c130c 1902 if (!SIZE_ONLY) {
1903 if (regflags & PMf_LOCALE)
bbce6d69 1904 *opnd |= ANYOF_NSPACEL;
ae5c130c 1905 else {
1906 for (Class = 0; Class < 256; Class++)
1907 if (!isSPACE(Class))
1908 ANYOF_SET(opnd, Class);
1909 }
bbce6d69 1910 }
a0d0e21e 1911 lastclass = 1234;
1912 continue;
1913 case 'd':
ae5c130c 1914 if (!SIZE_ONLY) {
1915 for (Class = '0'; Class <= '9'; Class++)
1916 ANYOF_SET(opnd, Class);
1917 }
a0d0e21e 1918 lastclass = 1234;
1919 continue;
1920 case 'D':
ae5c130c 1921 if (!SIZE_ONLY) {
1922 for (Class = 0; Class < '0'; Class++)
1923 ANYOF_SET(opnd, Class);
1924 for (Class = '9' + 1; Class < 256; Class++)
1925 ANYOF_SET(opnd, Class);
1926 }
a0d0e21e 1927 lastclass = 1234;
1928 continue;
1929 case 'n':
8ac85365 1930 Class = '\n';
a0d0e21e 1931 break;
1932 case 'r':
8ac85365 1933 Class = '\r';
a0d0e21e 1934 break;
1935 case 't':
8ac85365 1936 Class = '\t';
a0d0e21e 1937 break;
1938 case 'f':
8ac85365 1939 Class = '\f';
a0d0e21e 1940 break;
1941 case 'b':
8ac85365 1942 Class = '\b';
a0d0e21e 1943 break;
1944 case 'e':
8ac85365 1945 Class = '\033';
a0d0e21e 1946 break;
1947 case 'a':
8ac85365 1948 Class = '\007';
a0d0e21e 1949 break;
1950 case 'x':
77d41b28 1951 Class = scan_hex(regcomp_parse, 2, &numlen);
1952 regcomp_parse += numlen;
a0d0e21e 1953 break;
1954 case 'c':
77d41b28 1955 Class = UCHARAT(regcomp_parse++);
8ac85365 1956 Class = toCTRL(Class);
a0d0e21e 1957 break;
1958 case '0': case '1': case '2': case '3': case '4':
1959 case '5': case '6': case '7': case '8': case '9':
77d41b28 1960 Class = scan_oct(--regcomp_parse, 3, &numlen);
1961 regcomp_parse += numlen;
a0d0e21e 1962 break;
1963 }
1964 }
1965 if (range) {
8ac85365 1966 if (lastclass > Class)
a0d0e21e 1967 FAIL("invalid [] range in regexp");
1968 range = 0;
1969 }
1970 else {
8ac85365 1971 lastclass = Class;
77d41b28 1972 if (*regcomp_parse == '-' && regcomp_parse+1 < regxend &&
1973 regcomp_parse[1] != ']') {
1974 regcomp_parse++;
a0d0e21e 1975 range = 1;
1976 continue; /* do it next time */
1977 }
a687059c 1978 }
ae5c130c 1979 if (!SIZE_ONLY) {
1980 for ( ; lastclass <= Class; lastclass++)
1981 ANYOF_SET(opnd, lastclass);
1982 }
8ac85365 1983 lastclass = Class;
a0d0e21e 1984 }
77d41b28 1985 if (*regcomp_parse != ']')
a0d0e21e 1986 FAIL("unmatched [] in regexp");
1987 nextchar();
ae5c130c 1988 /* optimize case-insensitive simple patterns (e.g. /[a-z]/i) */
1989 if (!SIZE_ONLY && (*opnd & (0xFF ^ ANYOF_INVERT)) == ANYOF_FOLD) {
1990 for (Class = 0; Class < 256; ++Class) {
1991 if (ANYOF_TEST(opnd, Class)) {
1992 I32 cf = fold[Class];
1993 ANYOF_SET(opnd, cf);
1994 }
1995 }
1996 *opnd &= ~ANYOF_FOLD;
1997 }
1998 /* optimize inverted simple patterns (e.g. [^a-z]) */
1999 if (!SIZE_ONLY && (*opnd & 0xFF) == ANYOF_INVERT) {
2000 for (Class = 0; Class < 32; ++Class)
2001 opnd[1 + Class] ^= 0xFF;
2002 *opnd = 0;
2003 }
a0d0e21e 2004 return ret;
2005}
2006
76e3520e 2007STATIC char*
8ac85365 2008nextchar(void)
a0d0e21e 2009{
77d41b28 2010 char* retval = regcomp_parse++;
a0d0e21e 2011
4633a7c4 2012 for (;;) {
77d41b28 2013 if (*regcomp_parse == '(' && regcomp_parse[1] == '?' &&
2014 regcomp_parse[2] == '#') {
2015 while (*regcomp_parse && *regcomp_parse != ')')
2016 regcomp_parse++;
2017 regcomp_parse++;
4633a7c4 2018 continue;
2019 }
2020 if (regflags & PMf_EXTENDED) {
77d41b28 2021 if (isSPACE(*regcomp_parse)) {
2022 regcomp_parse++;
748a9306 2023 continue;
2024 }
77d41b28 2025 else if (*regcomp_parse == '#') {
2026 while (*regcomp_parse && *regcomp_parse != '\n')
2027 regcomp_parse++;
2028 regcomp_parse++;
748a9306 2029 continue;
2030 }
748a9306 2031 }
4633a7c4 2032 return retval;
a0d0e21e 2033 }
a687059c 2034}
2035
2036/*
c277df42 2037- reg_node - emit a node
a0d0e21e 2038*/
76e3520e 2039STATIC regnode * /* Location. */
c277df42 2040reg_node(U8 op)
a687059c 2041{
c277df42 2042 register regnode *ret;
2043 register regnode *ptr;
a687059c 2044
a0d0e21e 2045 ret = regcode;
c277df42 2046 if (SIZE_ONLY) {
2047 SIZE_ALIGN(regsize);
c277df42 2048 regsize += 1;
a0d0e21e 2049 return(ret);
2050 }
a687059c 2051
c277df42 2052 NODE_ALIGN_FILL(ret);
a0d0e21e 2053 ptr = ret;
c277df42 2054 FILL_ADVANCE_NODE(ptr, op);
a0d0e21e 2055 regcode = ptr;
a687059c 2056
a0d0e21e 2057 return(ret);
a687059c 2058}
2059
2060/*
a0d0e21e 2061- reganode - emit a node with an argument
2062*/
76e3520e 2063STATIC regnode * /* Location. */
c277df42 2064reganode(U8 op, U32 arg)
fe14fcc3 2065{
c277df42 2066 register regnode *ret;
2067 register regnode *ptr;
fe14fcc3 2068
a0d0e21e 2069 ret = regcode;
c277df42 2070 if (SIZE_ONLY) {
2071 SIZE_ALIGN(regsize);
c277df42 2072 regsize += 2;
a0d0e21e 2073 return(ret);
2074 }
fe14fcc3 2075
c277df42 2076 NODE_ALIGN_FILL(ret);
a0d0e21e 2077 ptr = ret;
c277df42 2078 FILL_ADVANCE_NODE_ARG(ptr, op, arg);
a0d0e21e 2079 regcode = ptr;
fe14fcc3 2080
a0d0e21e 2081 return(ret);
fe14fcc3 2082}
2083
2084/*
a0d0e21e 2085- regc - emit (if appropriate) a byte of code
2086*/
76e3520e 2087STATIC void
c277df42 2088regc(U8 b, char* s)
a687059c 2089{
c277df42 2090 if (!SIZE_ONLY)
2091 *s = b;
a687059c 2092}
2093
2094/*
a0d0e21e 2095- reginsert - insert an operator in front of already-emitted operand
2096*
2097* Means relocating the operand.
2098*/
76e3520e 2099STATIC void
c277df42 2100reginsert(U8 op, regnode *opnd)
a687059c 2101{
c277df42 2102 register regnode *src;
2103 register regnode *dst;
2104 register regnode *place;
2105 register int offset = regarglen[(U8)op];
2106
2107/* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */
2108
2109 if (SIZE_ONLY) {
2110 regsize += NODE_STEP_REGNODE + offset;
a0d0e21e 2111 return;
2112 }
a687059c 2113
a0d0e21e 2114 src = regcode;
c277df42 2115 regcode += NODE_STEP_REGNODE + offset;
a0d0e21e 2116 dst = regcode;
2117 while (src > opnd)
c277df42 2118 StructCopy(--src, --dst, regnode);
a0d0e21e 2119
2120 place = opnd; /* Op node, where operand used to be. */
c277df42 2121 src = NEXTOPER(place);
2122 FILL_ADVANCE_NODE(place, op);
2123 Zero(src, offset, regnode);
a687059c 2124}
2125
2126/*
c277df42 2127- regtail - set the next-pointer at the end of a node chain of p to val.
a0d0e21e 2128*/
76e3520e 2129STATIC void
c277df42 2130regtail(regnode *p, regnode *val)
a687059c 2131{
c277df42 2132 register regnode *scan;
2133 register regnode *temp;
a0d0e21e 2134 register I32 offset;
2135
c277df42 2136 if (SIZE_ONLY)
a0d0e21e 2137 return;
2138
2139 /* Find last node. */
2140 scan = p;
2141 for (;;) {
2142 temp = regnext(scan);
2143 if (temp == NULL)
2144 break;
2145 scan = temp;
2146 }
a687059c 2147
c277df42 2148 if (reg_off_by_arg[OP(scan)]) {
2149 ARG_SET(scan, val - scan);
2150 } else {
2151 NEXT_OFF(scan) = val - scan;
2152 }
a687059c 2153}
2154
2155/*
a0d0e21e 2156- regoptail - regtail on operand of first argument; nop if operandless
2157*/
76e3520e 2158STATIC void
c277df42 2159regoptail(regnode *p, regnode *val)
a687059c 2160{
a0d0e21e 2161 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
c277df42 2162 if (p == NULL || SIZE_ONLY)
2163 return;
2164 if (regkind[(U8)OP(p)] == BRANCH) {
2165 regtail(NEXTOPER(p), val);
2166 } else if ( regkind[(U8)OP(p)] == BRANCHJ) {
2167 regtail(NEXTOPER(NEXTOPER(p)), val);
2168 } else
a0d0e21e 2169 return;
a687059c 2170}
2171
2172/*
2173 - regcurly - a little FSA that accepts {\d+,?\d*}
2174 */
79072805 2175STATIC I32
8ac85365 2176regcurly(register char *s)
a687059c 2177{
2178 if (*s++ != '{')
2179 return FALSE;
f0fcb552 2180 if (!isDIGIT(*s))
a687059c 2181 return FALSE;
f0fcb552 2182 while (isDIGIT(*s))
a687059c 2183 s++;
2184 if (*s == ',')
2185 s++;
f0fcb552 2186 while (isDIGIT(*s))
a687059c 2187 s++;
2188 if (*s != '}')
2189 return FALSE;
2190 return TRUE;
2191}
2192
a687059c 2193
76e3520e 2194STATIC regnode *
c277df42 2195dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l)
2196{
35ff7856 2197#ifdef DEBUGGING
c277df42 2198 register char op = EXACT; /* Arbitrary non-END op. */
2199 register regnode *next, *onode;
2200
2201 while (op != END && (!last || node < last)) {
2202 /* While that wasn't END last time... */
2203
2204 NODE_ALIGN(node);
2205 op = OP(node);
2206 if (op == CLOSE)
2207 l--;
2208 next = regnext(node);
2209 /* Where, what. */
2210 if (OP(node) == OPTIMIZED)
2211 goto after_print;
2212 regprop(sv, node);
2213 PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start,
2214 2*l + 1, "", SvPVX(sv));
2215 if (next == NULL) /* Next ptr. */
2216 PerlIO_printf(Perl_debug_log, "(0)");
2217 else
2218 PerlIO_printf(Perl_debug_log, "(%d)", next - start);
2219 (void)PerlIO_putc(Perl_debug_log, '\n');
2220 after_print:
2221 if (regkind[(U8)op] == BRANCHJ) {
2222 register regnode *nnode = (OP(next) == LONGJMP
2223 ? regnext(next)
2224 : next);
2225 if (last && nnode > last)
2226 nnode = last;
2227 node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1);
2228 } else if (regkind[(U8)op] == BRANCH) {
2229 node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1);
2230 } else if ( op == CURLY) { /* `next' might be very big: optimizer */
2231 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2232 NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1);
2233 } else if (regkind[(U8)op] == CURLY && op != CURLYX) {
2234 node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS,
2235 next, sv, l + 1);
2236 } else if ( op == PLUS || op == STAR) {
2237 node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1);
2238 } else if (op == ANYOF) {
2239 node = NEXTOPER(node);
2240 node += ANY_SKIP;
2241 } else if (regkind[(U8)op] == EXACT) {
2242 /* Literal string, where present. */
2243 node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode);
2244 node = NEXTOPER(node);
2245 } else {
2246 node = NEXTOPER(node);
2247 node += regarglen[(U8)op];
2248 }
2249 if (op == CURLYX || op == OPEN)
2250 l++;
2251 else if (op == WHILEM)
2252 l--;
2253 }
17c3b450 2254#endif /* DEBUGGING */
c277df42 2255 return node;
2256}
2257
a687059c 2258/*
fd181c75 2259 - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form
a687059c 2260 */
2261void
8ac85365 2262regdump(regexp *r)
a687059c 2263{
35ff7856 2264#ifdef DEBUGGING
46fc3d4c 2265 SV *sv = sv_newmortal();
a687059c 2266
c277df42 2267 (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0);
a0d0e21e 2268
2269 /* Header fields of interest. */
c277df42 2270 if (r->anchored_substr)
2271 PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ",
2272 colors[0],
2273 SvPVX(r->anchored_substr),
2274 colors[1],
2275 SvTAIL(r->anchored_substr) ? "$" : "",
2276 r->anchored_offset);
2277 if (r->float_substr)
2278 PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ",
2279 colors[0],
2280 SvPVX(r->float_substr),
2281 colors[1],
2282 SvTAIL(r->float_substr) ? "$" : "",
2283 r->float_min_offset, r->float_max_offset);
2284 if (r->check_substr)
2285 PerlIO_printf(Perl_debug_log,
2286 r->check_substr == r->float_substr
2287 ? "(checking floating" : "(checking anchored");
2288 if (r->reganch & ROPT_NOSCAN)
2289 PerlIO_printf(Perl_debug_log, " noscan");
2290 if (r->reganch & ROPT_CHECK_ALL)
2291 PerlIO_printf(Perl_debug_log, " isall");
2292 if (r->check_substr)
2293 PerlIO_printf(Perl_debug_log, ") ");
2294
46fc3d4c 2295 if (r->regstclass) {
2296 regprop(sv, r->regstclass);
2297 PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv));
2298 }
774d564b 2299 if (r->reganch & ROPT_ANCH) {
2300 PerlIO_printf(Perl_debug_log, "anchored");
2301 if (r->reganch & ROPT_ANCH_BOL)
2302 PerlIO_printf(Perl_debug_log, "(BOL)");
c277df42 2303 if (r->reganch & ROPT_ANCH_MBOL)
2304 PerlIO_printf(Perl_debug_log, "(MBOL)");
774d564b 2305 if (r->reganch & ROPT_ANCH_GPOS)
2306 PerlIO_printf(Perl_debug_log, "(GPOS)");
2307 PerlIO_putc(Perl_debug_log, ' ');
2308 }
c277df42 2309 if (r->reganch & ROPT_GPOS_SEEN)
2310 PerlIO_printf(Perl_debug_log, "GPOS ");
a0d0e21e 2311 if (r->reganch & ROPT_SKIP)
760ac839 2312 PerlIO_printf(Perl_debug_log, "plus ");
a0d0e21e 2313 if (r->reganch & ROPT_IMPLICIT)
760ac839 2314 PerlIO_printf(Perl_debug_log, "implicit ");
760ac839 2315 PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen);
ce862d02 2316 if (r->reganch & ROPT_EVAL_SEEN)
2317 PerlIO_printf(Perl_debug_log, "with eval ");
760ac839 2318 PerlIO_printf(Perl_debug_log, "\n");
17c3b450 2319#endif /* DEBUGGING */
a687059c 2320}
2321
2322/*
a0d0e21e 2323- regprop - printable representation of opcode
2324*/
46fc3d4c 2325void
c277df42 2326regprop(SV *sv, regnode *o)
a687059c 2327{
35ff7856 2328#ifdef DEBUGGING
a0d0e21e 2329 register char *p = 0;
2330
46fc3d4c 2331 sv_setpv(sv, ":");
11343788 2332 switch (OP(o)) {
a0d0e21e 2333 case BOL:
2334 p = "BOL";
2335 break;
2336 case MBOL:
2337 p = "MBOL";
2338 break;
2339 case SBOL:
2340 p = "SBOL";
2341 break;
2342 case EOL:
2343 p = "EOL";
2344 break;
b85d18e9 2345 case EOS:
2346 p = "EOS";
2347 break;
a0d0e21e 2348 case MEOL:
2349 p = "MEOL";
2350 break;
2351 case SEOL:
2352 p = "SEOL";
2353 break;
2354 case ANY:
2355 p = "ANY";
2356 break;
2357 case SANY:
2358 p = "SANY";
2359 break;
2360 case ANYOF:
2361 p = "ANYOF";
2362 break;
2363 case BRANCH:
2364 p = "BRANCH";
2365 break;
bbce6d69 2366 case EXACT:
c277df42 2367 sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
bbce6d69 2368 break;
2369 case EXACTF:
c277df42 2370 sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
bbce6d69 2371 break;
2372 case EXACTFL:
c277df42 2373 sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]);
a0d0e21e 2374 break;
2375 case NOTHING:
2376 p = "NOTHING";
2377 break;
c277df42 2378 case TAIL:
2379 p = "TAIL";
2380 break;
a0d0e21e 2381 case BACK:
2382 p = "BACK";
2383 break;
2384 case END:
2385 p = "END";
2386 break;
a0d0e21e 2387 case BOUND:
2388 p = "BOUND";
2389 break;
bbce6d69 2390 case BOUNDL:
2391 p = "BOUNDL";
2392 break;
a0d0e21e 2393 case NBOUND:
2394 p = "NBOUND";
2395 break;
bbce6d69 2396 case NBOUNDL:
2397 p = "NBOUNDL";
a0d0e21e 2398 break;
2399 case CURLY:
5dc0d613 2400 sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2401 break;
c277df42 2402 case CURLYM:
c277df42 2403 sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2404 break;
2405 case CURLYN:
c277df42 2406 sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o));
c277df42 2407 break;
a0d0e21e 2408 case CURLYX:
5dc0d613 2409 sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o));
a0d0e21e 2410 break;
2411 case REF:
c277df42 2412 sv_catpvf(sv, "REF%d", ARG(o));
a0d0e21e 2413 break;
c8756f30 2414 case REFF:
c277df42 2415 sv_catpvf(sv, "REFF%d", ARG(o));
c8756f30 2416 break;
2417 case REFFL:
c277df42 2418 sv_catpvf(sv, "REFFL%d", ARG(o));
c8756f30 2419 break;
a0d0e21e 2420 case OPEN:
c277df42 2421 sv_catpvf(sv, "OPEN%d", ARG(o));
a0d0e21e 2422 break;
2423 case CLOSE:
c277df42 2424 sv_catpvf(sv, "CLOSE%d", ARG(o));
a0d0e21e 2425 p = NULL;
2426 break;
2427 case STAR:
2428 p = "STAR";
2429 break;
2430 case PLUS:
2431 p = "PLUS";
2432 break;
2433 case MINMOD:
2434 p = "MINMOD";
2435 break;
774d564b 2436 case GPOS:
2437 p = "GPOS";
a0d0e21e 2438 break;
2439 case UNLESSM:
c277df42 2440 sv_catpvf(sv, "UNLESSM[-%d]", o->flags);
a0d0e21e 2441 break;
2442 case IFMATCH:
c277df42 2443 sv_catpvf(sv, "IFMATCH[-%d]", o->flags);
a0d0e21e 2444 break;
2445 case SUCCEED:
2446 p = "SUCCEED";
2447 break;
2448 case WHILEM:
2449 p = "WHILEM";
2450 break;
bbce6d69 2451 case DIGIT:
2452 p = "DIGIT";
2453 break;
2454 case NDIGIT:
2455 p = "NDIGIT";
2456 break;
2457 case ALNUM:
2458 p = "ALNUM";
2459 break;
2460 case NALNUM:
2461 p = "NALNUM";
2462 break;
2463 case SPACE:
2464 p = "SPACE";
2465 break;
2466 case NSPACE:
2467 p = "NSPACE";
2468 break;
2469 case ALNUML:
2470 p = "ALNUML";
2471 break;
2472 case NALNUML:
2473 p = "NALNUML";
2474 break;
2475 case SPACEL:
2476 p = "SPACEL";
2477 break;
2478 case NSPACEL:
2479 p = "NSPACEL";
2480 break;
c277df42 2481 case EVAL:
2482 p = "EVAL";
2483 break;
2484 case LONGJMP:
2485 p = "LONGJMP";
2486 break;
2487 case BRANCHJ:
2488 p = "BRANCHJ";
2489 break;
2490 case IFTHEN:
2491 p = "IFTHEN";
2492 break;
2493 case GROUPP:
2494 sv_catpvf(sv, "GROUPP%d", ARG(o));
2495 break;
2496 case LOGICAL:
2497 p = "LOGICAL";
2498 break;
2499 case SUSPEND:
2500 p = "SUSPEND";
2501 break;
2502 case RENUM:
2503 p = "RENUM";
2504 break;
2505 case OPTIMIZED:
2506 p = "OPTIMIZED";
2507 break;
a0d0e21e 2508 default:
2509 FAIL("corrupted regexp opcode");
2510 }
46fc3d4c 2511 if (p)
2512 sv_catpv(sv, p);
17c3b450 2513#endif /* DEBUGGING */
35ff7856 2514}
a687059c 2515
2b69d0c2 2516void
8ac85365 2517pregfree(struct regexp *r)
a687059c 2518{
c277df42 2519 if (!r || (--r->refcnt > 0))
a0d0e21e 2520 return;
c277df42 2521 if (r->precomp)
a0d0e21e 2522 Safefree(r->precomp);
c277df42 2523 if (r->subbase)
a0d0e21e 2524 Safefree(r->subbase);
a193d654 2525 if (r->substrs) {
2526 if (r->anchored_substr)
2527 SvREFCNT_dec(r->anchored_substr);
2528 if (r->float_substr)
2529 SvREFCNT_dec(r->float_substr);
2779dcf1 2530 Safefree(r->substrs);
a193d654 2531 }
c277df42 2532 if (r->data) {
2533 int n = r->data->count;
2534 while (--n >= 0) {
2535 switch (r->data->what[n]) {
2536 case 's':
2537 SvREFCNT_dec((SV*)r->data->data[n]);
2538 break;
2539 case 'o':
2540 op_free((OP_4tree*)r->data->data[n]);
2541 break;
2542 case 'n':
2543 break;
2544 default:
2545 FAIL2("panic: regfree data code '%c'", r->data->what[n]);
2546 }
2547 }
2548 Safefree(r->data->what);
2549 Safefree(r->data);
a0d0e21e 2550 }
2551 Safefree(r->startp);
2552 Safefree(r->endp);
2553 Safefree(r);
a687059c 2554}
c277df42 2555
2556/*
2557 - regnext - dig the "next" pointer out of a node
2558 *
2559 * [Note, when REGALIGN is defined there are two places in regmatch()
2560 * that bypass this code for speed.]
2561 */
2562regnode *
2563regnext(register regnode *p)
2564{
2565 register I32 offset;
2566
2567 if (p == &regdummy)
2568 return(NULL);
2569
2570 offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p));
2571 if (offset == 0)
2572 return(NULL);
2573
c277df42 2574 return(p+offset);
c277df42 2575}
2576
01f988be 2577STATIC void
c277df42 2578re_croak2(const char* pat1,const char* pat2,...)
c277df42 2579{
2580 va_list args;
2581 STRLEN l1 = strlen(pat1);
2582 STRLEN l2 = strlen(pat2);
2583 char buf[512];
2584 char *message;
2585
2586 if (l1 > 510)
2587 l1 = 510;
2588 if (l1 + l2 > 510)
2589 l2 = 510 - l1;
2590 Copy(pat1, buf, l1 , char);
2591 Copy(pat2, buf + l1, l2 , char);
3b818b81 2592 buf[l1 + l2] = '\n';
2593 buf[l1 + l2 + 1] = '\0';
c277df42 2594 va_start(args, pat2);
c277df42 2595 message = mess(buf, &args);
2596 va_end(args);
2597 l1 = strlen(message);
2598 if (l1 > 512)
2599 l1 = 512;
2600 Copy(message, buf, l1 , char);
2601 buf[l1] = '\0'; /* Overwrite \n */
2602 croak("%s", buf);
2603}