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.
45 * The "internal use only" fields in regexp.h are present to pass info from
46 * compile to execute that permits the execute phase to run lots faster on
47 * simple cases. They are:
49 * regstart char that must begin a match; '\0' if none obvious
50 * reganch is the match anchored (at beginning-of-line only)?
51 * regmust string (pointer into program) that match must include, or NULL
52 * regmlen length of regmust string
54 * Regstart and reganch permit very fast decisions on suitable starting points
55 * for a match, cutting down the work a lot. Regmust permits fast rejection
56 * of lines that cannot possibly match. The regmust tests are costly enough
57 * that regcomp() supplies a regmust only if the r.e. contains something
58 * potentially expensive (at present, the only such thing detected is * or +
59 * at the start of the r.e., which can involve a lot of backup). Regmlen is
60 * supplied because the test in regexec() needs it and regcomp() is computing
65 * Structure for regexp "program". This is essentially a linear encoding
66 * of a nondeterministic finite-state machine (aka syntax charts or
67 * "railroad normal form" in parsing technology). Each node is an opcode
68 * plus a "next" pointer, possibly plus an operand. "Next" pointers of
69 * all nodes except BRANCH implement concatenation; a "next" pointer with
70 * a BRANCH on both ends of it is connecting two alternatives. (Here we
71 * have one of the subtle syntax dependencies: an individual BRANCH (as
72 * opposed to a collection of them) is never concatenated with anything
73 * because of operator precedence.) The operand of some types of node is
74 * a literal string; for others, it is a node leading into a sub-FSM. In
75 * particular, the operand of a BRANCH node is the first node of the branch.
76 * (NB this is *not* a tree structure: the tail of the branch connects
77 * to the thing following the set of BRANCHes.) The opcodes are:
80 /* definition number opnd? meaning */
81 #define END 0 /* no End of program. */
82 #define BOL 1 /* no Match "" at beginning of line. */
83 #define EOL 2 /* no Match "" at end of line. */
84 #define ANY 3 /* no Match any one character. */
85 #define ANYOF 4 /* str Match any character in this string. */
86 #define ANYBUT 5 /* str Match any character not in this string. */
87 #define BRANCH 6 /* node Match this alternative, or the next... */
88 #define BACK 7 /* no Match "", "next" ptr points backward. */
89 #define EXACTLY 8 /* str Match this string. */
90 #define NOTHING 9 /* no Match empty string. */
91 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
92 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
93 #define WORDA 12 /* no Match "" at wordchar, where prev is nonword */
94 #define WORDZ 13 /* no Match "" at nonwordchar, where prev is word */
95 #define OPEN 20 /* no Mark this point in input as start of #n. */
96 /* OPEN+1 is number 1, etc. */
97 #define CLOSE 30 /* no Analogous to OPEN. */
102 * BRANCH The set of branches constituting a single choice are hooked
103 * together with their "next" pointers, since precedence prevents
104 * anything being concatenated to any individual branch. The
105 * "next" pointer of the last BRANCH in a choice points to the
106 * thing following the whole choice. This is also where the
107 * final "next" pointer of each individual branch points; each
108 * branch starts with the operand node of a BRANCH node.
110 * BACK Normal "next" pointers all implicitly point forward; BACK
111 * exists to make loop structures possible.
113 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
114 * BRANCH structures using BACK. Simple cases (one character
115 * per match) are implemented with STAR and PLUS for speed
116 * and to minimize recursive plunges.
118 * OPEN,CLOSE ...are numbered at compile time.
122 * A node is one char of opcode followed by two chars of "next" pointer.
123 * "Next" pointers are stored as two 8-bit pieces, high order first. The
124 * value is a positive offset from the opcode of the node containing it.
125 * An operand, if any, simply follows the node. (Note that much of the
126 * code generation knows about this implicit relationship.)
128 * Using two bytes for the "next" pointer is vast overkill for most things,
129 * but allows patterns to get big without disasters.
132 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
133 #define OPERAND(p) ((p) + 3)
136 * See regmagic.h for one further detail of program structure.
141 * Utility definitions.
144 #define UCHARAT(p) ((int)*(unsigned char *)(p))
146 #define UCHARAT(p) ((int)*(p)&CHARBITS)
149 #define FAIL(m) { regerror(m); return(NULL); }
150 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
153 * Flags to be passed up and down.
155 #define HASWIDTH 01 /* Known never to match null string. */
156 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
157 #define SPSTART 04 /* Starts with * or +. */
158 #define WORST 0 /* Worst case. */
161 * Global work variables for regcomp().
163 static char *regparse
; /* Input-scan pointer. */
164 static int regnpar
; /* () count. */
165 static char regdummy
;
166 static char *regcode
; /* Code-emit pointer; ®dummy = don't. */
167 static long regsize
; /* Code size. */
170 * Forward declarations for regcomp()'s friends.
173 #define STATIC static
175 STATIC
char *reg(int, int *);
176 STATIC
char *regbranch(int *);
177 STATIC
char *regpiece(int *);
178 STATIC
char *regatom(int *);
179 STATIC
char *regnode(char);
180 STATIC
char *regnext(char *);
181 STATIC
void regc(char);
182 STATIC
void reginsert(char, char *);
183 STATIC
void regtail(char *, char *);
184 STATIC
void regoptail(char *, char *);
186 STATIC
int strcspn(char *, char *);
190 - regcomp - compile a regular expression into internal code
192 * We can't allocate space until we know how big the compiled form will be,
193 * but we can't compile it (and thus know how big it is) until we've got a
194 * place to put the code. So we cheat: we compile it twice, once with code
195 * generation turned off and size counting turned on, and once "for real".
196 * This also means that we don't allocate space until we are sure that the
197 * thing really will compile successfully, and we never have to move the
198 * code and thus invalidate pointers into it. (Note that it has to be in
199 * one piece because free() must be able to free it all.)
201 * Beware that the optimization-preparation code in here knows about some
202 * of the structure of the compiled regexp.
205 regcomp(const char *exp
)
214 FAIL("NULL argument");
216 /* First pass: determine size, legality. */
218 if (exp
[0] == '.' && exp
[1] == '*') exp
+= 2; /* aid grep */
220 regparse
= (char *)exp
;
225 if (reg(0, &flags
) == NULL
)
228 /* Small enough for pointer-storage convention? */
229 if (regsize
>= 32767L) /* Probably could be 65535L. */
230 FAIL("regexp too big");
232 /* Allocate space. */
233 r
= (regexp
*)malloc(sizeof(regexp
) + (unsigned)regsize
);
235 FAIL("out of space");
237 /* Second pass: emit code. */
238 regparse
= (char *)exp
;
240 regcode
= r
->program
;
242 if (reg(0, &flags
) == NULL
)
245 /* Dig out information for optimizations. */
246 r
->regstart
= '\0'; /* Worst-case defaults. */
250 scan
= r
->program
+1; /* First BRANCH. */
251 if (OP(regnext(scan
)) == END
) { /* Only one top-level choice. */
252 scan
= OPERAND(scan
);
254 /* Starting-point info. */
255 if (OP(scan
) == EXACTLY
)
256 r
->regstart
= *OPERAND(scan
);
257 else if (OP(scan
) == BOL
)
261 * If there's something expensive in the r.e., find the
262 * longest literal string that must appear and make it the
263 * regmust. Resolve ties in favor of later strings, since
264 * the regstart check works with the beginning of the r.e.
265 * and avoiding duplication strengthens checking. Not a
266 * strong reason, but sufficient in the absence of others.
271 for (; scan
!= NULL
; scan
= regnext(scan
))
272 if (OP(scan
) == EXACTLY
&& strlen(OPERAND(scan
)) >= len
) {
273 longest
= OPERAND(scan
);
274 len
= strlen(OPERAND(scan
));
276 r
->regmust
= longest
;
285 - reg - regular expression, i.e. main body or parenthesized thing
287 * Caller must absorb opening parenthesis.
289 * Combining parenthesis handling with the base level of regular expression
290 * is a trifle forced, but the need to tie the tails of the branches to what
291 * follows makes it hard to avoid.
294 reg(int paren
, int *flagp
)
302 *flagp
= HASWIDTH
; /* Tentatively. */
304 /* Make an OPEN node, if parenthesized. */
306 if (regnpar
>= NSUBEXP
)
310 ret
= regnode(OPEN
+parno
);
314 /* Pick up the branches, linking them together. */
315 br
= regbranch(&flags
);
319 regtail(ret
, br
); /* OPEN -> first. */
322 if (!(flags
&HASWIDTH
))
324 *flagp
|= flags
&SPSTART
;
325 while (*regparse
== '|' || *regparse
== '\n') {
327 br
= regbranch(&flags
);
330 regtail(ret
, br
); /* BRANCH -> BRANCH. */
331 if (!(flags
&HASWIDTH
))
333 *flagp
|= flags
&SPSTART
;
336 /* Make a closing node, and hook it on the end. */
337 ender
= regnode((paren
) ? CLOSE
+parno
: END
);
340 /* Hook the tails of the branches to the closing node. */
341 for (br
= ret
; br
!= NULL
; br
= regnext(br
))
342 regoptail(br
, ender
);
344 /* Check for proper termination. */
345 if (paren
&& *regparse
++ != ')') {
346 FAIL("unmatched ()");
347 } else if (!paren
&& *regparse
!= '\0') {
348 if (*regparse
== ')') {
349 FAIL("unmatched ()");
351 FAIL("junk on end"); /* "Can't happen". */
359 - regbranch - one alternative of an | operator
361 * Implements the concatenation operator.
364 regbranch(int *flagp
)
371 *flagp
= WORST
; /* Tentatively. */
373 ret
= regnode(BRANCH
);
375 while (*regparse
!= '\0' && *regparse
!= ')' &&
376 *regparse
!= '\n' && *regparse
!= '|') {
377 latest
= regpiece(&flags
);
380 *flagp
|= flags
&HASWIDTH
;
381 if (chain
== NULL
) /* First piece. */
382 *flagp
|= flags
&SPSTART
;
384 regtail(chain
, latest
);
387 if (chain
== NULL
) /* Loop ran zero times. */
388 (void) regnode(NOTHING
);
394 - regpiece - something followed by possible [*+?]
396 * Note that the branching code sequences used for ? and the general cases
397 * of * and + are somewhat optimized: they use the same NOTHING node as
398 * both the endmarker for their branch list and the body of the last branch.
399 * It might seem that this node could be dispensed with entirely, but the
400 * endmarker role is not redundant.
410 ret
= regatom(&flags
);
420 if (!(flags
&HASWIDTH
) && op
!= '?')
421 FAIL("*+ operand could be empty");
422 *flagp
= (op
!= '+') ? (WORST
|SPSTART
) : (WORST
|HASWIDTH
);
424 if (op
== '*' && (flags
&SIMPLE
))
425 reginsert(STAR
, ret
);
426 else if (op
== '*') {
427 /* Emit x* as (x&|), where & means "self". */
428 reginsert(BRANCH
, ret
); /* Either x */
429 regoptail(ret
, regnode(BACK
)); /* and loop */
430 regoptail(ret
, ret
); /* back */
431 regtail(ret
, regnode(BRANCH
)); /* or */
432 regtail(ret
, regnode(NOTHING
)); /* null. */
433 } else if (op
== '+' && (flags
&SIMPLE
))
434 reginsert(PLUS
, ret
);
435 else if (op
== '+') {
436 /* Emit x+ as x(&|), where & means "self". */
437 next
= regnode(BRANCH
); /* Either */
439 regtail(regnode(BACK
), ret
); /* loop back */
440 regtail(next
, regnode(BRANCH
)); /* or */
441 regtail(ret
, regnode(NOTHING
)); /* null. */
442 } else if (op
== '?') {
443 /* Emit x? as (x|) */
444 reginsert(BRANCH
, ret
); /* Either x */
445 regtail(ret
, regnode(BRANCH
)); /* or */
446 next
= regnode(NOTHING
); /* null. */
448 regoptail(ret
, next
);
451 if (ISMULT(*regparse
))
458 - regatom - the lowest level
460 * Optimization: gobbles an entire sequence of ordinary characters so that
461 * it can turn them into a single node, which is smaller to store and
462 * faster to run. Backslashed characters are exceptions, each becoming a
463 * separate node; the code is simpler that way and it's not worth fixing.
471 *flagp
= WORST
; /* Tentatively. */
473 switch (*regparse
++) {
474 /* FIXME: these chars only have meaning at beg/end of pat? */
483 *flagp
|= HASWIDTH
|SIMPLE
;
490 if (*regparse
== '^') { /* Complement of range. */
491 ret
= regnode(ANYBUT
);
494 ret
= regnode(ANYOF
);
495 if (*regparse
== ']' || *regparse
== '-')
497 while (*regparse
!= '\0' && *regparse
!= ']') {
498 if (*regparse
== '-') {
500 if (*regparse
== ']' || *regparse
== '\0')
503 class = UCHARAT(regparse
-2);
504 classend
= UCHARAT(regparse
);
505 if (__collate_load_error
) {
506 if (class > classend
)
507 FAIL("invalid [] range");
508 for (class++; class <= classend
; class++)
511 if (__collate_range_cmp(class, classend
) > 0)
512 FAIL("invalid [] range");
513 for (i
= 0; i
<= UCHAR_MAX
; i
++)
515 && __collate_range_cmp(class, i
) <= 0
516 && __collate_range_cmp(i
, classend
) <= 0
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. */
547 FAIL("?+* follows nothing");
550 switch (*regparse
++) {
555 ret
= regnode(WORDA
);
558 ret
= regnode(WORDZ
);
560 /* FIXME: Someday handle \1, \2, ... */
562 /* Handle general quoted chars in exact-match routine */
569 * Encode a string of characters to be matched exactly.
571 * This is a bit tricky due to quoted chars and due to
572 * '*', '+', and '?' taking the SINGLE char previous
575 * On entry, the char at regparse[-1] is going to go
576 * into the string, no matter what it is. (It could be
577 * following a \ if we are entered from the '\' case.)
579 * Basic idea is to pick up a good char in ch and
580 * examine the next char. If it's *+? then we twiddle.
581 * If it's \ then we frozzle. If it's other magic char
582 * we push ch and terminate the string. If none of the
583 * above, we push ch on the string and go around again.
585 * regprev is used to remember where "the current char"
586 * starts in the string, if due to a *+? we need to back
587 * up and put the current char in a separate, 1-char, string.
588 * When regprev is NULL, ch is the only char in the
589 * string; this is used in *+? handling, and in setting
590 * flags |= SIMPLE at the end.
596 regparse
--; /* Look at cur char */
597 ret
= regnode(EXACTLY
);
598 for ( regprev
= NULL
; ; ) {
599 ch
= *regparse
++; /* Get current char */
600 switch (*regparse
) { /* look at next one */
603 regc(ch
); /* Add cur to string */
606 case '.': case '[': case '(':
607 case ')': case '|': case '\n':
610 /* FIXME, $ and ^ should not always be magic */
612 regc(ch
); /* dump cur char */
613 goto done
; /* and we are done */
615 case '?': case '+': case '*':
616 if (!regprev
) /* If just ch in str, */
617 goto magic
; /* use it */
618 /* End mult-char string one early */
619 regparse
= regprev
; /* Back up parse */
623 regc(ch
); /* Cur char OK */
624 switch (regparse
[1]){ /* Look after \ */
628 /* FIXME: Someday handle \1, \2, ... */
629 goto done
; /* Not quoted */
631 /* Backup point is \, scan * point is after it. */
634 continue; /* NOT break; */
637 regprev
= regparse
; /* Set backup point */
642 if (!regprev
) /* One char? */
652 - regnode - emit a node
654 static char * /* Location. */
661 if (ret
== ®dummy
) {
668 *ptr
++ = '\0'; /* Null "next" pointer. */
676 - 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.
693 reginsert(char op
, char *opnd
)
699 if (regcode
== ®dummy
) {
710 place
= opnd
; /* Op node, where operand used to be. */
717 - regtail - set the next-pointer at the end of a node chain
720 regtail(char *p
, char *val
)
729 /* Find last node. */
732 temp
= regnext(scan
);
738 if (OP(scan
) == BACK
)
742 *(scan
+1) = (offset
>>8)&0377;
743 *(scan
+2) = offset
&0377;
747 - regoptail - regtail on operand of first argument; nop if operandless
750 regoptail(char *p
, char *val
)
752 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
753 if (p
== NULL
|| p
== ®dummy
|| OP(p
) != BRANCH
)
755 regtail(OPERAND(p
), val
);
759 * regexec and friends
763 * Global work variables for regexec().
765 static char *reginput
; /* String-input pointer. */
766 static char *regbol
; /* Beginning of input, for ^ check. */
767 static char **regstartp
; /* Pointer to startp array. */
768 static char **regendp
; /* Ditto for endp. */
774 STATIC
int regmatch();
775 STATIC
int regrepeat();
780 STATIC
char *regprop();
784 - regexec - match a regexp against a string
787 regexec(const regexp
*prog
, const char *string
)
790 extern char *strchr();
793 if (prog
== NULL
|| string
== NULL
) {
794 regerror("NULL parameter");
798 /* Check validity of program. */
799 if (UCHARAT(prog
->program
) != MAGIC
) {
800 regerror("corrupted program");
804 /* If there is a "must appear" string, look for it. */
805 if (prog
->regmust
!= NULL
) {
807 while ((s
= strchr(s
, prog
->regmust
[0])) != NULL
) {
808 if (strncmp(s
, prog
->regmust
, prog
->regmlen
) == 0)
809 break; /* Found it. */
812 if (s
== NULL
) /* Not present. */
816 /* Mark beginning of line for ^ . */
817 regbol
= (char *)string
;
819 /* Simplest case: anchored match need be tried only once. */
821 return(regtry(prog
, string
));
823 /* Messy cases: unanchored match. */
825 if (prog
->regstart
!= '\0')
826 /* We know what char it must start with. */
827 while ((s
= strchr(s
, prog
->regstart
)) != NULL
) {
833 /* We don't -- general case. */
837 } while (*s
++ != '\0');
844 - regtry - try match at specific point
846 static int /* 0 failure, 1 success */
847 regtry(regexp
*prog
, char *string
)
854 regstartp
= prog
->startp
;
855 regendp
= prog
->endp
;
859 for (i
= NSUBEXP
; i
> 0; i
--) {
863 if (regmatch(prog
->program
+ 1)) {
864 prog
->startp
[0] = string
;
865 prog
->endp
[0] = reginput
;
872 - regmatch - main matching routine
874 * Conceptually the strategy is simple: check to see whether the current
875 * node matches, call self recursively to see whether the rest matches,
876 * and then act accordingly. In practice we make some effort to avoid
877 * recursion, in particular by going through "ordinary" nodes (that don't
878 * need to know whether the rest of the match failed) by a loop instead of
881 static int /* 0 failure, 1 success */
884 char *scan
; /* Current node. */
885 char *next
; /* Next node. */
889 if (scan
!= NULL
&& regnarrate
)
890 fprintf(stderr
, "%s(\n", regprop(scan
));
892 while (scan
!= NULL
) {
895 fprintf(stderr
, "%s...\n", regprop(scan
));
897 next
= regnext(scan
);
901 if (reginput
!= regbol
)
905 if (*reginput
!= '\0')
909 /* Must be looking at a letter, digit, or _ */
910 if ((!isalnum((unsigned char)*reginput
)) && *reginput
!= '_')
912 /* Prev must be BOL or nonword */
913 if (reginput
> regbol
&&
914 (isalnum((unsigned char)reginput
[-1]) || reginput
[-1] == '_'))
918 /* Must be looking at non letter, digit, or _ */
919 if (isalnum((unsigned char)*reginput
) || *reginput
== '_')
921 /* We don't care what the previous char was */
924 if (*reginput
== '\0')
932 opnd
= OPERAND(scan
);
933 /* Inline the first character, for speed. */
934 if (*opnd
!= *reginput
)
937 if (len
> 1 && strncmp(opnd
, reginput
, len
) != 0)
943 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) == NULL
)
948 if (*reginput
== '\0' || strchr(OPERAND(scan
), *reginput
) != NULL
)
968 no
= OP(scan
) - OPEN
;
971 if (regmatch(next
)) {
973 * Don't set startp if some later
974 * invocation of the same parentheses
977 if (regstartp
[no
] == NULL
)
978 regstartp
[no
] = save
;
996 no
= OP(scan
) - CLOSE
;
999 if (regmatch(next
)) {
1001 * Don't set endp if some later
1002 * invocation of the same parentheses
1005 if (regendp
[no
] == NULL
)
1015 if (OP(next
) != BRANCH
) /* No choice. */
1016 next
= OPERAND(scan
); /* Avoid recursion. */
1020 if (regmatch(OPERAND(scan
)))
1023 scan
= regnext(scan
);
1024 } while (scan
!= NULL
&& OP(scan
) == BRANCH
);
1038 * Lookahead to avoid useless match attempts
1039 * when we know what character comes next.
1042 if (OP(next
) == EXACTLY
)
1043 nextch
= *OPERAND(next
);
1044 min
= (OP(scan
) == STAR
) ? 0 : 1;
1046 no
= regrepeat(OPERAND(scan
));
1048 /* If it could work, try it. */
1049 if (nextch
== '\0' || *reginput
== nextch
)
1052 /* Couldn't or didn't -- back up. */
1054 reginput
= save
+ no
;
1060 return(1); /* Success! */
1063 regerror("memory corruption");
1072 * We get here only if there's trouble -- normally "case END" is
1073 * the terminating point.
1075 regerror("corrupted pointers");
1080 - regrepeat - repeatedly match something simple, report how many
1093 count
= strlen(scan
);
1097 while (*opnd
== *scan
) {
1103 while (*scan
!= '\0' && strchr(opnd
, *scan
) != NULL
) {
1109 while (*scan
!= '\0' && strchr(opnd
, *scan
) == NULL
) {
1114 default: /* Oh dear. Called inappropriately. */
1115 regerror("internal foulup");
1116 count
= 0; /* Best compromise. */
1125 - regnext - dig the "next" pointer out of a node
1147 STATIC
char *regprop();
1150 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1156 char op
= EXACTLY
; /* Arbitrary non-END op. */
1158 extern char *strchr();
1162 while (op
!= END
) { /* While that wasn't END last time... */
1164 printf("%2d%s", s
-r
->program
, regprop(s
)); /* Where, what. */
1166 if (next
== NULL
) /* Next ptr. */
1169 printf("(%d)", (s
-r
->program
)+(next
-s
));
1171 if (op
== ANYOF
|| op
== ANYBUT
|| op
== EXACTLY
) {
1172 /* Literal string, where present. */
1173 while (*s
!= '\0') {
1182 /* Header fields of interest. */
1183 if (r
->regstart
!= '\0')
1184 printf("start `%c' ", r
->regstart
);
1186 printf("anchored ");
1187 if (r
->regmust
!= NULL
)
1188 printf("must have \"%s\"", r
->regmust
);
1193 - regprop - printable representation of opcode
1199 static char buf
[50];
1201 (void) strcpy(buf
, ":");
1243 sprintf(buf
+strlen(buf
), "OPEN%d", OP(op
)-OPEN
);
1255 sprintf(buf
+strlen(buf
), "CLOSE%d", OP(op
)-CLOSE
);
1271 regerror("corrupted opcode");
1275 (void) strcat(buf
, p
);
1281 * The following is provided for those people who do not have strcspn() in
1282 * their C libraries. They should get off their butts and do something
1283 * about it; at least one public-domain implementation of those (highly
1284 * useful) string routines has been published on Usenet.
1288 * strcspn - find length of initial segment of s1 consisting entirely
1289 * of characters not from s2
1293 strcspn(char *s1
, char *s2
)
1300 for (scan1
= s1
; *scan1
!= '\0'; scan1
++) {
1301 for (scan2
= s2
; *scan2
!= '\0';) /* ++ moved down. */
1302 if (*scan1
== *scan2
++)