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