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!
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.
10 /* $Header: regcomp.c,v 3.0.1.1 89/11/11 04:51:04 lwall Locked $
13 * Revision 3.0.1.1 89/11/11 04:51:04 lwall
14 * patch2: /[\000]/ didn't work
16 * Revision 3.0 89/10/18 15:22:29 lwall
22 * regcomp and regexec -- regsub and regerror are not used in perl
24 * Copyright (c) 1986 by University of Toronto.
25 * Written by Henry Spencer. Not derived from licensed software.
27 * Permission is granted to anyone to use this software for any
28 * purpose on any computer system, and to redistribute it freely,
29 * subject to the following restrictions:
31 * 1. The author is not responsible for the consequences of use of
32 * this software, no matter how awful, even if they arise
35 * 2. The origin of this software must not be misrepresented, either
36 * by explicit claim or by omission.
38 * 3. Altered versions must be plainly marked as such, and must not
39 * be misrepresented as being the original software.
42 **** Alterations to Henry's code are...
44 **** Copyright (c) 1989, Larry Wall
46 **** You may distribute under the terms of the GNU General Public License
47 **** as specified in the README file that comes with the perl 3.0 kit.
49 * Beware that some of this code is subtly aware of the way operator
50 * precedence is structured in regular expressions. Serious changes in
51 * regular-expression syntax might require a total rethink.
62 #define ISMULT1(c) ((c) == '*' || (c) == '+' || (c) == '?')
63 #define ISMULT2(s) ((*s) == '*' || (*s) == '+' || (*s) == '?' || \
64 ((*s) == '{' && regcurly(s)))
65 #define META "^$.[()|?+*\\"
68 * Flags to be passed up and down.
70 #define HASWIDTH 01 /* Known never to match null string. */
71 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
72 #define SPSTART 04 /* Starts with * or +. */
73 #define WORST 0 /* Worst case. */
76 * Global work variables for regcomp().
78 static char *regprecomp; /* uncompiled string. */
79 static char *regparse; /* Input-scan pointer. */
80 static char *regxend; /* End of input for compile */
81 static int regnpar; /* () count. */
82 static char *regcode; /* Code-emit pointer; ®dummy = don't. */
83 static long regsize; /* Code size. */
85 static int regsawbracket; /* Did we do {d,d} trick? */
88 * Forward declarations for regcomp()'s friends.
90 STATIC int regcurly();
92 STATIC char *regbranch();
93 STATIC char *regpiece();
94 STATIC char *regatom();
95 STATIC char *regclass();
96 STATIC char *regnode();
98 STATIC void reginsert();
99 STATIC void regtail();
100 STATIC void regoptail();
103 - regcomp - compile a regular expression into internal code
105 * We can't allocate space until we know how big the compiled form will be,
106 * but we can't compile it (and thus know how big it is) until we've got a
107 * place to put the code. So we cheat: we compile it twice, once with code
108 * generation turned off and size counting turned on, and once "for real".
109 * This also means that we don't allocate space until we are sure that the
110 * thing really will compile successfully, and we never have to move the
111 * code and thus invalidate pointers into it. (Note that it has to be in
112 * one piece because free() must be able to free it all.) [NB: not true in perl]
114 * Beware that the optimization-preparation code in here knows about some
115 * of the structure of the compiled regexp. [I'll say.]
118 regcomp(exp,xend,fold,rare)
126 register STR *longest;
128 register char *first;
132 extern char *safemalloc();
133 extern char *savestr();
136 fatal("NULL regexp argument");
138 /* First pass: determine size, legality. */
142 regprecomp = nsavestr(exp,xend-exp);
148 if (reg(0, &flags) == NULL) {
149 Safefree(regprecomp);
153 /* Small enough for pointer-storage convention? */
154 if (regsize >= 32767L) /* Probably could be 65535L. */
155 FAIL("regexp too big");
157 /* Allocate space. */
158 Newc(1001, r, sizeof(regexp) + (unsigned)regsize, char, regexp);
160 FAIL("regexp out of space");
162 /* Second pass: emit code. */
164 bcopy(regprecomp,exp,xend-exp);
165 r->precomp = regprecomp;
169 regcode = r->program;
171 if (reg(0, &flags) == NULL)
174 /* Dig out information for optimizations. */
175 r->regstart = Nullstr; /* Worst-case defaults. */
177 r->regmust = Nullstr;
179 r->regstclass = Nullch;
180 scan = r->program+1; /* First BRANCH. */
181 if (OP(regnext(scan)) == END) {/* Only one top-level choice. */
182 scan = NEXTOPER(scan);
185 while ((OP(first) > OPEN && OP(first) < CLOSE) ||
186 (OP(first) == BRANCH && OP(regnext(first)) != BRANCH) ||
187 (OP(first) == PLUS) )
188 first = NEXTOPER(first);
190 /* Starting-point info. */
191 if (OP(first) == EXACTLY) {
193 str_make(OPERAND(first)+1,*OPERAND(first));
194 if (r->regstart->str_cur > !(sawstudy|fold))
195 fbmcompile(r->regstart,fold);
197 else if ((exp = index(simple,OP(first))) && exp > simple)
198 r->regstclass = first;
199 else if (OP(first) == BOUND || OP(first) == NBOUND)
200 r->regstclass = first;
201 else if (OP(first) == BOL)
206 fprintf(stderr,"first %d next %d offset %d\n",
207 OP(first), OP(NEXTOPER(first)), first - scan);
210 * If there's something expensive in the r.e., find the
211 * longest literal string that must appear and make it the
212 * regmust. Resolve ties in favor of later strings, since
213 * the regstart check works with the beginning of the r.e.
214 * and avoiding duplication strengthens checking. Not a
215 * strong reason, but sufficient in the absence of others.
216 * [Now we resolve ties in favor of the earlier string if
217 * it happens that curback has been invalidated, since the
218 * earlier string may buy us something the later one won't.]
220 longest = str_make("",0);
224 while (scan != NULL) {
225 if (OP(scan) == BRANCH) {
226 if (OP(regnext(scan)) == BRANCH) {
228 while (OP(scan) == BRANCH)
229 scan = regnext(scan);
231 else /* single branch is ok */
232 scan = NEXTOPER(scan);
234 if (OP(scan) == EXACTLY) {
236 while (OP(regnext(scan)) >= CLOSE)
237 scan = regnext(scan);
238 if (curback - back == len) {
239 str_ncat(longest, OPERAND(first)+1,
241 len += *OPERAND(first);
242 curback += *OPERAND(first);
243 first = regnext(scan);
245 else if (*OPERAND(first) >= len + (curback >= 0)) {
246 len = *OPERAND(first);
247 str_nset(longest, OPERAND(first)+1,len);
250 first = regnext(scan);
253 curback += *OPERAND(first);
255 else if (index(varies,OP(scan)))
257 else if (index(simple,OP(scan)))
259 scan = regnext(scan);
262 r->regmust = longest;
266 if (len > !(sawstudy||fold||OP(first)==EOL))
267 fbmcompile(r->regmust,fold);
268 r->regmust->str_u.str_useful = 100;
269 if (OP(first) == EOL) /* is match anchored to EOL? */
270 r->regmust->str_pok |= SP_TAIL;
276 r->do_folding = fold;
277 r->nparens = regnpar - 1;
286 - reg - regular expression, i.e. main body or parenthesized thing
288 * Caller must absorb opening parenthesis.
290 * Combining parenthesis handling with the base level of regular expression
291 * is a trifle forced, but the need to tie the tails of the branches to what
292 * follows makes it hard to avoid.
296 int paren; /* Parenthesized? */
301 register char *ender;
305 *flagp = HASWIDTH; /* Tentatively. */
307 /* Make an OPEN node, if parenthesized. */
309 if (regnpar >= NSUBEXP)
310 FAIL("too many () in regexp");
313 ret = regnode(OPEN+parno);
317 /* Pick up the branches, linking them together. */
318 br = regbranch(&flags);
322 regtail(ret, br); /* OPEN -> first. */
325 if (!(flags&HASWIDTH))
327 *flagp |= flags&SPSTART;
328 while (*regparse == '|') {
330 br = regbranch(&flags);
333 regtail(ret, br); /* BRANCH -> BRANCH. */
334 if (!(flags&HASWIDTH))
336 *flagp |= flags&SPSTART;
339 /* Make a closing node, and hook it on the end. */
340 ender = regnode((paren) ? CLOSE+parno : END);
343 /* Hook the tails of the branches to the closing node. */
344 for (br = ret; br != NULL; br = regnext(br))
345 regoptail(br, ender);
347 /* Check for proper termination. */
348 if (paren && *regparse++ != ')') {
349 FAIL("unmatched () in regexp");
350 } else if (!paren && regparse < regxend) {
351 if (*regparse == ')') {
352 FAIL("unmatched () in regexp");
354 FAIL("junk on end of regexp"); /* "Can't happen". */
362 - regbranch - one alternative of an | operator
364 * Implements the concatenation operator.
371 register char *chain;
372 register char *latest;
375 *flagp = WORST; /* Tentatively. */
377 ret = regnode(BRANCH);
379 while (regparse < regxend && *regparse != '|' && *regparse != ')') {
380 latest = regpiece(&flags);
383 *flagp |= flags&HASWIDTH;
384 if (chain == NULL) /* First piece. */
385 *flagp |= flags&SPSTART;
387 regtail(chain, latest);
390 if (chain == NULL) /* Loop ran zero times. */
391 (void) regnode(NOTHING);
397 - regpiece - something followed by possible [*+?]
399 * Note that the branching code sequences used for ? and the general cases
400 * of * and + are somewhat optimized: they use the same NOTHING node as
401 * both the endmarker for their branch list and the body of the last branch.
402 * It might seem that this node could be dispensed with entirely, but the
403 * endmarker role is not redundant.
413 char *origparse = regparse;
414 int orignpar = regnpar;
419 ret = regatom(&flags);
425 /* Here's a total kludge: if after the atom there's a {\d+,?\d*}
426 * then we decrement the first number by one and reset our
427 * parsing back to the beginning of the same atom. If the first number
428 * is down to 0, decrement the second number instead and fake up
429 * a ? after it. Given the way this compiler doesn't keep track
430 * of offsets on the first pass, this is the only way to replicate
431 * a piece of code. Sigh.
433 if (op == '{' && regcurly(regparse)) {
436 while (isdigit(*next) || *next == ',') {
445 if (*next == '}') { /* got one */
446 regsawbracket++; /* remember we clobbered exp */
450 iter = atoi(regparse);
453 sprintf(regparse,"%.*d", max-regparse, iter - 1);
455 if (*max == ',' && atoi(max+1) > 0) {
457 sprintf(max+1,"%.*d", next-(max+1), atoi(max+1) - 1);
460 if (iter != 1 || (*max == ',' || atoi(max+1))) {
461 regparse = origparse; /* back up input pointer */
462 regnpar = orignpar; /* don't make more parens */
474 if (max == next) { /* any number more? */
476 op = '*'; /* fake up one with a star */
479 op = '?'; /* fake up optional atom */
481 sprintf(max,"%.*d", next-max, iter - 1);
486 regparse = origparse - 1; /* offset ++ below */
491 fatal("Can't do {n,0}");
494 fatal("Can't do {0}");
503 if (!(flags&HASWIDTH) && op != '?')
504 FAIL("regexp *+ operand could be empty");
505 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
507 if (op == '*' && (flags&SIMPLE))
508 reginsert(STAR, ret);
509 else if (op == '*') {
510 /* Emit x* as (x&|), where & means "self". */
511 reginsert(BRANCH, ret); /* Either x */
512 regoptail(ret, regnode(BACK)); /* and loop */
513 regoptail(ret, ret); /* back */
514 regtail(ret, regnode(BRANCH)); /* or */
515 regtail(ret, regnode(NOTHING)); /* null. */
516 } else if (op == '+' && (flags&SIMPLE))
517 reginsert(PLUS, ret);
518 else if (op == '+') {
519 /* Emit x+ as x(&|), where & means "self". */
520 next = regnode(BRANCH); /* Either */
522 regtail(regnode(BACK), ret); /* loop back */
523 regtail(next, regnode(BRANCH)); /* or */
524 regtail(ret, regnode(NOTHING)); /* null. */
525 } else if (op == '?') {
526 /* Emit x? as (x|) */
527 reginsert(BRANCH, ret); /* Either x */
528 regtail(ret, regnode(BRANCH)); /* or */
529 next = regnode(NOTHING); /* null. */
531 regoptail(ret, next);
535 if (ISMULT2(regparse))
536 FAIL("nested *?+ in regexp");
542 - regatom - the lowest level
544 * Optimization: gobbles an entire sequence of ordinary characters so that
545 * it can turn them into a single node, which is smaller to store and
546 * faster to run. Backslashed characters are exceptions, each becoming a
547 * separate node; the code is simpler that way and it's not worth fixing.
549 * [Yes, it is worth fixing, some scripts can run twice the speed.]
558 *flagp = WORST; /* Tentatively. */
560 switch (*regparse++) {
569 *flagp |= HASWIDTH|SIMPLE;
573 *flagp |= HASWIDTH|SIMPLE;
576 ret = reg(1, &flags);
579 *flagp |= flags&(HASWIDTH|SPSTART);
583 FAIL("internal urp in regexp"); /* Supposed to be caught earlier. */
588 FAIL("?+* follows nothing in regexp");
593 ret = regnode(ALNUM);
594 *flagp |= HASWIDTH|SIMPLE;
598 ret = regnode(NALNUM);
599 *flagp |= HASWIDTH|SIMPLE;
603 ret = regnode(BOUND);
608 ret = regnode(NBOUND);
613 ret = regnode(SPACE);
614 *flagp |= HASWIDTH|SIMPLE;
618 ret = regnode(NSPACE);
619 *flagp |= HASWIDTH|SIMPLE;
623 ret = regnode(DIGIT);
624 *flagp |= HASWIDTH|SIMPLE;
628 ret = regnode(NDIGIT);
629 *flagp |= HASWIDTH|SIMPLE;
637 case '0': case '1': case '2': case '3': case '4':
638 case '5': case '6': case '7': case '8': case '9':
639 if (isdigit(regparse[1]))
642 ret = regnode(REF + *regparse++ - '0');
647 if (regparse >= regxend)
648 FAIL("trailing \\ in regexp");
662 ret = regnode(EXACTLY);
663 regc(0); /* save spot for len */
664 for (len=0, p=regparse-1;
665 len < 127 && p < regxend;
706 case '0': case '1': case '2': case '3':case '4':
707 case '5': case '6': case '7': case '8':case '9':
713 foo = (foo<<3) + *++p - '0';
724 FAIL("trailing \\ in regexp");
735 if (regfold && isupper(ender))
736 ender = tolower(ender);
737 if (ISMULT2(p)) { /* Back off on ?+*. */
751 FAIL("internal disaster in regexp");
755 if (regcode != ®dummy)
771 if (regcode == ®dummy)
774 bits[c >> 3] &= ~(1 << (c & 7));
776 bits[c >> 3] |= (1 << (c & 7));
784 register int lastclass;
785 register int range = 0;
789 if (*regparse == '^') { /* Complement of range. */
790 ret = regnode(ANYBUT);
794 ret = regnode(ANYOF);
798 for (class = 0; class < 32; class++)
800 if (*regparse == ']' || *regparse == '-')
801 regset(bits,def,lastclass = *regparse++);
802 while (regparse < regxend && *regparse != ']') {
803 class = UCHARAT(regparse++);
805 class = UCHARAT(regparse++);
808 for (class = 'a'; class <= 'z'; class++)
809 regset(bits,def,class);
810 for (class = 'A'; class <= 'Z'; class++)
811 regset(bits,def,class);
812 for (class = '0'; class <= '9'; class++)
813 regset(bits,def,class);
814 regset(bits,def,'_');
818 regset(bits,def,' ');
819 regset(bits,def,'\t');
820 regset(bits,def,'\r');
821 regset(bits,def,'\f');
822 regset(bits,def,'\n');
826 for (class = '0'; class <= '9'; class++)
827 regset(bits,def,class);
845 case '0': case '1': case '2': case '3': case '4':
846 case '5': case '6': case '7': case '8': case '9':
848 if (isdigit(*regparse)) {
850 class += *regparse++ - '0';
852 if (isdigit(*regparse)) {
854 class += *regparse++ - '0';
859 if (!range && class == '-' && regparse < regxend &&
865 if (lastclass > class)
866 FAIL("invalid [] range in regexp");
869 lastclass = class - 1;
871 for (lastclass++; lastclass <= class; lastclass++) {
872 regset(bits,def,lastclass);
873 if (regfold && isupper(lastclass))
874 regset(bits,def,tolower(lastclass));
878 if (*regparse != ']')
879 FAIL("unmatched [] in regexp");
885 - regnode - emit a node
887 static char * /* Location. */
895 if (ret == ®dummy) {
906 if (!((long)ret & 1))
912 *ptr++ = '\0'; /* Null "next" pointer. */
920 - regc - emit (if appropriate) a byte of code
926 if (regcode != ®dummy)
933 - reginsert - insert an operator in front of already-emitted operand
935 * Means relocating the operand.
944 register char *place;
946 if (regcode == ®dummy) {
965 place = opnd; /* Op node, where operand used to be. */
972 - regtail - set the next-pointer at the end of a node chain
986 /* Find last node. */
989 temp = regnext(scan);
998 *(short*)(scan+1) = offset;
1003 if (OP(scan) == BACK)
1004 offset = scan - val;
1006 offset = val - scan;
1007 *(scan+1) = (offset>>8)&0377;
1008 *(scan+2) = offset&0377;
1013 - regoptail - regtail on operand of first argument; nop if operandless
1020 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
1021 if (p == NULL || p == ®dummy || OP(p) != BRANCH)
1023 regtail(NEXTOPER(p), val);
1027 - regcurly - a little FSA that accepts {\d+,?\d*}
1051 - regdump - dump a regexp onto stderr in vaguely comprehensible form
1058 register char op = EXACTLY; /* Arbitrary non-END op. */
1059 register char *next;
1060 extern char *index();
1064 while (op != END) { /* While that wasn't END last time... */
1070 fprintf(stderr,"%2d%s", s-r->program, regprop(s)); /* Where, what. */
1072 if (next == NULL) /* Next ptr. */
1073 fprintf(stderr,"(0)");
1075 fprintf(stderr,"(%d)", (s-r->program)+(next-s));
1077 if (op == ANYOF || op == ANYBUT) {
1080 if (op == EXACTLY) {
1081 /* Literal string, where present. */
1083 while (*s != '\0') {
1089 (void)putchar('\n');
1092 /* Header fields of interest. */
1094 fprintf(stderr,"start `%s' ", r->regstart->str_ptr);
1096 fprintf(stderr,"stclass `%s' ", regprop(r->regstclass));
1098 fprintf(stderr,"anchored ");
1099 if (r->regmust != NULL)
1100 fprintf(stderr,"must have \"%s\" back %d ", r->regmust->str_ptr,
1102 fprintf(stderr,"\n");
1106 - regprop - printable representation of opcode
1114 (void) strcpy(buf, ":");
1181 (void)sprintf(buf+strlen(buf), "REF%d", OP(op)-REF);
1193 (void)sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
1205 (void)sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1215 FAIL("corrupted regexp opcode");
1218 (void) strcat(buf, p);
1221 #endif /* DEBUGGING */
1227 Safefree(r->precomp);
1229 Safefree(r->subbase);
1231 str_free(r->regmust);
1233 str_free(r->regstart);