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.
30 *** THIS IS AN ALTERED VERSION. It was altered by Christopher Seiwald
31 *** seiwald@vix.com, on 28 August 1993, for use in jam. Regmagic.h
32 *** was moved into regexp.h, and the include of regexp.h now uses "'s
33 *** to avoid conflicting with the system regexp.h. Const, bless its
34 *** soul, was removed so it can compile everywhere. The declaration
35 *** of strchr() was in conflict on AIX, so it was removed (as it is
36 *** happily defined in string.h).
37 *** THIS IS AN ALTERED VERSION. It was altered by Christopher Seiwald
38 *** seiwald@perforce.com, on 20 January 2000, to use function prototypes.
40 * Beware that some of this code is subtly aware of the way operator precedence
41 * is structured in regular expressions. Serious changes in regular-expression
42 * syntax might require a total rethink.
57 * The "internal use only" fields in regexp.h are present to pass info from
58 * compile to execute that permits the execute phase to run lots faster on
59 * simple cases. They are:
61 * regstart char that must begin a match; '\0' if none obvious.
62 * reganch is the match anchored (at beginning-of-line only)?
63 * regmust string (pointer into program) that match must include, or NULL.
64 * regmlen length of regmust string.
66 * Regstart and reganch permit very fast decisions on suitable starting points
67 * for a match, cutting down the work a lot. Regmust permits fast rejection of
68 * lines that cannot possibly match. The regmust tests are costly enough that
69 * regcomp() supplies a regmust only if the r.e. contains something potentially
70 * expensive (at present, the only such thing detected is * or + at the start of
71 * the r.e., which can involve a lot of backup). Regmlen is supplied because the
72 * test in regexec() needs it and regcomp() is computing it anyway.
76 * Structure for regexp "program". This is essentially a linear encoding of a
77 * nondeterministic finite-state machine (aka syntax charts or "railroad normal
78 * form" in parsing technology). Each node is an opcode plus a "next" pointer,
79 * possibly plus an operand. "Next" pointers of all nodes except BRANCH
80 * implement concatenation; a "next" pointer with a BRANCH on both ends of it is
81 * connecting two alternatives. [Here we have one of the subtle syntax
82 * dependencies: an individual BRANCH, as opposed to a collection of them, is
83 * never concatenated with anything because of operator precedence.] The operand
84 * of some types of node is a literal string; for others, it is a node leading
85 * into a sub-FSM. In particular, the operand of a BRANCH node is the first node
86 * of the branch. [NB this is *not* a tree structure: the tail of the branch
87 * connects to the thing following the set of BRANCHes.] The opcodes are:
90 /* definition number opnd? meaning */
91 #define END 0 /* no End of program. */
92 #define BOL 1 /* no Match "" at beginning of line. */
93 #define EOL 2 /* no Match "" at end of line. */
94 #define ANY 3 /* no Match any one character. */
95 #define ANYOF 4 /* str Match any character in this string. */
96 #define ANYBUT 5 /* str Match any character not in this string. */
97 #define BRANCH 6 /* node Match this alternative, or the next... */
98 #define BACK 7 /* no Match "", "next" ptr points backward. */
99 #define EXACTLY 8 /* str Match this string. */
100 #define NOTHING 9 /* no Match empty string. */
101 #define STAR 10 /* node Match this (simple) thing 0 or more times. */
102 #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
103 #define WORDA 12 /* no Match "" at wordchar, where prev is nonword */
104 #define WORDZ 13 /* no Match "" at nonwordchar, where prev is word */
105 #define OPEN 20 /* no Mark this point in input as start of #n. */
106 /* OPEN+1 is number 1, etc. */
107 #define CLOSE 30 /* no Analogous to OPEN. */
113 * BRANCH The set of branches constituting a single choice are hooked
114 * together with their "next" pointers, since precedence prevents
115 * anything being concatenated to any individual branch. The
116 * "next" pointer of the last BRANCH in a choice points to the
117 * thing following the whole choice. This is also where the
118 * final "next" pointer of each individual branch points; each
119 * branch starts with the operand node of a BRANCH node.
121 * BACK Normal "next" pointers all implicitly point forward; BACK
122 * exists to make loop structures possible.
124 * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
125 * BRANCH structures using BACK. Simple cases (one character
126 * per match) are implemented with STAR and PLUS for speed
127 * and to minimize recursive plunges.
129 * OPEN,CLOSE ...are numbered at compile time.
133 * A node is one char of opcode followed by two chars of "next" pointer.
134 * "Next" pointers are stored as two 8-bit pieces, high order first. The
135 * value is a positive offset from the opcode of the node containing it.
136 * An operand, if any, simply follows the node. (Note that much of the
137 * code generation knows about this implicit relationship.)
139 * Using two bytes for the "next" pointer is vast overkill for most things,
140 * but allows patterns to get big without disasters.
143 #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
144 #define OPERAND(p) ((p) + 3)
147 * See regmagic.h for one further detail of program structure.
152 * Utility definitions.
155 #define UCHARAT(p) ((int)*(const unsigned char *)(p))
157 #define UCHARAT(p) ((int)*(p)&CHARBITS)
160 #define FAIL(m) { regerror(m); return(NULL); }
161 #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
164 * Flags to be passed up and down.
166 #define HASWIDTH 01 /* Known never to match null string. */
167 #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
168 #define SPSTART 04 /* Starts with * or +. */
169 #define WORST 0 /* Worst case. */
172 * Global work variables for regcomp().
174 static char *regparse; /* Input-scan pointer. */
175 static int regnpar; /* () count. */
176 static char regdummy;
177 static char *regcode; /* Code-emit pointer; ®dummy = don't. */
178 static long regsize; /* Code size. */
181 * Forward declarations for regcomp()'s friends.
184 #define STATIC static
186 STATIC char *reg( int paren, int *flagp );
187 STATIC char *regbranch( int *flagp );
188 STATIC char *regpiece( int *flagp );
189 STATIC char *regatom( int *flagp );
190 STATIC char *regnode( int op );
191 STATIC char *regnext( register char *p );
192 STATIC void regc( int b );
193 STATIC void reginsert( char op, char *opnd );
194 STATIC void regtail( char *p, char *val );
195 STATIC void regoptail( char *p, char *val );
197 STATIC int strcspn();
201 - regcomp - compile a regular expression into internal code
203 * We can't allocate space until we know how big the compiled form will be,
204 * but we can't compile it (and thus know how big it is) until we've got a
205 * place to put the code. So we cheat: we compile it twice, once with code
206 * generation turned off and size counting turned on, and once "for real".
207 * This also means that we don't allocate space until we are sure that the
208 * thing really will compile successfully, and we never have to move the
209 * code and thus invalidate pointers into it. (Note that it has to be in
210 * one piece because free() must be able to free it all.)
212 * Beware that the optimization-preparation code in here knows about some
213 * of the structure of the compiled regexp.
216 regcomp( const char *exp )
220 register char *longest;
221 register unsigned len;
225 FAIL("NULL argument");
227 /* First pass: determine size, legality. */
229 if (exp[0] == '.' && exp[1] == '*') exp += 2; /* aid grep */
231 regparse = (char *)exp;
236 if (reg(0, &flags) == NULL)
239 /* Small enough for pointer-storage convention? */
240 if (regsize >= 32767L) /* Probably could be 65535L. */
241 FAIL("regexp too big");
243 /* Allocate space. */
244 r = (regexp *)BJAM_MALLOC(sizeof(regexp) + (unsigned)regsize);
246 FAIL("out of space");
248 /* Second pass: emit code. */
249 regparse = (char *)exp;
251 regcode = r->program;
253 if (reg(0, &flags) == NULL)
256 /* Dig out information for optimizations. */
257 r->regstart = '\0'; /* Worst-case defaults. */
261 scan = r->program+1; /* First BRANCH. */
262 if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
263 scan = OPERAND(scan);
265 /* Starting-point info. */
266 if (OP(scan) == EXACTLY)
267 r->regstart = *OPERAND(scan);
268 else if (OP(scan) == BOL)
272 * If there's something expensive in the r.e., find the
273 * longest literal string that must appear and make it the
274 * regmust. Resolve ties in favor of later strings, since
275 * the regstart check works with the beginning of the r.e.
276 * and avoiding duplication strengthens checking. Not a
277 * strong reason, but sufficient in the absence of others.
282 for (; scan != NULL; scan = regnext(scan))
283 if (OP(scan) == EXACTLY && strlen(OPERAND(scan)) >= len) {
284 longest = OPERAND(scan);
285 len = strlen(OPERAND(scan));
287 r->regmust = longest;
296 - reg - regular expression, i.e. main body or parenthesized thing
298 * Caller must absorb opening parenthesis.
300 * Combining parenthesis handling with the base level of regular expression
301 * is a trifle forced, but the need to tie the tails of the branches to what
302 * follows makes it hard to avoid.
306 int paren, /* Parenthesized? */
311 register char *ender;
312 register int parno = 0;
315 *flagp = HASWIDTH; /* Tentatively. */
317 /* Make an OPEN node, if parenthesized. */
319 if (regnpar >= NSUBEXP)
323 ret = regnode(OPEN+parno);
327 /* Pick up the branches, linking them together. */
328 br = regbranch(&flags);
332 regtail(ret, br); /* OPEN -> first. */
335 if (!(flags&HASWIDTH))
337 *flagp |= flags&SPSTART;
338 while (*regparse == '|' || *regparse == '\n') {
340 br = regbranch(&flags);
343 regtail(ret, br); /* BRANCH -> BRANCH. */
344 if (!(flags&HASWIDTH))
346 *flagp |= flags&SPSTART;
349 /* Make a closing node, and hook it on the end. */
350 ender = regnode((paren) ? CLOSE+parno : END);
353 /* Hook the tails of the branches to the closing node. */
354 for (br = ret; br != NULL; br = regnext(br))
355 regoptail(br, ender);
357 /* Check for proper termination. */
358 if (paren && *regparse++ != ')') {
359 FAIL("unmatched ()");
360 } else if (!paren && *regparse != '\0') {
361 if (*regparse == ')') {
362 FAIL("unmatched ()");
364 FAIL("junk on end"); /* "Can't happen". */
372 - regbranch - one alternative of an | operator
374 * Implements the concatenation operator.
377 regbranch( int *flagp )
380 register char *chain;
381 register char *latest;
384 *flagp = WORST; /* Tentatively. */
386 ret = regnode(BRANCH);
388 while (*regparse != '\0' && *regparse != ')' &&
389 *regparse != '\n' && *regparse != '|') {
390 latest = regpiece(&flags);
393 *flagp |= flags&HASWIDTH;
394 if (chain == NULL) /* First piece. */
395 *flagp |= flags&SPSTART;
397 regtail(chain, latest);
400 if (chain == NULL) /* Loop ran zero times. */
401 (void) regnode(NOTHING);
407 - regpiece - something followed by possible [*+?]
409 * Note that the branching code sequences used for ? and the general cases
410 * of * and + are somewhat optimized: they use the same NOTHING node as
411 * both the endmarker for their branch list and the body of the last branch.
412 * It might seem that this node could be dispensed with entirely, but the
413 * endmarker role is not redundant.
416 regpiece( int *flagp )
423 ret = regatom(&flags);
433 if (!(flags&HASWIDTH) && op != '?')
434 FAIL("*+ operand could be empty");
435 *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
437 if (op == '*' && (flags&SIMPLE))
438 reginsert(STAR, ret);
439 else if (op == '*') {
440 /* Emit x* as (x&|), where & means "self". */
441 reginsert(BRANCH, ret); /* Either x */
442 regoptail(ret, regnode(BACK)); /* and loop */
443 regoptail(ret, ret); /* back */
444 regtail(ret, regnode(BRANCH)); /* or */
445 regtail(ret, regnode(NOTHING)); /* null. */
446 } else if (op == '+' && (flags&SIMPLE))
447 reginsert(PLUS, ret);
448 else if (op == '+') {
449 /* Emit x+ as x(&|), where & means "self". */
450 next = regnode(BRANCH); /* Either */
452 regtail(regnode(BACK), ret); /* loop back */
453 regtail(next, regnode(BRANCH)); /* or */
454 regtail(ret, regnode(NOTHING)); /* null. */
455 } else if (op == '?') {
456 /* Emit x? as (x|) */
457 reginsert(BRANCH, ret); /* Either x */
458 regtail(ret, regnode(BRANCH)); /* or */
459 next = regnode(NOTHING); /* null. */
461 regoptail(ret, next);
464 if (ISMULT(*regparse))
471 - regatom - the lowest level
473 * Optimization: gobbles an entire sequence of ordinary characters so that
474 * it can turn them into a single node, which is smaller to store and
475 * faster to run. Backslashed characters are exceptions, each becoming a
476 * separate node; the code is simpler that way and it's not worth fixing.
479 regatom( int *flagp )
484 *flagp = WORST; /* Tentatively. */
486 switch (*regparse++) {
487 /* FIXME: these chars only have meaning at beg/end of pat? */
496 *flagp |= HASWIDTH|SIMPLE;
500 register int classend;
502 if (*regparse == '^') { /* Complement of range. */
503 ret = regnode(ANYBUT);
506 ret = regnode(ANYOF);
507 if (*regparse == ']' || *regparse == '-')
509 while (*regparse != '\0' && *regparse != ']') {
510 if (*regparse == '-') {
512 if (*regparse == ']' || *regparse == '\0')
515 classr = UCHARAT(regparse-2)+1;
516 classend = UCHARAT(regparse);
517 if (classr > classend+1)
518 FAIL("invalid [] range");
519 for (; classr <= classend; classr++)
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.
700 register char *place;
702 if (regcode == ®dummy) {
713 place = opnd; /* Op node, where operand used to be. */
720 - regtail - set the next-pointer at the end of a node chain
734 /* Find last node. */
737 temp = regnext(scan);
743 if (OP(scan) == BACK)
747 *(scan+1) = (offset>>8)&0377;
748 *(scan+2) = offset&0377;
752 - regoptail - regtail on operand of first argument; nop if operandless
760 /* "Operandless" and "op != BRANCH" are synonymous in practice. */
761 if (p == NULL || p == ®dummy || OP(p) != BRANCH)
763 regtail(OPERAND(p), val);
767 * regexec and friends
771 * Global work variables for regexec().
773 static const char *reginput; /* String-input pointer. */
774 static const char *regbol; /* Beginning of input, for ^ check. */
775 static const char **regstartp; /* Pointer to startp array. */
776 static const char **regendp; /* Ditto for endp. */
781 STATIC int regtry( regexp *prog, const char *string );
782 STATIC int regmatch( char *prog );
783 STATIC int regrepeat( char *p );
788 STATIC char *regprop();
792 - regexec - match a regexp against a string
796 register regexp *prog,
797 register const char *string )
802 if (prog == NULL || string == NULL) {
803 regerror("NULL parameter");
807 /* Check validity of program. */
808 if (UCHARAT(prog->program) != MAGIC) {
809 regerror("corrupted program");
813 /* If there is a "must appear" string, look for it. */
814 if ( prog->regmust != NULL )
817 while ( ( s = strchr( s, prog->regmust[ 0 ] ) ) != NULL )
819 if ( !strncmp( s, prog->regmust, prog->regmlen ) )
820 break; /* Found it. */
823 if ( s == NULL ) /* Not present. */
827 /* Mark beginning of line for ^ . */
828 regbol = (char *)string;
830 /* Simplest case: anchored match need be tried only once. */
832 return regtry( prog, string );
834 /* Messy cases: unanchored match. */
836 if (prog->regstart != '\0')
837 /* We know what char it must start with. */
838 while ((s = strchr(s, prog->regstart)) != NULL) {
844 /* We do not -- general case. */
846 if ( regtry( prog, s ) )
848 } while ( *s++ != '\0' );
856 * regtry() - try match at specific point.
859 static int /* 0 failure, 1 success */
865 register const char * * sp;
866 register const char * * ep;
869 regstartp = prog->startp;
870 regendp = prog->endp;
874 for ( i = NSUBEXP; i > 0; --i )
879 if ( regmatch( prog->program + 1 ) )
881 prog->startp[ 0 ] = string;
882 prog->endp[ 0 ] = reginput;
891 * regmatch() - main matching routine.
893 * Conceptually the strategy is simple: check to see whether the current node
894 * matches, call self recursively to see whether the rest matches, and then act
895 * accordingly. In practice we make some effort to avoid recursion, in
896 * particular by going through "ordinary" nodes (that do not need to know
897 * whether the rest of the match failed) by a loop instead of by recursion.
900 static int /* 0 failure, 1 success */
901 regmatch( char * prog )
903 char * scan; /* Current node. */
904 char * next; /* Next node. */
908 if (scan != NULL && regnarrate)
909 fprintf(stderr, "%s(\n", regprop(scan));
911 while (scan != NULL) {
914 fprintf(stderr, "%s...\n", regprop(scan));
916 next = regnext(scan);
920 if (reginput != regbol)
924 if (*reginput != '\0')
928 /* Must be looking at a letter, digit, or _ */
929 if ((!isalnum(*reginput)) && *reginput != '_')
931 /* Prev must be BOL or nonword */
932 if (reginput > regbol &&
933 (isalnum(reginput[-1]) || reginput[-1] == '_'))
937 /* Must be looking at non letter, digit, or _ */
938 if (isalnum(*reginput) || *reginput == '_')
940 /* We don't care what the previous char was */
943 if (*reginput == '\0')
951 opnd = OPERAND(scan);
952 /* Inline the first character, for speed. */
953 if (*opnd != *reginput)
956 if (len > 1 && strncmp(opnd, reginput, len) != 0)
962 if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
967 if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
985 register const char *save;
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;
1013 register const char *save;
1015 no = OP(scan) - CLOSE;
1018 if (regmatch(next)) {
1020 * Don't set endp if some later
1021 * invocation of the same parentheses
1024 if (regendp[no] == NULL)
1032 register const char *save;
1034 if (OP(next) != BRANCH) /* No choice. */
1035 next = OPERAND(scan); /* Avoid recursion. */
1039 if (regmatch(OPERAND(scan)))
1042 scan = regnext(scan);
1043 } while (scan != NULL && OP(scan) == BRANCH);
1051 register char nextch;
1053 register const char *save;
1057 * Lookahead to avoid useless match attempts
1058 * when we know what character comes next.
1061 if (OP(next) == EXACTLY)
1062 nextch = *OPERAND(next);
1063 min = (OP(scan) == STAR) ? 0 : 1;
1065 no = regrepeat(OPERAND(scan));
1067 /* If it could work, try it. */
1068 if (nextch == '\0' || *reginput == nextch)
1071 /* Couldn't or didn't -- back up. */
1073 reginput = save + no;
1079 return(1); /* Success! */
1082 regerror("memory corruption");
1091 * We get here only if there's trouble -- normally "case END" is
1092 * the terminating point.
1094 regerror("corrupted pointers");
1099 - regrepeat - repeatedly match something simple, report how many
1102 regrepeat( char *p )
1104 register int count = 0;
1105 register const char *scan;
1106 register char *opnd;
1112 count = strlen(scan);
1116 while (*opnd == *scan) {
1122 while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
1128 while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
1133 default: /* Oh dear. Called inappropriately. */
1134 regerror("internal foulup");
1135 count = 0; /* Best compromise. */
1144 - regnext - dig the "next" pointer out of a node
1147 regnext( register char *p )
1149 register int offset;
1166 STATIC char *regprop();
1169 - regdump - dump a regexp onto stdout in vaguely comprehensible form
1172 regdump( regexp *r )
1175 register char op = EXACTLY; /* Arbitrary non-END op. */
1176 register char *next;
1180 while (op != END) { /* While that wasn't END last time... */
1182 printf("%2d%s", s-r->program, regprop(s)); /* Where, what. */
1184 if (next == NULL) /* Next ptr. */
1187 printf("(%d)", (s-r->program)+(next-s));
1189 if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
1190 /* Literal string, where present. */
1191 while (*s != '\0') {
1200 /* Header fields of interest. */
1201 if (r->regstart != '\0')
1202 printf("start `%c' ", r->regstart);
1204 printf("anchored ");
1205 if (r->regmust != NULL)
1206 printf("must have \"%s\"", r->regmust);
1211 - regprop - printable representation of opcode
1217 static char buf[50];
1219 (void) strcpy(buf, ":");
1261 sprintf(buf+strlen(buf), "OPEN%d", OP(op)-OPEN);
1273 sprintf(buf+strlen(buf), "CLOSE%d", OP(op)-CLOSE);
1289 regerror("corrupted opcode");
1293 (void) strcat(buf, p);
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
1315 register char *scan1;
1316 register char *scan2;
1320 for (scan1 = s1; *scan1 != '\0'; scan1++) {
1321 for (scan2 = s2; *scan2 != '\0';) /* ++ moved down. */
1322 if (*scan1 == *scan2++)