Commit | Line | Data |
a687059c |
1 | /* NOTE: this is derived from Henry Spencer's regexp code, and should not |
2 | * confused with the original package (see point 3 below). Thanks, Henry! |
3 | */ |
4 | |
5 | /* Additional note: this code is very heavily munged from Henry's version |
6 | * in places. In some spots I've traded clarity for efficiency, so don't |
7 | * blame Henry for some of the lack of readability. |
8 | */ |
9 | |
2b69d0c2 |
10 | /* $RCSfile: regcomp.c,v $$Revision: 4.0.1.5 $$Date: 92/06/08 15:23:36 $ |
a687059c |
11 | * |
12 | * $Log: regcomp.c,v $ |
2b69d0c2 |
13 | * Revision 4.0.1.5 92/06/08 15:23:36 lwall |
14 | * patch20: Perl now distinguishes overlapped copies from non-overlapped |
15 | * patch20: /^stuff/ wrongly assumed an implicit $* == 1 |
16 | * patch20: /x{0}/ was wrongly interpreted as /x{0,}/ |
17 | * patch20: added \W, \S and \D inside /[...]/ |
18 | * |
55204971 |
19 | * Revision 4.0.1.4 91/11/05 22:55:14 lwall |
20 | * patch11: Erratum |
21 | * |
f0fcb552 |
22 | * Revision 4.0.1.3 91/11/05 18:22:28 lwall |
23 | * patch11: minimum match length calculation in regexp is now cumulative |
24 | * patch11: initial .* in pattern had dependency on value of $* |
25 | * patch11: certain patterns made use of garbage pointers from uncleared memory |
26 | * patch11: prepared for ctype implementations that don't define isascii() |
27 | * |
9ef589d8 |
28 | * Revision 4.0.1.2 91/06/07 11:48:24 lwall |
29 | * patch4: new copyright notice |
30 | * patch4: /(x+) \1/ incorrectly optimized to not match "xxx xx" |
31 | * patch4: // wouldn't use previous pattern if it started with a null character |
32 | * |
35c8bce7 |
33 | * Revision 4.0.1.1 91/04/12 09:04:45 lwall |
34 | * patch1: random cleanup in cpp namespace |
35 | * |
fe14fcc3 |
36 | * Revision 4.0 91/03/20 01:39:01 lwall |
37 | * 4.0 baseline. |
a687059c |
38 | * |
39 | */ |
f0fcb552 |
40 | /*SUPPRESS 112*/ |
a687059c |
41 | /* |
42 | * regcomp and regexec -- regsub and regerror are not used in perl |
43 | * |
44 | * Copyright (c) 1986 by University of Toronto. |
45 | * Written by Henry Spencer. Not derived from licensed software. |
46 | * |
47 | * Permission is granted to anyone to use this software for any |
48 | * purpose on any computer system, and to redistribute it freely, |
49 | * subject to the following restrictions: |
50 | * |
51 | * 1. The author is not responsible for the consequences of use of |
52 | * this software, no matter how awful, even if they arise |
53 | * from defects in it. |
54 | * |
55 | * 2. The origin of this software must not be misrepresented, either |
56 | * by explicit claim or by omission. |
57 | * |
58 | * 3. Altered versions must be plainly marked as such, and must not |
59 | * be misrepresented as being the original software. |
60 | * |
61 | * |
62 | **** Alterations to Henry's code are... |
63 | **** |
9ef589d8 |
64 | **** Copyright (c) 1991, Larry Wall |
a687059c |
65 | **** |
9ef589d8 |
66 | **** You may distribute under the terms of either the GNU General Public |
67 | **** License or the Artistic License, as specified in the README file. |
68 | |
a687059c |
69 | * |
70 | * Beware that some of this code is subtly aware of the way operator |
71 | * precedence is structured in regular expressions. Serious changes in |
72 | * regular-expression syntax might require a total rethink. |
73 | */ |
74 | #include "EXTERN.h" |
75 | #include "perl.h" |
76 | #include "INTERN.h" |
77 | #include "regcomp.h" |
78 | |
fe14fcc3 |
79 | #ifdef MSDOS |
80 | # if defined(BUGGY_MSC6) |
81 | /* MSC 6.00A breaks on op/regexp.t test 85 unless we turn this off */ |
82 | # pragma optimize("a",off) |
83 | /* But MSC 6.00A is happy with 'w', for aliases only across function calls*/ |
84 | # pragma optimize("w",on ) |
85 | # endif /* BUGGY_MSC6 */ |
86 | #endif /* MSDOS */ |
87 | |
a687059c |
88 | #ifndef STATIC |
89 | #define STATIC static |
90 | #endif |
91 | |
92 | #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?') |
93 | #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \ |
94 | ((*s) == '{' && regcurly(s))) |
2b69d0c2 |
95 | #ifdef atarist |
96 | #define PERL_META "^$.[()|?+*\\" |
97 | #else |
a687059c |
98 | #define META "^$.[()|?+*\\" |
2b69d0c2 |
99 | #endif |
a687059c |
100 | |
35c8bce7 |
101 | #ifdef SPSTART |
102 | #undef SPSTART /* dratted cpp namespace... */ |
103 | #endif |
a687059c |
104 | /* |
105 | * Flags to be passed up and down. |
106 | */ |
107 | #define HASWIDTH 01 /* Known never to match null string. */ |
108 | #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */ |
109 | #define SPSTART 04 /* Starts with * or +. */ |
110 | #define WORST 0 /* Worst case. */ |
111 | |
112 | /* |
113 | * Global work variables for regcomp(). |
114 | */ |
115 | static char *regprecomp; /* uncompiled string. */ |
116 | static char *regparse; /* Input-scan pointer. */ |
117 | static char *regxend; /* End of input for compile */ |
118 | static int regnpar; /* () count. */ |
119 | static char *regcode; /* Code-emit pointer; ®dummy = don't. */ |
120 | static long regsize; /* Code size. */ |
121 | static int regfold; |
122 | static int regsawbracket; /* Did we do {d,d} trick? */ |
9ef589d8 |
123 | static int regsawback; /* Did we see \1, ...? */ |
a687059c |
124 | |
125 | /* |
126 | * Forward declarations for regcomp()'s friends. |
127 | */ |
128 | STATIC int regcurly(); |
129 | STATIC char *reg(); |
130 | STATIC char *regbranch(); |
131 | STATIC char *regpiece(); |
132 | STATIC char *regatom(); |
133 | STATIC char *regclass(); |
134 | STATIC char *regnode(); |
fe14fcc3 |
135 | STATIC char *reganode(); |
a687059c |
136 | STATIC void regc(); |
137 | STATIC void reginsert(); |
138 | STATIC void regtail(); |
139 | STATIC void regoptail(); |
140 | |
141 | /* |
142 | - regcomp - compile a regular expression into internal code |
143 | * |
144 | * We can't allocate space until we know how big the compiled form will be, |
145 | * but we can't compile it (and thus know how big it is) until we've got a |
146 | * place to put the code. So we cheat: we compile it twice, once with code |
147 | * generation turned off and size counting turned on, and once "for real". |
148 | * This also means that we don't allocate space until we are sure that the |
149 | * thing really will compile successfully, and we never have to move the |
150 | * code and thus invalidate pointers into it. (Note that it has to be in |
151 | * one piece because free() must be able to free it all.) [NB: not true in perl] |
152 | * |
153 | * Beware that the optimization-preparation code in here knows about some |
154 | * of the structure of the compiled regexp. [I'll say.] |
155 | */ |
156 | regexp * |
00bf170e |
157 | regcomp(exp,xend,fold) |
a687059c |
158 | char *exp; |
159 | char *xend; |
160 | int fold; |
a687059c |
161 | { |
162 | register regexp *r; |
163 | register char *scan; |
0a12ae7d |
164 | register STR *longish; |
165 | STR *longest; |
a687059c |
166 | register int len; |
167 | register char *first; |
168 | int flags; |
34de22dd |
169 | int backish; |
170 | int backest; |
a687059c |
171 | int curback; |
f0fcb552 |
172 | int minlen; |
00bf170e |
173 | int sawplus = 0; |
9ef589d8 |
174 | int sawopen = 0; |
a687059c |
175 | |
176 | if (exp == NULL) |
177 | fatal("NULL regexp argument"); |
178 | |
179 | /* First pass: determine size, legality. */ |
180 | regfold = fold; |
181 | regparse = exp; |
182 | regxend = xend; |
183 | regprecomp = nsavestr(exp,xend-exp); |
184 | regsawbracket = 0; |
9ef589d8 |
185 | regsawback = 0; |
a687059c |
186 | regnpar = 1; |
187 | regsize = 0L; |
188 | regcode = ®dummy; |
f0fcb552 |
189 | regc((char)MAGIC); |
a687059c |
190 | if (reg(0, &flags) == NULL) { |
191 | Safefree(regprecomp); |
fe14fcc3 |
192 | regprecomp = Nullch; |
a687059c |
193 | return(NULL); |
194 | } |
195 | |
196 | /* Small enough for pointer-storage convention? */ |
197 | if (regsize >= 32767L) /* Probably could be 65535L. */ |
198 | FAIL("regexp too big"); |
199 | |
200 | /* Allocate space. */ |
201 | Newc(1001, r, sizeof(regexp) + (unsigned)regsize, char, regexp); |
202 | if (r == NULL) |
203 | FAIL("regexp out of space"); |
204 | |
205 | /* Second pass: emit code. */ |
206 | if (regsawbracket) |
2b69d0c2 |
207 | Copy(regprecomp,exp,xend-exp,char); |
9ef589d8 |
208 | r->prelen = xend-exp; |
a687059c |
209 | r->precomp = regprecomp; |
9ef589d8 |
210 | r->subbeg = r->subbase = NULL; |
a687059c |
211 | regparse = exp; |
212 | regnpar = 1; |
213 | regcode = r->program; |
f0fcb552 |
214 | regc((char)MAGIC); |
a687059c |
215 | if (reg(0, &flags) == NULL) |
216 | return(NULL); |
217 | |
218 | /* Dig out information for optimizations. */ |
219 | r->regstart = Nullstr; /* Worst-case defaults. */ |
220 | r->reganch = 0; |
221 | r->regmust = Nullstr; |
222 | r->regback = -1; |
223 | r->regstclass = Nullch; |
224 | scan = r->program+1; /* First BRANCH. */ |
225 | if (OP(regnext(scan)) == END) {/* Only one top-level choice. */ |
226 | scan = NEXTOPER(scan); |
227 | |
228 | first = scan; |
9ef589d8 |
229 | while ((OP(first) == OPEN && (sawopen = 1)) || |
a687059c |
230 | (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) || |
00bf170e |
231 | (OP(first) == PLUS) || |
232 | (OP(first) == CURLY && ARG1(first) > 0) ) { |
fe14fcc3 |
233 | if (OP(first) == PLUS) |
9ef589d8 |
234 | sawplus = 1; |
fe14fcc3 |
235 | else |
236 | first += regarglen[OP(first)]; |
a687059c |
237 | first = NEXTOPER(first); |
00bf170e |
238 | } |
a687059c |
239 | |
240 | /* Starting-point info. */ |
9ef589d8 |
241 | again: |
a687059c |
242 | if (OP(first) == EXACTLY) { |
243 | r->regstart = |
244 | str_make(OPERAND(first)+1,*OPERAND(first)); |
245 | if (r->regstart->str_cur > !(sawstudy|fold)) |
246 | fbmcompile(r->regstart,fold); |
247 | } |
248 | else if ((exp = index(simple,OP(first))) && exp > simple) |
249 | r->regstclass = first; |
250 | else if (OP(first) == BOUND || OP(first) == NBOUND) |
251 | r->regstclass = first; |
2b69d0c2 |
252 | else if (OP(first) == BOL) { |
253 | r->reganch = ROPT_ANCH; |
254 | first = NEXTOPER(first); |
255 | goto again; |
256 | } |
257 | else if ((OP(first) == STAR && OP(NEXTOPER(first)) == ANY) && |
258 | !(r->reganch & ROPT_ANCH) ) { |
259 | /* turn .* into ^.* with an implied $*=1 */ |
f0fcb552 |
260 | r->reganch = ROPT_ANCH | ROPT_IMPLICIT; |
9ef589d8 |
261 | first = NEXTOPER(first); |
262 | goto again; |
263 | } |
264 | if (sawplus && (!sawopen || !regsawback)) |
265 | r->reganch |= ROPT_SKIP; /* x+ must match 1st of run */ |
a687059c |
266 | |
267 | #ifdef DEBUGGING |
268 | if (debug & 512) |
269 | fprintf(stderr,"first %d next %d offset %d\n", |
270 | OP(first), OP(NEXTOPER(first)), first - scan); |
271 | #endif |
272 | /* |
273 | * If there's something expensive in the r.e., find the |
274 | * longest literal string that must appear and make it the |
275 | * regmust. Resolve ties in favor of later strings, since |
276 | * the regstart check works with the beginning of the r.e. |
277 | * and avoiding duplication strengthens checking. Not a |
278 | * strong reason, but sufficient in the absence of others. |
279 | * [Now we resolve ties in favor of the earlier string if |
280 | * it happens that curback has been invalidated, since the |
281 | * earlier string may buy us something the later one won't.] |
282 | */ |
0a12ae7d |
283 | longish = str_make("",0); |
a687059c |
284 | longest = str_make("",0); |
285 | len = 0; |
f0fcb552 |
286 | minlen = 0; |
a687059c |
287 | curback = 0; |
34de22dd |
288 | backish = 0; |
289 | backest = 0; |
e5d73d77 |
290 | while (OP(scan) != END) { |
a687059c |
291 | if (OP(scan) == BRANCH) { |
292 | if (OP(regnext(scan)) == BRANCH) { |
293 | curback = -30000; |
294 | while (OP(scan) == BRANCH) |
295 | scan = regnext(scan); |
296 | } |
297 | else /* single branch is ok */ |
298 | scan = NEXTOPER(scan); |
299 | } |
300 | if (OP(scan) == EXACTLY) { |
fe14fcc3 |
301 | char *t; |
302 | |
a687059c |
303 | first = scan; |
fe14fcc3 |
304 | while (OP(t = regnext(scan)) == CLOSE) |
305 | scan = t; |
f0fcb552 |
306 | minlen += *OPERAND(first); |
34de22dd |
307 | if (curback - backish == len) { |
0a12ae7d |
308 | str_ncat(longish, OPERAND(first)+1, |
a687059c |
309 | *OPERAND(first)); |
310 | len += *OPERAND(first); |
311 | curback += *OPERAND(first); |
312 | first = regnext(scan); |
313 | } |
314 | else if (*OPERAND(first) >= len + (curback >= 0)) { |
315 | len = *OPERAND(first); |
0a12ae7d |
316 | str_nset(longish, OPERAND(first)+1,len); |
34de22dd |
317 | backish = curback; |
a687059c |
318 | curback += len; |
319 | first = regnext(scan); |
320 | } |
321 | else |
322 | curback += *OPERAND(first); |
323 | } |
0a12ae7d |
324 | else if (index(varies,OP(scan))) { |
325 | curback = -30000; |
326 | len = 0; |
34de22dd |
327 | if (longish->str_cur > longest->str_cur) { |
0a12ae7d |
328 | str_sset(longest,longish); |
34de22dd |
329 | backest = backish; |
330 | } |
0a12ae7d |
331 | str_nset(longish,"",0); |
f0fcb552 |
332 | if (OP(scan) == PLUS && |
333 | index(simple,OP(NEXTOPER(scan)))) |
334 | minlen++; |
335 | else if (OP(scan) == CURLY && |
336 | index(simple,OP(NEXTOPER(scan)+4))) |
337 | minlen += ARG1(scan); |
0a12ae7d |
338 | } |
e5d73d77 |
339 | else if (index(simple,OP(scan))) { |
0a12ae7d |
340 | curback++; |
f0fcb552 |
341 | minlen++; |
e5d73d77 |
342 | len = 0; |
34de22dd |
343 | if (longish->str_cur > longest->str_cur) { |
e5d73d77 |
344 | str_sset(longest,longish); |
34de22dd |
345 | backest = backish; |
346 | } |
e5d73d77 |
347 | str_nset(longish,"",0); |
348 | } |
a687059c |
349 | scan = regnext(scan); |
350 | } |
e5d73d77 |
351 | |
352 | /* Prefer earlier on tie, unless we can tail match latter */ |
353 | |
34de22dd |
354 | if (longish->str_cur + (OP(first) == EOL) > longest->str_cur) { |
0a12ae7d |
355 | str_sset(longest,longish); |
34de22dd |
356 | backest = backish; |
357 | } |
e5d73d77 |
358 | else |
359 | str_nset(longish,"",0); |
34de22dd |
360 | if (longest->str_cur |
361 | && |
362 | (!r->regstart |
363 | || |
f0fcb552 |
364 | !fbminstr((unsigned char*) r->regstart->str_ptr, |
365 | (unsigned char *) r->regstart->str_ptr |
366 | + r->regstart->str_cur, |
34de22dd |
367 | longest) |
368 | ) |
369 | ) |
370 | { |
a687059c |
371 | r->regmust = longest; |
34de22dd |
372 | if (backest < 0) |
373 | backest = -1; |
374 | r->regback = backest; |
0a12ae7d |
375 | if (longest->str_cur |
376 | > !(sawstudy || fold || OP(first) == EOL) ) |
a687059c |
377 | fbmcompile(r->regmust,fold); |
378 | r->regmust->str_u.str_useful = 100; |
e5d73d77 |
379 | if (OP(first) == EOL && longish->str_cur) |
a687059c |
380 | r->regmust->str_pok |= SP_TAIL; |
381 | } |
fe14fcc3 |
382 | else { |
a687059c |
383 | str_free(longest); |
fe14fcc3 |
384 | longest = Nullstr; |
385 | } |
e5d73d77 |
386 | str_free(longish); |
a687059c |
387 | } |
388 | |
389 | r->do_folding = fold; |
390 | r->nparens = regnpar - 1; |
f0fcb552 |
391 | r->minlen = minlen; |
392 | Newz(1002, r->startp, regnpar, char*); |
393 | Newz(1002, r->endp, regnpar, char*); |
a687059c |
394 | #ifdef DEBUGGING |
395 | if (debug & 512) |
396 | regdump(r); |
397 | #endif |
398 | return(r); |
399 | } |
400 | |
401 | /* |
402 | - reg - regular expression, i.e. main body or parenthesized thing |
403 | * |
404 | * Caller must absorb opening parenthesis. |
405 | * |
406 | * Combining parenthesis handling with the base level of regular expression |
407 | * is a trifle forced, but the need to tie the tails of the branches to what |
408 | * follows makes it hard to avoid. |
409 | */ |
410 | static char * |
411 | reg(paren, flagp) |
412 | int paren; /* Parenthesized? */ |
413 | int *flagp; |
414 | { |
415 | register char *ret; |
416 | register char *br; |
417 | register char *ender; |
418 | register int parno; |
419 | int flags; |
420 | |
421 | *flagp = HASWIDTH; /* Tentatively. */ |
422 | |
423 | /* Make an OPEN node, if parenthesized. */ |
424 | if (paren) { |
a687059c |
425 | parno = regnpar; |
426 | regnpar++; |
fe14fcc3 |
427 | ret = reganode(OPEN, parno); |
a687059c |
428 | } else |
429 | ret = NULL; |
430 | |
431 | /* Pick up the branches, linking them together. */ |
432 | br = regbranch(&flags); |
433 | if (br == NULL) |
434 | return(NULL); |
435 | if (ret != NULL) |
436 | regtail(ret, br); /* OPEN -> first. */ |
437 | else |
438 | ret = br; |
439 | if (!(flags&HASWIDTH)) |
440 | *flagp &= ~HASWIDTH; |
441 | *flagp |= flags&SPSTART; |
442 | while (*regparse == '|') { |
443 | regparse++; |
444 | br = regbranch(&flags); |
445 | if (br == NULL) |
446 | return(NULL); |
447 | regtail(ret, br); /* BRANCH -> BRANCH. */ |
448 | if (!(flags&HASWIDTH)) |
449 | *flagp &= ~HASWIDTH; |
450 | *flagp |= flags&SPSTART; |
451 | } |
452 | |
453 | /* Make a closing node, and hook it on the end. */ |
fe14fcc3 |
454 | if (paren) |
455 | ender = reganode(CLOSE, parno); |
456 | else |
457 | ender = regnode(END); |
a687059c |
458 | regtail(ret, ender); |
459 | |
460 | /* Hook the tails of the branches to the closing node. */ |
461 | for (br = ret; br != NULL; br = regnext(br)) |
462 | regoptail(br, ender); |
463 | |
464 | /* Check for proper termination. */ |
465 | if (paren && *regparse++ != ')') { |
466 | FAIL("unmatched () in regexp"); |
467 | } else if (!paren && regparse < regxend) { |
468 | if (*regparse == ')') { |
469 | FAIL("unmatched () in regexp"); |
470 | } else |
471 | FAIL("junk on end of regexp"); /* "Can't happen". */ |
472 | /* NOTREACHED */ |
473 | } |
474 | |
475 | return(ret); |
476 | } |
477 | |
478 | /* |
479 | - regbranch - one alternative of an | operator |
480 | * |
481 | * Implements the concatenation operator. |
482 | */ |
483 | static char * |
484 | regbranch(flagp) |
485 | int *flagp; |
486 | { |
487 | register char *ret; |
488 | register char *chain; |
489 | register char *latest; |
490 | int flags; |
491 | |
492 | *flagp = WORST; /* Tentatively. */ |
493 | |
494 | ret = regnode(BRANCH); |
495 | chain = NULL; |
496 | while (regparse < regxend && *regparse != '|' && *regparse != ')') { |
497 | latest = regpiece(&flags); |
498 | if (latest == NULL) |
499 | return(NULL); |
500 | *flagp |= flags&HASWIDTH; |
501 | if (chain == NULL) /* First piece. */ |
502 | *flagp |= flags&SPSTART; |
503 | else |
504 | regtail(chain, latest); |
505 | chain = latest; |
506 | } |
507 | if (chain == NULL) /* Loop ran zero times. */ |
508 | (void) regnode(NOTHING); |
509 | |
510 | return(ret); |
511 | } |
512 | |
513 | /* |
514 | - regpiece - something followed by possible [*+?] |
515 | * |
516 | * Note that the branching code sequences used for ? and the general cases |
517 | * of * and + are somewhat optimized: they use the same NOTHING node as |
518 | * both the endmarker for their branch list and the body of the last branch. |
519 | * It might seem that this node could be dispensed with entirely, but the |
520 | * endmarker role is not redundant. |
521 | */ |
522 | static char * |
523 | regpiece(flagp) |
524 | int *flagp; |
525 | { |
526 | register char *ret; |
527 | register char op; |
528 | register char *next; |
529 | int flags; |
530 | char *origparse = regparse; |
531 | int orignpar = regnpar; |
532 | char *max; |
533 | int iter; |
534 | char ch; |
535 | |
536 | ret = regatom(&flags); |
537 | if (ret == NULL) |
538 | return(NULL); |
539 | |
540 | op = *regparse; |
541 | |
542 | /* Here's a total kludge: if after the atom there's a {\d+,?\d*} |
543 | * then we decrement the first number by one and reset our |
544 | * parsing back to the beginning of the same atom. If the first number |
545 | * is down to 0, decrement the second number instead and fake up |
546 | * a ? after it. Given the way this compiler doesn't keep track |
547 | * of offsets on the first pass, this is the only way to replicate |
548 | * a piece of code. Sigh. |
549 | */ |
550 | if (op == '{' && regcurly(regparse)) { |
551 | next = regparse + 1; |
552 | max = Nullch; |
f0fcb552 |
553 | while (isDIGIT(*next) || *next == ',') { |
a687059c |
554 | if (*next == ',') { |
555 | if (max) |
556 | break; |
557 | else |
558 | max = next; |
559 | } |
560 | next++; |
561 | } |
562 | if (*next == '}') { /* got one */ |
a687059c |
563 | if (!max) |
564 | max = next; |
565 | regparse++; |
566 | iter = atoi(regparse); |
00bf170e |
567 | if (flags&SIMPLE) { /* we can do it right after all */ |
568 | int tmp; |
569 | |
570 | reginsert(CURLY, ret); |
fe14fcc3 |
571 | if (iter > 0) |
572 | *flagp = (WORST|HASWIDTH); |
00bf170e |
573 | if (*max == ',') |
574 | max++; |
6eb13c3b |
575 | else |
576 | max = regparse; |
00bf170e |
577 | tmp = atoi(max); |
2b69d0c2 |
578 | if (!tmp && *max != '0') |
579 | tmp = 32767; /* meaning "infinity" */ |
00bf170e |
580 | if (tmp && tmp < iter) |
581 | fatal("Can't do {n,m} with n > m"); |
582 | if (regcode != ®dummy) { |
583 | #ifdef REGALIGN |
584 | *(unsigned short *)(ret+3) = iter; |
585 | *(unsigned short *)(ret+5) = tmp; |
586 | #else |
587 | ret[3] = iter >> 8; ret[4] = iter & 0377; |
588 | ret[5] = tmp >> 8; ret[6] = tmp & 0377; |
589 | #endif |
590 | } |
591 | regparse = next; |
592 | goto nest_check; |
593 | } |
594 | regsawbracket++; /* remember we clobbered exp */ |
a687059c |
595 | if (iter > 0) { |
596 | ch = *max; |
597 | sprintf(regparse,"%.*d", max-regparse, iter - 1); |
598 | *max = ch; |
00bf170e |
599 | if (*max == ',' && max[1] != '}') { |
600 | if (atoi(max+1) <= 0) |
601 | fatal("Can't do {n,m} with n > m"); |
a687059c |
602 | ch = *next; |
603 | sprintf(max+1,"%.*d", next-(max+1), atoi(max+1) - 1); |
604 | *next = ch; |
605 | } |
00bf170e |
606 | if (iter != 1 || *max == ',') { |
a687059c |
607 | regparse = origparse; /* back up input pointer */ |
608 | regnpar = orignpar; /* don't make more parens */ |
609 | } |
610 | else { |
611 | regparse = next; |
612 | goto nest_check; |
613 | } |
614 | *flagp = flags; |
615 | return ret; |
616 | } |
617 | if (*max == ',') { |
618 | max++; |
619 | iter = atoi(max); |
620 | if (max == next) { /* any number more? */ |
621 | regparse = next; |
622 | op = '*'; /* fake up one with a star */ |
623 | } |
624 | else if (iter > 0) { |
625 | op = '?'; /* fake up optional atom */ |
626 | ch = *next; |
627 | sprintf(max,"%.*d", next-max, iter - 1); |
628 | *next = ch; |
629 | if (iter == 1) |
630 | regparse = next; |
631 | else { |
632 | regparse = origparse - 1; /* offset ++ below */ |
633 | regnpar = orignpar; |
634 | } |
635 | } |
636 | else |
637 | fatal("Can't do {n,0}"); |
638 | } |
639 | else |
640 | fatal("Can't do {0}"); |
641 | } |
642 | } |
643 | |
644 | if (!ISMULT1(op)) { |
645 | *flagp = flags; |
646 | return(ret); |
647 | } |
648 | |
649 | if (!(flags&HASWIDTH) && op != '?') |
650 | FAIL("regexp *+ operand could be empty"); |
651 | *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH); |
652 | |
653 | if (op == '*' && (flags&SIMPLE)) |
654 | reginsert(STAR, ret); |
655 | else if (op == '*') { |
656 | /* Emit x* as (x&|), where & means "self". */ |
657 | reginsert(BRANCH, ret); /* Either x */ |
658 | regoptail(ret, regnode(BACK)); /* and loop */ |
659 | regoptail(ret, ret); /* back */ |
660 | regtail(ret, regnode(BRANCH)); /* or */ |
661 | regtail(ret, regnode(NOTHING)); /* null. */ |
662 | } else if (op == '+' && (flags&SIMPLE)) |
663 | reginsert(PLUS, ret); |
664 | else if (op == '+') { |
665 | /* Emit x+ as x(&|), where & means "self". */ |
666 | next = regnode(BRANCH); /* Either */ |
667 | regtail(ret, next); |
668 | regtail(regnode(BACK), ret); /* loop back */ |
669 | regtail(next, regnode(BRANCH)); /* or */ |
670 | regtail(ret, regnode(NOTHING)); /* null. */ |
671 | } else if (op == '?') { |
672 | /* Emit x? as (x|) */ |
673 | reginsert(BRANCH, ret); /* Either x */ |
674 | regtail(ret, regnode(BRANCH)); /* or */ |
675 | next = regnode(NOTHING); /* null. */ |
676 | regtail(ret, next); |
677 | regoptail(ret, next); |
678 | } |
679 | nest_check: |
680 | regparse++; |
681 | if (ISMULT2(regparse)) |
682 | FAIL("nested *?+ in regexp"); |
683 | |
684 | return(ret); |
685 | } |
686 | |
687 | /* |
688 | - regatom - the lowest level |
689 | * |
690 | * Optimization: gobbles an entire sequence of ordinary characters so that |
691 | * it can turn them into a single node, which is smaller to store and |
692 | * faster to run. Backslashed characters are exceptions, each becoming a |
693 | * separate node; the code is simpler that way and it's not worth fixing. |
694 | * |
695 | * [Yes, it is worth fixing, some scripts can run twice the speed.] |
696 | */ |
697 | static char * |
698 | regatom(flagp) |
699 | int *flagp; |
700 | { |
701 | register char *ret; |
702 | int flags; |
703 | |
704 | *flagp = WORST; /* Tentatively. */ |
705 | |
706 | switch (*regparse++) { |
707 | case '^': |
708 | ret = regnode(BOL); |
709 | break; |
710 | case '$': |
711 | ret = regnode(EOL); |
712 | break; |
713 | case '.': |
714 | ret = regnode(ANY); |
715 | *flagp |= HASWIDTH|SIMPLE; |
716 | break; |
717 | case '[': |
718 | ret = regclass(); |
719 | *flagp |= HASWIDTH|SIMPLE; |
720 | break; |
721 | case '(': |
722 | ret = reg(1, &flags); |
723 | if (ret == NULL) |
724 | return(NULL); |
725 | *flagp |= flags&(HASWIDTH|SPSTART); |
726 | break; |
727 | case '|': |
728 | case ')': |
729 | FAIL("internal urp in regexp"); /* Supposed to be caught earlier. */ |
730 | break; |
731 | case '?': |
732 | case '+': |
733 | case '*': |
734 | FAIL("?+* follows nothing in regexp"); |
735 | break; |
736 | case '\\': |
737 | switch (*regparse) { |
738 | case 'w': |
739 | ret = regnode(ALNUM); |
740 | *flagp |= HASWIDTH|SIMPLE; |
741 | regparse++; |
742 | break; |
743 | case 'W': |
744 | ret = regnode(NALNUM); |
745 | *flagp |= HASWIDTH|SIMPLE; |
746 | regparse++; |
747 | break; |
748 | case 'b': |
749 | ret = regnode(BOUND); |
750 | *flagp |= SIMPLE; |
751 | regparse++; |
752 | break; |
753 | case 'B': |
754 | ret = regnode(NBOUND); |
755 | *flagp |= SIMPLE; |
756 | regparse++; |
757 | break; |
758 | case 's': |
759 | ret = regnode(SPACE); |
760 | *flagp |= HASWIDTH|SIMPLE; |
761 | regparse++; |
762 | break; |
763 | case 'S': |
764 | ret = regnode(NSPACE); |
765 | *flagp |= HASWIDTH|SIMPLE; |
766 | regparse++; |
767 | break; |
768 | case 'd': |
769 | ret = regnode(DIGIT); |
770 | *flagp |= HASWIDTH|SIMPLE; |
771 | regparse++; |
772 | break; |
773 | case 'D': |
774 | ret = regnode(NDIGIT); |
775 | *flagp |= HASWIDTH|SIMPLE; |
776 | regparse++; |
777 | break; |
778 | case 'n': |
779 | case 'r': |
780 | case 't': |
781 | case 'f': |
fe14fcc3 |
782 | case 'e': |
783 | case 'a': |
784 | case 'x': |
785 | case 'c': |
786 | case '0': |
a687059c |
787 | goto defchar; |
fe14fcc3 |
788 | case '1': case '2': case '3': case '4': |
a687059c |
789 | case '5': case '6': case '7': case '8': case '9': |
fe14fcc3 |
790 | { |
791 | int num = atoi(regparse); |
792 | |
793 | if (num > 9 && num >= regnpar) |
a687059c |
794 | goto defchar; |
fe14fcc3 |
795 | else { |
9ef589d8 |
796 | regsawback = 1; |
fe14fcc3 |
797 | ret = reganode(REF, num); |
f0fcb552 |
798 | while (isDIGIT(*regparse)) |
fe14fcc3 |
799 | regparse++; |
a687059c |
800 | *flagp |= SIMPLE; |
fe14fcc3 |
801 | } |
a687059c |
802 | } |
803 | break; |
804 | case '\0': |
805 | if (regparse >= regxend) |
806 | FAIL("trailing \\ in regexp"); |
807 | /* FALL THROUGH */ |
808 | default: |
809 | goto defchar; |
810 | } |
811 | break; |
812 | default: { |
813 | register int len; |
814 | register char ender; |
815 | register char *p; |
816 | char *oldp; |
fe14fcc3 |
817 | int numlen; |
a687059c |
818 | |
819 | defchar: |
820 | ret = regnode(EXACTLY); |
821 | regc(0); /* save spot for len */ |
822 | for (len=0, p=regparse-1; |
823 | len < 127 && p < regxend; |
824 | len++) |
825 | { |
826 | oldp = p; |
827 | switch (*p) { |
828 | case '^': |
829 | case '$': |
830 | case '.': |
831 | case '[': |
832 | case '(': |
833 | case ')': |
834 | case '|': |
835 | goto loopdone; |
836 | case '\\': |
837 | switch (*++p) { |
838 | case 'w': |
839 | case 'W': |
840 | case 'b': |
841 | case 'B': |
842 | case 's': |
843 | case 'S': |
844 | case 'd': |
845 | case 'D': |
846 | --p; |
847 | goto loopdone; |
848 | case 'n': |
849 | ender = '\n'; |
850 | p++; |
851 | break; |
852 | case 'r': |
853 | ender = '\r'; |
854 | p++; |
855 | break; |
856 | case 't': |
857 | ender = '\t'; |
858 | p++; |
859 | break; |
860 | case 'f': |
861 | ender = '\f'; |
862 | p++; |
863 | break; |
fe14fcc3 |
864 | case 'e': |
865 | ender = '\033'; |
866 | p++; |
867 | break; |
868 | case 'a': |
869 | ender = '\007'; |
870 | p++; |
871 | break; |
872 | case 'x': |
873 | ender = scanhex(++p, 2, &numlen); |
874 | p += numlen; |
875 | break; |
876 | case 'c': |
877 | p++; |
878 | ender = *p++; |
f0fcb552 |
879 | if (isLOWER(ender)) |
fe14fcc3 |
880 | ender = toupper(ender); |
881 | ender ^= 64; |
882 | break; |
a687059c |
883 | case '0': case '1': case '2': case '3':case '4': |
884 | case '5': case '6': case '7': case '8':case '9': |
fe14fcc3 |
885 | if (*p == '0' || |
f0fcb552 |
886 | (isDIGIT(p[1]) && atoi(p) >= regnpar) ) { |
fe14fcc3 |
887 | ender = scanoct(p, 3, &numlen); |
888 | p += numlen; |
a687059c |
889 | } |
890 | else { |
891 | --p; |
892 | goto loopdone; |
893 | } |
894 | break; |
895 | case '\0': |
896 | if (p >= regxend) |
897 | FAIL("trailing \\ in regexp"); |
898 | /* FALL THROUGH */ |
899 | default: |
900 | ender = *p++; |
901 | break; |
902 | } |
903 | break; |
904 | default: |
905 | ender = *p++; |
906 | break; |
907 | } |
f0fcb552 |
908 | if (regfold && isUPPER(ender)) |
a687059c |
909 | ender = tolower(ender); |
910 | if (ISMULT2(p)) { /* Back off on ?+*. */ |
911 | if (len) |
912 | p = oldp; |
913 | else { |
914 | len++; |
915 | regc(ender); |
916 | } |
917 | break; |
918 | } |
919 | regc(ender); |
920 | } |
921 | loopdone: |
922 | regparse = p; |
923 | if (len <= 0) |
924 | FAIL("internal disaster in regexp"); |
925 | *flagp |= HASWIDTH; |
926 | if (len == 1) |
927 | *flagp |= SIMPLE; |
928 | if (regcode != ®dummy) |
929 | *OPERAND(ret) = len; |
930 | regc('\0'); |
931 | } |
932 | break; |
933 | } |
934 | |
935 | return(ret); |
936 | } |
937 | |
938 | static void |
939 | regset(bits,def,c) |
940 | char *bits; |
941 | int def; |
942 | register int c; |
943 | { |
944 | if (regcode == ®dummy) |
945 | return; |
ac58e20f |
946 | c &= 255; |
a687059c |
947 | if (def) |
948 | bits[c >> 3] &= ~(1 << (c & 7)); |
949 | else |
950 | bits[c >> 3] |= (1 << (c & 7)); |
951 | } |
952 | |
953 | static char * |
954 | regclass() |
955 | { |
956 | register char *bits; |
957 | register int class; |
958 | register int lastclass; |
959 | register int range = 0; |
960 | register char *ret; |
961 | register int def; |
fe14fcc3 |
962 | int numlen; |
a687059c |
963 | |
00bf170e |
964 | ret = regnode(ANYOF); |
a687059c |
965 | if (*regparse == '^') { /* Complement of range. */ |
a687059c |
966 | regparse++; |
967 | def = 0; |
968 | } else { |
a687059c |
969 | def = 255; |
970 | } |
971 | bits = regcode; |
972 | for (class = 0; class < 32; class++) |
973 | regc(def); |
974 | if (*regparse == ']' || *regparse == '-') |
00bf170e |
975 | goto skipcond; /* allow 1st char to be ] or - */ |
a687059c |
976 | while (regparse < regxend && *regparse != ']') { |
00bf170e |
977 | skipcond: |
a687059c |
978 | class = UCHARAT(regparse++); |
979 | if (class == '\\') { |
980 | class = UCHARAT(regparse++); |
981 | switch (class) { |
982 | case 'w': |
2b69d0c2 |
983 | for (class = 0; class < 256; class++) |
984 | if (isALNUM(class)) |
a687059c |
985 | regset(bits,def,class); |
2b69d0c2 |
986 | lastclass = 1234; |
987 | continue; |
988 | case 'W': |
989 | for (class = 0; class < 256; class++) |
990 | if (!isALNUM(class)) |
a687059c |
991 | regset(bits,def,class); |
a687059c |
992 | lastclass = 1234; |
993 | continue; |
994 | case 's': |
2b69d0c2 |
995 | for (class = 0; class < 256; class++) |
996 | if (isSPACE(class)) |
997 | regset(bits,def,class); |
998 | lastclass = 1234; |
999 | continue; |
1000 | case 'S': |
1001 | for (class = 0; class < 256; class++) |
1002 | if (!isSPACE(class)) |
1003 | regset(bits,def,class); |
a687059c |
1004 | lastclass = 1234; |
1005 | continue; |
1006 | case 'd': |
1007 | for (class = '0'; class <= '9'; class++) |
1008 | regset(bits,def,class); |
1009 | lastclass = 1234; |
1010 | continue; |
2b69d0c2 |
1011 | case 'D': |
1012 | for (class = 0; class < '0'; class++) |
1013 | regset(bits,def,class); |
1014 | for (class = '9' + 1; class < 256; class++) |
1015 | regset(bits,def,class); |
1016 | lastclass = 1234; |
1017 | continue; |
a687059c |
1018 | case 'n': |
1019 | class = '\n'; |
1020 | break; |
1021 | case 'r': |
1022 | class = '\r'; |
1023 | break; |
1024 | case 't': |
1025 | class = '\t'; |
1026 | break; |
1027 | case 'f': |
1028 | class = '\f'; |
1029 | break; |
1030 | case 'b': |
1031 | class = '\b'; |
1032 | break; |
fe14fcc3 |
1033 | case 'e': |
1034 | class = '\033'; |
1035 | break; |
1036 | case 'a': |
1037 | class = '\007'; |
1038 | break; |
1039 | case 'x': |
1040 | class = scanhex(regparse, 2, &numlen); |
1041 | regparse += numlen; |
1042 | break; |
1043 | case 'c': |
1044 | class = *regparse++; |
f0fcb552 |
1045 | if (isLOWER(class)) |
fe14fcc3 |
1046 | class = toupper(class); |
1047 | class ^= 64; |
1048 | break; |
a687059c |
1049 | case '0': case '1': case '2': case '3': case '4': |
1050 | case '5': case '6': case '7': case '8': case '9': |
fe14fcc3 |
1051 | class = scanoct(--regparse, 3, &numlen); |
1052 | regparse += numlen; |
a687059c |
1053 | break; |
1054 | } |
1055 | } |
a687059c |
1056 | if (range) { |
1057 | if (lastclass > class) |
1058 | FAIL("invalid [] range in regexp"); |
00bf170e |
1059 | range = 0; |
a687059c |
1060 | } |
00bf170e |
1061 | else { |
1062 | lastclass = class; |
1063 | if (*regparse == '-' && regparse+1 < regxend && |
1064 | regparse[1] != ']') { |
1065 | regparse++; |
1066 | range = 1; |
1067 | continue; /* do it next time */ |
1068 | } |
1069 | } |
1070 | for ( ; lastclass <= class; lastclass++) { |
a687059c |
1071 | regset(bits,def,lastclass); |
f0fcb552 |
1072 | if (regfold && isUPPER(lastclass)) |
a687059c |
1073 | regset(bits,def,tolower(lastclass)); |
1074 | } |
1075 | lastclass = class; |
1076 | } |
1077 | if (*regparse != ']') |
1078 | FAIL("unmatched [] in regexp"); |
a687059c |
1079 | regparse++; |
1080 | return ret; |
1081 | } |
1082 | |
1083 | /* |
1084 | - regnode - emit a node |
1085 | */ |
1086 | static char * /* Location. */ |
1087 | regnode(op) |
1088 | char op; |
1089 | { |
1090 | register char *ret; |
1091 | register char *ptr; |
1092 | |
1093 | ret = regcode; |
1094 | if (ret == ®dummy) { |
1095 | #ifdef REGALIGN |
1096 | if (!(regsize & 1)) |
1097 | regsize++; |
1098 | #endif |
1099 | regsize += 3; |
1100 | return(ret); |
1101 | } |
1102 | |
1103 | #ifdef REGALIGN |
1104 | #ifndef lint |
1105 | if (!((long)ret & 1)) |
1106 | *ret++ = 127; |
1107 | #endif |
1108 | #endif |
1109 | ptr = ret; |
1110 | *ptr++ = op; |
1111 | *ptr++ = '\0'; /* Null "next" pointer. */ |
1112 | *ptr++ = '\0'; |
1113 | regcode = ptr; |
1114 | |
1115 | return(ret); |
1116 | } |
1117 | |
1118 | /* |
fe14fcc3 |
1119 | - reganode - emit a node with an argument |
1120 | */ |
1121 | static char * /* Location. */ |
1122 | reganode(op, arg) |
1123 | char op; |
1124 | unsigned short arg; |
1125 | { |
1126 | register char *ret; |
1127 | register char *ptr; |
1128 | |
1129 | ret = regcode; |
1130 | if (ret == ®dummy) { |
1131 | #ifdef REGALIGN |
1132 | if (!(regsize & 1)) |
1133 | regsize++; |
1134 | #endif |
1135 | regsize += 5; |
1136 | return(ret); |
1137 | } |
1138 | |
1139 | #ifdef REGALIGN |
1140 | #ifndef lint |
1141 | if (!((long)ret & 1)) |
1142 | *ret++ = 127; |
1143 | #endif |
1144 | #endif |
1145 | ptr = ret; |
1146 | *ptr++ = op; |
1147 | *ptr++ = '\0'; /* Null "next" pointer. */ |
1148 | *ptr++ = '\0'; |
1149 | #ifdef REGALIGN |
1150 | *(unsigned short *)(ret+3) = arg; |
1151 | #else |
1152 | ret[3] = arg >> 8; ret[4] = arg & 0377; |
1153 | #endif |
1154 | ptr += 2; |
1155 | regcode = ptr; |
1156 | |
1157 | return(ret); |
1158 | } |
1159 | |
1160 | /* |
a687059c |
1161 | - regc - emit (if appropriate) a byte of code |
1162 | */ |
1163 | static void |
1164 | regc(b) |
1165 | char b; |
1166 | { |
1167 | if (regcode != ®dummy) |
1168 | *regcode++ = b; |
1169 | else |
1170 | regsize++; |
1171 | } |
1172 | |
1173 | /* |
1174 | - reginsert - insert an operator in front of already-emitted operand |
1175 | * |
1176 | * Means relocating the operand. |
1177 | */ |
1178 | static void |
1179 | reginsert(op, opnd) |
1180 | char op; |
1181 | char *opnd; |
1182 | { |
1183 | register char *src; |
1184 | register char *dst; |
1185 | register char *place; |
00bf170e |
1186 | register offset = (op == CURLY ? 4 : 0); |
a687059c |
1187 | |
1188 | if (regcode == ®dummy) { |
1189 | #ifdef REGALIGN |
00bf170e |
1190 | regsize += 4 + offset; |
a687059c |
1191 | #else |
00bf170e |
1192 | regsize += 3 + offset; |
a687059c |
1193 | #endif |
1194 | return; |
1195 | } |
1196 | |
1197 | src = regcode; |
1198 | #ifdef REGALIGN |
00bf170e |
1199 | regcode += 4 + offset; |
a687059c |
1200 | #else |
00bf170e |
1201 | regcode += 3 + offset; |
a687059c |
1202 | #endif |
1203 | dst = regcode; |
1204 | while (src > opnd) |
1205 | *--dst = *--src; |
1206 | |
1207 | place = opnd; /* Op node, where operand used to be. */ |
1208 | *place++ = op; |
1209 | *place++ = '\0'; |
1210 | *place++ = '\0'; |
00bf170e |
1211 | while (offset-- > 0) |
1212 | *place++ = '\0'; |
2b69d0c2 |
1213 | #ifdef REGALIGN |
1214 | *place++ = '\177'; |
1215 | #endif |
a687059c |
1216 | } |
1217 | |
1218 | /* |
1219 | - regtail - set the next-pointer at the end of a node chain |
1220 | */ |
1221 | static void |
1222 | regtail(p, val) |
1223 | char *p; |
1224 | char *val; |
1225 | { |
1226 | register char *scan; |
1227 | register char *temp; |
1228 | register int offset; |
1229 | |
1230 | if (p == ®dummy) |
1231 | return; |
1232 | |
1233 | /* Find last node. */ |
1234 | scan = p; |
1235 | for (;;) { |
1236 | temp = regnext(scan); |
1237 | if (temp == NULL) |
1238 | break; |
1239 | scan = temp; |
1240 | } |
1241 | |
1242 | #ifdef REGALIGN |
1243 | offset = val - scan; |
1244 | #ifndef lint |
1245 | *(short*)(scan+1) = offset; |
1246 | #else |
1247 | offset = offset; |
1248 | #endif |
1249 | #else |
1250 | if (OP(scan) == BACK) |
1251 | offset = scan - val; |
1252 | else |
1253 | offset = val - scan; |
1254 | *(scan+1) = (offset>>8)&0377; |
1255 | *(scan+2) = offset&0377; |
1256 | #endif |
1257 | } |
1258 | |
1259 | /* |
1260 | - regoptail - regtail on operand of first argument; nop if operandless |
1261 | */ |
1262 | static void |
1263 | regoptail(p, val) |
1264 | char *p; |
1265 | char *val; |
1266 | { |
1267 | /* "Operandless" and "op != BRANCH" are synonymous in practice. */ |
1268 | if (p == NULL || p == ®dummy || OP(p) != BRANCH) |
1269 | return; |
1270 | regtail(NEXTOPER(p), val); |
1271 | } |
1272 | |
1273 | /* |
1274 | - regcurly - a little FSA that accepts {\d+,?\d*} |
1275 | */ |
1276 | STATIC int |
1277 | regcurly(s) |
1278 | register char *s; |
1279 | { |
1280 | if (*s++ != '{') |
1281 | return FALSE; |
f0fcb552 |
1282 | if (!isDIGIT(*s)) |
a687059c |
1283 | return FALSE; |
f0fcb552 |
1284 | while (isDIGIT(*s)) |
a687059c |
1285 | s++; |
1286 | if (*s == ',') |
1287 | s++; |
f0fcb552 |
1288 | while (isDIGIT(*s)) |
a687059c |
1289 | s++; |
1290 | if (*s != '}') |
1291 | return FALSE; |
1292 | return TRUE; |
1293 | } |
1294 | |
1295 | #ifdef DEBUGGING |
1296 | |
1297 | /* |
1298 | - regdump - dump a regexp onto stderr in vaguely comprehensible form |
1299 | */ |
1300 | void |
1301 | regdump(r) |
1302 | regexp *r; |
1303 | { |
1304 | register char *s; |
1305 | register char op = EXACTLY; /* Arbitrary non-END op. */ |
1306 | register char *next; |
a687059c |
1307 | |
1308 | |
1309 | s = r->program + 1; |
1310 | while (op != END) { /* While that wasn't END last time... */ |
1311 | #ifdef REGALIGN |
1312 | if (!((long)s & 1)) |
1313 | s++; |
1314 | #endif |
1315 | op = OP(s); |
1316 | fprintf(stderr,"%2d%s", s-r->program, regprop(s)); /* Where, what. */ |
1317 | next = regnext(s); |
fe14fcc3 |
1318 | s += regarglen[op]; |
a687059c |
1319 | if (next == NULL) /* Next ptr. */ |
1320 | fprintf(stderr,"(0)"); |
1321 | else |
1322 | fprintf(stderr,"(%d)", (s-r->program)+(next-s)); |
1323 | s += 3; |
00bf170e |
1324 | if (op == ANYOF) { |
a687059c |
1325 | s += 32; |
1326 | } |
1327 | if (op == EXACTLY) { |
1328 | /* Literal string, where present. */ |
1329 | s++; |
1330 | while (*s != '\0') { |
1331 | (void)putchar(*s); |
1332 | s++; |
1333 | } |
1334 | s++; |
1335 | } |
1336 | (void)putchar('\n'); |
1337 | } |
1338 | |
1339 | /* Header fields of interest. */ |
1340 | if (r->regstart) |
1341 | fprintf(stderr,"start `%s' ", r->regstart->str_ptr); |
1342 | if (r->regstclass) |
1343 | fprintf(stderr,"stclass `%s' ", regprop(r->regstclass)); |
9ef589d8 |
1344 | if (r->reganch & ROPT_ANCH) |
a687059c |
1345 | fprintf(stderr,"anchored "); |
9ef589d8 |
1346 | if (r->reganch & ROPT_SKIP) |
00bf170e |
1347 | fprintf(stderr,"plus "); |
f0fcb552 |
1348 | if (r->reganch & ROPT_IMPLICIT) |
1349 | fprintf(stderr,"implicit "); |
a687059c |
1350 | if (r->regmust != NULL) |
1351 | fprintf(stderr,"must have \"%s\" back %d ", r->regmust->str_ptr, |
1352 | r->regback); |
f0fcb552 |
1353 | fprintf(stderr, "minlen %d ", r->minlen); |
a687059c |
1354 | fprintf(stderr,"\n"); |
1355 | } |
1356 | |
1357 | /* |
1358 | - regprop - printable representation of opcode |
1359 | */ |
1360 | char * |
1361 | regprop(op) |
1362 | char *op; |
1363 | { |
1364 | register char *p; |
1365 | |
1366 | (void) strcpy(buf, ":"); |
1367 | |
1368 | switch (OP(op)) { |
1369 | case BOL: |
1370 | p = "BOL"; |
1371 | break; |
1372 | case EOL: |
1373 | p = "EOL"; |
1374 | break; |
1375 | case ANY: |
1376 | p = "ANY"; |
1377 | break; |
1378 | case ANYOF: |
1379 | p = "ANYOF"; |
1380 | break; |
a687059c |
1381 | case BRANCH: |
1382 | p = "BRANCH"; |
1383 | break; |
1384 | case EXACTLY: |
1385 | p = "EXACTLY"; |
1386 | break; |
1387 | case NOTHING: |
1388 | p = "NOTHING"; |
1389 | break; |
1390 | case BACK: |
1391 | p = "BACK"; |
1392 | break; |
1393 | case END: |
1394 | p = "END"; |
1395 | break; |
1396 | case ALNUM: |
1397 | p = "ALNUM"; |
1398 | break; |
1399 | case NALNUM: |
1400 | p = "NALNUM"; |
1401 | break; |
1402 | case BOUND: |
1403 | p = "BOUND"; |
1404 | break; |
1405 | case NBOUND: |
1406 | p = "NBOUND"; |
1407 | break; |
1408 | case SPACE: |
1409 | p = "SPACE"; |
1410 | break; |
1411 | case NSPACE: |
1412 | p = "NSPACE"; |
1413 | break; |
1414 | case DIGIT: |
1415 | p = "DIGIT"; |
1416 | break; |
1417 | case NDIGIT: |
1418 | p = "NDIGIT"; |
1419 | break; |
00bf170e |
1420 | case CURLY: |
1421 | (void)sprintf(buf+strlen(buf), "CURLY {%d,%d}", |
1422 | ARG1(op),ARG2(op)); |
1423 | p = NULL; |
1424 | break; |
a687059c |
1425 | case REF: |
fe14fcc3 |
1426 | (void)sprintf(buf+strlen(buf), "REF%d", ARG1(op)); |
a687059c |
1427 | p = NULL; |
1428 | break; |
fe14fcc3 |
1429 | case OPEN: |
1430 | (void)sprintf(buf+strlen(buf), "OPEN%d", ARG1(op)); |
a687059c |
1431 | p = NULL; |
1432 | break; |
fe14fcc3 |
1433 | case CLOSE: |
1434 | (void)sprintf(buf+strlen(buf), "CLOSE%d", ARG1(op)); |
a687059c |
1435 | p = NULL; |
1436 | break; |
1437 | case STAR: |
1438 | p = "STAR"; |
1439 | break; |
1440 | case PLUS: |
1441 | p = "PLUS"; |
1442 | break; |
1443 | default: |
1444 | FAIL("corrupted regexp opcode"); |
1445 | } |
1446 | if (p != NULL) |
1447 | (void) strcat(buf, p); |
1448 | return(buf); |
1449 | } |
1450 | #endif /* DEBUGGING */ |
1451 | |
2b69d0c2 |
1452 | void |
a687059c |
1453 | regfree(r) |
1454 | struct regexp *r; |
1455 | { |
fe14fcc3 |
1456 | if (r->precomp) { |
a687059c |
1457 | Safefree(r->precomp); |
fe14fcc3 |
1458 | r->precomp = Nullch; |
1459 | } |
1460 | if (r->subbase) { |
a687059c |
1461 | Safefree(r->subbase); |
fe14fcc3 |
1462 | r->subbase = Nullch; |
1463 | } |
1464 | if (r->regmust) { |
a687059c |
1465 | str_free(r->regmust); |
fe14fcc3 |
1466 | r->regmust = Nullstr; |
1467 | } |
1468 | if (r->regstart) { |
a687059c |
1469 | str_free(r->regstart); |
fe14fcc3 |
1470 | r->regstart = Nullstr; |
1471 | } |
1472 | Safefree(r->startp); |
1473 | Safefree(r->endp); |
a687059c |
1474 | Safefree(r); |
1475 | } |