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.
20 *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
21 *** hoptoad!gnu, on 27 Dec 1986, to add \n as an alternative to |
22 *** to assist in implementing egrep.
23 *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
24 *** hoptoad!gnu, on 27 Dec 1986, to add \< and \> for word-matching
25 *** as in BSD grep and ex.
26 *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
27 *** hoptoad!gnu, on 28 Dec 1986, to optimize characters quoted with \.
28 *** THIS IS AN ALTERED VERSION. It was altered by James A. Woods,
29 *** ames!jaw, on 19 June 1987, to quash a regcomp() redundancy.
31 * Beware that some of this code is subtly aware of the way operator
32 * precedence is structured in regular expressions. Serious changes in
33 * regular-expression syntax might require a total rethink.
36 #include <sys/cdefs.h>
37 #if defined(LIBC_SCCS) && !defined(lint)
38 __RCSID("$NetBSD: regexp.c,v 1.17 2006/04/08 22:05:36 christos Exp $");
39 #endif /* LIBC_SCCS and not lint */
49 * The "internal use only" fields in regexp.h are present to pass info from
50 * compile to execute that permits the execute phase to run lots faster on
51 * simple cases. They are:
53 * regstart char that must begin a match; '\0' if none obvious
54 * reganch is the match anchored (at beginning-of-line only)?
55 * regmust string (pointer into program) that match must include, or NULL
56 * regmlen length of regmust string
58 * Regstart and reganch permit very fast decisions on suitable starting points
59 * for a match, cutting down the work a lot. Regmust permits fast rejection
60 * of lines that cannot possibly match. The regmust tests are costly enough
61 * that regcomp() supplies a regmust only if the r.e. contains something
62 * potentially expensive (at present, the only such thing detected is * or +
63 * at the start of the r.e., which can involve a lot of backup). Regmlen is
64 * supplied because the test in regexec() needs it and regcomp() is computing
69 * Structure for regexp "program". This is essentially a linear encoding
70 * of a nondeterministic finite-state machine (aka syntax charts or
71 * "railroad normal form" in parsing technology). Each node is an opcode
72 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
73 * all nodes except BRANCH implement concatenation; a "next" pointer with
74 * a BRANCH on both ends of it is connecting two alternatives. (Here we
75 * have one of the subtle syntax dependencies: an individual BRANCH (as
76 * opposed to a collection of them) is never concatenated with anything
77 * because of operator precedence.) The operand of some types of node is
78 * a literal string; for others, it is a node leading into a sub-FSM. In
79 * particular, the operand of a BRANCH node is the first node of the branch.
80 * (NB this is *not* a tree structure: the tail of the branch connects
81 * to the thing following the set of BRANCHes.) The opcodes are:
84 /* definition number opnd? meaning */
85 #define END 0 /* no End of program. */
86 #define BOL 1 /* no Match "" at beginning of line. */
87 #define EOL 2 /* no Match "" at end of line. */
88 #define ANY 3 /* no Match any one character. */
89 #define ANYOF 4 /* str Match any character in this string. */
90 #define ANYBUT 5 /* str Match any character not in this string. */
91 #define BRANCH 6 /* node Match this alternative, or the next... */
92 #define BACK 7 /* no Match "", "next" ptr points backward. */
93 #define EXACTLY 8 /* str Match this string. */
94 #define NOTHING 9 /* no Match empty string. */
95 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
96 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
97 #define WORDA 12 /* no Match "" at wordchar, where prev is nonword */
98 #define WORDZ 13 /* no Match "" at nonwordchar, where prev is word */
99 #define OPEN 20 /* no Mark this point in input as start of #n. */
100 /* OPEN+1 is number 1, etc. */
101 #define CLOSE 30 /* no Analogous to OPEN. */
106 * BRANCH The set of branches constituting a single choice are hooked
107 * together with their "next" pointers, since precedence prevents
108 * anything being concatenated to any individual branch. The
109 * "next" pointer of the last BRANCH in a choice points to the
110 * thing following the whole choice. This is also where the
111 * final "next" pointer of each individual branch points; each
112 * branch starts with the operand node of a BRANCH node.
114 * BACK Normal "next" pointers all implicitly point forward; BACK
115 * exists to make loop structures possible.
117 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
118 * BRANCH structures using BACK. Simple cases (one character
119 * per match) are implemented with STAR and PLUS for speed
120 * and to minimize recursive plunges.
122 * OPEN,CLOSE ...are numbered at compile time.
126 * A node is one char of opcode followed by two chars of "next" pointer.
127 * "Next" pointers are stored as two 8-bit pieces, high order first. The
128 * value is a positive offset from the opcode of the node containing it.
129 * An operand, if any, simply follows the node. (Note that much of the
130 * code generation knows about this implicit relationship.)
132 * Using two bytes for the "next" pointer is vast overkill for most things,
133 * but allows patterns to get big without disasters.
136 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
137 #define OPERAND(p) ((p) + 3)
140 * See regmagic.h for one further detail of program structure.
145 * Utility definitions.
148 #define UCHARAT(p) ((int)*(unsigned char *)(p))
150 #define UCHARAT(p) ((int)*(p)&CHARBITS)
153 #define FAIL(m) { regerror(m); return(NULL); }
154 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
157 * Flags to be passed up and down.
159 #define HASWIDTH 01 /* Known never to match null string. */
160 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
161 #define SPSTART 04 /* Starts with * or +. */
162 #define WORST 0 /* Worst case. */
165 * Global work variables for regcomp().
167 static char *regparse
; /* Input-scan pointer. */
168 static int regnpar
; /* () count. */
169 static char regdummy
;
170 static char *regcode
; /* Code-emit pointer; ®dummy = don't. */
171 static long regsize
; /* Code size. */
174 * Forward declarations for regcomp()'s friends.
177 #define STATIC static
179 STATIC
char *reg
__P((int, int *));
180 STATIC
char *regbranch
__P((int *));
181 STATIC
char *regpiece
__P((int *));
182 STATIC
char *regatom
__P((int *));
183 STATIC
char *regnode
__P((int));
184 STATIC
char *regnext
__P((char *));
185 STATIC
void regc
__P((int));
186 STATIC
void reginsert
__P((int, char *));
187 STATIC
void regtail
__P((char *, char *));
188 STATIC
void regoptail
__P((char *, char *));
190 STATIC
int strcspn
__P((char *, char *));
194 - regcomp - compile a regular expression into internal code
196 * We can't allocate space until we know how big the compiled form will be,
197 * but we can't compile it (and thus know how big it is) until we've got a
198 * place to put the code. So we cheat: we compile it twice, once with code
199 * generation turned off and size counting turned on, and once "for real".
200 * This also means that we don't allocate space until we are sure that the
201 * thing really will compile successfully, and we never have to move the
202 * code and thus invalidate pointers into it. (Note that it has to be in
203 * one piece because free() must be able to free it all.)
205 * Beware that the optimization-preparation code in here knows about some
206 * of the structure of the compiled regexp.
209 __compat_regcomp(expn
)
219 FAIL("NULL argument");
221 /* First pass: determine size, legality. */
223 if (expn
[0] == '.' && expn
[1] == '*') expn
+= 2; /* aid grep */
225 /* LINTED const castaway */
226 regparse
= (char *)expn
;
231 if (reg(0, &flags
) == NULL
)
234 /* Small enough for pointer-storage convention? */
235 if (regsize
>= 32767L) /* Probably could be 65535L. */
236 FAIL("regexp too big");
238 /* Allocate space. */
239 r
= (regexp
*)malloc(sizeof(regexp
) + (unsigned)regsize
);
241 FAIL("out of space");
243 /* Second pass: emit code. */
244 /* LINTED const castaway */
245 regparse
= (char *)expn
;
247 regcode
= r
->program
;
249 if (reg(0, &flags
) == NULL
)
252 /* Dig out information for optimizations. */
253 r
->regstart
= '\0'; /* Worst-case defaults. */
257 scan
= r
->program
+1; /* First BRANCH. */
258 if (OP(regnext(scan
)) == END
) { /* Only one top-level choice. */
259 scan
= OPERAND(scan
);
261 /* Starting-point info. */
262 if (OP(scan
) == EXACTLY
)
263 r
->regstart
= *OPERAND(scan
);
264 else if (OP(scan
) == BOL
)
268 * If there's something expensive in the r.e., find the
269 * longest literal string that must appear and make it the
270 * regmust. Resolve ties in favor of later strings, since
271 * the regstart check works with the beginning of the r.e.
272 * and avoiding duplication strengthens checking. Not a
273 * strong reason, but sufficient in the absence of others.
278 for (; scan
!= NULL
; scan
= regnext(scan
))
279 if (OP(scan
) == EXACTLY
&& (int) strlen(OPERAND(scan
)) >= len
) {
280 longest
= OPERAND(scan
);
281 len
= strlen(OPERAND(scan
));
283 r
->regmust
= longest
;
292 - reg - regular expression, i.e. main body or parenthesized thing
294 * Caller must absorb opening parenthesis.
296 * Combining parenthesis handling with the base level of regular expression
297 * is a trifle forced, but the need to tie the tails of the branches to what
298 * follows makes it hard to avoid.
302 int paren
; /* Parenthesized? */
311 *flagp
= HASWIDTH
; /* Tentatively. */
313 /* Make an OPEN node, if parenthesized. */
315 if (regnpar
>= NSUBEXP
)
319 ret
= regnode(OPEN
+parno
);
323 /* Pick up the branches, linking them together. */
324 br
= regbranch(&flags
);
328 regtail(ret
, br
); /* OPEN -> first. */
331 if (!(flags
&HASWIDTH
))
333 *flagp
|= flags
&SPSTART
;
334 while (*regparse
== '|' || *regparse
== '\n') {
336 br
= regbranch(&flags
);
339 regtail(ret
, br
); /* BRANCH -> BRANCH. */
340 if (!(flags
&HASWIDTH
))
342 *flagp
|= flags
&SPSTART
;
345 /* Make a closing node, and hook it on the end. */
346 ender
= regnode((paren
) ? CLOSE
+parno
: END
);
349 /* Hook the tails of the branches to the closing node. */
350 for (br
= ret
; br
!= NULL
; br
= regnext(br
))
351 regoptail(br
, ender
);
353 /* Check for proper termination. */
354 if (paren
&& *regparse
++ != ')') {
355 FAIL("unmatched ()");
356 } else if (!paren
&& *regparse
!= '\0') {
357 if (*regparse
== ')') {
358 FAIL("unmatched ()");
360 FAIL("junk on end"); /* "Can't happen". */
368 - regbranch - one alternative of an | operator
370 * Implements the concatenation operator.
381 *flagp
= WORST
; /* Tentatively. */
383 ret
= regnode(BRANCH
);
385 while (*regparse
!= '\0' && *regparse
!= ')' &&
386 *regparse
!= '\n' && *regparse
!= '|') {
387 latest
= regpiece(&flags
);
390 *flagp
|= flags
&HASWIDTH
;
391 if (chain
== NULL
) /* First piece. */
392 *flagp
|= flags
&SPSTART
;
394 regtail(chain
, latest
);
397 if (chain
== NULL
) /* Loop ran zero times. */
398 (void) regnode(NOTHING
);
404 - regpiece - something followed by possible [*+?]
406 * Note that the branching code sequences used for ? and the general cases
407 * of * and + are somewhat optimized: they use the same NOTHING node as
408 * both the endmarker for their branch list and the body of the last branch.
409 * It might seem that this node could be dispensed with entirely, but the
410 * endmarker role is not redundant.
421 ret
= regatom(&flags
);
431 if (!(flags
&HASWIDTH
) && op
!= '?')
432 FAIL("*+ operand could be empty");
433 *flagp
= (op
!= '+') ? (WORST
|SPSTART
) : (WORST
|HASWIDTH
);
435 if (op
== '*' && (flags
&SIMPLE
))
436 reginsert(STAR
, ret
);
437 else if (op
== '*') {
438 /* Emit x* as (x&|), where & means "self". */
439 reginsert(BRANCH
, ret
); /* Either x */
440 regoptail(ret
, regnode(BACK
)); /* and loop */
441 regoptail(ret
, ret
); /* back */
442 regtail(ret
, regnode(BRANCH
)); /* or */
443 regtail(ret
, regnode(NOTHING
)); /* null. */
444 } else if (op
== '+' && (flags
&SIMPLE
))
445 reginsert(PLUS
, ret
);
446 else if (op
== '+') {
447 /* Emit x+ as x(&|), where & means "self". */
448 next
= regnode(BRANCH
); /* Either */
450 regtail(regnode(BACK
), ret
); /* loop back */
451 regtail(next
, regnode(BRANCH
)); /* or */
452 regtail(ret
, regnode(NOTHING
)); /* null. */
453 } else if (op
== '?') {
454 /* Emit x? as (x|) */
455 reginsert(BRANCH
, ret
); /* Either x */
456 regtail(ret
, regnode(BRANCH
)); /* or */
457 next
= regnode(NOTHING
); /* null. */
459 regoptail(ret
, next
);
462 if (ISMULT(*regparse
))
469 - regatom - the lowest level
471 * Optimization: gobbles an entire sequence of ordinary characters so that
472 * it can turn them into a single node, which is smaller to store and
473 * faster to run. Backslashed characters are exceptions, each becoming a
474 * separate node; the code is simpler that way and it's not worth fixing.
483 *flagp
= WORST
; /* Tentatively. */
485 switch (*regparse
++) {
486 /* FIXME: these chars only have meaning at beg/end of pat? */
495 *flagp
|= HASWIDTH
|SIMPLE
;
501 if (*regparse
== '^') { /* Complement of range. */
502 ret
= regnode(ANYBUT
);
505 ret
= regnode(ANYOF
);
506 if (*regparse
== ']' || *regparse
== '-')
508 while (*regparse
!= '\0' && *regparse
!= ']') {
509 if (*regparse
== '-') {
511 if (*regparse
== ']' || *regparse
== '\0')
514 class = UCHARAT(regparse
-2)+1;
515 classend
= UCHARAT(regparse
);
516 if (class > classend
+1)
517 FAIL("invalid [] range");
518 for (; class <= classend
; class++)
526 if (*regparse
!= ']')
527 FAIL("unmatched []");
529 *flagp
|= HASWIDTH
|SIMPLE
;
533 ret
= reg(1, &flags
);
536 *flagp
|= flags
&(HASWIDTH
|SPSTART
);
542 FAIL("internal urp"); /* Supposed to be caught earlier. */
546 FAIL("?+* follows nothing");
548 switch (*regparse
++) {
552 ret
= regnode(WORDA
);
555 ret
= regnode(WORDZ
);
557 /* FIXME: Someday handle \1, \2, ... */
559 /* Handle general quoted chars in exact-match routine */
567 * Encode a string of characters to be matched exactly.
569 * This is a bit tricky due to quoted chars and due to
570 * '*', '+', and '?' taking the SINGLE char previous
573 * On entry, the char at regparse[-1] is going to go
574 * into the string, no matter what it is. (It could be
575 * following a \ if we are entered from the '\' case.)
577 * Basic idea is to pick up a good char in ch and
578 * examine the next char. If it's *+? then we twiddle.
579 * If it's \ then we frozzle. If it's other magic char
580 * we push ch and terminate the string. If none of the
581 * above, we push ch on the string and go around again.
583 * regprev is used to remember where "the current char"
584 * starts in the string, if due to a *+? we need to back
585 * up and put the current char in a separate, 1-char, string.
586 * When regprev is NULL, ch is the only char in the
587 * string; this is used in *+? handling, and in setting
588 * flags |= SIMPLE at the end.
594 regparse
--; /* Look at cur char */
595 ret
= regnode(EXACTLY
);
596 for ( regprev
= 0 ; ; ) {
597 ch
= *regparse
++; /* Get current char */
598 switch (*regparse
) { /* look at next one */
601 regc(ch
); /* Add cur to string */
604 case '.': case '[': case '(':
605 case ')': case '|': case '\n':
608 /* FIXME, $ and ^ should not always be magic */
610 regc(ch
); /* dump cur char */
611 goto done
; /* and we are done */
613 case '?': case '+': case '*':
614 if (!regprev
) /* If just ch in str, */
615 goto magic
; /* use it */
616 /* End mult-char string one early */
617 regparse
= regprev
; /* Back up parse */
621 regc(ch
); /* Cur char OK */
622 switch (regparse
[1]){ /* Look after \ */
626 /* FIXME: Someday handle \1, \2, ... */
627 goto done
; /* Not quoted */
629 /* Backup point is \, scan * point is after it. */
632 continue; /* NOT break; */
635 regprev
= regparse
; /* Set backup point */
640 if (!regprev
) /* One char? */
650 - regnode - emit a node
652 static char * /* Location. */
660 if (ret
== ®dummy
) {
667 *ptr
++ = '\0'; /* Null "next" pointer. */
675 - regc - emit (if appropriate) a byte of code
681 if (regcode
!= ®dummy
)
688 - reginsert - insert an operator in front of already-emitted operand
690 * Means relocating the operand.
701 if (regcode
== ®dummy
) {
712 place
= opnd
; /* Op node, where operand used to be. */
719 - regtail - set the next-pointer at the end of a node chain
733 /* Find last node. */
736 temp
= regnext(scan
);
742 if (OP(scan
) == BACK
)
746 *(scan
+1) = ((unsigned int)offset
>>8)&0377;
747 *(scan
+2) = offset
&0377;
751 - regoptail - regtail on operand of first argument; nop if operandless
758 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
759 if (p
== NULL
|| p
== ®dummy
|| OP(p
) != BRANCH
)
761 regtail(OPERAND(p
), val
);
765 * regexec and friends
769 * Global work variables for regexec().
771 static char *reginput
; /* String-input pointer. */
772 static char *regbol
; /* Beginning of input, for ^ check. */
773 static char **regstartp
; /* Pointer to startp array. */
774 static char **regendp
; /* Ditto for endp. */
779 STATIC
int regtry
__P((const regexp
*, const char *));
780 STATIC
int regmatch
__P((char *));
781 STATIC
int regrepeat
__P((char *));
785 void regdump
__P((regexp
*));
786 STATIC
char *regprop
__P((char *));
790 - regexec - match a regexp against a string
793 __compat_regexec(prog
, string
)
800 if (prog
== NULL
|| string
== NULL
) {
801 regerror("NULL parameter");
805 /* Check validity of program. */
806 if (UCHARAT(prog
->program
) != MAGIC
) {
807 regerror("corrupted program");
811 /* If there is a "must appear" string, look for it. */
812 if (prog
->regmust
!= NULL
) {
813 /* LINTED const castaway */
815 while ((s
= strchr(s
, prog
->regmust
[0])) != NULL
) {
816 if (strncmp(s
, prog
->regmust
,
817 (size_t)prog
->regmlen
) == 0)
818 break; /* Found it. */
821 if (s
== NULL
) /* Not present. */
825 /* Mark beginning of line for ^ . */
826 /* LINTED const castaway */
827 regbol
= (char *)string
;
829 /* Simplest case: anchored match need be tried only once. */
831 return(regtry(prog
, string
));
833 /* Messy cases: unanchored match. */
834 /* LINTED const castaway */
836 if (prog
->regstart
!= '\0')
837 /* We know what char it must start with. */
838 while ((s
= strchr(s
, prog
->regstart
)) != NULL
) {
844 /* We don't -- general case. */
848 } while (*s
++ != '\0');
855 - regtry - try match at specific point
857 static int /* 0 failure, 1 success */
866 /* LINTED const castaway */
867 reginput
= (char *)string
; /* XXX */
868 regstartp
= (char **)prog
->startp
; /* XXX */
869 regendp
= (char **)prog
->endp
; /* XXX */
871 sp
= (char **)prog
->startp
; /* XXX */
872 ep
= (char **)prog
->endp
; /* XXX */
873 for (i
= NSUBEXP
; i
> 0; i
--) {
877 if (regmatch((char *)prog
->program
+ 1)) { /* XXX */
878 /* LINTED const castaway */
879 ((regexp
*)prog
)->startp
[0] = (char *)string
; /* XXX */
880 /* LINTED const castaway */
881 ((regexp
*)prog
)->endp
[0] = reginput
; /* XXX */
888 - regmatch - main matching routine
890 * Conceptually the strategy is simple: check to see whether the current
891 * node matches, call self recursively to see whether the rest matches,
892 * and then act accordingly. In practice we make some effort to avoid
893 * recursion, in particular by going through "ordinary" nodes (that don't
894 * need to know whether the rest of the match failed) by a loop instead of
897 static int /* 0 failure, 1 success */
901 char *scan
; /* Current node. */
902 char *next
; /* Next node. */
906 if (scan
!= NULL
&& regnarrate
)
907 fprintf(stderr
, "%s(\n", regprop(scan
));
909 while (scan
!= NULL
) {
912 fprintf(stderr
, "%s...\n", regprop(scan
));
914 next
= regnext(scan
);
918 if (reginput
!= regbol
)
922 if (*reginput
!= '\0')
926 /* Must be looking at a letter, digit, or _ */
927 if ((!isalnum(UCHARAT(reginput
))) && *reginput
!= '_')
929 /* Prev must be BOL or nonword */
930 if (reginput
> regbol
&&
931 (isalnum(UCHARAT(reginput
- 1))
932 || reginput
[-1] == '_'))
936 /* Must be looking at non letter, digit, or _ */
937 if (isalnum(UCHARAT(reginput
)) || *reginput
== '_')
939 /* We don't care what the previous char was */
942 if (*reginput
== '\0')
950 opnd
= OPERAND(scan
);
951 /* Inline the first character, for speed. */
952 if (*opnd
!= *reginput
)
955 if (len
> 1 && strncmp(opnd
, reginput
,
962 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) == NULL
)
967 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) != NULL
)
987 no
= OP(scan
) - OPEN
;
990 if (regmatch(next
)) {
992 * Don't set startp if some later
993 * invocation of the same parentheses
996 if (regstartp
[no
] == NULL
)
997 regstartp
[no
] = save
;
1014 no
= OP(scan
) - CLOSE
;
1017 if (regmatch(next
)) {
1019 * Don't set endp if some later
1020 * invocation of the same parentheses
1023 if (regendp
[no
] == NULL
)
1032 if (OP(next
) != BRANCH
) /* No choice. */
1033 next
= OPERAND(scan
); /* Avoid recursion. */
1037 if (regmatch(OPERAND(scan
)))
1040 scan
= regnext(scan
);
1041 } while (scan
!= NULL
&& OP(scan
) == BRANCH
);
1055 * Lookahead to avoid useless match attempts
1056 * when we know what character comes next.
1059 if (OP(next
) == EXACTLY
)
1060 nextch
= *OPERAND(next
);
1061 min
= (OP(scan
) == STAR
) ? 0 : 1;
1063 no
= regrepeat(OPERAND(scan
));
1065 /* If it could work, try it. */
1066 if (nextch
== '\0' || *reginput
== nextch
)
1069 /* Couldn't or didn't -- back up. */
1071 reginput
= save
+ no
;
1076 return(1); /* Success! */
1078 regerror("memory corruption");
1086 * We get here only if there's trouble -- normally "case END" is
1087 * the terminating point.
1089 regerror("corrupted pointers");
1094 - regrepeat - repeatedly match something simple, report how many
1108 count
= strlen(scan
);
1112 while (*opnd
== *scan
) {
1118 while (*scan
!= '\0' && strchr(opnd
, *scan
) != NULL
) {
1124 while (*scan
!= '\0' && strchr(opnd
, *scan
) == NULL
) {
1129 default: /* Oh dear. Called inappropriately. */
1130 regerror("internal foulup");
1131 count
= 0; /* Best compromise. */
1140 - regnext - dig the "next" pointer out of a node
1164 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1171 char op
= EXACTLY
; /* Arbitrary non-END op. */
1176 while (op
!= END
) { /* While that wasn't END last time... */
1178 printf("%2td%s", s
-r
->program
, regprop(s
)); /* Where, what. */
1180 if (next
== NULL
) /* Next ptr. */
1183 printf("(%td)", (s
-r
->program
)+(next
-s
));
1185 if (op
== ANYOF
|| op
== ANYBUT
|| op
== EXACTLY
) {
1186 /* Literal string, where present. */
1187 while (*s
!= '\0') {
1196 /* Header fields of interest. */
1197 if (r
->regstart
!= '\0')
1198 printf("start `%c' ", r
->regstart
);
1200 printf("anchored ");
1201 if (r
->regmust
!= NULL
)
1202 printf("must have \"%s\"", r
->regmust
);
1207 - regprop - printable representation of opcode
1214 static char buf
[50];
1216 (void)strncpy(buf
, ":", sizeof(buf
) - 1);
1258 (void)snprintf(buf
+strlen(buf
), sizeof(buf
) - strlen(buf
),
1259 "OPEN%d", OP(op
)-OPEN
);
1271 (void)snprintf(buf
+strlen(buf
), sizeof(buf
) - strlen(buf
),
1272 "CLOSE%d", OP(op
)-CLOSE
);
1289 regerror("corrupted opcode");
1293 (void)strncat(buf
, p
, sizeof(buf
) - strlen(buf
) - 1);
1299 * The following is provided for those people who do not have strcspn() in
1300 * their C libraries. They should get off their butts and do something
1301 * about it; at least one public-domain implementation of those (highly
1302 * useful) string routines has been published on Usenet.
1306 * strcspn - find length of initial segment of s1 consisting entirely
1307 * of characters not from s2
1320 for (scan1
= s1
; *scan1
!= '\0'; scan1
++) {
1321 for (scan2
= s2
; *scan2
!= '\0';) /* ++ moved down. */
1322 if (*scan1
== *scan2
++)