1 /* $DragonFly: src/lib/libcompat/regexp/regexp.c,v 1.3 2008/09/30 16:57:04 swildner Exp $ */
3 * regcomp and regexec -- regsub and regerror are elsewhere
5 * Copyright (c) 1986 by University of Toronto.
6 * Written by Henry Spencer. Not derived from licensed software.
8 * Permission is granted to anyone to use this software for any
9 * purpose on any computer system, and to redistribute it freely,
10 * subject to the following restrictions:
12 * 1. The author is not responsible for the consequences of use of
13 * this software, no matter how awful, even if they arise
16 * 2. The origin of this software must not be misrepresented, either
17 * by explicit claim or by omission.
19 * 3. Altered versions must be plainly marked as such, and must not
20 * be misrepresented as being the original software.
21 *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
22 *** hoptoad!gnu, on 27 Dec 1986, to add \n as an alternative to |
23 *** to assist in implementing egrep.
24 *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
25 *** hoptoad!gnu, on 27 Dec 1986, to add \< and \> for word-matching
26 *** as in BSD grep and ex.
27 *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
28 *** hoptoad!gnu, on 28 Dec 1986, to optimize characters quoted with \.
29 *** THIS IS AN ALTERED VERSION. It was altered by James A. Woods,
30 *** ames!jaw, on 19 June 1987, to quash a regcomp() redundancy.
32 * Beware that some of this code is subtly aware of the way operator
33 * precedence is structured in regular expressions. Serious changes in
34 * regular-expression syntax might require a total rethink.
46 * The "internal use only" fields in regexp.h are present to pass info from
47 * compile to execute that permits the execute phase to run lots faster on
48 * simple cases. They are:
50 * regstart char that must begin a match; '\0' if none obvious
51 * reganch is the match anchored (at beginning-of-line only)?
52 * regmust string (pointer into program) that match must include, or NULL
53 * regmlen length of regmust string
55 * Regstart and reganch permit very fast decisions on suitable starting points
56 * for a match, cutting down the work a lot. Regmust permits fast rejection
57 * of lines that cannot possibly match. The regmust tests are costly enough
58 * that regcomp() supplies a regmust only if the r.e. contains something
59 * potentially expensive (at present, the only such thing detected is * or +
60 * at the start of the r.e., which can involve a lot of backup). Regmlen is
61 * supplied because the test in regexec() needs it and regcomp() is computing
66 * Structure for regexp "program". This is essentially a linear encoding
67 * of a nondeterministic finite-state machine (aka syntax charts or
68 * "railroad normal form" in parsing technology). Each node is an opcode
69 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
70 * all nodes except BRANCH implement concatenation; a "next" pointer with
71 * a BRANCH on both ends of it is connecting two alternatives. (Here we
72 * have one of the subtle syntax dependencies: an individual BRANCH (as
73 * opposed to a collection of them) is never concatenated with anything
74 * because of operator precedence.) The operand of some types of node is
75 * a literal string; for others, it is a node leading into a sub-FSM. In
76 * particular, the operand of a BRANCH node is the first node of the branch.
77 * (NB this is *not* a tree structure: the tail of the branch connects
78 * to the thing following the set of BRANCHes.) The opcodes are:
81 /* definition number opnd? meaning */
82 #define END 0 /* no End of program. */
83 #define BOL 1 /* no Match "" at beginning of line. */
84 #define EOL 2 /* no Match "" at end of line. */
85 #define ANY 3 /* no Match any one character. */
86 #define ANYOF 4 /* str Match any character in this string. */
87 #define ANYBUT 5 /* str Match any character not in this string. */
88 #define BRANCH 6 /* node Match this alternative, or the next... */
89 #define BACK 7 /* no Match "", "next" ptr points backward. */
90 #define EXACTLY 8 /* str Match this string. */
91 #define NOTHING 9 /* no Match empty string. */
92 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
93 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
94 #define WORDA 12 /* no Match "" at wordchar, where prev is nonword */
95 #define WORDZ 13 /* no Match "" at nonwordchar, where prev is word */
96 #define OPEN 20 /* no Mark this point in input as start of #n. */
97 /* OPEN+1 is number 1, etc. */
98 #define CLOSE 30 /* no Analogous to OPEN. */
103 * BRANCH The set of branches constituting a single choice are hooked
104 * together with their "next" pointers, since precedence prevents
105 * anything being concatenated to any individual branch. The
106 * "next" pointer of the last BRANCH in a choice points to the
107 * thing following the whole choice. This is also where the
108 * final "next" pointer of each individual branch points; each
109 * branch starts with the operand node of a BRANCH node.
111 * BACK Normal "next" pointers all implicitly point forward; BACK
112 * exists to make loop structures possible.
114 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
115 * BRANCH structures using BACK. Simple cases (one character
116 * per match) are implemented with STAR and PLUS for speed
117 * and to minimize recursive plunges.
119 * OPEN,CLOSE ...are numbered at compile time.
123 * A node is one char of opcode followed by two chars of "next" pointer.
124 * "Next" pointers are stored as two 8-bit pieces, high order first. The
125 * value is a positive offset from the opcode of the node containing it.
126 * An operand, if any, simply follows the node. (Note that much of the
127 * code generation knows about this implicit relationship.)
129 * Using two bytes for the "next" pointer is vast overkill for most things,
130 * but allows patterns to get big without disasters.
133 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
134 #define OPERAND(p) ((p) + 3)
137 * See regmagic.h for one further detail of program structure.
142 * Utility definitions.
145 #define UCHARAT(p) ((int)*(unsigned char *)(p))
147 #define UCHARAT(p) ((int)*(p)&CHARBITS)
150 #define FAIL(m) { regerror(m); return(NULL); }
151 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
154 * Flags to be passed up and down.
156 #define HASWIDTH 01 /* Known never to match null string. */
157 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
158 #define SPSTART 04 /* Starts with * or +. */
159 #define WORST 0 /* Worst case. */
162 * Global work variables for regcomp().
164 static char *regparse
; /* Input-scan pointer. */
165 static int regnpar
; /* () count. */
166 static char regdummy
;
167 static char *regcode
; /* Code-emit pointer; ®dummy = don't. */
168 static long regsize
; /* Code size. */
171 * Forward declarations for regcomp()'s friends.
174 #define STATIC static
176 STATIC
char *reg(int, int *);
177 STATIC
char *regbranch(int *);
178 STATIC
char *regpiece(int *);
179 STATIC
char *regatom(int *);
180 STATIC
char *regnode(char);
181 STATIC
char *regnext(char *);
182 STATIC
void regc(char);
183 STATIC
void reginsert(char, char *);
184 STATIC
void regtail(char *, char *);
185 STATIC
void regoptail(char *, char *);
187 STATIC
int strcspn(char *, char *);
191 - regcomp - compile a regular expression into internal code
193 * We can't allocate space until we know how big the compiled form will be,
194 * but we can't compile it (and thus know how big it is) until we've got a
195 * place to put the code. So we cheat: we compile it twice, once with code
196 * generation turned off and size counting turned on, and once "for real".
197 * This also means that we don't allocate space until we are sure that the
198 * thing really will compile successfully, and we never have to move the
199 * code and thus invalidate pointers into it. (Note that it has to be in
200 * one piece because free() must be able to free it all.)
202 * Beware that the optimization-preparation code in here knows about some
203 * of the structure of the compiled regexp.
206 regcomp(const char *exp
)
215 FAIL("NULL argument");
217 /* First pass: determine size, legality. */
219 if (exp
[0] == '.' && exp
[1] == '*') exp
+= 2; /* aid grep */
221 regparse
= (char *)exp
;
226 if (reg(0, &flags
) == NULL
)
229 /* Small enough for pointer-storage convention? */
230 if (regsize
>= 32767L) /* Probably could be 65535L. */
231 FAIL("regexp too big");
233 /* Allocate space. */
234 r
= (regexp
*)malloc(sizeof(regexp
) + (unsigned)regsize
);
236 FAIL("out of space");
238 /* Second pass: emit code. */
239 regparse
= (char *)exp
;
241 regcode
= r
->program
;
243 if (reg(0, &flags
) == NULL
)
246 /* Dig out information for optimizations. */
247 r
->regstart
= '\0'; /* Worst-case defaults. */
251 scan
= r
->program
+1; /* First BRANCH. */
252 if (OP(regnext(scan
)) == END
) { /* Only one top-level choice. */
253 scan
= OPERAND(scan
);
255 /* Starting-point info. */
256 if (OP(scan
) == EXACTLY
)
257 r
->regstart
= *OPERAND(scan
);
258 else if (OP(scan
) == BOL
)
262 * If there's something expensive in the r.e., find the
263 * longest literal string that must appear and make it the
264 * regmust. Resolve ties in favor of later strings, since
265 * the regstart check works with the beginning of the r.e.
266 * and avoiding duplication strengthens checking. Not a
267 * strong reason, but sufficient in the absence of others.
272 for (; scan
!= NULL
; scan
= regnext(scan
))
273 if (OP(scan
) == EXACTLY
&& strlen(OPERAND(scan
)) >= len
) {
274 longest
= OPERAND(scan
);
275 len
= strlen(OPERAND(scan
));
277 r
->regmust
= longest
;
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.
295 reg(int paren
, int *flagp
)
303 *flagp
= HASWIDTH
; /* Tentatively. */
305 /* Make an OPEN node, if parenthesized. */
307 if (regnpar
>= NSUBEXP
)
311 ret
= regnode(OPEN
+parno
);
315 /* Pick up the branches, linking them together. */
316 br
= regbranch(&flags
);
320 regtail(ret
, br
); /* OPEN -> first. */
323 if (!(flags
&HASWIDTH
))
325 *flagp
|= flags
&SPSTART
;
326 while (*regparse
== '|' || *regparse
== '\n') {
328 br
= regbranch(&flags
);
331 regtail(ret
, br
); /* BRANCH -> BRANCH. */
332 if (!(flags
&HASWIDTH
))
334 *flagp
|= flags
&SPSTART
;
337 /* Make a closing node, and hook it on the end. */
338 ender
= regnode((paren
) ? CLOSE
+parno
: END
);
341 /* Hook the tails of the branches to the closing node. */
342 for (br
= ret
; br
!= NULL
; br
= regnext(br
))
343 regoptail(br
, ender
);
345 /* Check for proper termination. */
346 if (paren
&& *regparse
++ != ')') {
347 FAIL("unmatched ()");
348 } else if (!paren
&& *regparse
!= '\0') {
349 if (*regparse
== ')') {
350 FAIL("unmatched ()");
352 FAIL("junk on end"); /* "Can't happen". */
360 - regbranch - one alternative of an | operator
362 * Implements the concatenation operator.
365 regbranch(int *flagp
)
372 *flagp
= WORST
; /* Tentatively. */
374 ret
= regnode(BRANCH
);
376 while (*regparse
!= '\0' && *regparse
!= ')' &&
377 *regparse
!= '\n' && *regparse
!= '|') {
378 latest
= regpiece(&flags
);
381 *flagp
|= flags
&HASWIDTH
;
382 if (chain
== NULL
) /* First piece. */
383 *flagp
|= flags
&SPSTART
;
385 regtail(chain
, latest
);
388 if (chain
== NULL
) /* Loop ran zero times. */
389 (void) regnode(NOTHING
);
395 - regpiece - something followed by possible [*+?]
397 * Note that the branching code sequences used for ? and the general cases
398 * of * and + are somewhat optimized: they use the same NOTHING node as
399 * both the endmarker for their branch list and the body of the last branch.
400 * It might seem that this node could be dispensed with entirely, but the
401 * endmarker role is not redundant.
411 ret
= regatom(&flags
);
421 if (!(flags
&HASWIDTH
) && op
!= '?')
422 FAIL("*+ operand could be empty");
423 *flagp
= (op
!= '+') ? (WORST
|SPSTART
) : (WORST
|HASWIDTH
);
425 if (op
== '*' && (flags
&SIMPLE
))
426 reginsert(STAR
, ret
);
427 else if (op
== '*') {
428 /* Emit x* as (x&|), where & means "self". */
429 reginsert(BRANCH
, ret
); /* Either x */
430 regoptail(ret
, regnode(BACK
)); /* and loop */
431 regoptail(ret
, ret
); /* back */
432 regtail(ret
, regnode(BRANCH
)); /* or */
433 regtail(ret
, regnode(NOTHING
)); /* null. */
434 } else if (op
== '+' && (flags
&SIMPLE
))
435 reginsert(PLUS
, ret
);
436 else if (op
== '+') {
437 /* Emit x+ as x(&|), where & means "self". */
438 next
= regnode(BRANCH
); /* Either */
440 regtail(regnode(BACK
), ret
); /* loop back */
441 regtail(next
, regnode(BRANCH
)); /* or */
442 regtail(ret
, regnode(NOTHING
)); /* null. */
443 } else if (op
== '?') {
444 /* Emit x? as (x|) */
445 reginsert(BRANCH
, ret
); /* Either x */
446 regtail(ret
, regnode(BRANCH
)); /* or */
447 next
= regnode(NOTHING
); /* null. */
449 regoptail(ret
, next
);
452 if (ISMULT(*regparse
))
459 - regatom - the lowest level
461 * Optimization: gobbles an entire sequence of ordinary characters so that
462 * it can turn them into a single node, which is smaller to store and
463 * faster to run. Backslashed characters are exceptions, each becoming a
464 * separate node; the code is simpler that way and it's not worth fixing.
472 *flagp
= WORST
; /* Tentatively. */
474 switch (*regparse
++) {
475 /* FIXME: these chars only have meaning at beg/end of pat? */
484 *flagp
|= HASWIDTH
|SIMPLE
;
491 if (*regparse
== '^') { /* Complement of range. */
492 ret
= regnode(ANYBUT
);
495 ret
= regnode(ANYOF
);
496 if (*regparse
== ']' || *regparse
== '-')
498 while (*regparse
!= '\0' && *regparse
!= ']') {
499 if (*regparse
== '-') {
501 if (*regparse
== ']' || *regparse
== '\0')
504 class = UCHARAT(regparse
-2);
505 classend
= UCHARAT(regparse
);
506 if (__collate_load_error
) {
507 if (class > classend
)
508 FAIL("invalid [] range");
509 for (class++; class <= classend
; class++)
512 if (__collate_range_cmp(class, classend
) > 0)
513 FAIL("invalid [] range");
514 for (i
= 0; i
<= UCHAR_MAX
; i
++)
516 && __collate_range_cmp(class, i
) <= 0
517 && __collate_range_cmp(i
, classend
) <= 0
527 if (*regparse
!= ']')
528 FAIL("unmatched []");
530 *flagp
|= HASWIDTH
|SIMPLE
;
534 ret
= reg(1, &flags
);
537 *flagp
|= flags
&(HASWIDTH
|SPSTART
);
543 FAIL("internal urp"); /* Supposed to be caught earlier. */
548 FAIL("?+* follows nothing");
551 switch (*regparse
++) {
556 ret
= regnode(WORDA
);
559 ret
= regnode(WORDZ
);
561 /* FIXME: Someday handle \1, \2, ... */
563 /* Handle general quoted chars in exact-match routine */
570 * Encode a string of characters to be matched exactly.
572 * This is a bit tricky due to quoted chars and due to
573 * '*', '+', and '?' taking the SINGLE char previous
576 * On entry, the char at regparse[-1] is going to go
577 * into the string, no matter what it is. (It could be
578 * following a \ if we are entered from the '\' case.)
580 * Basic idea is to pick up a good char in ch and
581 * examine the next char. If it's *+? then we twiddle.
582 * If it's \ then we frozzle. If it's other magic char
583 * we push ch and terminate the string. If none of the
584 * above, we push ch on the string and go around again.
586 * regprev is used to remember where "the current char"
587 * starts in the string, if due to a *+? we need to back
588 * up and put the current char in a separate, 1-char, string.
589 * When regprev is NULL, ch is the only char in the
590 * string; this is used in *+? handling, and in setting
591 * flags |= SIMPLE at the end.
597 regparse
--; /* Look at cur char */
598 ret
= regnode(EXACTLY
);
599 for ( regprev
= 0 ; ; ) {
600 ch
= *regparse
++; /* Get current char */
601 switch (*regparse
) { /* look at next one */
604 regc(ch
); /* Add cur to string */
607 case '.': case '[': case '(':
608 case ')': case '|': case '\n':
611 /* FIXME, $ and ^ should not always be magic */
613 regc(ch
); /* dump cur char */
614 goto done
; /* and we are done */
616 case '?': case '+': case '*':
617 if (!regprev
) /* If just ch in str, */
618 goto magic
; /* use it */
619 /* End mult-char string one early */
620 regparse
= regprev
; /* Back up parse */
624 regc(ch
); /* Cur char OK */
625 switch (regparse
[1]){ /* Look after \ */
629 /* FIXME: Someday handle \1, \2, ... */
630 goto done
; /* Not quoted */
632 /* Backup point is \, scan * point is after it. */
635 continue; /* NOT break; */
638 regprev
= regparse
; /* Set backup point */
643 if (!regprev
) /* One char? */
653 - regnode - emit a node
655 static char * /* Location. */
662 if (ret
== ®dummy
) {
669 *ptr
++ = '\0'; /* Null "next" pointer. */
677 - regc - emit (if appropriate) a byte of code
682 if (regcode
!= ®dummy
)
689 - reginsert - insert an operator in front of already-emitted operand
691 * Means relocating the operand.
694 reginsert(char op
, char *opnd
)
700 if (regcode
== ®dummy
) {
711 place
= opnd
; /* Op node, where operand used to be. */
718 - regtail - set the next-pointer at the end of a node chain
721 regtail(char *p
, char *val
)
730 /* Find last node. */
733 temp
= regnext(scan
);
739 if (OP(scan
) == BACK
)
743 *(scan
+1) = (offset
>>8)&0377;
744 *(scan
+2) = offset
&0377;
748 - regoptail - regtail on operand of first argument; nop if operandless
751 regoptail(char *p
, char *val
)
753 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
754 if (p
== NULL
|| p
== ®dummy
|| OP(p
) != BRANCH
)
756 regtail(OPERAND(p
), val
);
760 * regexec and friends
764 * Global work variables for regexec().
766 static char *reginput
; /* String-input pointer. */
767 static char *regbol
; /* Beginning of input, for ^ check. */
768 static char **regstartp
; /* Pointer to startp array. */
769 static char **regendp
; /* Ditto for endp. */
775 STATIC
int regmatch();
776 STATIC
int regrepeat();
781 STATIC
char *regprop();
785 - regexec - match a regexp against a string
788 regexec(const regexp
*prog
, const char *string
)
791 extern char *strchr();
794 if (prog
== NULL
|| string
== NULL
) {
795 regerror("NULL parameter");
799 /* Check validity of program. */
800 if (UCHARAT(prog
->program
) != MAGIC
) {
801 regerror("corrupted program");
805 /* If there is a "must appear" string, look for it. */
806 if (prog
->regmust
!= NULL
) {
808 while ((s
= strchr(s
, prog
->regmust
[0])) != NULL
) {
809 if (strncmp(s
, prog
->regmust
, prog
->regmlen
) == 0)
810 break; /* Found it. */
813 if (s
== NULL
) /* Not present. */
817 /* Mark beginning of line for ^ . */
818 regbol
= (char *)string
;
820 /* Simplest case: anchored match need be tried only once. */
822 return(regtry(prog
, string
));
824 /* Messy cases: unanchored match. */
826 if (prog
->regstart
!= '\0')
827 /* We know what char it must start with. */
828 while ((s
= strchr(s
, prog
->regstart
)) != NULL
) {
834 /* We don't -- general case. */
838 } while (*s
++ != '\0');
845 - regtry - try match at specific point
847 static int /* 0 failure, 1 success */
848 regtry(regexp
*prog
, char *string
)
855 regstartp
= prog
->startp
;
856 regendp
= prog
->endp
;
860 for (i
= NSUBEXP
; i
> 0; i
--) {
864 if (regmatch(prog
->program
+ 1)) {
865 prog
->startp
[0] = string
;
866 prog
->endp
[0] = reginput
;
873 - regmatch - main matching routine
875 * Conceptually the strategy is simple: check to see whether the current
876 * node matches, call self recursively to see whether the rest matches,
877 * and then act accordingly. In practice we make some effort to avoid
878 * recursion, in particular by going through "ordinary" nodes (that don't
879 * need to know whether the rest of the match failed) by a loop instead of
882 static int /* 0 failure, 1 success */
885 char *scan
; /* Current node. */
886 char *next
; /* Next node. */
890 if (scan
!= NULL
&& regnarrate
)
891 fprintf(stderr
, "%s(\n", regprop(scan
));
893 while (scan
!= NULL
) {
896 fprintf(stderr
, "%s...\n", regprop(scan
));
898 next
= regnext(scan
);
902 if (reginput
!= regbol
)
906 if (*reginput
!= '\0')
910 /* Must be looking at a letter, digit, or _ */
911 if ((!isalnum((unsigned char)*reginput
)) && *reginput
!= '_')
913 /* Prev must be BOL or nonword */
914 if (reginput
> regbol
&&
915 (isalnum((unsigned char)reginput
[-1]) || reginput
[-1] == '_'))
919 /* Must be looking at non letter, digit, or _ */
920 if (isalnum((unsigned char)*reginput
) || *reginput
== '_')
922 /* We don't care what the previous char was */
925 if (*reginput
== '\0')
933 opnd
= OPERAND(scan
);
934 /* Inline the first character, for speed. */
935 if (*opnd
!= *reginput
)
938 if (len
> 1 && strncmp(opnd
, reginput
, len
) != 0)
944 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) == NULL
)
949 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) != NULL
)
969 no
= OP(scan
) - OPEN
;
972 if (regmatch(next
)) {
974 * Don't set startp if some later
975 * invocation of the same parentheses
978 if (regstartp
[no
] == NULL
)
979 regstartp
[no
] = save
;
997 no
= OP(scan
) - CLOSE
;
1000 if (regmatch(next
)) {
1002 * Don't set endp if some later
1003 * invocation of the same parentheses
1006 if (regendp
[no
] == NULL
)
1016 if (OP(next
) != BRANCH
) /* No choice. */
1017 next
= OPERAND(scan
); /* Avoid recursion. */
1021 if (regmatch(OPERAND(scan
)))
1024 scan
= regnext(scan
);
1025 } while (scan
!= NULL
&& OP(scan
) == BRANCH
);
1039 * Lookahead to avoid useless match attempts
1040 * when we know what character comes next.
1043 if (OP(next
) == EXACTLY
)
1044 nextch
= *OPERAND(next
);
1045 min
= (OP(scan
) == STAR
) ? 0 : 1;
1047 no
= regrepeat(OPERAND(scan
));
1049 /* If it could work, try it. */
1050 if (nextch
== '\0' || *reginput
== nextch
)
1053 /* Couldn't or didn't -- back up. */
1055 reginput
= save
+ no
;
1061 return(1); /* Success! */
1064 regerror("memory corruption");
1073 * We get here only if there's trouble -- normally "case END" is
1074 * the terminating point.
1076 regerror("corrupted pointers");
1081 - regrepeat - repeatedly match something simple, report how many
1094 count
= strlen(scan
);
1098 while (*opnd
== *scan
) {
1104 while (*scan
!= '\0' && strchr(opnd
, *scan
) != NULL
) {
1110 while (*scan
!= '\0' && strchr(opnd
, *scan
) == NULL
) {
1115 default: /* Oh dear. Called inappropriately. */
1116 regerror("internal foulup");
1117 count
= 0; /* Best compromise. */
1126 - regnext - dig the "next" pointer out of a node
1148 STATIC
char *regprop();
1151 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1157 char op
= EXACTLY
; /* Arbitrary non-END op. */
1159 extern char *strchr();
1163 while (op
!= END
) { /* While that wasn't END last time... */
1165 printf("%2d%s", s
-r
->program
, regprop(s
)); /* Where, what. */
1167 if (next
== NULL
) /* Next ptr. */
1170 printf("(%d)", (s
-r
->program
)+(next
-s
));
1172 if (op
== ANYOF
|| op
== ANYBUT
|| op
== EXACTLY
) {
1173 /* Literal string, where present. */
1174 while (*s
!= '\0') {
1183 /* Header fields of interest. */
1184 if (r
->regstart
!= '\0')
1185 printf("start `%c' ", r
->regstart
);
1187 printf("anchored ");
1188 if (r
->regmust
!= NULL
)
1189 printf("must have \"%s\"", r
->regmust
);
1194 - regprop - printable representation of opcode
1200 static char buf
[50];
1202 (void) strcpy(buf
, ":");
1244 sprintf(buf
+strlen(buf
), "OPEN%d", OP(op
)-OPEN
);
1256 sprintf(buf
+strlen(buf
), "CLOSE%d", OP(op
)-CLOSE
);
1272 regerror("corrupted opcode");
1276 (void) strcat(buf
, p
);
1282 * The following is provided for those people who do not have strcspn() in
1283 * their C libraries. They should get off their butts and do something
1284 * about it; at least one public-domain implementation of those (highly
1285 * useful) string routines has been published on Usenet.
1289 * strcspn - find length of initial segment of s1 consisting entirely
1290 * of characters not from s2
1294 strcspn(char *s1
, char *s2
)
1301 for (scan1
= s1
; *scan1
!= '\0'; scan1
++) {
1302 for (scan2
= s2
; *scan2
!= '\0';) /* ++ moved down. */
1303 if (*scan1
== *scan2
++)