]>
cvs.zerfleddert.de Git - micropolis/blob - src/tcl/regexp.c
2 * regcomp and regexec -- regsub and regerror are elsewhere
4 * Copyright (c) 1986 by University of Toronto.
5 * Written by Henry Spencer. Not derived from licensed software.
7 * Permission is granted to anyone to use this software for any
8 * purpose on any computer system, and to redistribute it freely,
9 * subject to the following restrictions:
11 * 1. The author is not responsible for the consequences of use of
12 * this software, no matter how awful, even if they arise
15 * 2. The origin of this software must not be misrepresented, either
16 * by explicit claim or by omission.
18 * 3. Altered versions must be plainly marked as such, and must not
19 * be misrepresented as being the original software.
21 * Beware that some of this code is subtly aware of the way operator
22 * precedence is structured in regular expressions. Serious changes in
23 * regular-expression syntax might require a total rethink.
25 * *** NOTE: this code has been altered slightly for use in Tcl. ***
26 * *** The only change is to use ckalloc and ckfree instead of ***
27 * *** malloc and free. ***
32 * The "internal use only" fields in regexp.h are present to pass info from
33 * compile to execute that permits the execute phase to run lots faster on
34 * simple cases. They are:
36 * regstart char that must begin a match; '\0' if none obvious
37 * reganch is the match anchored (at beginning-of-line only)?
38 * regmust string (pointer into program) that match must include, or NULL
39 * regmlen length of regmust string
41 * Regstart and reganch permit very fast decisions on suitable starting points
42 * for a match, cutting down the work a lot. Regmust permits fast rejection
43 * of lines that cannot possibly match. The regmust tests are costly enough
44 * that regcomp() supplies a regmust only if the r.e. contains something
45 * potentially expensive (at present, the only such thing detected is * or +
46 * at the start of the r.e., which can involve a lot of backup). Regmlen is
47 * supplied because the test in regexec() needs it and regcomp() is computing
52 * Structure for regexp "program". This is essentially a linear encoding
53 * of a nondeterministic finite-state machine (aka syntax charts or
54 * "railroad normal form" in parsing technology). Each node is an opcode
55 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
56 * all nodes except BRANCH implement concatenation; a "next" pointer with
57 * a BRANCH on both ends of it is connecting two alternatives. (Here we
58 * have one of the subtle syntax dependencies: an individual BRANCH (as
59 * opposed to a collection of them) is never concatenated with anything
60 * because of operator precedence.) The operand of some types of node is
61 * a literal string; for others, it is a node leading into a sub-FSM. In
62 * particular, the operand of a BRANCH node is the first node of the branch.
63 * (NB this is *not* a tree structure: the tail of the branch connects
64 * to the thing following the set of BRANCHes.) The opcodes are:
67 /* definition number opnd? meaning */
68 #define END 0 /* no End of program. */
69 #define BOL 1 /* no Match "" at beginning of line. */
70 #define EOL 2 /* no Match "" at end of line. */
71 #define ANY 3 /* no Match any one character. */
72 #define ANYOF 4 /* str Match any character in this string. */
73 #define ANYBUT 5 /* str Match any character not in this string. */
74 #define BRANCH 6 /* node Match this alternative, or the next... */
75 #define BACK 7 /* no Match "", "next" ptr points backward. */
76 #define EXACTLY 8 /* str Match this string. */
77 #define NOTHING 9 /* no Match empty string. */
78 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
79 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
80 #define OPEN 20 /* no Mark this point in input as start of #n. */
81 /* OPEN+1 is number 1, etc. */
82 #define CLOSE 30 /* no Analogous to OPEN. */
87 * BRANCH The set of branches constituting a single choice are hooked
88 * together with their "next" pointers, since precedence prevents
89 * anything being concatenated to any individual branch. The
90 * "next" pointer of the last BRANCH in a choice points to the
91 * thing following the whole choice. This is also where the
92 * final "next" pointer of each individual branch points; each
93 * branch starts with the operand node of a BRANCH node.
95 * BACK Normal "next" pointers all implicitly point forward; BACK
96 * exists to make loop structures possible.
98 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
99 * BRANCH structures using BACK. Simple cases (one character
100 * per match) are implemented with STAR and PLUS for speed
101 * and to minimize recursive plunges.
103 * OPEN,CLOSE ...are numbered at compile time.
107 * A node is one char of opcode followed by two chars of "next" pointer.
108 * "Next" pointers are stored as two 8-bit pieces, high order first. The
109 * value is a positive offset from the opcode of the node containing it.
110 * An operand, if any, simply follows the node. (Note that much of the
111 * code generation knows about this implicit relationship.)
113 * Using two bytes for the "next" pointer is vast overkill for most things,
114 * but allows patterns to get big without disasters.
117 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
118 #define OPERAND(p) ((p) + 3)
121 * See regmagic.h for one further detail of program structure.
126 * Utility definitions.
129 #define UCHARAT(p) ((int)*(unsigned char *)(p))
131 #define UCHARAT(p) ((int)*(p)&CHARBITS)
134 #define FAIL(m) { regerror(m); return(NULL); }
135 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
136 #define META "^$.[()|?+*\\"
139 * Flags to be passed up and down.
141 #define HASWIDTH 01 /* Known never to match null string. */
142 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
143 #define SPSTART 04 /* Starts with * or +. */
144 #define WORST 0 /* Worst case. */
147 * Global work variables for regcomp().
149 static char *regparse
; /* Input-scan pointer. */
150 static int regnpar
; /* () count. */
151 static char regdummy
;
152 static char *regcode
; /* Code-emit pointer; ®dummy = don't. */
153 static long regsize
; /* Code size. */
156 * The first byte of the regexp internal "program" is actually this magic
157 * number; the start node begins in the second byte.
163 * Forward declarations for regcomp()'s friends.
166 #define STATIC static
168 STATIC
char *reg(int paren
, int *flagp
);
169 STATIC
char *regbranch(int *flagp
);
170 STATIC
char *regpiece(int *flagp
);
171 STATIC
char *regatom(int *flagp
);
172 STATIC
char *regnode(int op
);
173 STATIC
char *regnext(register char *p
);
174 STATIC
void regc(int b
);
175 STATIC
void reginsert(int op
, char *opnd
);
176 STATIC
void regtail(char *p
, char *val
);
177 STATIC
void regoptail(char *p
, char *val
);
179 STATIC
int strcspn();
183 - regcomp - compile a regular expression into internal code
185 * We can't allocate space until we know how big the compiled form will be,
186 * but we can't compile it (and thus know how big it is) until we've got a
187 * place to put the code. So we cheat: we compile it twice, once with code
188 * generation turned off and size counting turned on, and once "for real".
189 * This also means that we don't allocate space until we are sure that the
190 * thing really will compile successfully, and we never have to move the
191 * code and thus invalidate pointers into it. (Note that it has to be in
192 * one piece because free() must be able to free it all.)
194 * Beware that the optimization-preparation code in here knows about some
195 * of the structure of the compiled regexp.
202 register char *longest
;
207 FAIL("NULL argument");
209 /* First pass: determine size, legality. */
215 if (reg(0, &flags
) == NULL
)
218 /* Small enough for pointer-storage convention? */
219 if (regsize
>= 32767L) /* Probably could be 65535L. */
220 FAIL("regexp too big");
222 /* Allocate space. */
223 r
= (regexp
*)ckalloc(sizeof(regexp
) + (unsigned)regsize
);
225 FAIL("out of space");
227 /* Second pass: emit code. */
230 regcode
= r
->program
;
232 if (reg(0, &flags
) == NULL
)
235 /* Dig out information for optimizations. */
236 r
->regstart
= '\0'; /* Worst-case defaults. */
240 scan
= r
->program
+1; /* First BRANCH. */
241 if (OP(regnext(scan
)) == END
) { /* Only one top-level choice. */
242 scan
= OPERAND(scan
);
244 /* Starting-point info. */
245 if (OP(scan
) == EXACTLY
)
246 r
->regstart
= *OPERAND(scan
);
247 else if (OP(scan
) == BOL
)
251 * If there's something expensive in the r.e., find the
252 * longest literal string that must appear and make it the
253 * regmust. Resolve ties in favor of later strings, since
254 * the regstart check works with the beginning of the r.e.
255 * and avoiding duplication strengthens checking. Not a
256 * strong reason, but sufficient in the absence of others.
261 for (; scan
!= NULL
; scan
= regnext(scan
))
262 if (OP(scan
) == EXACTLY
&& strlen(OPERAND(scan
)) >= len
) {
263 longest
= OPERAND(scan
);
264 len
= strlen(OPERAND(scan
));
266 r
->regmust
= longest
;
275 - reg - regular expression, i.e. main body or parenthesized thing
277 * Caller must absorb opening parenthesis.
279 * Combining parenthesis handling with the base level of regular expression
280 * is a trifle forced, but the need to tie the tails of the branches to what
281 * follows makes it hard to avoid.
285 int paren
, /* Parenthesized? */
291 register char *ender
;
292 register int parno
= 0;
295 *flagp
= HASWIDTH
; /* Tentatively. */
297 /* Make an OPEN node, if parenthesized. */
299 if (regnpar
>= NSUBEXP
)
303 ret
= regnode(OPEN
+parno
);
307 /* Pick up the branches, linking them together. */
308 br
= regbranch(&flags
);
312 regtail(ret
, br
); /* OPEN -> first. */
315 if (!(flags
&HASWIDTH
))
317 *flagp
|= flags
&SPSTART
;
318 while (*regparse
== '|') {
320 br
= regbranch(&flags
);
323 regtail(ret
, br
); /* BRANCH -> BRANCH. */
324 if (!(flags
&HASWIDTH
))
326 *flagp
|= flags
&SPSTART
;
329 /* Make a closing node, and hook it on the end. */
330 ender
= regnode((paren
) ? CLOSE
+parno
: END
);
333 /* Hook the tails of the branches to the closing node. */
334 for (br
= ret
; br
!= NULL
; br
= regnext(br
))
335 regoptail(br
, ender
);
337 /* Check for proper termination. */
338 if (paren
&& *regparse
++ != ')') {
339 FAIL("unmatched ()");
340 } else if (!paren
&& *regparse
!= '\0') {
341 if (*regparse
== ')') {
342 FAIL("unmatched ()");
344 FAIL("junk on end"); /* "Can't happen". */
352 - regbranch - one alternative of an | operator
354 * Implements the concatenation operator.
357 regbranch (int *flagp
)
360 register char *chain
;
361 register char *latest
;
364 *flagp
= WORST
; /* Tentatively. */
366 ret
= regnode(BRANCH
);
368 while (*regparse
!= '\0' && *regparse
!= '|' && *regparse
!= ')') {
369 latest
= regpiece(&flags
);
372 *flagp
|= flags
&HASWIDTH
;
373 if (chain
== NULL
) /* First piece. */
374 *flagp
|= flags
&SPSTART
;
376 regtail(chain
, latest
);
379 if (chain
== NULL
) /* Loop ran zero times. */
380 (void) regnode(NOTHING
);
386 - regpiece - something followed by possible [*+?]
388 * Note that the branching code sequences used for ? and the general cases
389 * of * and + are somewhat optimized: they use the same NOTHING node as
390 * both the endmarker for their branch list and the body of the last branch.
391 * It might seem that this node could be dispensed with entirely, but the
392 * endmarker role is not redundant.
395 regpiece (int *flagp
)
402 ret
= regatom(&flags
);
412 if (!(flags
&HASWIDTH
) && op
!= '?')
413 FAIL("*+ operand could be empty");
414 *flagp
= (op
!= '+') ? (WORST
|SPSTART
) : (WORST
|HASWIDTH
);
416 if (op
== '*' && (flags
&SIMPLE
))
417 reginsert(STAR
, ret
);
418 else if (op
== '*') {
419 /* Emit x* as (x&|), where & means "self". */
420 reginsert(BRANCH
, ret
); /* Either x */
421 regoptail(ret
, regnode(BACK
)); /* and loop */
422 regoptail(ret
, ret
); /* back */
423 regtail(ret
, regnode(BRANCH
)); /* or */
424 regtail(ret
, regnode(NOTHING
)); /* null. */
425 } else if (op
== '+' && (flags
&SIMPLE
))
426 reginsert(PLUS
, ret
);
427 else if (op
== '+') {
428 /* Emit x+ as x(&|), where & means "self". */
429 next
= regnode(BRANCH
); /* Either */
431 regtail(regnode(BACK
), ret
); /* loop back */
432 regtail(next
, regnode(BRANCH
)); /* or */
433 regtail(ret
, regnode(NOTHING
)); /* null. */
434 } else if (op
== '?') {
435 /* Emit x? as (x|) */
436 reginsert(BRANCH
, ret
); /* Either x */
437 regtail(ret
, regnode(BRANCH
)); /* or */
438 next
= regnode(NOTHING
); /* null. */
440 regoptail(ret
, next
);
443 if (ISMULT(*regparse
))
450 - regatom - the lowest level
452 * Optimization: gobbles an entire sequence of ordinary characters so that
453 * it can turn them into a single node, which is smaller to store and
454 * faster to run. Backslashed characters are exceptions, each becoming a
455 * separate node; the code is simpler that way and it's not worth fixing.
463 *flagp
= WORST
; /* Tentatively. */
465 switch (*regparse
++) {
474 *flagp
|= HASWIDTH
|SIMPLE
;
478 register int classend
;
480 if (*regparse
== '^') { /* Complement of range. */
481 ret
= regnode(ANYBUT
);
484 ret
= regnode(ANYOF
);
485 if (*regparse
== ']' || *regparse
== '-')
487 while (*regparse
!= '\0' && *regparse
!= ']') {
488 if (*regparse
== '-') {
490 if (*regparse
== ']' || *regparse
== '\0')
493 clss
= UCHARAT(regparse
-2)+1;
494 classend
= UCHARAT(regparse
);
495 if (clss
> classend
+1)
496 FAIL("invalid [] range");
497 for (; clss
<= classend
; clss
++)
505 if (*regparse
!= ']')
506 FAIL("unmatched []");
508 *flagp
|= HASWIDTH
|SIMPLE
;
512 ret
= reg(1, &flags
);
515 *flagp
|= flags
&(HASWIDTH
|SPSTART
);
520 FAIL("internal urp"); /* Supposed to be caught earlier. */
526 FAIL("?+* follows nothing");
530 if (*regparse
== '\0')
532 ret
= regnode(EXACTLY
);
535 *flagp
|= HASWIDTH
|SIMPLE
;
542 len
= strcspn(regparse
, META
);
544 FAIL("internal disaster");
545 ender
= *(regparse
+len
);
546 if (len
> 1 && ISMULT(ender
))
547 len
--; /* Back off clear of ?+* operand. */
551 ret
= regnode(EXACTLY
);
565 - regnode - emit a node
574 if (ret
== ®dummy
) {
581 *ptr
++ = '\0'; /* Null "next" pointer. */
589 - regc - emit (if appropriate) a byte of code
594 if (regcode
!= ®dummy
)
601 - reginsert - insert an operator in front of already-emitted operand
603 * Means relocating the operand.
606 reginsert (int op
, char *opnd
)
610 register char *place
;
612 if (regcode
== ®dummy
) {
623 place
= opnd
; /* Op node, where operand used to be. */
630 - regtail - set the next-pointer at the end of a node chain
633 regtail (char *p
, char *val
)
642 /* Find last node. */
645 temp
= regnext(scan
);
651 if (OP(scan
) == BACK
)
655 *(scan
+1) = (offset
>>8)&0377;
656 *(scan
+2) = offset
&0377;
660 - regoptail - regtail on operand of first argument; nop if operandless
663 regoptail (char *p
, char *val
)
665 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
666 if (p
== NULL
|| p
== ®dummy
|| OP(p
) != BRANCH
)
668 regtail(OPERAND(p
), val
);
672 * regexec and friends
676 * Global work variables for regexec().
678 static char *reginput
; /* String-input pointer. */
679 static char *regbol
; /* Beginning of input, for ^ check. */
680 static char **regstartp
; /* Pointer to startp array. */
681 static char **regendp
; /* Ditto for endp. */
686 STATIC
int regtry(regexp
*prog
, char *string
);
687 STATIC
int regmatch(char *prog
);
688 STATIC
int regrepeat(char *p
);
693 STATIC
char *regprop();
697 - regexec - match a regexp against a string
700 regexec (register regexp
*prog
, register char *string
)
704 extern char *strchr();
708 if (prog
== NULL
|| string
== NULL
) {
709 regerror("NULL parameter");
713 /* Check validity of program. */
714 if (UCHARAT(prog
->program
) != MAGIC
) {
715 regerror("corrupted program");
719 /* If there is a "must appear" string, look for it. */
720 if (prog
->regmust
!= NULL
) {
722 while ((s
= strchr(s
, prog
->regmust
[0])) != NULL
) {
723 if (strncmp(s
, prog
->regmust
, prog
->regmlen
) == 0)
724 break; /* Found it. */
727 if (s
== NULL
) /* Not present. */
731 /* Mark beginning of line for ^ . */
734 /* Simplest case: anchored match need be tried only once. */
736 return(regtry(prog
, string
));
738 /* Messy cases: unanchored match. */
740 if (prog
->regstart
!= '\0')
741 /* We know what char it must start with. */
742 while ((s
= strchr(s
, prog
->regstart
)) != NULL
) {
748 /* We don't -- general case. */
752 } while (*s
++ != '\0');
759 - regtry - try match at specific point
761 static int /* 0 failure, 1 success */
762 regtry(regexp
*prog
, char *string
)
769 regstartp
= prog
->startp
;
770 regendp
= prog
->endp
;
774 for (i
= NSUBEXP
; i
> 0; i
--) {
778 if (regmatch(prog
->program
+ 1)) {
779 prog
->startp
[0] = string
;
780 prog
->endp
[0] = reginput
;
787 - regmatch - main matching routine
789 * Conceptually the strategy is simple: check to see whether the current
790 * node matches, call self recursively to see whether the rest matches,
791 * and then act accordingly. In practice we make some effort to avoid
792 * recursion, in particular by going through "ordinary" nodes (that don't
793 * need to know whether the rest of the match failed) by a loop instead of
796 static int /* 0 failure, 1 success */
799 register char *scan
; /* Current node. */
800 char *next
; /* Next node. */
802 extern char *strchr();
807 if (scan
!= NULL
&& regnarrate
)
808 fprintf(stderr
, "%s(\n", regprop(scan
));
810 while (scan
!= NULL
) {
813 fprintf(stderr
, "%s...\n", regprop(scan
));
815 next
= regnext(scan
);
819 if (reginput
!= regbol
)
823 if (*reginput
!= '\0')
827 if (*reginput
== '\0')
835 opnd
= OPERAND(scan
);
836 /* Inline the first character, for speed. */
837 if (*opnd
!= *reginput
)
840 if (len
> 1 && strncmp(opnd
, reginput
, len
) != 0)
846 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) == NULL
)
851 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) != NULL
)
871 no
= OP(scan
) - OPEN
;
874 if (regmatch(next
)) {
876 * Don't set startp if some later
877 * invocation of the same parentheses
880 if (regstartp
[no
] == NULL
)
881 regstartp
[no
] = save
;
900 no
= OP(scan
) - CLOSE
;
903 if (regmatch(next
)) {
905 * Don't set endp if some later
906 * invocation of the same parentheses
909 if (regendp
[no
] == NULL
)
920 if (OP(next
) != BRANCH
) /* No choice. */
921 next
= OPERAND(scan
); /* Avoid recursion. */
925 if (regmatch(OPERAND(scan
)))
928 scan
= regnext(scan
);
929 } while (scan
!= NULL
&& OP(scan
) == BRANCH
);
938 register char nextch
;
944 * Lookahead to avoid useless match attempts
945 * when we know what character comes next.
948 if (OP(next
) == EXACTLY
)
949 nextch
= *OPERAND(next
);
950 min
= (OP(scan
) == STAR
) ? 0 : 1;
952 no
= regrepeat(OPERAND(scan
));
954 /* If it could work, try it. */
955 if (nextch
== '\0' || *reginput
== nextch
)
958 /* Couldn't or didn't -- back up. */
960 reginput
= save
+ no
;
967 return(1); /* Success! */
971 regerror("memory corruption");
981 * We get here only if there's trouble -- normally "case END" is
982 * the terminating point.
984 regerror("corrupted pointers");
989 - regrepeat - repeatedly match something simple, report how many
994 register int count
= 0;
1002 count
= strlen(scan
);
1006 while (*opnd
== *scan
) {
1012 while (*scan
!= '\0' && strchr(opnd
, *scan
) != NULL
) {
1018 while (*scan
!= '\0' && strchr(opnd
, *scan
) == NULL
) {
1023 default: /* Oh dear. Called inappropriately. */
1024 regerror("internal foulup");
1025 count
= 0; /* Best compromise. */
1034 - regnext - dig the "next" pointer out of a node
1037 regnext (register char *p
)
1039 register int offset
;
1056 STATIC
char *regprop();
1059 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1065 register char op
= EXACTLY
; /* Arbitrary non-END op. */
1066 register char *next
;
1067 extern char *strchr();
1071 while (op
!= END
) { /* While that wasn't END last time... */
1073 printf("%2d%s", s
-r
->program
, regprop(s
)); /* Where, what. */
1075 if (next
== NULL
) /* Next ptr. */
1078 printf("(%d)", (s
-r
->program
)+(next
-s
));
1080 if (op
== ANYOF
|| op
== ANYBUT
|| op
== EXACTLY
) {
1081 /* Literal string, where present. */
1082 while (*s
!= '\0') {
1091 /* Header fields of interest. */
1092 if (r
->regstart
!= '\0')
1093 printf("start `%c' ", r
->regstart
);
1095 printf("anchored ");
1096 if (r
->regmust
!= NULL
)
1097 printf("must have \"%s\"", r
->regmust
);
1102 - regprop - printable representation of opcode
1108 static char buf
[50];
1110 (void) strcpy(buf
, ":");
1152 sprintf(buf
+strlen(buf
), "OPEN%d", OP(op
)-OPEN
);
1164 sprintf(buf
+strlen(buf
), "CLOSE%d", OP(op
)-CLOSE
);
1174 regerror("corrupted opcode");
1178 (void) strcat(buf
, p
);
1184 * The following is provided for those people who do not have strcspn() in
1185 * their C libraries. They should get off their butts and do something
1186 * about it; at least one public-domain implementation of those (highly
1187 * useful) string routines has been published on Usenet.
1191 * strcspn - find length of initial segment of s1 consisting entirely
1192 * of characters not from s2
1196 strcspn (char *s1
, char *s2
)
1198 register char *scan1
;
1199 register char *scan2
;
1203 for (scan1
= s1
; *scan1
!= '\0'; scan1
++) {
1204 for (scan2
= s2
; *scan2
!= '\0';) /* ++ moved down. */
1205 if (*scan1
== *scan2
++)