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