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