Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | /* regcomp.c |
2 | */ | |
3 | ||
4 | /* | |
5 | * "A fair jaw-cracker dwarf-language must be." --Samwise Gamgee | |
6 | */ | |
7 | ||
a687059c LW |
8 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
9 | * confused with the original package (see point 3 below). Thanks, Henry! | |
10 | */ | |
11 | ||
12 | /* Additional note: this code is very heavily munged from Henry's version | |
13 | * in places. In some spots I've traded clarity for efficiency, so don't | |
14 | * blame Henry for some of the lack of readability. | |
15 | */ | |
16 | ||
e50aee73 AD |
17 | /* The names of the functions have been changed from regcomp and |
18 | * regexec to pregcomp and pregexec in order to avoid conflicts | |
19 | * with the POSIX routines of the same names. | |
20 | */ | |
21 | ||
f0fcb552 | 22 | /*SUPPRESS 112*/ |
a687059c | 23 | /* |
e50aee73 | 24 | * pregcomp and pregexec -- regsub and regerror are not used in perl |
a687059c LW |
25 | * |
26 | * Copyright (c) 1986 by University of Toronto. | |
27 | * Written by Henry Spencer. Not derived from licensed software. | |
28 | * | |
29 | * Permission is granted to anyone to use this software for any | |
30 | * purpose on any computer system, and to redistribute it freely, | |
31 | * subject to the following restrictions: | |
32 | * | |
33 | * 1. The author is not responsible for the consequences of use of | |
34 | * this software, no matter how awful, even if they arise | |
35 | * from defects in it. | |
36 | * | |
37 | * 2. The origin of this software must not be misrepresented, either | |
38 | * by explicit claim or by omission. | |
39 | * | |
40 | * 3. Altered versions must be plainly marked as such, and must not | |
41 | * be misrepresented as being the original software. | |
42 | * | |
43 | * | |
44 | **** Alterations to Henry's code are... | |
45 | **** | |
9607fc9c | 46 | **** Copyright (c) 1991-1997, Larry Wall |
a687059c | 47 | **** |
9ef589d8 LW |
48 | **** You may distribute under the terms of either the GNU General Public |
49 | **** License or the Artistic License, as specified in the README file. | |
50 | ||
a687059c LW |
51 | * |
52 | * Beware that some of this code is subtly aware of the way operator | |
53 | * precedence is structured in regular expressions. Serious changes in | |
54 | * regular-expression syntax might require a total rethink. | |
55 | */ | |
56 | #include "EXTERN.h" | |
57 | #include "perl.h" | |
58 | #include "INTERN.h" | |
c277df42 IZ |
59 | |
60 | #define REG_COMP_C | |
a687059c LW |
61 | #include "regcomp.h" |
62 | ||
11343788 MB |
63 | #ifdef USE_THREADS |
64 | #undef op | |
65 | #endif /* USE_THREADS */ | |
66 | ||
c277df42 IZ |
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 LW |
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 LW |
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 LW |
97 | #ifdef atarist |
98 | #define PERL_META "^$.[()|?+*\\" | |
99 | #else | |
a687059c | 100 | #define META "^$.[()|?+*\\" |
2b69d0c2 | 101 | #endif |
a687059c | 102 | |
35c8bce7 LW |
103 | #ifdef SPSTART |
104 | #undef SPSTART /* dratted cpp namespace... */ | |
105 | #endif | |
a687059c LW |
106 | /* |
107 | * Flags to be passed up and down. | |
108 | */ | |
a687059c | 109 | #define WORST 0 /* Worst case. */ |
a0d0e21e LW |
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 LW |
114 | |
115 | /* | |
e50aee73 | 116 | * Forward declarations for pregcomp()'s friends. |
a687059c | 117 | */ |
a0d0e21e | 118 | |
c277df42 IZ |
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 IZ |
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 IZ |
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 | } | |
375 | sv_catpvn(data->last_found, OPERAND(scan)+1, l); | |
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 LW |
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 IZ |
712 | regnode *scan; |
713 | SV **longest; | |
714 | SV *longest_fixed; | |
715 | SV *longest_float; | |
716 | regnode *first; | |
a0d0e21e | 717 | I32 flags; |
a0d0e21e LW |
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 IZ |
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 IZ |
731 | regseen = 0; |
732 | seen_zerolen = *exp == '^' ? -1 : 0; | |
733 | extralen = 0; | |
734 | ||
bbce6d69 | 735 | /* First pass: determine size, legality. */ |
a0d0e21e LW |
736 | regparse = exp; |
737 | regxend = xend; | |
a0d0e21e | 738 | regnaughty = 0; |
a0d0e21e LW |
739 | regnpar = 1; |
740 | regsize = 0L; | |
741 | regcode = ®dummy; | |
c277df42 | 742 | regc((U8)MAGIC, (char*)regcode); |
a0d0e21e LW |
743 | if (reg(0, &flags) == NULL) { |
744 | Safefree(regprecomp); | |
745 | regprecomp = Nullch; | |
746 | return(NULL); | |
747 | } | |
c277df42 IZ |
748 | DEBUG_r(PerlIO_printf(Perl_debug_log, "size %d ", regsize)); |
749 | ||
750 | DEBUG_r( | |
751 | if (!colorset) { | |
752 | int i = 0; | |
753 | char *s = getenv("TERMCAP_COLORS"); | |
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 IZ |
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 IZ |
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 IZ |
788 | Newc(1001, r, sizeof(regexp) + (unsigned)regsize * sizeof(regnode), |
789 | char, regexp); | |
a0d0e21e LW |
790 | if (r == NULL) |
791 | FAIL("regexp out of space"); | |
c277df42 | 792 | r->refcnt = 1; |
bbce6d69 | 793 | r->prelen = xend - exp; |
a0d0e21e LW |
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 LW |
802 | regnpar = 1; |
803 | regcode = r->program; | |
c277df42 IZ |
804 | regc((U8)MAGIC, (char*) regcode++); |
805 | r->data = 0; | |
a0d0e21e LW |
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 IZ |
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 LW |
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 LW |
833 | } |
834 | ||
a0d0e21e LW |
835 | /* Starting-point info. */ |
836 | again: | |
c277df42 | 837 | if (OP(first) == EXACT); /* Empty, get anchored substr later. */ |
a0d0e21e LW |
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 LW |
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 LW |
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 LW |
861 | } |
862 | if (sawplus && (!sawopen || !regsawback)) | |
863 | r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */ | |
864 | ||
c277df42 IZ |
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 LW |
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 LW |
877 | * earlier string may buy us something the later one won't.] |
878 | */ | |
a0d0e21e | 879 | minlen = 0; |
a687059c | 880 | |
c277df42 IZ |
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 MB |
897 | longest_float_length = SvCUR(data.longest_float); |
898 | if (longest_float_length | |
c277df42 IZ |
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 MB |
922 | longest_fixed_length = SvCUR(data.longest_fixed); |
923 | if (longest_fixed_length | |
c277df42 IZ |
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 IZ |
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 IZ |
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 IZ |
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 LW |
960 | } |
961 | ||
a0d0e21e LW |
962 | r->nparens = regnpar - 1; |
963 | r->minlen = minlen; | |
c277df42 IZ |
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 LW |
968 | Newz(1002, r->startp, regnpar, char*); |
969 | Newz(1002, r->endp, regnpar, char*); | |
970 | DEBUG_r(regdump(r)); | |
971 | return(r); | |
a687059c LW |
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 IZ |
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 IZ |
992 | I32 flags, oregflags = regflags, have_branch = 0, open = 0; |
993 | char c; | |
a0d0e21e LW |
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 IZ |
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 LW |
1014 | case '=': |
1015 | case '!': | |
c277df42 IZ |
1016 | seen_zerolen++; |
1017 | case ':': | |
1018 | case '>': | |
a0d0e21e LW |
1019 | break; |
1020 | case '$': | |
1021 | case '@': | |
c277df42 | 1022 | FAIL2("Sequence (?%c...) not implemented", (int)paren); |
a0d0e21e LW |
1023 | break; |
1024 | case '#': | |
1025 | while (*regparse && *regparse != ')') | |
1026 | regparse++; | |
1027 | if (*regparse != ')') | |
c277df42 | 1028 | FAIL("Sequence (?#... not terminated"); |
a0d0e21e LW |
1029 | nextchar(); |
1030 | *flagp = TRYAGAIN; | |
1031 | return NULL; | |
c277df42 IZ |
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); | |
1068 | } | |
1069 | ||
1070 | nextchar(); | |
1071 | if (tainted) | |
1072 | FAIL("Eval-group in insecure regular expression"); | |
1073 | return reganode(EVAL, n); | |
1074 | } | |
1075 | case '(': | |
1076 | { | |
1077 | if (regparse[0] == '?') { | |
1078 | if (regparse[1] == '=' || regparse[1] == '!' | |
1079 | || regparse[1] == '<' | |
1080 | || regparse[1] == '{') { /* Lookahead or eval. */ | |
1081 | I32 flag; | |
1082 | ||
1083 | ret = reg_node(LOGICAL); | |
1084 | regtail(ret, reg(1, &flag)); | |
1085 | goto insert_if; | |
1086 | } | |
1087 | } else if (regparse[0] >= '1' && regparse[0] <= '9' ) { | |
1088 | parno = atoi(regparse++); | |
1089 | ||
1090 | while (isDIGIT(*regparse)) | |
1091 | regparse++; | |
1092 | ret = reganode(GROUPP, parno); | |
1093 | if ((c = *nextchar()) != ')') | |
1094 | FAIL2("Switch (?(number%c not recognized", c); | |
1095 | insert_if: | |
1096 | regtail(ret, reganode(IFTHEN, 0)); | |
1097 | br = regbranch(&flags, 1); | |
1098 | if (br == NULL) | |
1099 | br = reganode(LONGJMP, 0); | |
1100 | else | |
1101 | regtail(br, reganode(LONGJMP, 0)); | |
1102 | c = *nextchar(); | |
1103 | if (c == '|') { | |
1104 | lastbr = reganode(IFTHEN, 0); /* Fake one for optimizer. */ | |
1105 | regbranch(&flags, 1); | |
1106 | regtail(ret, lastbr); | |
1107 | c = *nextchar(); | |
1108 | } else | |
1109 | lastbr = NULL; | |
1110 | if (c != ')') | |
1111 | FAIL("Switch (?(condition)... contains too many branches"); | |
1112 | ender = reg_node(TAIL); | |
1113 | regtail(br, ender); | |
1114 | if (lastbr) { | |
1115 | regtail(lastbr, ender); | |
1116 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); | |
1117 | } else | |
1118 | regtail(ret, ender); | |
1119 | return ret; | |
1120 | } else { | |
1121 | FAIL2("Unknown condition for (?(%.2s", regparse); | |
1122 | } | |
1123 | } | |
1b1626e4 | 1124 | case 0: |
c277df42 | 1125 | FAIL("Sequence (? incomplete"); |
1b1626e4 | 1126 | break; |
a0d0e21e LW |
1127 | default: |
1128 | --regparse; | |
c90c0ff4 | 1129 | while (*regparse && strchr("iogcmsx", *regparse)) |
a0d0e21e | 1130 | pmflag(®flags, *regparse++); |
c277df42 | 1131 | unknown: |
a0d0e21e | 1132 | if (*regparse != ')') |
c277df42 | 1133 | FAIL2("Sequence (?%c...) not recognized", *regparse); |
a0d0e21e LW |
1134 | nextchar(); |
1135 | *flagp = TRYAGAIN; | |
1136 | return NULL; | |
1137 | } | |
1138 | } | |
1139 | else { | |
1140 | parno = regnpar; | |
1141 | regnpar++; | |
1142 | ret = reganode(OPEN, parno); | |
c277df42 | 1143 | open = 1; |
a0d0e21e LW |
1144 | } |
1145 | } else | |
1146 | ret = NULL; | |
1147 | ||
1148 | /* Pick up the branches, linking them together. */ | |
c277df42 | 1149 | br = regbranch(&flags, 1); |
a0d0e21e LW |
1150 | if (br == NULL) |
1151 | return(NULL); | |
c277df42 IZ |
1152 | if (*regparse == '|') { |
1153 | if (!SIZE_ONLY && extralen) { | |
1154 | reginsert(BRANCHJ, br); | |
1155 | } else | |
1156 | reginsert(BRANCH, br); | |
1157 | have_branch = 1; | |
1158 | if (SIZE_ONLY) | |
1159 | extralen += 1; /* For BRANCHJ-BRANCH. */ | |
1160 | } else if (paren == ':') { | |
1161 | *flagp |= flags&SIMPLE; | |
1162 | } | |
1163 | if (open) { /* Starts with OPEN. */ | |
1164 | regtail(ret, br); /* OPEN -> first. */ | |
1165 | } else if (paren != '?') /* Not Conditional */ | |
a0d0e21e LW |
1166 | ret = br; |
1167 | if (!(flags&HASWIDTH)) | |
1168 | *flagp &= ~HASWIDTH; | |
1169 | *flagp |= flags&SPSTART; | |
c277df42 | 1170 | lastbr = br; |
a0d0e21e | 1171 | while (*regparse == '|') { |
c277df42 IZ |
1172 | if (!SIZE_ONLY && extralen) { |
1173 | ender = reganode(LONGJMP,0); | |
1174 | regtail(NEXTOPER(NEXTOPER(lastbr)), ender); /* Append to the previous. */ | |
1175 | } | |
1176 | if (SIZE_ONLY) | |
1177 | extralen += 2; /* Account for LONGJMP. */ | |
a0d0e21e | 1178 | nextchar(); |
c277df42 | 1179 | br = regbranch(&flags, 0); |
a687059c | 1180 | if (br == NULL) |
a0d0e21e | 1181 | return(NULL); |
c277df42 IZ |
1182 | regtail(lastbr, br); /* BRANCH -> BRANCH. */ |
1183 | lastbr = br; | |
a687059c | 1184 | if (!(flags&HASWIDTH)) |
a0d0e21e | 1185 | *flagp &= ~HASWIDTH; |
a687059c | 1186 | *flagp |= flags&SPSTART; |
a0d0e21e LW |
1187 | } |
1188 | ||
c277df42 IZ |
1189 | if (have_branch || paren != ':') { |
1190 | /* Make a closing node, and hook it on the end. */ | |
1191 | switch (paren) { | |
1192 | case ':': | |
1193 | ender = reg_node(TAIL); | |
1194 | break; | |
1195 | case 1: | |
1196 | ender = reganode(CLOSE, parno); | |
1197 | break; | |
1198 | case '<': | |
1199 | case '>': | |
1200 | case ',': | |
1201 | case '=': | |
1202 | case '!': | |
1203 | ender = reg_node(SUCCEED); | |
1204 | *flagp &= ~HASWIDTH; | |
1205 | break; | |
1206 | case 0: | |
1207 | ender = reg_node(END); | |
1208 | break; | |
1209 | } | |
1210 | regtail(lastbr, ender); | |
a0d0e21e | 1211 | |
c277df42 IZ |
1212 | if (have_branch) { |
1213 | /* Hook the tails of the branches to the closing node. */ | |
1214 | for (br = ret; br != NULL; br = regnext(br)) { | |
1215 | regoptail(br, ender); | |
1216 | } | |
1217 | } | |
a0d0e21e | 1218 | } |
c277df42 IZ |
1219 | |
1220 | { | |
1221 | char *p; | |
1222 | static char parens[] = "=!<,>"; | |
1223 | ||
1224 | if (paren && (p = strchr(parens, paren))) { | |
1225 | int node = ((p - parens) % 2) ? UNLESSM : IFMATCH; | |
1226 | int flag = (p - parens) > 1; | |
1227 | ||
1228 | if (paren == '>') | |
1229 | node = SUSPEND, flag = 0; | |
1230 | reginsert(node,ret); | |
1231 | #ifdef REGALIGN_STRUCT | |
1232 | ret->flags = flag; | |
1233 | #endif | |
1234 | regtail(ret, reg_node(TAIL)); | |
1235 | } | |
a0d0e21e LW |
1236 | } |
1237 | ||
1238 | /* Check for proper termination. */ | |
4633a7c4 | 1239 | if (paren && (regparse >= regxend || *nextchar() != ')')) { |
a0d0e21e LW |
1240 | FAIL("unmatched () in regexp"); |
1241 | } else if (!paren && regparse < regxend) { | |
1242 | if (*regparse == ')') { | |
1243 | FAIL("unmatched () in regexp"); | |
1244 | } else | |
1245 | FAIL("junk on end of regexp"); /* "Can't happen". */ | |
1246 | /* NOTREACHED */ | |
1247 | } | |
c277df42 IZ |
1248 | if (paren != 0) { |
1249 | regflags = oregflags; | |
1250 | } | |
a687059c | 1251 | |
a0d0e21e | 1252 | return(ret); |
a687059c LW |
1253 | } |
1254 | ||
1255 | /* | |
1256 | - regbranch - one alternative of an | operator | |
1257 | * | |
1258 | * Implements the concatenation operator. | |
1259 | */ | |
c277df42 IZ |
1260 | static regnode * |
1261 | regbranch(I32 *flagp, I32 first) | |
a687059c | 1262 | { |
c277df42 IZ |
1263 | register regnode *ret; |
1264 | register regnode *chain = NULL; | |
1265 | register regnode *latest; | |
1266 | I32 flags = 0, c = 0; | |
a0d0e21e | 1267 | |
c277df42 IZ |
1268 | if (first) |
1269 | ret = NULL; | |
1270 | else { | |
1271 | if (!SIZE_ONLY && extralen) | |
1272 | ret = reganode(BRANCHJ,0); | |
1273 | else | |
1274 | ret = reg_node(BRANCH); | |
1275 | } | |
1276 | ||
1277 | if (!first && SIZE_ONLY) | |
1278 | extralen += 1; /* BRANCHJ */ | |
1279 | ||
1280 | *flagp = WORST; /* Tentatively. */ | |
a0d0e21e | 1281 | |
a0d0e21e LW |
1282 | regparse--; |
1283 | nextchar(); | |
1284 | while (regparse < regxend && *regparse != '|' && *regparse != ')') { | |
1285 | flags &= ~TRYAGAIN; | |
1286 | latest = regpiece(&flags); | |
1287 | if (latest == NULL) { | |
1288 | if (flags & TRYAGAIN) | |
1289 | continue; | |
1290 | return(NULL); | |
c277df42 IZ |
1291 | } else if (ret == NULL) |
1292 | ret = latest; | |
a0d0e21e | 1293 | *flagp |= flags&HASWIDTH; |
c277df42 | 1294 | if (chain == NULL) /* First piece. */ |
a0d0e21e LW |
1295 | *flagp |= flags&SPSTART; |
1296 | else { | |
1297 | regnaughty++; | |
1298 | regtail(chain, latest); | |
a687059c | 1299 | } |
a0d0e21e | 1300 | chain = latest; |
c277df42 IZ |
1301 | c++; |
1302 | } | |
1303 | if (chain == NULL) { /* Loop ran zero times. */ | |
1304 | chain = reg_node(NOTHING); | |
1305 | if (ret == NULL) | |
1306 | ret = chain; | |
1307 | } | |
1308 | if (c == 1) { | |
1309 | *flagp |= flags&SIMPLE; | |
a0d0e21e | 1310 | } |
a687059c | 1311 | |
a0d0e21e | 1312 | return(ret); |
a687059c LW |
1313 | } |
1314 | ||
1315 | /* | |
1316 | - regpiece - something followed by possible [*+?] | |
1317 | * | |
1318 | * Note that the branching code sequences used for ? and the general cases | |
1319 | * of * and + are somewhat optimized: they use the same NOTHING node as | |
1320 | * both the endmarker for their branch list and the body of the last branch. | |
1321 | * It might seem that this node could be dispensed with entirely, but the | |
1322 | * endmarker role is not redundant. | |
1323 | */ | |
c277df42 | 1324 | static regnode * |
8ac85365 | 1325 | regpiece(I32 *flagp) |
a687059c | 1326 | { |
c277df42 | 1327 | register regnode *ret; |
a0d0e21e LW |
1328 | register char op; |
1329 | register char *next; | |
1330 | I32 flags; | |
1331 | char *origparse = regparse; | |
1332 | char *maxpos; | |
1333 | I32 min; | |
c277df42 | 1334 | I32 max = REG_INFTY; |
a0d0e21e LW |
1335 | |
1336 | ret = regatom(&flags); | |
1337 | if (ret == NULL) { | |
1338 | if (flags & TRYAGAIN) | |
1339 | *flagp |= TRYAGAIN; | |
1340 | return(NULL); | |
1341 | } | |
1342 | ||
1343 | op = *regparse; | |
a0d0e21e LW |
1344 | |
1345 | if (op == '{' && regcurly(regparse)) { | |
1346 | next = regparse + 1; | |
1347 | maxpos = Nullch; | |
1348 | while (isDIGIT(*next) || *next == ',') { | |
1349 | if (*next == ',') { | |
1350 | if (maxpos) | |
1351 | break; | |
1352 | else | |
1353 | maxpos = next; | |
a687059c | 1354 | } |
a0d0e21e LW |
1355 | next++; |
1356 | } | |
1357 | if (*next == '}') { /* got one */ | |
1358 | if (!maxpos) | |
1359 | maxpos = next; | |
1360 | regparse++; | |
1361 | min = atoi(regparse); | |
1362 | if (*maxpos == ',') | |
1363 | maxpos++; | |
1364 | else | |
1365 | maxpos = regparse; | |
1366 | max = atoi(maxpos); | |
1367 | if (!max && *maxpos != '0') | |
c277df42 IZ |
1368 | max = REG_INFTY; /* meaning "infinity" */ |
1369 | else if (max >= REG_INFTY) | |
1370 | FAIL2("Quantifier in {,} bigger than %d", REG_INFTY - 1); | |
a0d0e21e LW |
1371 | regparse = next; |
1372 | nextchar(); | |
1373 | ||
1374 | do_curly: | |
1375 | if ((flags&SIMPLE)) { | |
1376 | regnaughty += 2 + regnaughty / 2; | |
1377 | reginsert(CURLY, ret); | |
1378 | } | |
1379 | else { | |
1380 | regnaughty += 4 + regnaughty; /* compound interest */ | |
c277df42 IZ |
1381 | regtail(ret, reg_node(WHILEM)); |
1382 | if (!SIZE_ONLY && extralen) { | |
1383 | reginsert(LONGJMP,ret); | |
1384 | reginsert(NOTHING,ret); | |
1385 | NEXT_OFF(ret) = 3; /* Go over LONGJMP. */ | |
1386 | } | |
a0d0e21e | 1387 | reginsert(CURLYX,ret); |
c277df42 IZ |
1388 | if (!SIZE_ONLY && extralen) |
1389 | NEXT_OFF(ret) = 3; /* Go over NOTHING to LONGJMP. */ | |
1390 | regtail(ret, reg_node(NOTHING)); | |
1391 | if (SIZE_ONLY) | |
1392 | extralen += 3; | |
a0d0e21e | 1393 | } |
c277df42 IZ |
1394 | #ifdef REGALIGN_STRUCT |
1395 | ret->flags = 0; | |
1396 | #endif | |
a0d0e21e LW |
1397 | |
1398 | if (min > 0) | |
1399 | *flagp = (WORST|HASWIDTH); | |
1400 | if (max && max < min) | |
c277df42 IZ |
1401 | FAIL("Can't do {n,m} with n > m"); |
1402 | if (!SIZE_ONLY) { | |
1403 | ARG1_SET(ret, min); | |
1404 | ARG2_SET(ret, max); | |
a687059c | 1405 | } |
a687059c | 1406 | |
a0d0e21e | 1407 | goto nest_check; |
a687059c | 1408 | } |
a0d0e21e | 1409 | } |
a687059c | 1410 | |
a0d0e21e LW |
1411 | if (!ISMULT1(op)) { |
1412 | *flagp = flags; | |
a687059c | 1413 | return(ret); |
a0d0e21e | 1414 | } |
bb20fd44 | 1415 | |
c277df42 | 1416 | #if 0 /* Now runtime fix should be reliable. */ |
bb20fd44 | 1417 | if (!(flags&HASWIDTH) && op != '?') |
c277df42 IZ |
1418 | FAIL("regexp *+ operand could be empty"); |
1419 | #endif | |
bb20fd44 | 1420 | |
a0d0e21e LW |
1421 | nextchar(); |
1422 | ||
1423 | *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH); | |
1424 | ||
1425 | if (op == '*' && (flags&SIMPLE)) { | |
1426 | reginsert(STAR, ret); | |
c277df42 IZ |
1427 | #ifdef REGALIGN_STRUCT |
1428 | ret->flags = 0; | |
1429 | #endif | |
a0d0e21e LW |
1430 | regnaughty += 4; |
1431 | } | |
1432 | else if (op == '*') { | |
1433 | min = 0; | |
1434 | goto do_curly; | |
1435 | } else if (op == '+' && (flags&SIMPLE)) { | |
1436 | reginsert(PLUS, ret); | |
c277df42 IZ |
1437 | #ifdef REGALIGN_STRUCT |
1438 | ret->flags = 0; | |
1439 | #endif | |
a0d0e21e LW |
1440 | regnaughty += 3; |
1441 | } | |
1442 | else if (op == '+') { | |
1443 | min = 1; | |
1444 | goto do_curly; | |
1445 | } else if (op == '?') { | |
1446 | min = 0; max = 1; | |
1447 | goto do_curly; | |
1448 | } | |
1449 | nest_check: | |
c277df42 | 1450 | if (dowarn && !SIZE_ONLY && !(flags&HASWIDTH) && max > 10000) { |
a0d0e21e LW |
1451 | warn("%.*s matches null string many times", |
1452 | regparse - origparse, origparse); | |
1453 | } | |
1454 | ||
1455 | if (*regparse == '?') { | |
1456 | nextchar(); | |
1457 | reginsert(MINMOD, ret); | |
1458 | #ifdef REGALIGN | |
c277df42 | 1459 | regtail(ret, ret + NODE_STEP_REGNODE); |
a0d0e21e LW |
1460 | #else |
1461 | regtail(ret, ret + 3); | |
1462 | #endif | |
1463 | } | |
1464 | if (ISMULT2(regparse)) | |
1465 | FAIL("nested *?+ in regexp"); | |
1466 | ||
1467 | return(ret); | |
a687059c LW |
1468 | } |
1469 | ||
1470 | /* | |
1471 | - regatom - the lowest level | |
1472 | * | |
1473 | * Optimization: gobbles an entire sequence of ordinary characters so that | |
1474 | * it can turn them into a single node, which is smaller to store and | |
1475 | * faster to run. Backslashed characters are exceptions, each becoming a | |
1476 | * separate node; the code is simpler that way and it's not worth fixing. | |
1477 | * | |
1478 | * [Yes, it is worth fixing, some scripts can run twice the speed.] | |
1479 | */ | |
c277df42 | 1480 | static regnode * |
8ac85365 | 1481 | regatom(I32 *flagp) |
a687059c | 1482 | { |
c277df42 | 1483 | register regnode *ret = 0; |
a0d0e21e LW |
1484 | I32 flags; |
1485 | ||
1486 | *flagp = WORST; /* Tentatively. */ | |
1487 | ||
1488 | tryagain: | |
1489 | switch (*regparse) { | |
1490 | case '^': | |
c277df42 | 1491 | seen_zerolen++; |
a0d0e21e LW |
1492 | nextchar(); |
1493 | if (regflags & PMf_MULTILINE) | |
c277df42 | 1494 | ret = reg_node(MBOL); |
a0d0e21e | 1495 | else if (regflags & PMf_SINGLELINE) |
c277df42 | 1496 | ret = reg_node(SBOL); |
a0d0e21e | 1497 | else |
c277df42 | 1498 | ret = reg_node(BOL); |
a0d0e21e LW |
1499 | break; |
1500 | case '$': | |
c277df42 IZ |
1501 | if (regparse[1]) |
1502 | seen_zerolen++; | |
a0d0e21e LW |
1503 | nextchar(); |
1504 | if (regflags & PMf_MULTILINE) | |
c277df42 | 1505 | ret = reg_node(MEOL); |
a0d0e21e | 1506 | else if (regflags & PMf_SINGLELINE) |
c277df42 | 1507 | ret = reg_node(SEOL); |
a0d0e21e | 1508 | else |
c277df42 | 1509 | ret = reg_node(EOL); |
a0d0e21e LW |
1510 | break; |
1511 | case '.': | |
1512 | nextchar(); | |
1513 | if (regflags & PMf_SINGLELINE) | |
c277df42 | 1514 | ret = reg_node(SANY); |
a0d0e21e | 1515 | else |
c277df42 | 1516 | ret = reg_node(ANY); |
a0d0e21e LW |
1517 | regnaughty++; |
1518 | *flagp |= HASWIDTH|SIMPLE; | |
1519 | break; | |
1520 | case '[': | |
1521 | regparse++; | |
1522 | ret = regclass(); | |
1523 | *flagp |= HASWIDTH|SIMPLE; | |
1524 | break; | |
1525 | case '(': | |
1526 | nextchar(); | |
1527 | ret = reg(1, &flags); | |
1528 | if (ret == NULL) { | |
1529 | if (flags & TRYAGAIN) | |
1530 | goto tryagain; | |
1531 | return(NULL); | |
1532 | } | |
c277df42 | 1533 | *flagp |= flags&(HASWIDTH|SPSTART|SIMPLE); |
a0d0e21e LW |
1534 | break; |
1535 | case '|': | |
1536 | case ')': | |
1537 | if (flags & TRYAGAIN) { | |
1538 | *flagp |= TRYAGAIN; | |
1539 | return NULL; | |
1540 | } | |
c277df42 | 1541 | FAIL2("internal urp in regexp at /%s/", regparse); |
a0d0e21e LW |
1542 | /* Supposed to be caught earlier. */ |
1543 | break; | |
85afd4ae CS |
1544 | case '{': |
1545 | if (!regcurly(regparse)) { | |
1546 | regparse++; | |
1547 | goto defchar; | |
1548 | } | |
1549 | /* FALL THROUGH */ | |
a0d0e21e LW |
1550 | case '?': |
1551 | case '+': | |
1552 | case '*': | |
3115e423 | 1553 | FAIL("?+*{} follows nothing in regexp"); |
a0d0e21e LW |
1554 | break; |
1555 | case '\\': | |
1556 | switch (*++regparse) { | |
1557 | case 'A': | |
c277df42 IZ |
1558 | seen_zerolen++; |
1559 | ret = reg_node(SBOL); | |
a0d0e21e LW |
1560 | *flagp |= SIMPLE; |
1561 | nextchar(); | |
1562 | break; | |
1563 | case 'G': | |
c277df42 IZ |
1564 | ret = reg_node(GPOS); |
1565 | regseen |= REG_SEEN_GPOS; | |
a0d0e21e LW |
1566 | *flagp |= SIMPLE; |
1567 | nextchar(); | |
1568 | break; | |
1569 | case 'Z': | |
c277df42 | 1570 | ret = reg_node(SEOL); |
a0d0e21e LW |
1571 | *flagp |= SIMPLE; |
1572 | nextchar(); | |
1573 | break; | |
1574 | case 'w': | |
c277df42 | 1575 | ret = reg_node((regflags & PMf_LOCALE) ? ALNUML : ALNUM); |
a0d0e21e LW |
1576 | *flagp |= HASWIDTH|SIMPLE; |
1577 | nextchar(); | |
1578 | break; | |
1579 | case 'W': | |
c277df42 | 1580 | ret = reg_node((regflags & PMf_LOCALE) ? NALNUML : NALNUM); |
a0d0e21e LW |
1581 | *flagp |= HASWIDTH|SIMPLE; |
1582 | nextchar(); | |
1583 | break; | |
1584 | case 'b': | |
c277df42 IZ |
1585 | seen_zerolen++; |
1586 | ret = reg_node((regflags & PMf_LOCALE) ? BOUNDL : BOUND); | |
a0d0e21e LW |
1587 | *flagp |= SIMPLE; |
1588 | nextchar(); | |
1589 | break; | |
1590 | case 'B': | |
c277df42 IZ |
1591 | seen_zerolen++; |
1592 | ret = reg_node((regflags & PMf_LOCALE) ? NBOUNDL : NBOUND); | |
a0d0e21e LW |
1593 | *flagp |= SIMPLE; |
1594 | nextchar(); | |
1595 | break; | |
1596 | case 's': | |
c277df42 | 1597 | ret = reg_node((regflags & PMf_LOCALE) ? SPACEL : SPACE); |
a0d0e21e LW |
1598 | *flagp |= HASWIDTH|SIMPLE; |
1599 | nextchar(); | |
1600 | break; | |
1601 | case 'S': | |
c277df42 | 1602 | ret = reg_node((regflags & PMf_LOCALE) ? NSPACEL : NSPACE); |
a0d0e21e LW |
1603 | *flagp |= HASWIDTH|SIMPLE; |
1604 | nextchar(); | |
1605 | break; | |
1606 | case 'd': | |
c277df42 | 1607 | ret = reg_node(DIGIT); |
a0d0e21e LW |
1608 | *flagp |= HASWIDTH|SIMPLE; |
1609 | nextchar(); | |
1610 | break; | |
1611 | case 'D': | |
c277df42 | 1612 | ret = reg_node(NDIGIT); |
a0d0e21e LW |
1613 | *flagp |= HASWIDTH|SIMPLE; |
1614 | nextchar(); | |
1615 | break; | |
1616 | case 'n': | |
1617 | case 'r': | |
1618 | case 't': | |
1619 | case 'f': | |
1620 | case 'e': | |
1621 | case 'a': | |
1622 | case 'x': | |
1623 | case 'c': | |
1624 | case '0': | |
1625 | goto defchar; | |
1626 | case '1': case '2': case '3': case '4': | |
1627 | case '5': case '6': case '7': case '8': case '9': | |
1628 | { | |
1629 | I32 num = atoi(regparse); | |
1630 | ||
1631 | if (num > 9 && num >= regnpar) | |
1632 | goto defchar; | |
1633 | else { | |
1634 | regsawback = 1; | |
c8756f30 AK |
1635 | ret = reganode((regflags & PMf_FOLD) |
1636 | ? ((regflags & PMf_LOCALE) ? REFFL : REFF) | |
1637 | : REF, num); | |
a0d0e21e LW |
1638 | *flagp |= HASWIDTH; |
1639 | while (isDIGIT(*regparse)) | |
a687059c | 1640 | regparse++; |
a0d0e21e LW |
1641 | regparse--; |
1642 | nextchar(); | |
1643 | } | |
1644 | } | |
1645 | break; | |
1646 | case '\0': | |
1647 | if (regparse >= regxend) | |
1648 | FAIL("trailing \\ in regexp"); | |
1649 | /* FALL THROUGH */ | |
1650 | default: | |
1651 | goto defchar; | |
1652 | } | |
1653 | break; | |
4633a7c4 LW |
1654 | |
1655 | case '#': | |
1656 | if (regflags & PMf_EXTENDED) { | |
1657 | while (regparse < regxend && *regparse != '\n') regparse++; | |
1658 | if (regparse < regxend) | |
1659 | goto tryagain; | |
1660 | } | |
1661 | /* FALL THROUGH */ | |
1662 | ||
a0d0e21e LW |
1663 | default: { |
1664 | register I32 len; | |
c277df42 | 1665 | register U8 ender; |
a0d0e21e | 1666 | register char *p; |
c277df42 | 1667 | char *oldp, *s; |
a0d0e21e LW |
1668 | I32 numlen; |
1669 | ||
1670 | regparse++; | |
1671 | ||
1672 | defchar: | |
c277df42 | 1673 | ret = reg_node((regflags & PMf_FOLD) |
bbce6d69 | 1674 | ? ((regflags & PMf_LOCALE) ? EXACTFL : EXACTF) |
1675 | : EXACT); | |
c277df42 IZ |
1676 | s = OPERAND(ret); |
1677 | regc(0, s++); /* save spot for len */ | |
a0d0e21e LW |
1678 | for (len = 0, p = regparse - 1; |
1679 | len < 127 && p < regxend; | |
1680 | len++) | |
1681 | { | |
1682 | oldp = p; | |
5b5a24f7 CS |
1683 | |
1684 | if (regflags & PMf_EXTENDED) | |
1685 | p = regwhite(p, regxend); | |
a0d0e21e LW |
1686 | switch (*p) { |
1687 | case '^': | |
1688 | case '$': | |
1689 | case '.': | |
1690 | case '[': | |
1691 | case '(': | |
1692 | case ')': | |
1693 | case '|': | |
1694 | goto loopdone; | |
1695 | case '\\': | |
1696 | switch (*++p) { | |
1697 | case 'A': | |
1698 | case 'G': | |
1699 | case 'Z': | |
1700 | case 'w': | |
1701 | case 'W': | |
1702 | case 'b': | |
1703 | case 'B': | |
1704 | case 's': | |
1705 | case 'S': | |
1706 | case 'd': | |
1707 | case 'D': | |
1708 | --p; | |
1709 | goto loopdone; | |
1710 | case 'n': | |
1711 | ender = '\n'; | |
1712 | p++; | |
a687059c | 1713 | break; |
a0d0e21e LW |
1714 | case 'r': |
1715 | ender = '\r'; | |
1716 | p++; | |
a687059c | 1717 | break; |
a0d0e21e LW |
1718 | case 't': |
1719 | ender = '\t'; | |
1720 | p++; | |
a687059c | 1721 | break; |
a0d0e21e LW |
1722 | case 'f': |
1723 | ender = '\f'; | |
1724 | p++; | |
a687059c | 1725 | break; |
a0d0e21e LW |
1726 | case 'e': |
1727 | ender = '\033'; | |
1728 | p++; | |
a687059c | 1729 | break; |
a0d0e21e LW |
1730 | case 'a': |
1731 | ender = '\007'; | |
1732 | p++; | |
a687059c | 1733 | break; |
a0d0e21e LW |
1734 | case 'x': |
1735 | ender = scan_hex(++p, 2, &numlen); | |
1736 | p += numlen; | |
a687059c | 1737 | break; |
a0d0e21e LW |
1738 | case 'c': |
1739 | p++; | |
bbce6d69 | 1740 | ender = UCHARAT(p++); |
1741 | ender = toCTRL(ender); | |
a687059c | 1742 | break; |
a0d0e21e LW |
1743 | case '0': case '1': case '2': case '3':case '4': |
1744 | case '5': case '6': case '7': case '8':case '9': | |
1745 | if (*p == '0' || | |
1746 | (isDIGIT(p[1]) && atoi(p) >= regnpar) ) { | |
1747 | ender = scan_oct(p, 3, &numlen); | |
1748 | p += numlen; | |
1749 | } | |
1750 | else { | |
1751 | --p; | |
1752 | goto loopdone; | |
a687059c LW |
1753 | } |
1754 | break; | |
a0d0e21e LW |
1755 | case '\0': |
1756 | if (p >= regxend) | |
a687059c LW |
1757 | FAIL("trailing \\ in regexp"); |
1758 | /* FALL THROUGH */ | |
a0d0e21e LW |
1759 | default: |
1760 | ender = *p++; | |
1761 | break; | |
1762 | } | |
1763 | break; | |
a687059c | 1764 | default: |
a0d0e21e LW |
1765 | ender = *p++; |
1766 | break; | |
a687059c | 1767 | } |
5b5a24f7 CS |
1768 | if (regflags & PMf_EXTENDED) |
1769 | p = regwhite(p, regxend); | |
a0d0e21e LW |
1770 | if (ISMULT2(p)) { /* Back off on ?+*. */ |
1771 | if (len) | |
1772 | p = oldp; | |
1773 | else { | |
1774 | len++; | |
c277df42 | 1775 | regc(ender, s++); |
a0d0e21e LW |
1776 | } |
1777 | break; | |
a687059c | 1778 | } |
c277df42 | 1779 | regc(ender, s++); |
a0d0e21e LW |
1780 | } |
1781 | loopdone: | |
1782 | regparse = p - 1; | |
1783 | nextchar(); | |
1784 | if (len < 0) | |
1785 | FAIL("internal disaster in regexp"); | |
1786 | if (len > 0) | |
1787 | *flagp |= HASWIDTH; | |
1788 | if (len == 1) | |
1789 | *flagp |= SIMPLE; | |
c277df42 | 1790 | if (!SIZE_ONLY) |
a0d0e21e | 1791 | *OPERAND(ret) = len; |
c277df42 IZ |
1792 | regc('\0', s++); |
1793 | if (SIZE_ONLY) { | |
1794 | #ifdef REGALIGN_STRUCT | |
1795 | regsize += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); | |
1796 | #endif | |
1797 | } else { | |
1798 | regcode += (len + 2 + sizeof(regnode) - 1) / sizeof(regnode); | |
1799 | } | |
a687059c | 1800 | } |
a0d0e21e LW |
1801 | break; |
1802 | } | |
a687059c | 1803 | |
a0d0e21e | 1804 | return(ret); |
a687059c LW |
1805 | } |
1806 | ||
5b5a24f7 | 1807 | static char * |
8ac85365 | 1808 | regwhite(char *p, char *e) |
5b5a24f7 CS |
1809 | { |
1810 | while (p < e) { | |
1811 | if (isSPACE(*p)) | |
1812 | ++p; | |
1813 | else if (*p == '#') { | |
1814 | do { | |
1815 | p++; | |
1816 | } while (p < e && *p != '\n'); | |
1817 | } | |
1818 | else | |
1819 | break; | |
1820 | } | |
1821 | return p; | |
1822 | } | |
1823 | ||
a687059c | 1824 | static void |
8ac85365 | 1825 | regset(char *opnd, register I32 c) |
a687059c | 1826 | { |
c277df42 | 1827 | if (SIZE_ONLY) |
bbce6d69 | 1828 | return; |
1829 | c &= 0xFF; | |
1830 | opnd[1 + (c >> 3)] |= (1 << (c & 7)); | |
a687059c LW |
1831 | } |
1832 | ||
c277df42 | 1833 | static regnode * |
8ac85365 | 1834 | regclass(void) |
a687059c | 1835 | { |
c277df42 | 1836 | register char *opnd, *s; |
8ac85365 | 1837 | register I32 Class; |
a0d0e21e LW |
1838 | register I32 lastclass = 1234; |
1839 | register I32 range = 0; | |
c277df42 | 1840 | register regnode *ret; |
a0d0e21e LW |
1841 | register I32 def; |
1842 | I32 numlen; | |
1843 | ||
c277df42 IZ |
1844 | s = opnd = OPERAND(regcode); |
1845 | ret = reg_node(ANYOF); | |
8ac85365 | 1846 | for (Class = 0; Class < 33; Class++) |
c277df42 | 1847 | regc(0, s++); |
a0d0e21e LW |
1848 | if (*regparse == '^') { /* Complement of range. */ |
1849 | regnaughty++; | |
1850 | regparse++; | |
c277df42 | 1851 | if (!SIZE_ONLY) |
bbce6d69 | 1852 | *opnd |= ANYOF_INVERT; |
1853 | } | |
c277df42 IZ |
1854 | if (!SIZE_ONLY) { |
1855 | regcode += ANY_SKIP; | |
bbce6d69 | 1856 | if (regflags & PMf_FOLD) |
1857 | *opnd |= ANYOF_FOLD; | |
1858 | if (regflags & PMf_LOCALE) | |
1859 | *opnd |= ANYOF_LOCALE; | |
c277df42 IZ |
1860 | } else { |
1861 | regsize += ANY_SKIP; | |
a0d0e21e | 1862 | } |
a0d0e21e LW |
1863 | if (*regparse == ']' || *regparse == '-') |
1864 | goto skipcond; /* allow 1st char to be ] or - */ | |
1865 | while (regparse < regxend && *regparse != ']') { | |
1866 | skipcond: | |
8ac85365 NIS |
1867 | Class = UCHARAT(regparse++); |
1868 | if (Class == '\\') { | |
1869 | Class = UCHARAT(regparse++); | |
1870 | switch (Class) { | |
a0d0e21e | 1871 | case 'w': |
bbce6d69 | 1872 | if (regflags & PMf_LOCALE) { |
c277df42 | 1873 | if (!SIZE_ONLY) |
bbce6d69 | 1874 | *opnd |= ANYOF_ALNUML; |
1875 | } | |
1876 | else { | |
8ac85365 NIS |
1877 | for (Class = 0; Class < 256; Class++) |
1878 | if (isALNUM(Class)) | |
1879 | regset(opnd, Class); | |
bbce6d69 | 1880 | } |
a0d0e21e LW |
1881 | lastclass = 1234; |
1882 | continue; | |
1883 | case 'W': | |
bbce6d69 | 1884 | if (regflags & PMf_LOCALE) { |
c277df42 | 1885 | if (!SIZE_ONLY) |
bbce6d69 | 1886 | *opnd |= ANYOF_NALNUML; |
1887 | } | |
1888 | else { | |
8ac85365 NIS |
1889 | for (Class = 0; Class < 256; Class++) |
1890 | if (!isALNUM(Class)) | |
1891 | regset(opnd, Class); | |
bbce6d69 | 1892 | } |
a0d0e21e LW |
1893 | lastclass = 1234; |
1894 | continue; | |
1895 | case 's': | |
bbce6d69 | 1896 | if (regflags & PMf_LOCALE) { |
c277df42 | 1897 | if (!SIZE_ONLY) |
bbce6d69 | 1898 | *opnd |= ANYOF_SPACEL; |
1899 | } | |
1900 | else { | |
8ac85365 NIS |
1901 | for (Class = 0; Class < 256; Class++) |
1902 | if (isSPACE(Class)) | |
1903 | regset(opnd, Class); | |
bbce6d69 | 1904 | } |
a0d0e21e LW |
1905 | lastclass = 1234; |
1906 | continue; | |
1907 | case 'S': | |
bbce6d69 | 1908 | if (regflags & PMf_LOCALE) { |
c277df42 | 1909 | if (!SIZE_ONLY) |
bbce6d69 | 1910 | *opnd |= ANYOF_NSPACEL; |
1911 | } | |
1912 | else { | |
8ac85365 NIS |
1913 | for (Class = 0; Class < 256; Class++) |
1914 | if (!isSPACE(Class)) | |
1915 | regset(opnd, Class); | |
bbce6d69 | 1916 | } |
a0d0e21e LW |
1917 | lastclass = 1234; |
1918 | continue; | |
1919 | case 'd': | |
8ac85365 NIS |
1920 | for (Class = '0'; Class <= '9'; Class++) |
1921 | regset(opnd, Class); | |
a0d0e21e LW |
1922 | lastclass = 1234; |
1923 | continue; | |
1924 | case 'D': | |
8ac85365 NIS |
1925 | for (Class = 0; Class < '0'; Class++) |
1926 | regset(opnd, Class); | |
1927 | for (Class = '9' + 1; Class < 256; Class++) | |
1928 | regset(opnd, Class); | |
a0d0e21e LW |
1929 | lastclass = 1234; |
1930 | continue; | |
1931 | case 'n': | |
8ac85365 | 1932 | Class = '\n'; |
a0d0e21e LW |
1933 | break; |
1934 | case 'r': | |
8ac85365 | 1935 | Class = '\r'; |
a0d0e21e LW |
1936 | break; |
1937 | case 't': | |
8ac85365 | 1938 | Class = '\t'; |
a0d0e21e LW |
1939 | break; |
1940 | case 'f': | |
8ac85365 | 1941 | Class = '\f'; |
a0d0e21e LW |
1942 | break; |
1943 | case 'b': | |
8ac85365 | 1944 | Class = '\b'; |
a0d0e21e LW |
1945 | break; |
1946 | case 'e': | |
8ac85365 | 1947 | Class = '\033'; |
a0d0e21e LW |
1948 | break; |
1949 | case 'a': | |
8ac85365 | 1950 | Class = '\007'; |
a0d0e21e LW |
1951 | break; |
1952 | case 'x': | |
8ac85365 | 1953 | Class = scan_hex(regparse, 2, &numlen); |
a0d0e21e LW |
1954 | regparse += numlen; |
1955 | break; | |
1956 | case 'c': | |
8ac85365 NIS |
1957 | Class = UCHARAT(regparse++); |
1958 | Class = toCTRL(Class); | |
a0d0e21e LW |
1959 | break; |
1960 | case '0': case '1': case '2': case '3': case '4': | |
1961 | case '5': case '6': case '7': case '8': case '9': | |
8ac85365 | 1962 | Class = scan_oct(--regparse, 3, &numlen); |
a0d0e21e LW |
1963 | regparse += numlen; |
1964 | break; | |
1965 | } | |
1966 | } | |
1967 | if (range) { | |
8ac85365 | 1968 | if (lastclass > Class) |
a0d0e21e LW |
1969 | FAIL("invalid [] range in regexp"); |
1970 | range = 0; | |
1971 | } | |
1972 | else { | |
8ac85365 | 1973 | lastclass = Class; |
a0d0e21e LW |
1974 | if (*regparse == '-' && regparse+1 < regxend && |
1975 | regparse[1] != ']') { | |
a687059c | 1976 | regparse++; |
a0d0e21e LW |
1977 | range = 1; |
1978 | continue; /* do it next time */ | |
1979 | } | |
a687059c | 1980 | } |
8ac85365 | 1981 | for ( ; lastclass <= Class; lastclass++) |
bbce6d69 | 1982 | regset(opnd, lastclass); |
8ac85365 | 1983 | lastclass = Class; |
a0d0e21e LW |
1984 | } |
1985 | if (*regparse != ']') | |
1986 | FAIL("unmatched [] in regexp"); | |
1987 | nextchar(); | |
1988 | return ret; | |
1989 | } | |
1990 | ||
1991 | static char* | |
8ac85365 | 1992 | nextchar(void) |
a0d0e21e LW |
1993 | { |
1994 | char* retval = regparse++; | |
1995 | ||
4633a7c4 LW |
1996 | for (;;) { |
1997 | if (*regparse == '(' && regparse[1] == '?' && | |
1998 | regparse[2] == '#') { | |
1999 | while (*regparse && *regparse != ')') | |
748a9306 | 2000 | regparse++; |
4633a7c4 LW |
2001 | regparse++; |
2002 | continue; | |
2003 | } | |
2004 | if (regflags & PMf_EXTENDED) { | |
2005 | if (isSPACE(*regparse)) { | |
748a9306 LW |
2006 | regparse++; |
2007 | continue; | |
2008 | } | |
2009 | else if (*regparse == '#') { | |
2010 | while (*regparse && *regparse != '\n') | |
2011 | regparse++; | |
2012 | regparse++; | |
2013 | continue; | |
2014 | } | |
748a9306 | 2015 | } |
4633a7c4 | 2016 | return retval; |
a0d0e21e | 2017 | } |
a687059c LW |
2018 | } |
2019 | ||
2020 | /* | |
c277df42 | 2021 | - reg_node - emit a node |
a0d0e21e | 2022 | */ |
c277df42 | 2023 | static regnode * /* Location. */ |
a0d0e21e | 2024 | #ifdef CAN_PROTOTYPE |
c277df42 | 2025 | reg_node(U8 op) |
a0d0e21e | 2026 | #else |
c277df42 IZ |
2027 | reg_node(op) |
2028 | U8 op; | |
a0d0e21e | 2029 | #endif |
a687059c | 2030 | { |
c277df42 IZ |
2031 | register regnode *ret; |
2032 | register regnode *ptr; | |
a687059c | 2033 | |
a0d0e21e | 2034 | ret = regcode; |
c277df42 IZ |
2035 | if (SIZE_ONLY) { |
2036 | SIZE_ALIGN(regsize); | |
2037 | #ifdef REGALIGN_STRUCT | |
2038 | regsize += 1; | |
2039 | #else | |
a0d0e21e | 2040 | regsize += 3; |
c277df42 | 2041 | #endif |
a0d0e21e LW |
2042 | return(ret); |
2043 | } | |
a687059c | 2044 | |
c277df42 | 2045 | NODE_ALIGN_FILL(ret); |
a0d0e21e | 2046 | ptr = ret; |
c277df42 | 2047 | FILL_ADVANCE_NODE(ptr, op); |
a0d0e21e | 2048 | regcode = ptr; |
a687059c | 2049 | |
a0d0e21e | 2050 | return(ret); |
a687059c LW |
2051 | } |
2052 | ||
2053 | /* | |
a0d0e21e LW |
2054 | - reganode - emit a node with an argument |
2055 | */ | |
c277df42 | 2056 | static regnode * /* Location. */ |
a0d0e21e | 2057 | #ifdef CAN_PROTOTYPE |
c277df42 | 2058 | reganode(U8 op, U32 arg) |
a0d0e21e | 2059 | #else |
fe14fcc3 | 2060 | reganode(op, arg) |
c277df42 IZ |
2061 | U8 op; |
2062 | U32 arg; | |
a0d0e21e | 2063 | #endif |
fe14fcc3 | 2064 | { |
c277df42 IZ |
2065 | register regnode *ret; |
2066 | register regnode *ptr; | |
fe14fcc3 | 2067 | |
a0d0e21e | 2068 | ret = regcode; |
c277df42 IZ |
2069 | if (SIZE_ONLY) { |
2070 | SIZE_ALIGN(regsize); | |
fe14fcc3 | 2071 | #ifdef REGALIGN |
c277df42 IZ |
2072 | regsize += 2; |
2073 | #else | |
a0d0e21e | 2074 | regsize += 5; |
c277df42 | 2075 | #endif |
a0d0e21e LW |
2076 | return(ret); |
2077 | } | |
fe14fcc3 | 2078 | |
c277df42 | 2079 | NODE_ALIGN_FILL(ret); |
a0d0e21e | 2080 | ptr = ret; |
c277df42 | 2081 | FILL_ADVANCE_NODE_ARG(ptr, op, arg); |
a0d0e21e | 2082 | regcode = ptr; |
fe14fcc3 | 2083 | |
a0d0e21e | 2084 | return(ret); |
fe14fcc3 LW |
2085 | } |
2086 | ||
2087 | /* | |
a0d0e21e LW |
2088 | - regc - emit (if appropriate) a byte of code |
2089 | */ | |
2090 | #ifdef CAN_PROTOTYPE | |
2091 | static void | |
c277df42 | 2092 | regc(U8 b, char* s) |
a0d0e21e | 2093 | #else |
a687059c | 2094 | static void |
c277df42 IZ |
2095 | regc(b, s) |
2096 | U8 b; | |
2097 | char *s; | |
a0d0e21e | 2098 | #endif |
a687059c | 2099 | { |
c277df42 IZ |
2100 | if (!SIZE_ONLY) |
2101 | *s = b; | |
a687059c LW |
2102 | } |
2103 | ||
2104 | /* | |
a0d0e21e LW |
2105 | - reginsert - insert an operator in front of already-emitted operand |
2106 | * | |
2107 | * Means relocating the operand. | |
2108 | */ | |
2109 | #ifdef CAN_PROTOTYPE | |
2110 | static void | |
c277df42 | 2111 | reginsert(U8 op, regnode *opnd) |
a0d0e21e | 2112 | #else |
a687059c LW |
2113 | static void |
2114 | reginsert(op, opnd) | |
c277df42 IZ |
2115 | U8 op; |
2116 | regnode *opnd; | |
a0d0e21e | 2117 | #endif |
a687059c | 2118 | { |
c277df42 IZ |
2119 | register regnode *src; |
2120 | register regnode *dst; | |
2121 | register regnode *place; | |
2122 | register int offset = regarglen[(U8)op]; | |
2123 | ||
2124 | /* (regkind[(U8)op] == CURLY ? EXTRA_STEP_2ARGS : 0); */ | |
2125 | ||
2126 | if (SIZE_ONLY) { | |
2127 | regsize += NODE_STEP_REGNODE + offset; | |
a0d0e21e LW |
2128 | return; |
2129 | } | |
a687059c | 2130 | |
a0d0e21e | 2131 | src = regcode; |
c277df42 | 2132 | regcode += NODE_STEP_REGNODE + offset; |
a0d0e21e LW |
2133 | dst = regcode; |
2134 | while (src > opnd) | |
c277df42 | 2135 | StructCopy(--src, --dst, regnode); |
a0d0e21e LW |
2136 | |
2137 | place = opnd; /* Op node, where operand used to be. */ | |
c277df42 IZ |
2138 | src = NEXTOPER(place); |
2139 | FILL_ADVANCE_NODE(place, op); | |
2140 | Zero(src, offset, regnode); | |
2141 | #if defined(REGALIGN) && !defined(REGALIGN_STRUCT) | |
2142 | src[offset + 1] = '\177'; | |
2b69d0c2 | 2143 | #endif |
a687059c LW |
2144 | } |
2145 | ||
2146 | /* | |
c277df42 | 2147 | - regtail - set the next-pointer at the end of a node chain of p to val. |
a0d0e21e | 2148 | */ |
a687059c | 2149 | static void |
c277df42 | 2150 | regtail(regnode *p, regnode *val) |
a687059c | 2151 | { |
c277df42 IZ |
2152 | register regnode *scan; |
2153 | register regnode *temp; | |
a0d0e21e LW |
2154 | register I32 offset; |
2155 | ||
c277df42 | 2156 | if (SIZE_ONLY) |
a0d0e21e LW |
2157 | return; |
2158 | ||
2159 | /* Find last node. */ | |
2160 | scan = p; | |
2161 | for (;;) { | |
2162 | temp = regnext(scan); | |
2163 | if (temp == NULL) | |
2164 | break; | |
2165 | scan = temp; | |
2166 | } | |
a687059c LW |
2167 | |
2168 | #ifdef REGALIGN | |
c277df42 IZ |
2169 | # ifdef REGALIGN_STRUCT |
2170 | if (reg_off_by_arg[OP(scan)]) { | |
2171 | ARG_SET(scan, val - scan); | |
2172 | } else { | |
2173 | NEXT_OFF(scan) = val - scan; | |
2174 | } | |
2175 | # else | |
a0d0e21e | 2176 | offset = val - scan; |
c277df42 | 2177 | # ifndef lint |
a0d0e21e | 2178 | *(short*)(scan+1) = offset; |
c277df42 IZ |
2179 | # endif |
2180 | #endif | |
a687059c | 2181 | #else |
a0d0e21e LW |
2182 | if (OP(scan) == BACK) |
2183 | offset = scan - val; | |
2184 | else | |
2185 | offset = val - scan; | |
2186 | *(scan+1) = (offset>>8)&0377; | |
2187 | *(scan+2) = offset&0377; | |
a687059c LW |
2188 | #endif |
2189 | } | |
2190 | ||
2191 | /* | |
a0d0e21e LW |
2192 | - regoptail - regtail on operand of first argument; nop if operandless |
2193 | */ | |
a687059c | 2194 | static void |
c277df42 | 2195 | regoptail(regnode *p, regnode *val) |
a687059c | 2196 | { |
a0d0e21e | 2197 | /* "Operandless" and "op != BRANCH" are synonymous in practice. */ |
c277df42 IZ |
2198 | if (p == NULL || SIZE_ONLY) |
2199 | return; | |
2200 | if (regkind[(U8)OP(p)] == BRANCH) { | |
2201 | regtail(NEXTOPER(p), val); | |
2202 | } else if ( regkind[(U8)OP(p)] == BRANCHJ) { | |
2203 | regtail(NEXTOPER(NEXTOPER(p)), val); | |
2204 | } else | |
a0d0e21e | 2205 | return; |
a687059c LW |
2206 | } |
2207 | ||
2208 | /* | |
2209 | - regcurly - a little FSA that accepts {\d+,?\d*} | |
2210 | */ | |
79072805 | 2211 | STATIC I32 |
8ac85365 | 2212 | regcurly(register char *s) |
a687059c LW |
2213 | { |
2214 | if (*s++ != '{') | |
2215 | return FALSE; | |
f0fcb552 | 2216 | if (!isDIGIT(*s)) |
a687059c | 2217 | return FALSE; |
f0fcb552 | 2218 | while (isDIGIT(*s)) |
a687059c LW |
2219 | s++; |
2220 | if (*s == ',') | |
2221 | s++; | |
f0fcb552 | 2222 | while (isDIGIT(*s)) |
a687059c LW |
2223 | s++; |
2224 | if (*s != '}') | |
2225 | return FALSE; | |
2226 | return TRUE; | |
2227 | } | |
2228 | ||
2229 | #ifdef DEBUGGING | |
2230 | ||
c277df42 IZ |
2231 | static regnode * |
2232 | dumpuntil(regnode *start, regnode *node, regnode *last, SV* sv, I32 l) | |
2233 | { | |
2234 | register char op = EXACT; /* Arbitrary non-END op. */ | |
2235 | register regnode *next, *onode; | |
2236 | ||
2237 | while (op != END && (!last || node < last)) { | |
2238 | /* While that wasn't END last time... */ | |
2239 | ||
2240 | NODE_ALIGN(node); | |
2241 | op = OP(node); | |
2242 | if (op == CLOSE) | |
2243 | l--; | |
2244 | next = regnext(node); | |
2245 | /* Where, what. */ | |
2246 | if (OP(node) == OPTIMIZED) | |
2247 | goto after_print; | |
2248 | regprop(sv, node); | |
2249 | PerlIO_printf(Perl_debug_log, "%4d%*s%s", node - start, | |
2250 | 2*l + 1, "", SvPVX(sv)); | |
2251 | if (next == NULL) /* Next ptr. */ | |
2252 | PerlIO_printf(Perl_debug_log, "(0)"); | |
2253 | else | |
2254 | PerlIO_printf(Perl_debug_log, "(%d)", next - start); | |
2255 | (void)PerlIO_putc(Perl_debug_log, '\n'); | |
2256 | after_print: | |
2257 | if (regkind[(U8)op] == BRANCHJ) { | |
2258 | register regnode *nnode = (OP(next) == LONGJMP | |
2259 | ? regnext(next) | |
2260 | : next); | |
2261 | if (last && nnode > last) | |
2262 | nnode = last; | |
2263 | node = dumpuntil(start, NEXTOPER(NEXTOPER(node)), nnode, sv, l + 1); | |
2264 | } else if (regkind[(U8)op] == BRANCH) { | |
2265 | node = dumpuntil(start, NEXTOPER(node), next, sv, l + 1); | |
2266 | } else if ( op == CURLY) { /* `next' might be very big: optimizer */ | |
2267 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, | |
2268 | NEXTOPER(node) + EXTRA_STEP_2ARGS + 1, sv, l + 1); | |
2269 | } else if (regkind[(U8)op] == CURLY && op != CURLYX) { | |
2270 | node = dumpuntil(start, NEXTOPER(node) + EXTRA_STEP_2ARGS, | |
2271 | next, sv, l + 1); | |
2272 | } else if ( op == PLUS || op == STAR) { | |
2273 | node = dumpuntil(start, NEXTOPER(node), NEXTOPER(node) + 1, sv, l + 1); | |
2274 | } else if (op == ANYOF) { | |
2275 | node = NEXTOPER(node); | |
2276 | node += ANY_SKIP; | |
2277 | } else if (regkind[(U8)op] == EXACT) { | |
2278 | /* Literal string, where present. */ | |
2279 | node += ((*OPERAND(node)) + 2 + sizeof(regnode) - 1) / sizeof(regnode); | |
2280 | node = NEXTOPER(node); | |
2281 | } else { | |
2282 | node = NEXTOPER(node); | |
2283 | node += regarglen[(U8)op]; | |
2284 | } | |
2285 | if (op == CURLYX || op == OPEN) | |
2286 | l++; | |
2287 | else if (op == WHILEM) | |
2288 | l--; | |
2289 | } | |
2290 | return node; | |
2291 | } | |
2292 | ||
a687059c | 2293 | /* |
fd181c75 | 2294 | - regdump - dump a regexp onto Perl_debug_log in vaguely comprehensible form |
a687059c LW |
2295 | */ |
2296 | void | |
8ac85365 | 2297 | regdump(regexp *r) |
a687059c | 2298 | { |
46fc3d4c | 2299 | SV *sv = sv_newmortal(); |
a687059c | 2300 | |
c277df42 | 2301 | (void)dumpuntil(r->program, r->program + 1, NULL, sv, 0); |
a0d0e21e LW |
2302 | |
2303 | /* Header fields of interest. */ | |
c277df42 IZ |
2304 | if (r->anchored_substr) |
2305 | PerlIO_printf(Perl_debug_log, "anchored `%s%s%s'%s at %d ", | |
2306 | colors[0], | |
2307 | SvPVX(r->anchored_substr), | |
2308 | colors[1], | |
2309 | SvTAIL(r->anchored_substr) ? "$" : "", | |
2310 | r->anchored_offset); | |
2311 | if (r->float_substr) | |
2312 | PerlIO_printf(Perl_debug_log, "floating `%s%s%s'%s at %d..%u ", | |
2313 | colors[0], | |
2314 | SvPVX(r->float_substr), | |
2315 | colors[1], | |
2316 | SvTAIL(r->float_substr) ? "$" : "", | |
2317 | r->float_min_offset, r->float_max_offset); | |
2318 | if (r->check_substr) | |
2319 | PerlIO_printf(Perl_debug_log, | |
2320 | r->check_substr == r->float_substr | |
2321 | ? "(checking floating" : "(checking anchored"); | |
2322 | if (r->reganch & ROPT_NOSCAN) | |
2323 | PerlIO_printf(Perl_debug_log, " noscan"); | |
2324 | if (r->reganch & ROPT_CHECK_ALL) | |
2325 | PerlIO_printf(Perl_debug_log, " isall"); | |
2326 | if (r->check_substr) | |
2327 | PerlIO_printf(Perl_debug_log, ") "); | |
2328 | ||
46fc3d4c | 2329 | if (r->regstclass) { |
2330 | regprop(sv, r->regstclass); | |
2331 | PerlIO_printf(Perl_debug_log, "stclass `%s' ", SvPVX(sv)); | |
2332 | } | |
774d564b | 2333 | if (r->reganch & ROPT_ANCH) { |
2334 | PerlIO_printf(Perl_debug_log, "anchored"); | |
2335 | if (r->reganch & ROPT_ANCH_BOL) | |
2336 | PerlIO_printf(Perl_debug_log, "(BOL)"); | |
c277df42 IZ |
2337 | if (r->reganch & ROPT_ANCH_MBOL) |
2338 | PerlIO_printf(Perl_debug_log, "(MBOL)"); | |
774d564b | 2339 | if (r->reganch & ROPT_ANCH_GPOS) |
2340 | PerlIO_printf(Perl_debug_log, "(GPOS)"); | |
2341 | PerlIO_putc(Perl_debug_log, ' '); | |
2342 | } | |
c277df42 IZ |
2343 | if (r->reganch & ROPT_GPOS_SEEN) |
2344 | PerlIO_printf(Perl_debug_log, "GPOS "); | |
a0d0e21e | 2345 | if (r->reganch & ROPT_SKIP) |
760ac839 | 2346 | PerlIO_printf(Perl_debug_log, "plus "); |
a0d0e21e | 2347 | if (r->reganch & ROPT_IMPLICIT) |
760ac839 | 2348 | PerlIO_printf(Perl_debug_log, "implicit "); |
760ac839 LW |
2349 | PerlIO_printf(Perl_debug_log, "minlen %ld ", (long) r->minlen); |
2350 | PerlIO_printf(Perl_debug_log, "\n"); | |
a687059c LW |
2351 | } |
2352 | ||
2353 | /* | |
a0d0e21e LW |
2354 | - regprop - printable representation of opcode |
2355 | */ | |
46fc3d4c | 2356 | void |
c277df42 | 2357 | regprop(SV *sv, regnode *o) |
a687059c | 2358 | { |
a0d0e21e LW |
2359 | register char *p = 0; |
2360 | ||
46fc3d4c | 2361 | sv_setpv(sv, ":"); |
11343788 | 2362 | switch (OP(o)) { |
a0d0e21e LW |
2363 | case BOL: |
2364 | p = "BOL"; | |
2365 | break; | |
2366 | case MBOL: | |
2367 | p = "MBOL"; | |
2368 | break; | |
2369 | case SBOL: | |
2370 | p = "SBOL"; | |
2371 | break; | |
2372 | case EOL: | |
2373 | p = "EOL"; | |
2374 | break; | |
2375 | case MEOL: | |
2376 | p = "MEOL"; | |
2377 | break; | |
2378 | case SEOL: | |
2379 | p = "SEOL"; | |
2380 | break; | |
2381 | case ANY: | |
2382 | p = "ANY"; | |
2383 | break; | |
2384 | case SANY: | |
2385 | p = "SANY"; | |
2386 | break; | |
2387 | case ANYOF: | |
2388 | p = "ANYOF"; | |
2389 | break; | |
2390 | case BRANCH: | |
2391 | p = "BRANCH"; | |
2392 | break; | |
bbce6d69 | 2393 | case EXACT: |
c277df42 | 2394 | sv_catpvf(sv, "EXACT <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
bbce6d69 | 2395 | break; |
2396 | case EXACTF: | |
c277df42 | 2397 | sv_catpvf(sv, "EXACTF <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
bbce6d69 | 2398 | break; |
2399 | case EXACTFL: | |
c277df42 | 2400 | sv_catpvf(sv, "EXACTFL <%s%s%s>", colors[0], OPERAND(o) + 1, colors[1]); |
a0d0e21e LW |
2401 | break; |
2402 | case NOTHING: | |
2403 | p = "NOTHING"; | |
2404 | break; | |
c277df42 IZ |
2405 | case TAIL: |
2406 | p = "TAIL"; | |
2407 | break; | |
a0d0e21e LW |
2408 | case BACK: |
2409 | p = "BACK"; | |
2410 | break; | |
2411 | case END: | |
2412 | p = "END"; | |
2413 | break; | |
a0d0e21e LW |
2414 | case BOUND: |
2415 | p = "BOUND"; | |
2416 | break; | |
bbce6d69 | 2417 | case BOUNDL: |
2418 | p = "BOUNDL"; | |
2419 | break; | |
a0d0e21e LW |
2420 | case NBOUND: |
2421 | p = "NBOUND"; | |
2422 | break; | |
bbce6d69 | 2423 | case NBOUNDL: |
2424 | p = "NBOUNDL"; | |
a0d0e21e LW |
2425 | break; |
2426 | case CURLY: | |
5dc0d613 | 2427 | sv_catpvf(sv, "CURLY {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e | 2428 | break; |
c277df42 IZ |
2429 | case CURLYM: |
2430 | #ifdef REGALIGN_STRUCT | |
2431 | sv_catpvf(sv, "CURLYM[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); | |
2432 | #else | |
2433 | sv_catpvf(sv, "CURLYM {%d,%d}", ARG1(o), ARG2(o)); | |
2434 | #endif | |
2435 | break; | |
2436 | case CURLYN: | |
2437 | #ifdef REGALIGN_STRUCT | |
2438 | sv_catpvf(sv, "CURLYN[%d] {%d,%d}", o->flags, ARG1(o), ARG2(o)); | |
2439 | #else | |
2440 | sv_catpvf(sv, "CURLYN {%d,%d}", ARG1(o), ARG2(o)); | |
2441 | #endif | |
2442 | break; | |
a0d0e21e | 2443 | case CURLYX: |
5dc0d613 | 2444 | sv_catpvf(sv, "CURLYX {%d,%d}", ARG1(o), ARG2(o)); |
a0d0e21e LW |
2445 | break; |
2446 | case REF: | |
c277df42 | 2447 | sv_catpvf(sv, "REF%d", ARG(o)); |
a0d0e21e | 2448 | break; |
c8756f30 | 2449 | case REFF: |
c277df42 | 2450 | sv_catpvf(sv, "REFF%d", ARG(o)); |
c8756f30 AK |
2451 | break; |
2452 | case REFFL: | |
c277df42 | 2453 | sv_catpvf(sv, "REFFL%d", ARG(o)); |
c8756f30 | 2454 | break; |
a0d0e21e | 2455 | case OPEN: |
c277df42 | 2456 | sv_catpvf(sv, "OPEN%d", ARG(o)); |
a0d0e21e LW |
2457 | break; |
2458 | case CLOSE: | |
c277df42 | 2459 | sv_catpvf(sv, "CLOSE%d", ARG(o)); |
a0d0e21e LW |
2460 | p = NULL; |
2461 | break; | |
2462 | case STAR: | |
2463 | p = "STAR"; | |
2464 | break; | |
2465 | case PLUS: | |
2466 | p = "PLUS"; | |
2467 | break; | |
2468 | case MINMOD: | |
2469 | p = "MINMOD"; | |
2470 | break; | |
774d564b | 2471 | case GPOS: |
2472 | p = "GPOS"; | |
a0d0e21e LW |
2473 | break; |
2474 | case UNLESSM: | |
c277df42 IZ |
2475 | #ifdef REGALIGN_STRUCT |
2476 | sv_catpvf(sv, "UNLESSM[-%d]", o->flags); | |
2477 | #else | |
a0d0e21e | 2478 | p = "UNLESSM"; |
c277df42 | 2479 | #endif |
a0d0e21e LW |
2480 | break; |
2481 | case IFMATCH: | |
c277df42 IZ |
2482 | #ifdef REGALIGN_STRUCT |
2483 | sv_catpvf(sv, "IFMATCH[-%d]", o->flags); | |
2484 | #else | |
a0d0e21e | 2485 | p = "IFMATCH"; |
c277df42 | 2486 | #endif |
a0d0e21e LW |
2487 | break; |
2488 | case SUCCEED: | |
2489 | p = "SUCCEED"; | |
2490 | break; | |
2491 | case WHILEM: | |
2492 | p = "WHILEM"; | |
2493 | break; | |
bbce6d69 | 2494 | case DIGIT: |
2495 | p = "DIGIT"; | |
2496 | break; | |
2497 | case NDIGIT: | |
2498 | p = "NDIGIT"; | |
2499 | break; | |
2500 | case ALNUM: | |
2501 | p = "ALNUM"; | |
2502 | break; | |
2503 | case NALNUM: | |
2504 | p = "NALNUM"; | |
2505 | break; | |
2506 | case SPACE: | |
2507 | p = "SPACE"; | |
2508 | break; | |
2509 | case NSPACE: | |
2510 | p = "NSPACE"; | |
2511 | break; | |
2512 | case ALNUML: | |
2513 | p = "ALNUML"; | |
2514 | break; | |
2515 | case NALNUML: | |
2516 | p = "NALNUML"; | |
2517 | break; | |
2518 | case SPACEL: | |
2519 | p = "SPACEL"; | |
2520 | break; | |
2521 | case NSPACEL: | |
2522 | p = "NSPACEL"; | |
2523 | break; | |
c277df42 IZ |
2524 | case EVAL: |
2525 | p = "EVAL"; | |
2526 | break; | |
2527 | case LONGJMP: | |
2528 | p = "LONGJMP"; | |
2529 | break; | |
2530 | case BRANCHJ: | |
2531 | p = "BRANCHJ"; | |
2532 | break; | |
2533 | case IFTHEN: | |
2534 | p = "IFTHEN"; | |
2535 | break; | |
2536 | case GROUPP: | |
2537 | sv_catpvf(sv, "GROUPP%d", ARG(o)); | |
2538 | break; | |
2539 | case LOGICAL: | |
2540 | p = "LOGICAL"; | |
2541 | break; | |
2542 | case SUSPEND: | |
2543 | p = "SUSPEND"; | |
2544 | break; | |
2545 | case RENUM: | |
2546 | p = "RENUM"; | |
2547 | break; | |
2548 | case OPTIMIZED: | |
2549 | p = "OPTIMIZED"; | |
2550 | break; | |
a0d0e21e LW |
2551 | default: |
2552 | FAIL("corrupted regexp opcode"); | |
2553 | } | |
46fc3d4c | 2554 | if (p) |
2555 | sv_catpv(sv, p); | |
a687059c LW |
2556 | } |
2557 | #endif /* DEBUGGING */ | |
2558 | ||
2b69d0c2 | 2559 | void |
8ac85365 | 2560 | pregfree(struct regexp *r) |
a687059c | 2561 | { |
c277df42 | 2562 | if (!r || (--r->refcnt > 0)) |
a0d0e21e | 2563 | return; |
c277df42 | 2564 | if (r->precomp) |
a0d0e21e | 2565 | Safefree(r->precomp); |
c277df42 | 2566 | if (r->subbase) |
a0d0e21e | 2567 | Safefree(r->subbase); |
c277df42 IZ |
2568 | if (r->anchored_substr) |
2569 | SvREFCNT_dec(r->anchored_substr); | |
2570 | if (r->float_substr) | |
2571 | SvREFCNT_dec(r->float_substr); | |
2572 | if (r->data) { | |
2573 | int n = r->data->count; | |
2574 | while (--n >= 0) { | |
2575 | switch (r->data->what[n]) { | |
2576 | case 's': | |
2577 | SvREFCNT_dec((SV*)r->data->data[n]); | |
2578 | break; | |
2579 | case 'o': | |
2580 | op_free((OP_4tree*)r->data->data[n]); | |
2581 | break; | |
2582 | case 'n': | |
2583 | break; | |
2584 | default: | |
2585 | FAIL2("panic: regfree data code '%c'", r->data->what[n]); | |
2586 | } | |
2587 | } | |
2588 | Safefree(r->data->what); | |
2589 | Safefree(r->data); | |
a0d0e21e LW |
2590 | } |
2591 | Safefree(r->startp); | |
2592 | Safefree(r->endp); | |
2593 | Safefree(r); | |
a687059c | 2594 | } |
c277df42 IZ |
2595 | |
2596 | /* | |
2597 | - regnext - dig the "next" pointer out of a node | |
2598 | * | |
2599 | * [Note, when REGALIGN is defined there are two places in regmatch() | |
2600 | * that bypass this code for speed.] | |
2601 | */ | |
2602 | regnode * | |
2603 | regnext(register regnode *p) | |
2604 | { | |
2605 | register I32 offset; | |
2606 | ||
2607 | if (p == ®dummy) | |
2608 | return(NULL); | |
2609 | ||
2610 | offset = (reg_off_by_arg[OP(p)] ? ARG(p) : NEXT_OFF(p)); | |
2611 | if (offset == 0) | |
2612 | return(NULL); | |
2613 | ||
2614 | #ifdef REGALIGN | |
2615 | return(p+offset); | |
2616 | #else | |
2617 | if (OP(p) == BACK) | |
2618 | return(p-offset); | |
2619 | else | |
2620 | return(p+offset); | |
2621 | #endif | |
2622 | } | |
2623 | ||
2624 | #ifdef I_STDARG | |
2625 | void | |
2626 | re_croak2(const char* pat1,const char* pat2,...) | |
2627 | #else | |
2628 | /*VARARGS0*/ | |
2629 | void | |
2630 | re_croak2(const char* pat1,const char* pat2, va_alist) | |
2631 | const char* pat1; | |
2632 | const char* pat2; | |
2633 | va_dcl | |
2634 | #endif | |
2635 | { | |
2636 | va_list args; | |
2637 | STRLEN l1 = strlen(pat1); | |
2638 | STRLEN l2 = strlen(pat2); | |
2639 | char buf[512]; | |
2640 | char *message; | |
2641 | ||
2642 | if (l1 > 510) | |
2643 | l1 = 510; | |
2644 | if (l1 + l2 > 510) | |
2645 | l2 = 510 - l1; | |
2646 | Copy(pat1, buf, l1 , char); | |
2647 | Copy(pat2, buf + l1, l2 , char); | |
2648 | buf[l1 + l2 + 1] = '\n'; | |
2649 | buf[l1 + l2 + 2] = '\0'; | |
2650 | #ifdef I_STDARG | |
2651 | va_start(args, pat2); | |
2652 | #else | |
2653 | va_start(args); | |
2654 | #endif | |
2655 | message = mess(buf, &args); | |
2656 | va_end(args); | |
2657 | l1 = strlen(message); | |
2658 | if (l1 > 512) | |
2659 | l1 = 512; | |
2660 | Copy(message, buf, l1 , char); | |
2661 | buf[l1] = '\0'; /* Overwrite \n */ | |
2662 | croak("%s", buf); | |
2663 | } | |
2664 |