161f28255Scgd /*
261f28255Scgd * regcomp and regexec -- regsub and regerror are elsewhere
361f28255Scgd *
461f28255Scgd * Copyright (c) 1986 by University of Toronto.
561f28255Scgd * Written by Henry Spencer. Not derived from licensed software.
661f28255Scgd *
761f28255Scgd * Permission is granted to anyone to use this software for any
861f28255Scgd * purpose on any computer system, and to redistribute it freely,
961f28255Scgd * subject to the following restrictions:
1061f28255Scgd *
1161f28255Scgd * 1. The author is not responsible for the consequences of use of
1261f28255Scgd * this software, no matter how awful, even if they arise
1361f28255Scgd * from defects in it.
1461f28255Scgd *
1561f28255Scgd * 2. The origin of this software must not be misrepresented, either
1661f28255Scgd * by explicit claim or by omission.
1761f28255Scgd *
1861f28255Scgd * 3. Altered versions must be plainly marked as such, and must not
1961f28255Scgd * be misrepresented as being the original software.
2061f28255Scgd *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
2161f28255Scgd *** hoptoad!gnu, on 27 Dec 1986, to add \n as an alternative to |
2261f28255Scgd *** to assist in implementing egrep.
2361f28255Scgd *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
2461f28255Scgd *** hoptoad!gnu, on 27 Dec 1986, to add \< and \> for word-matching
2561f28255Scgd *** as in BSD grep and ex.
2661f28255Scgd *** THIS IS AN ALTERED VERSION. It was altered by John Gilmore,
2761f28255Scgd *** hoptoad!gnu, on 28 Dec 1986, to optimize characters quoted with \.
2861f28255Scgd *** THIS IS AN ALTERED VERSION. It was altered by James A. Woods,
2961f28255Scgd *** ames!jaw, on 19 June 1987, to quash a regcomp() redundancy.
3061f28255Scgd *
3161f28255Scgd * Beware that some of this code is subtly aware of the way operator
3261f28255Scgd * precedence is structured in regular expressions. Serious changes in
3361f28255Scgd * regular-expression syntax might require a total rethink.
3461f28255Scgd */
35aee4b07bSmycroft
36e37aec67Slukem #include <sys/cdefs.h>
37a5e4741aSmsaitoh #if defined(LIBC_SCCS) && !defined(lint)
38*3d64af45Schristos __RCSID("$NetBSD: regexp.c,v 1.19 2016/01/26 16:05:18 christos Exp $");
39a5e4741aSmsaitoh #endif /* LIBC_SCCS and not lint */
40aee4b07bSmycroft
412c6c9ee6Slukem #include <ctype.h>
4261f28255Scgd #include <regexp.h>
4361f28255Scgd #include <stdio.h>
4461f28255Scgd #include <stdlib.h>
4561f28255Scgd #include <string.h>
4661f28255Scgd #include "regmagic.h"
4761f28255Scgd
4861f28255Scgd /*
4961f28255Scgd * The "internal use only" fields in regexp.h are present to pass info from
5061f28255Scgd * compile to execute that permits the execute phase to run lots faster on
5161f28255Scgd * simple cases. They are:
5261f28255Scgd *
5361f28255Scgd * regstart char that must begin a match; '\0' if none obvious
5461f28255Scgd * reganch is the match anchored (at beginning-of-line only)?
5561f28255Scgd * regmust string (pointer into program) that match must include, or NULL
5661f28255Scgd * regmlen length of regmust string
5761f28255Scgd *
5861f28255Scgd * Regstart and reganch permit very fast decisions on suitable starting points
5961f28255Scgd * for a match, cutting down the work a lot. Regmust permits fast rejection
6061f28255Scgd * of lines that cannot possibly match. The regmust tests are costly enough
6161f28255Scgd * that regcomp() supplies a regmust only if the r.e. contains something
6261f28255Scgd * potentially expensive (at present, the only such thing detected is * or +
6361f28255Scgd * at the start of the r.e., which can involve a lot of backup). Regmlen is
6461f28255Scgd * supplied because the test in regexec() needs it and regcomp() is computing
6561f28255Scgd * it anyway.
6661f28255Scgd */
6761f28255Scgd
6861f28255Scgd /*
6961f28255Scgd * Structure for regexp "program". This is essentially a linear encoding
7061f28255Scgd * of a nondeterministic finite-state machine (aka syntax charts or
7161f28255Scgd * "railroad normal form" in parsing technology). Each node is an opcode
7261f28255Scgd * plus a "next" pointer, possibly plus an operand. "Next" pointers of
7361f28255Scgd * all nodes except BRANCH implement concatenation; a "next" pointer with
7461f28255Scgd * a BRANCH on both ends of it is connecting two alternatives. (Here we
7561f28255Scgd * have one of the subtle syntax dependencies: an individual BRANCH (as
7661f28255Scgd * opposed to a collection of them) is never concatenated with anything
7761f28255Scgd * because of operator precedence.) The operand of some types of node is
7861f28255Scgd * a literal string; for others, it is a node leading into a sub-FSM. In
7961f28255Scgd * particular, the operand of a BRANCH node is the first node of the branch.
8061f28255Scgd * (NB this is *not* a tree structure: the tail of the branch connects
8161f28255Scgd * to the thing following the set of BRANCHes.) The opcodes are:
8261f28255Scgd */
8361f28255Scgd
8461f28255Scgd /* definition number opnd? meaning */
8561f28255Scgd #define END 0 /* no End of program. */
8661f28255Scgd #define BOL 1 /* no Match "" at beginning of line. */
8761f28255Scgd #define EOL 2 /* no Match "" at end of line. */
8861f28255Scgd #define ANY 3 /* no Match any one character. */
8961f28255Scgd #define ANYOF 4 /* str Match any character in this string. */
9061f28255Scgd #define ANYBUT 5 /* str Match any character not in this string. */
9161f28255Scgd #define BRANCH 6 /* node Match this alternative, or the next... */
9261f28255Scgd #define BACK 7 /* no Match "", "next" ptr points backward. */
9361f28255Scgd #define EXACTLY 8 /* str Match this string. */
9461f28255Scgd #define NOTHING 9 /* no Match empty string. */
9561f28255Scgd #define STAR 10 /* node Match this (simple) thing 0 or more times. */
9661f28255Scgd #define PLUS 11 /* node Match this (simple) thing 1 or more times. */
9761f28255Scgd #define WORDA 12 /* no Match "" at wordchar, where prev is nonword */
9861f28255Scgd #define WORDZ 13 /* no Match "" at nonwordchar, where prev is word */
9961f28255Scgd #define OPEN 20 /* no Mark this point in input as start of #n. */
10061f28255Scgd /* OPEN+1 is number 1, etc. */
10161f28255Scgd #define CLOSE 30 /* no Analogous to OPEN. */
10261f28255Scgd
10361f28255Scgd /*
10461f28255Scgd * Opcode notes:
10561f28255Scgd *
10661f28255Scgd * BRANCH The set of branches constituting a single choice are hooked
10761f28255Scgd * together with their "next" pointers, since precedence prevents
10861f28255Scgd * anything being concatenated to any individual branch. The
10961f28255Scgd * "next" pointer of the last BRANCH in a choice points to the
11061f28255Scgd * thing following the whole choice. This is also where the
11161f28255Scgd * final "next" pointer of each individual branch points; each
11261f28255Scgd * branch starts with the operand node of a BRANCH node.
11361f28255Scgd *
11461f28255Scgd * BACK Normal "next" pointers all implicitly point forward; BACK
11561f28255Scgd * exists to make loop structures possible.
11661f28255Scgd *
11761f28255Scgd * STAR,PLUS '?', and complex '*' and '+', are implemented as circular
11861f28255Scgd * BRANCH structures using BACK. Simple cases (one character
11961f28255Scgd * per match) are implemented with STAR and PLUS for speed
12061f28255Scgd * and to minimize recursive plunges.
12161f28255Scgd *
12261f28255Scgd * OPEN,CLOSE ...are numbered at compile time.
12361f28255Scgd */
12461f28255Scgd
12561f28255Scgd /*
12661f28255Scgd * A node is one char of opcode followed by two chars of "next" pointer.
12761f28255Scgd * "Next" pointers are stored as two 8-bit pieces, high order first. The
12861f28255Scgd * value is a positive offset from the opcode of the node containing it.
12961f28255Scgd * An operand, if any, simply follows the node. (Note that much of the
13061f28255Scgd * code generation knows about this implicit relationship.)
13161f28255Scgd *
13261f28255Scgd * Using two bytes for the "next" pointer is vast overkill for most things,
13361f28255Scgd * but allows patterns to get big without disasters.
13461f28255Scgd */
13561f28255Scgd #define OP(p) (*(p))
13661f28255Scgd #define NEXT(p) (((*((p)+1)&0377)<<8) + (*((p)+2)&0377))
13761f28255Scgd #define OPERAND(p) ((p) + 3)
13861f28255Scgd
13961f28255Scgd /*
14061f28255Scgd * See regmagic.h for one further detail of program structure.
14161f28255Scgd */
14261f28255Scgd
14361f28255Scgd
14461f28255Scgd /*
14561f28255Scgd * Utility definitions.
14661f28255Scgd */
14761f28255Scgd #ifndef CHARBITS
14861f28255Scgd #define UCHARAT(p) ((int)*(unsigned char *)(p))
14961f28255Scgd #else
15061f28255Scgd #define UCHARAT(p) ((int)*(p)&CHARBITS)
15161f28255Scgd #endif
15261f28255Scgd
15361f28255Scgd #define FAIL(m) { regerror(m); return(NULL); }
15461f28255Scgd #define ISMULT(c) ((c) == '*' || (c) == '+' || (c) == '?')
15561f28255Scgd
15661f28255Scgd /*
15761f28255Scgd * Flags to be passed up and down.
15861f28255Scgd */
15961f28255Scgd #define HASWIDTH 01 /* Known never to match null string. */
16061f28255Scgd #define SIMPLE 02 /* Simple enough to be STAR/PLUS operand. */
16161f28255Scgd #define SPSTART 04 /* Starts with * or +. */
16261f28255Scgd #define WORST 0 /* Worst case. */
16361f28255Scgd
16461f28255Scgd /*
16561f28255Scgd * Global work variables for regcomp().
16661f28255Scgd */
16761f28255Scgd static char *regparse; /* Input-scan pointer. */
16861f28255Scgd static int regnpar; /* () count. */
16961f28255Scgd static char regdummy;
17061f28255Scgd static char *regcode; /* Code-emit pointer; ®dummy = don't. */
17161f28255Scgd static long regsize; /* Code size. */
17261f28255Scgd
17361f28255Scgd /*
17461f28255Scgd * Forward declarations for regcomp()'s friends.
17561f28255Scgd */
17661f28255Scgd #ifndef STATIC
17761f28255Scgd #define STATIC static
17861f28255Scgd #endif
179dde1c1a0Spk STATIC char *reg __P((int, int *));
180dde1c1a0Spk STATIC char *regbranch __P((int *));
181dde1c1a0Spk STATIC char *regpiece __P((int *));
182dde1c1a0Spk STATIC char *regatom __P((int *));
183d2c07ddcSchristos STATIC char *regnode __P((int));
184dde1c1a0Spk STATIC char *regnext __P((char *));
185d2c07ddcSchristos STATIC void regc __P((int));
186d2c07ddcSchristos STATIC void reginsert __P((int, char *));
187dde1c1a0Spk STATIC void regtail __P((char *, char *));
188dde1c1a0Spk STATIC void regoptail __P((char *, char *));
18961f28255Scgd #ifdef STRCSPN
190dde1c1a0Spk STATIC int strcspn __P((char *, char *));
19161f28255Scgd #endif
19261f28255Scgd
19361f28255Scgd /*
19461f28255Scgd - regcomp - compile a regular expression into internal code
19561f28255Scgd *
19661f28255Scgd * We can't allocate space until we know how big the compiled form will be,
19761f28255Scgd * but we can't compile it (and thus know how big it is) until we've got a
19861f28255Scgd * place to put the code. So we cheat: we compile it twice, once with code
19961f28255Scgd * generation turned off and size counting turned on, and once "for real".
20061f28255Scgd * This also means that we don't allocate space until we are sure that the
20161f28255Scgd * thing really will compile successfully, and we never have to move the
20261f28255Scgd * code and thus invalidate pointers into it. (Note that it has to be in
20361f28255Scgd * one piece because free() must be able to free it all.)
20461f28255Scgd *
20561f28255Scgd * Beware that the optimization-preparation code in here knows about some
20661f28255Scgd * of the structure of the compiled regexp.
20761f28255Scgd */
20861f28255Scgd regexp *
__compat_regcomp(expn)2094ba7375dSthorpej __compat_regcomp(expn)
2104ba7375dSthorpej const char *expn;
21161f28255Scgd {
21274174020Sperry regexp *r;
21374174020Sperry char *scan;
21474174020Sperry char *longest;
21574174020Sperry int len;
21661f28255Scgd int flags;
21761f28255Scgd
2184ba7375dSthorpej if (expn == NULL)
21961f28255Scgd FAIL("NULL argument");
22061f28255Scgd
22161f28255Scgd /* First pass: determine size, legality. */
22261f28255Scgd #ifdef notdef
2234ba7375dSthorpej if (expn[0] == '.' && expn[1] == '*') expn += 2; /* aid grep */
22461f28255Scgd #endif
225d2c07ddcSchristos /* LINTED const castaway */
2264ba7375dSthorpej regparse = (char *)expn;
22761f28255Scgd regnpar = 1;
22861f28255Scgd regsize = 0L;
22961f28255Scgd regcode = ®dummy;
23061f28255Scgd regc(MAGIC);
23161f28255Scgd if (reg(0, &flags) == NULL)
23261f28255Scgd return(NULL);
23361f28255Scgd
23461f28255Scgd /* Small enough for pointer-storage convention? */
23561f28255Scgd if (regsize >= 32767L) /* Probably could be 65535L. */
23661f28255Scgd FAIL("regexp too big");
23761f28255Scgd
23861f28255Scgd /* Allocate space. */
239*3d64af45Schristos r = malloc(sizeof(regexp) + (unsigned)regsize);
24061f28255Scgd if (r == NULL)
24161f28255Scgd FAIL("out of space");
24261f28255Scgd
24361f28255Scgd /* Second pass: emit code. */
244d2c07ddcSchristos /* LINTED const castaway */
2454ba7375dSthorpej regparse = (char *)expn;
24661f28255Scgd regnpar = 1;
24761f28255Scgd regcode = r->program;
24861f28255Scgd regc(MAGIC);
249*3d64af45Schristos if (reg(0, &flags) == NULL) {
250*3d64af45Schristos free(r);
25161f28255Scgd return(NULL);
252*3d64af45Schristos }
25361f28255Scgd
25461f28255Scgd /* Dig out information for optimizations. */
25561f28255Scgd r->regstart = '\0'; /* Worst-case defaults. */
25661f28255Scgd r->reganch = 0;
25761f28255Scgd r->regmust = NULL;
25861f28255Scgd r->regmlen = 0;
25961f28255Scgd scan = r->program+1; /* First BRANCH. */
26061f28255Scgd if (OP(regnext(scan)) == END) { /* Only one top-level choice. */
26161f28255Scgd scan = OPERAND(scan);
26261f28255Scgd
26361f28255Scgd /* Starting-point info. */
26461f28255Scgd if (OP(scan) == EXACTLY)
26561f28255Scgd r->regstart = *OPERAND(scan);
26661f28255Scgd else if (OP(scan) == BOL)
26761f28255Scgd r->reganch++;
26861f28255Scgd
26961f28255Scgd /*
27061f28255Scgd * If there's something expensive in the r.e., find the
27161f28255Scgd * longest literal string that must appear and make it the
27261f28255Scgd * regmust. Resolve ties in favor of later strings, since
27361f28255Scgd * the regstart check works with the beginning of the r.e.
27461f28255Scgd * and avoiding duplication strengthens checking. Not a
27561f28255Scgd * strong reason, but sufficient in the absence of others.
27661f28255Scgd */
27761f28255Scgd if (flags&SPSTART) {
27861f28255Scgd longest = NULL;
27961f28255Scgd len = 0;
28061f28255Scgd for (; scan != NULL; scan = regnext(scan))
2814ba7375dSthorpej if (OP(scan) == EXACTLY && (int) strlen(OPERAND(scan)) >= len) {
28261f28255Scgd longest = OPERAND(scan);
28361f28255Scgd len = strlen(OPERAND(scan));
28461f28255Scgd }
28561f28255Scgd r->regmust = longest;
28661f28255Scgd r->regmlen = len;
28761f28255Scgd }
28861f28255Scgd }
28961f28255Scgd
29061f28255Scgd return(r);
29161f28255Scgd }
29261f28255Scgd
29361f28255Scgd /*
29461f28255Scgd - reg - regular expression, i.e. main body or parenthesized thing
29561f28255Scgd *
29661f28255Scgd * Caller must absorb opening parenthesis.
29761f28255Scgd *
29861f28255Scgd * Combining parenthesis handling with the base level of regular expression
29961f28255Scgd * is a trifle forced, but the need to tie the tails of the branches to what
30061f28255Scgd * follows makes it hard to avoid.
30161f28255Scgd */
30261f28255Scgd static char *
reg(paren,flagp)30361f28255Scgd reg(paren, flagp)
30461f28255Scgd int paren; /* Parenthesized? */
30561f28255Scgd int *flagp;
30661f28255Scgd {
30774174020Sperry char *ret;
30874174020Sperry char *br;
30974174020Sperry char *ender;
31074174020Sperry int parno = 0;
31161f28255Scgd int flags;
31261f28255Scgd
31361f28255Scgd *flagp = HASWIDTH; /* Tentatively. */
31461f28255Scgd
31561f28255Scgd /* Make an OPEN node, if parenthesized. */
31661f28255Scgd if (paren) {
31761f28255Scgd if (regnpar >= NSUBEXP)
31861f28255Scgd FAIL("too many ()");
31961f28255Scgd parno = regnpar;
32061f28255Scgd regnpar++;
32161f28255Scgd ret = regnode(OPEN+parno);
32261f28255Scgd } else
32361f28255Scgd ret = NULL;
32461f28255Scgd
32561f28255Scgd /* Pick up the branches, linking them together. */
32661f28255Scgd br = regbranch(&flags);
32761f28255Scgd if (br == NULL)
32861f28255Scgd return(NULL);
32961f28255Scgd if (ret != NULL)
33061f28255Scgd regtail(ret, br); /* OPEN -> first. */
33161f28255Scgd else
33261f28255Scgd ret = br;
33361f28255Scgd if (!(flags&HASWIDTH))
33461f28255Scgd *flagp &= ~HASWIDTH;
33561f28255Scgd *flagp |= flags&SPSTART;
33661f28255Scgd while (*regparse == '|' || *regparse == '\n') {
33761f28255Scgd regparse++;
33861f28255Scgd br = regbranch(&flags);
33961f28255Scgd if (br == NULL)
34061f28255Scgd return(NULL);
34161f28255Scgd regtail(ret, br); /* BRANCH -> BRANCH. */
34261f28255Scgd if (!(flags&HASWIDTH))
34361f28255Scgd *flagp &= ~HASWIDTH;
34461f28255Scgd *flagp |= flags&SPSTART;
34561f28255Scgd }
34661f28255Scgd
34761f28255Scgd /* Make a closing node, and hook it on the end. */
34861f28255Scgd ender = regnode((paren) ? CLOSE+parno : END);
34961f28255Scgd regtail(ret, ender);
35061f28255Scgd
35161f28255Scgd /* Hook the tails of the branches to the closing node. */
35261f28255Scgd for (br = ret; br != NULL; br = regnext(br))
35361f28255Scgd regoptail(br, ender);
35461f28255Scgd
35561f28255Scgd /* Check for proper termination. */
35661f28255Scgd if (paren && *regparse++ != ')') {
35761f28255Scgd FAIL("unmatched ()");
35861f28255Scgd } else if (!paren && *regparse != '\0') {
35961f28255Scgd if (*regparse == ')') {
36061f28255Scgd FAIL("unmatched ()");
36161f28255Scgd } else
36261f28255Scgd FAIL("junk on end"); /* "Can't happen". */
36361f28255Scgd /* NOTREACHED */
36461f28255Scgd }
36561f28255Scgd
36661f28255Scgd return(ret);
36761f28255Scgd }
36861f28255Scgd
36961f28255Scgd /*
37061f28255Scgd - regbranch - one alternative of an | operator
37161f28255Scgd *
37261f28255Scgd * Implements the concatenation operator.
37361f28255Scgd */
37461f28255Scgd static char *
regbranch(flagp)37561f28255Scgd regbranch(flagp)
37661f28255Scgd int *flagp;
37761f28255Scgd {
37874174020Sperry char *ret;
37974174020Sperry char *chain;
38074174020Sperry char *latest;
38161f28255Scgd int flags;
38261f28255Scgd
38361f28255Scgd *flagp = WORST; /* Tentatively. */
38461f28255Scgd
38561f28255Scgd ret = regnode(BRANCH);
38661f28255Scgd chain = NULL;
38761f28255Scgd while (*regparse != '\0' && *regparse != ')' &&
38861f28255Scgd *regparse != '\n' && *regparse != '|') {
38961f28255Scgd latest = regpiece(&flags);
39061f28255Scgd if (latest == NULL)
39161f28255Scgd return(NULL);
39261f28255Scgd *flagp |= flags&HASWIDTH;
39361f28255Scgd if (chain == NULL) /* First piece. */
39461f28255Scgd *flagp |= flags&SPSTART;
39561f28255Scgd else
39661f28255Scgd regtail(chain, latest);
39761f28255Scgd chain = latest;
39861f28255Scgd }
39961f28255Scgd if (chain == NULL) /* Loop ran zero times. */
40061f28255Scgd (void) regnode(NOTHING);
40161f28255Scgd
40261f28255Scgd return(ret);
40361f28255Scgd }
40461f28255Scgd
40561f28255Scgd /*
40661f28255Scgd - regpiece - something followed by possible [*+?]
40761f28255Scgd *
40861f28255Scgd * Note that the branching code sequences used for ? and the general cases
40961f28255Scgd * of * and + are somewhat optimized: they use the same NOTHING node as
41061f28255Scgd * both the endmarker for their branch list and the body of the last branch.
41161f28255Scgd * It might seem that this node could be dispensed with entirely, but the
41261f28255Scgd * endmarker role is not redundant.
41361f28255Scgd */
41461f28255Scgd static char *
regpiece(flagp)41561f28255Scgd regpiece(flagp)
41661f28255Scgd int *flagp;
41761f28255Scgd {
41874174020Sperry char *ret;
41974174020Sperry char op;
42074174020Sperry char *next;
42161f28255Scgd int flags;
42261f28255Scgd
42361f28255Scgd ret = regatom(&flags);
42461f28255Scgd if (ret == NULL)
42561f28255Scgd return(NULL);
42661f28255Scgd
42761f28255Scgd op = *regparse;
42861f28255Scgd if (!ISMULT(op)) {
42961f28255Scgd *flagp = flags;
43061f28255Scgd return(ret);
43161f28255Scgd }
43261f28255Scgd
43361f28255Scgd if (!(flags&HASWIDTH) && op != '?')
43461f28255Scgd FAIL("*+ operand could be empty");
43561f28255Scgd *flagp = (op != '+') ? (WORST|SPSTART) : (WORST|HASWIDTH);
43661f28255Scgd
43761f28255Scgd if (op == '*' && (flags&SIMPLE))
43861f28255Scgd reginsert(STAR, ret);
43961f28255Scgd else if (op == '*') {
44061f28255Scgd /* Emit x* as (x&|), where & means "self". */
44161f28255Scgd reginsert(BRANCH, ret); /* Either x */
44261f28255Scgd regoptail(ret, regnode(BACK)); /* and loop */
44361f28255Scgd regoptail(ret, ret); /* back */
44461f28255Scgd regtail(ret, regnode(BRANCH)); /* or */
44561f28255Scgd regtail(ret, regnode(NOTHING)); /* null. */
44661f28255Scgd } else if (op == '+' && (flags&SIMPLE))
44761f28255Scgd reginsert(PLUS, ret);
44861f28255Scgd else if (op == '+') {
44961f28255Scgd /* Emit x+ as x(&|), where & means "self". */
45061f28255Scgd next = regnode(BRANCH); /* Either */
45161f28255Scgd regtail(ret, next);
45261f28255Scgd regtail(regnode(BACK), ret); /* loop back */
45361f28255Scgd regtail(next, regnode(BRANCH)); /* or */
45461f28255Scgd regtail(ret, regnode(NOTHING)); /* null. */
45561f28255Scgd } else if (op == '?') {
45661f28255Scgd /* Emit x? as (x|) */
45761f28255Scgd reginsert(BRANCH, ret); /* Either x */
45861f28255Scgd regtail(ret, regnode(BRANCH)); /* or */
45961f28255Scgd next = regnode(NOTHING); /* null. */
46061f28255Scgd regtail(ret, next);
46161f28255Scgd regoptail(ret, next);
46261f28255Scgd }
46361f28255Scgd regparse++;
46461f28255Scgd if (ISMULT(*regparse))
46561f28255Scgd FAIL("nested *?+");
46661f28255Scgd
46761f28255Scgd return(ret);
46861f28255Scgd }
46961f28255Scgd
47061f28255Scgd /*
47161f28255Scgd - regatom - the lowest level
47261f28255Scgd *
47361f28255Scgd * Optimization: gobbles an entire sequence of ordinary characters so that
47461f28255Scgd * it can turn them into a single node, which is smaller to store and
47561f28255Scgd * faster to run. Backslashed characters are exceptions, each becoming a
47661f28255Scgd * separate node; the code is simpler that way and it's not worth fixing.
47761f28255Scgd */
47861f28255Scgd static char *
regatom(flagp)47961f28255Scgd regatom(flagp)
48061f28255Scgd int *flagp;
48161f28255Scgd {
48274174020Sperry char *ret;
48361f28255Scgd int flags;
48461f28255Scgd
48561f28255Scgd *flagp = WORST; /* Tentatively. */
48661f28255Scgd
48761f28255Scgd switch (*regparse++) {
48861f28255Scgd /* FIXME: these chars only have meaning at beg/end of pat? */
48961f28255Scgd case '^':
49061f28255Scgd ret = regnode(BOL);
49161f28255Scgd break;
49261f28255Scgd case '$':
49361f28255Scgd ret = regnode(EOL);
49461f28255Scgd break;
49561f28255Scgd case '.':
49661f28255Scgd ret = regnode(ANY);
49761f28255Scgd *flagp |= HASWIDTH|SIMPLE;
49861f28255Scgd break;
49961f28255Scgd case '[': {
50074174020Sperry int class;
50174174020Sperry int classend;
50261f28255Scgd
50361f28255Scgd if (*regparse == '^') { /* Complement of range. */
50461f28255Scgd ret = regnode(ANYBUT);
50561f28255Scgd regparse++;
50661f28255Scgd } else
50761f28255Scgd ret = regnode(ANYOF);
50861f28255Scgd if (*regparse == ']' || *regparse == '-')
50961f28255Scgd regc(*regparse++);
51061f28255Scgd while (*regparse != '\0' && *regparse != ']') {
51161f28255Scgd if (*regparse == '-') {
51261f28255Scgd regparse++;
51361f28255Scgd if (*regparse == ']' || *regparse == '\0')
51461f28255Scgd regc('-');
51561f28255Scgd else {
51661f28255Scgd class = UCHARAT(regparse-2)+1;
51761f28255Scgd classend = UCHARAT(regparse);
51861f28255Scgd if (class > classend+1)
51961f28255Scgd FAIL("invalid [] range");
52061f28255Scgd for (; class <= classend; class++)
52161f28255Scgd regc(class);
52261f28255Scgd regparse++;
52361f28255Scgd }
52461f28255Scgd } else
52561f28255Scgd regc(*regparse++);
52661f28255Scgd }
52761f28255Scgd regc('\0');
52861f28255Scgd if (*regparse != ']')
52961f28255Scgd FAIL("unmatched []");
53061f28255Scgd regparse++;
53161f28255Scgd *flagp |= HASWIDTH|SIMPLE;
53261f28255Scgd }
53361f28255Scgd break;
53461f28255Scgd case '(':
53561f28255Scgd ret = reg(1, &flags);
53661f28255Scgd if (ret == NULL)
53761f28255Scgd return(NULL);
53861f28255Scgd *flagp |= flags&(HASWIDTH|SPSTART);
53961f28255Scgd break;
54061f28255Scgd case '\0':
54161f28255Scgd case '|':
54261f28255Scgd case '\n':
54361f28255Scgd case ')':
54461f28255Scgd FAIL("internal urp"); /* Supposed to be caught earlier. */
54561f28255Scgd case '?':
54661f28255Scgd case '+':
54761f28255Scgd case '*':
54861f28255Scgd FAIL("?+* follows nothing");
54961f28255Scgd case '\\':
55061f28255Scgd switch (*regparse++) {
55161f28255Scgd case '\0':
55261f28255Scgd FAIL("trailing \\");
55361f28255Scgd case '<':
55461f28255Scgd ret = regnode(WORDA);
55561f28255Scgd break;
55661f28255Scgd case '>':
55761f28255Scgd ret = regnode(WORDZ);
55861f28255Scgd break;
55961f28255Scgd /* FIXME: Someday handle \1, \2, ... */
56061f28255Scgd default:
56161f28255Scgd /* Handle general quoted chars in exact-match routine */
56261f28255Scgd goto de_fault;
56361f28255Scgd }
56461f28255Scgd break;
56561f28255Scgd de_fault:
566d2c07ddcSchristos /*FALLTHROUGH*/
56761f28255Scgd default:
56861f28255Scgd /*
56961f28255Scgd * Encode a string of characters to be matched exactly.
57061f28255Scgd *
57161f28255Scgd * This is a bit tricky due to quoted chars and due to
57261f28255Scgd * '*', '+', and '?' taking the SINGLE char previous
57361f28255Scgd * as their operand.
57461f28255Scgd *
57561f28255Scgd * On entry, the char at regparse[-1] is going to go
57661f28255Scgd * into the string, no matter what it is. (It could be
57761f28255Scgd * following a \ if we are entered from the '\' case.)
57861f28255Scgd *
57961f28255Scgd * Basic idea is to pick up a good char in ch and
58061f28255Scgd * examine the next char. If it's *+? then we twiddle.
58161f28255Scgd * If it's \ then we frozzle. If it's other magic char
58261f28255Scgd * we push ch and terminate the string. If none of the
58361f28255Scgd * above, we push ch on the string and go around again.
58461f28255Scgd *
58561f28255Scgd * regprev is used to remember where "the current char"
58661f28255Scgd * starts in the string, if due to a *+? we need to back
58761f28255Scgd * up and put the current char in a separate, 1-char, string.
58861f28255Scgd * When regprev is NULL, ch is the only char in the
58961f28255Scgd * string; this is used in *+? handling, and in setting
59061f28255Scgd * flags |= SIMPLE at the end.
59161f28255Scgd */
59261f28255Scgd {
59361f28255Scgd char *regprev;
59474174020Sperry char ch;
59561f28255Scgd
59661f28255Scgd regparse--; /* Look at cur char */
59761f28255Scgd ret = regnode(EXACTLY);
59861f28255Scgd for ( regprev = 0 ; ; ) {
59961f28255Scgd ch = *regparse++; /* Get current char */
60061f28255Scgd switch (*regparse) { /* look at next one */
60161f28255Scgd
60261f28255Scgd default:
60361f28255Scgd regc(ch); /* Add cur to string */
60461f28255Scgd break;
60561f28255Scgd
60661f28255Scgd case '.': case '[': case '(':
60761f28255Scgd case ')': case '|': case '\n':
60861f28255Scgd case '$': case '^':
60961f28255Scgd case '\0':
61061f28255Scgd /* FIXME, $ and ^ should not always be magic */
61161f28255Scgd magic:
61261f28255Scgd regc(ch); /* dump cur char */
61361f28255Scgd goto done; /* and we are done */
61461f28255Scgd
61561f28255Scgd case '?': case '+': case '*':
61661f28255Scgd if (!regprev) /* If just ch in str, */
61761f28255Scgd goto magic; /* use it */
61861f28255Scgd /* End mult-char string one early */
61961f28255Scgd regparse = regprev; /* Back up parse */
62061f28255Scgd goto done;
62161f28255Scgd
62261f28255Scgd case '\\':
62361f28255Scgd regc(ch); /* Cur char OK */
62461f28255Scgd switch (regparse[1]){ /* Look after \ */
62561f28255Scgd case '\0':
62661f28255Scgd case '<':
62761f28255Scgd case '>':
62861f28255Scgd /* FIXME: Someday handle \1, \2, ... */
62961f28255Scgd goto done; /* Not quoted */
63061f28255Scgd default:
63161f28255Scgd /* Backup point is \, scan * point is after it. */
63261f28255Scgd regprev = regparse;
63361f28255Scgd regparse++;
63461f28255Scgd continue; /* NOT break; */
63561f28255Scgd }
63661f28255Scgd }
63761f28255Scgd regprev = regparse; /* Set backup point */
63861f28255Scgd }
63961f28255Scgd done:
64061f28255Scgd regc('\0');
64161f28255Scgd *flagp |= HASWIDTH;
64261f28255Scgd if (!regprev) /* One char? */
64361f28255Scgd *flagp |= SIMPLE;
64461f28255Scgd }
64561f28255Scgd break;
64661f28255Scgd }
64761f28255Scgd
64861f28255Scgd return(ret);
64961f28255Scgd }
65061f28255Scgd
65161f28255Scgd /*
65261f28255Scgd - regnode - emit a node
65361f28255Scgd */
65461f28255Scgd static char * /* Location. */
regnode(op)65561f28255Scgd regnode(op)
656d2c07ddcSchristos int op;
65761f28255Scgd {
65874174020Sperry char *ret;
65974174020Sperry char *ptr;
66061f28255Scgd
66161f28255Scgd ret = regcode;
66261f28255Scgd if (ret == ®dummy) {
66361f28255Scgd regsize += 3;
66461f28255Scgd return(ret);
66561f28255Scgd }
66661f28255Scgd
66761f28255Scgd ptr = ret;
66861f28255Scgd *ptr++ = op;
66961f28255Scgd *ptr++ = '\0'; /* Null "next" pointer. */
67061f28255Scgd *ptr++ = '\0';
67161f28255Scgd regcode = ptr;
67261f28255Scgd
67361f28255Scgd return(ret);
67461f28255Scgd }
67561f28255Scgd
67661f28255Scgd /*
67761f28255Scgd - regc - emit (if appropriate) a byte of code
67861f28255Scgd */
67961f28255Scgd static void
regc(b)68061f28255Scgd regc(b)
681d2c07ddcSchristos int b;
68261f28255Scgd {
68361f28255Scgd if (regcode != ®dummy)
68461f28255Scgd *regcode++ = b;
68561f28255Scgd else
68661f28255Scgd regsize++;
68761f28255Scgd }
68861f28255Scgd
68961f28255Scgd /*
69061f28255Scgd - reginsert - insert an operator in front of already-emitted operand
69161f28255Scgd *
69261f28255Scgd * Means relocating the operand.
69361f28255Scgd */
69461f28255Scgd static void
reginsert(op,opnd)69561f28255Scgd reginsert(op, opnd)
696d2c07ddcSchristos int op;
69761f28255Scgd char *opnd;
69861f28255Scgd {
69974174020Sperry char *src;
70074174020Sperry char *dst;
70174174020Sperry char *place;
70261f28255Scgd
70361f28255Scgd if (regcode == ®dummy) {
70461f28255Scgd regsize += 3;
70561f28255Scgd return;
70661f28255Scgd }
70761f28255Scgd
70861f28255Scgd src = regcode;
70961f28255Scgd regcode += 3;
71061f28255Scgd dst = regcode;
71161f28255Scgd while (src > opnd)
71261f28255Scgd *--dst = *--src;
71361f28255Scgd
71461f28255Scgd place = opnd; /* Op node, where operand used to be. */
71561f28255Scgd *place++ = op;
71661f28255Scgd *place++ = '\0';
71761f28255Scgd *place++ = '\0';
71861f28255Scgd }
71961f28255Scgd
72061f28255Scgd /*
72161f28255Scgd - regtail - set the next-pointer at the end of a node chain
72261f28255Scgd */
72361f28255Scgd static void
regtail(p,val)72461f28255Scgd regtail(p, val)
72561f28255Scgd char *p;
72661f28255Scgd char *val;
72761f28255Scgd {
72874174020Sperry char *scan;
72974174020Sperry char *temp;
73074174020Sperry int offset;
73161f28255Scgd
73261f28255Scgd if (p == ®dummy)
73361f28255Scgd return;
73461f28255Scgd
73561f28255Scgd /* Find last node. */
73661f28255Scgd scan = p;
73761f28255Scgd for (;;) {
73861f28255Scgd temp = regnext(scan);
73961f28255Scgd if (temp == NULL)
74061f28255Scgd break;
74161f28255Scgd scan = temp;
74261f28255Scgd }
74361f28255Scgd
74461f28255Scgd if (OP(scan) == BACK)
74561f28255Scgd offset = scan - val;
74661f28255Scgd else
74761f28255Scgd offset = val - scan;
748d2c07ddcSchristos *(scan+1) = ((unsigned int)offset>>8)&0377;
74961f28255Scgd *(scan+2) = offset&0377;
75061f28255Scgd }
75161f28255Scgd
75261f28255Scgd /*
75361f28255Scgd - regoptail - regtail on operand of first argument; nop if operandless
75461f28255Scgd */
75561f28255Scgd static void
regoptail(p,val)75661f28255Scgd regoptail(p, val)
75761f28255Scgd char *p;
75861f28255Scgd char *val;
75961f28255Scgd {
76061f28255Scgd /* "Operandless" and "op != BRANCH" are synonymous in practice. */
76161f28255Scgd if (p == NULL || p == ®dummy || OP(p) != BRANCH)
76261f28255Scgd return;
76361f28255Scgd regtail(OPERAND(p), val);
76461f28255Scgd }
76561f28255Scgd
76661f28255Scgd /*
76761f28255Scgd * regexec and friends
76861f28255Scgd */
76961f28255Scgd
77061f28255Scgd /*
77161f28255Scgd * Global work variables for regexec().
77261f28255Scgd */
77361f28255Scgd static char *reginput; /* String-input pointer. */
77461f28255Scgd static char *regbol; /* Beginning of input, for ^ check. */
77561f28255Scgd static char **regstartp; /* Pointer to startp array. */
77661f28255Scgd static char **regendp; /* Ditto for endp. */
77761f28255Scgd
77861f28255Scgd /*
77961f28255Scgd * Forwards.
78061f28255Scgd */
781dde1c1a0Spk STATIC int regtry __P((const regexp *, const char *));
782dde1c1a0Spk STATIC int regmatch __P((char *));
783dde1c1a0Spk STATIC int regrepeat __P((char *));
78461f28255Scgd
78561f28255Scgd #ifdef DEBUG
78661f28255Scgd int regnarrate = 0;
787dde1c1a0Spk void regdump __P((regexp *));
788dde1c1a0Spk STATIC char *regprop __P((char *));
78961f28255Scgd #endif
79061f28255Scgd
79161f28255Scgd /*
79261f28255Scgd - regexec - match a regexp against a string
79361f28255Scgd */
79461f28255Scgd int
__compat_regexec(prog,string)7955426fd27Stv __compat_regexec(prog, string)
79674174020Sperry const regexp *prog;
79774174020Sperry const char *string;
79861f28255Scgd {
79974174020Sperry char *s;
80061f28255Scgd
80161f28255Scgd /* Be paranoid... */
80261f28255Scgd if (prog == NULL || string == NULL) {
80361f28255Scgd regerror("NULL parameter");
80461f28255Scgd return(0);
80561f28255Scgd }
80661f28255Scgd
80761f28255Scgd /* Check validity of program. */
80861f28255Scgd if (UCHARAT(prog->program) != MAGIC) {
80961f28255Scgd regerror("corrupted program");
81061f28255Scgd return(0);
81161f28255Scgd }
81261f28255Scgd
81361f28255Scgd /* If there is a "must appear" string, look for it. */
81461f28255Scgd if (prog->regmust != NULL) {
815d2c07ddcSchristos /* LINTED const castaway */
81661f28255Scgd s = (char *)string;
81761f28255Scgd while ((s = strchr(s, prog->regmust[0])) != NULL) {
818d2c07ddcSchristos if (strncmp(s, prog->regmust,
819d2c07ddcSchristos (size_t)prog->regmlen) == 0)
82061f28255Scgd break; /* Found it. */
82161f28255Scgd s++;
82261f28255Scgd }
82361f28255Scgd if (s == NULL) /* Not present. */
82461f28255Scgd return(0);
82561f28255Scgd }
82661f28255Scgd
82761f28255Scgd /* Mark beginning of line for ^ . */
828d2c07ddcSchristos /* LINTED const castaway */
82961f28255Scgd regbol = (char *)string;
83061f28255Scgd
83161f28255Scgd /* Simplest case: anchored match need be tried only once. */
83261f28255Scgd if (prog->reganch)
83361f28255Scgd return(regtry(prog, string));
83461f28255Scgd
83561f28255Scgd /* Messy cases: unanchored match. */
836d2c07ddcSchristos /* LINTED const castaway */
83761f28255Scgd s = (char *)string;
83861f28255Scgd if (prog->regstart != '\0')
83961f28255Scgd /* We know what char it must start with. */
84061f28255Scgd while ((s = strchr(s, prog->regstart)) != NULL) {
84161f28255Scgd if (regtry(prog, s))
84261f28255Scgd return(1);
84361f28255Scgd s++;
84461f28255Scgd }
84561f28255Scgd else
84661f28255Scgd /* We don't -- general case. */
84761f28255Scgd do {
84861f28255Scgd if (regtry(prog, s))
84961f28255Scgd return(1);
85061f28255Scgd } while (*s++ != '\0');
85161f28255Scgd
85261f28255Scgd /* Failure. */
85361f28255Scgd return(0);
85461f28255Scgd }
85561f28255Scgd
85661f28255Scgd /*
85761f28255Scgd - regtry - try match at specific point
85861f28255Scgd */
85961f28255Scgd static int /* 0 failure, 1 success */
regtry(prog,string)86061f28255Scgd regtry(prog, string)
861dde1c1a0Spk const regexp *prog;
862dde1c1a0Spk const char *string;
86361f28255Scgd {
86474174020Sperry int i;
86574174020Sperry char **sp;
86674174020Sperry char **ep;
86761f28255Scgd
868d2c07ddcSchristos /* LINTED const castaway */
8695fb36cfbScgd reginput = (char *)string; /* XXX */
8705fb36cfbScgd regstartp = (char **)prog->startp; /* XXX */
8715fb36cfbScgd regendp = (char **)prog->endp; /* XXX */
87261f28255Scgd
8735fb36cfbScgd sp = (char **)prog->startp; /* XXX */
8745fb36cfbScgd ep = (char **)prog->endp; /* XXX */
87561f28255Scgd for (i = NSUBEXP; i > 0; i--) {
87661f28255Scgd *sp++ = NULL;
87761f28255Scgd *ep++ = NULL;
87861f28255Scgd }
8795fb36cfbScgd if (regmatch((char *)prog->program + 1)) { /* XXX */
880d2c07ddcSchristos /* LINTED const castaway */
8815fb36cfbScgd ((regexp *)prog)->startp[0] = (char *)string; /* XXX */
882d2c07ddcSchristos /* LINTED const castaway */
8835fb36cfbScgd ((regexp *)prog)->endp[0] = reginput; /* XXX */
88461f28255Scgd return(1);
88561f28255Scgd } else
88661f28255Scgd return(0);
88761f28255Scgd }
88861f28255Scgd
88961f28255Scgd /*
89061f28255Scgd - regmatch - main matching routine
89161f28255Scgd *
89261f28255Scgd * Conceptually the strategy is simple: check to see whether the current
89361f28255Scgd * node matches, call self recursively to see whether the rest matches,
89461f28255Scgd * and then act accordingly. In practice we make some effort to avoid
89561f28255Scgd * recursion, in particular by going through "ordinary" nodes (that don't
89661f28255Scgd * need to know whether the rest of the match failed) by a loop instead of
89761f28255Scgd * by recursion.
89861f28255Scgd */
89961f28255Scgd static int /* 0 failure, 1 success */
regmatch(prog)90061f28255Scgd regmatch(prog)
90161f28255Scgd char *prog;
90261f28255Scgd {
90374174020Sperry char *scan; /* Current node. */
90461f28255Scgd char *next; /* Next node. */
90561f28255Scgd
90661f28255Scgd scan = prog;
90761f28255Scgd #ifdef DEBUG
90861f28255Scgd if (scan != NULL && regnarrate)
90961f28255Scgd fprintf(stderr, "%s(\n", regprop(scan));
91061f28255Scgd #endif
91161f28255Scgd while (scan != NULL) {
91261f28255Scgd #ifdef DEBUG
91361f28255Scgd if (regnarrate)
91461f28255Scgd fprintf(stderr, "%s...\n", regprop(scan));
91561f28255Scgd #endif
91661f28255Scgd next = regnext(scan);
91761f28255Scgd
91861f28255Scgd switch (OP(scan)) {
91961f28255Scgd case BOL:
92061f28255Scgd if (reginput != regbol)
92161f28255Scgd return(0);
92261f28255Scgd break;
92361f28255Scgd case EOL:
92461f28255Scgd if (*reginput != '\0')
92561f28255Scgd return(0);
92661f28255Scgd break;
92761f28255Scgd case WORDA:
92861f28255Scgd /* Must be looking at a letter, digit, or _ */
929f9863c47Sitohy if ((!isalnum(UCHARAT(reginput))) && *reginput != '_')
93061f28255Scgd return(0);
93161f28255Scgd /* Prev must be BOL or nonword */
93261f28255Scgd if (reginput > regbol &&
933f9863c47Sitohy (isalnum(UCHARAT(reginput - 1))
934f9863c47Sitohy || reginput[-1] == '_'))
93561f28255Scgd return(0);
93661f28255Scgd break;
93761f28255Scgd case WORDZ:
93861f28255Scgd /* Must be looking at non letter, digit, or _ */
939f9863c47Sitohy if (isalnum(UCHARAT(reginput)) || *reginput == '_')
94061f28255Scgd return(0);
94161f28255Scgd /* We don't care what the previous char was */
94261f28255Scgd break;
94361f28255Scgd case ANY:
94461f28255Scgd if (*reginput == '\0')
94561f28255Scgd return(0);
94661f28255Scgd reginput++;
94761f28255Scgd break;
94861f28255Scgd case EXACTLY: {
94974174020Sperry int len;
95074174020Sperry char *opnd;
95161f28255Scgd
95261f28255Scgd opnd = OPERAND(scan);
95361f28255Scgd /* Inline the first character, for speed. */
95461f28255Scgd if (*opnd != *reginput)
95561f28255Scgd return(0);
95661f28255Scgd len = strlen(opnd);
957d2c07ddcSchristos if (len > 1 && strncmp(opnd, reginput,
958d2c07ddcSchristos (size_t)len) != 0)
95961f28255Scgd return(0);
96061f28255Scgd reginput += len;
96161f28255Scgd }
96261f28255Scgd break;
96361f28255Scgd case ANYOF:
96461f28255Scgd if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) == NULL)
96561f28255Scgd return(0);
96661f28255Scgd reginput++;
96761f28255Scgd break;
96861f28255Scgd case ANYBUT:
96961f28255Scgd if (*reginput == '\0' || strchr(OPERAND(scan), *reginput) != NULL)
97061f28255Scgd return(0);
97161f28255Scgd reginput++;
97261f28255Scgd break;
97361f28255Scgd case NOTHING:
97461f28255Scgd break;
97561f28255Scgd case BACK:
97661f28255Scgd break;
97761f28255Scgd case OPEN+1:
97861f28255Scgd case OPEN+2:
97961f28255Scgd case OPEN+3:
98061f28255Scgd case OPEN+4:
98161f28255Scgd case OPEN+5:
98261f28255Scgd case OPEN+6:
98361f28255Scgd case OPEN+7:
98461f28255Scgd case OPEN+8:
98561f28255Scgd case OPEN+9: {
98674174020Sperry int no;
98774174020Sperry char *save;
98861f28255Scgd
98961f28255Scgd no = OP(scan) - OPEN;
99061f28255Scgd save = reginput;
99161f28255Scgd
99261f28255Scgd if (regmatch(next)) {
99361f28255Scgd /*
99461f28255Scgd * Don't set startp if some later
99561f28255Scgd * invocation of the same parentheses
99661f28255Scgd * already has.
99761f28255Scgd */
99861f28255Scgd if (regstartp[no] == NULL)
99961f28255Scgd regstartp[no] = save;
100061f28255Scgd return(1);
100161f28255Scgd } else
100261f28255Scgd return(0);
100361f28255Scgd }
100461f28255Scgd case CLOSE+1:
100561f28255Scgd case CLOSE+2:
100661f28255Scgd case CLOSE+3:
100761f28255Scgd case CLOSE+4:
100861f28255Scgd case CLOSE+5:
100961f28255Scgd case CLOSE+6:
101061f28255Scgd case CLOSE+7:
101161f28255Scgd case CLOSE+8:
101261f28255Scgd case CLOSE+9: {
101374174020Sperry int no;
101474174020Sperry char *save;
101561f28255Scgd
101661f28255Scgd no = OP(scan) - CLOSE;
101761f28255Scgd save = reginput;
101861f28255Scgd
101961f28255Scgd if (regmatch(next)) {
102061f28255Scgd /*
102161f28255Scgd * Don't set endp if some later
102261f28255Scgd * invocation of the same parentheses
102361f28255Scgd * already has.
102461f28255Scgd */
102561f28255Scgd if (regendp[no] == NULL)
102661f28255Scgd regendp[no] = save;
102761f28255Scgd return(1);
102861f28255Scgd } else
102961f28255Scgd return(0);
103061f28255Scgd }
103161f28255Scgd case BRANCH: {
103274174020Sperry char *save;
103361f28255Scgd
103461f28255Scgd if (OP(next) != BRANCH) /* No choice. */
103561f28255Scgd next = OPERAND(scan); /* Avoid recursion. */
103661f28255Scgd else {
103761f28255Scgd do {
103861f28255Scgd save = reginput;
103961f28255Scgd if (regmatch(OPERAND(scan)))
104061f28255Scgd return(1);
104161f28255Scgd reginput = save;
104261f28255Scgd scan = regnext(scan);
104361f28255Scgd } while (scan != NULL && OP(scan) == BRANCH);
104461f28255Scgd return(0);
104561f28255Scgd /* NOTREACHED */
104661f28255Scgd }
104761f28255Scgd }
104861f28255Scgd break;
104961f28255Scgd case STAR:
105061f28255Scgd case PLUS: {
105174174020Sperry char nextch;
105274174020Sperry int no;
105374174020Sperry char *save;
105474174020Sperry int min;
105561f28255Scgd
105661f28255Scgd /*
105761f28255Scgd * Lookahead to avoid useless match attempts
105861f28255Scgd * when we know what character comes next.
105961f28255Scgd */
106061f28255Scgd nextch = '\0';
106161f28255Scgd if (OP(next) == EXACTLY)
106261f28255Scgd nextch = *OPERAND(next);
106361f28255Scgd min = (OP(scan) == STAR) ? 0 : 1;
106461f28255Scgd save = reginput;
106561f28255Scgd no = regrepeat(OPERAND(scan));
106661f28255Scgd while (no >= min) {
106761f28255Scgd /* If it could work, try it. */
106861f28255Scgd if (nextch == '\0' || *reginput == nextch)
106961f28255Scgd if (regmatch(next))
107061f28255Scgd return(1);
107161f28255Scgd /* Couldn't or didn't -- back up. */
107261f28255Scgd no--;
107361f28255Scgd reginput = save + no;
107461f28255Scgd }
107561f28255Scgd return(0);
107661f28255Scgd }
107761f28255Scgd case END:
107861f28255Scgd return(1); /* Success! */
107961f28255Scgd default:
108061f28255Scgd regerror("memory corruption");
108161f28255Scgd return(0);
108261f28255Scgd }
108361f28255Scgd
108461f28255Scgd scan = next;
108561f28255Scgd }
108661f28255Scgd
108761f28255Scgd /*
108861f28255Scgd * We get here only if there's trouble -- normally "case END" is
108961f28255Scgd * the terminating point.
109061f28255Scgd */
109161f28255Scgd regerror("corrupted pointers");
109261f28255Scgd return(0);
109361f28255Scgd }
109461f28255Scgd
109561f28255Scgd /*
109661f28255Scgd - regrepeat - repeatedly match something simple, report how many
109761f28255Scgd */
109861f28255Scgd static int
regrepeat(p)109961f28255Scgd regrepeat(p)
110061f28255Scgd char *p;
110161f28255Scgd {
110274174020Sperry int count = 0;
110374174020Sperry char *scan;
110474174020Sperry char *opnd;
110561f28255Scgd
110661f28255Scgd scan = reginput;
110761f28255Scgd opnd = OPERAND(p);
110861f28255Scgd switch (OP(p)) {
110961f28255Scgd case ANY:
111061f28255Scgd count = strlen(scan);
111161f28255Scgd scan += count;
111261f28255Scgd break;
111361f28255Scgd case EXACTLY:
111461f28255Scgd while (*opnd == *scan) {
111561f28255Scgd count++;
111661f28255Scgd scan++;
111761f28255Scgd }
111861f28255Scgd break;
111961f28255Scgd case ANYOF:
112061f28255Scgd while (*scan != '\0' && strchr(opnd, *scan) != NULL) {
112161f28255Scgd count++;
112261f28255Scgd scan++;
112361f28255Scgd }
112461f28255Scgd break;
112561f28255Scgd case ANYBUT:
112661f28255Scgd while (*scan != '\0' && strchr(opnd, *scan) == NULL) {
112761f28255Scgd count++;
112861f28255Scgd scan++;
112961f28255Scgd }
113061f28255Scgd break;
113161f28255Scgd default: /* Oh dear. Called inappropriately. */
113261f28255Scgd regerror("internal foulup");
113361f28255Scgd count = 0; /* Best compromise. */
113461f28255Scgd break;
113561f28255Scgd }
113661f28255Scgd reginput = scan;
113761f28255Scgd
113861f28255Scgd return(count);
113961f28255Scgd }
114061f28255Scgd
114161f28255Scgd /*
114261f28255Scgd - regnext - dig the "next" pointer out of a node
114361f28255Scgd */
114461f28255Scgd static char *
regnext(p)114561f28255Scgd regnext(p)
114674174020Sperry char *p;
114761f28255Scgd {
114874174020Sperry int offset;
114961f28255Scgd
115061f28255Scgd if (p == ®dummy)
115161f28255Scgd return(NULL);
115261f28255Scgd
115361f28255Scgd offset = NEXT(p);
115461f28255Scgd if (offset == 0)
115561f28255Scgd return(NULL);
115661f28255Scgd
115761f28255Scgd if (OP(p) == BACK)
115861f28255Scgd return(p-offset);
115961f28255Scgd else
116061f28255Scgd return(p+offset);
116161f28255Scgd }
116261f28255Scgd
116361f28255Scgd #ifdef DEBUG
116461f28255Scgd
116561f28255Scgd /*
116661f28255Scgd - regdump - dump a regexp onto stdout in vaguely comprehensible form
116761f28255Scgd */
116861f28255Scgd void
regdump(r)116961f28255Scgd regdump(r)
117061f28255Scgd regexp *r;
117161f28255Scgd {
117274174020Sperry char *s;
117374174020Sperry char op = EXACTLY; /* Arbitrary non-END op. */
117474174020Sperry char *next;
117561f28255Scgd
117661f28255Scgd
117761f28255Scgd s = r->program + 1;
117861f28255Scgd while (op != END) { /* While that wasn't END last time... */
117961f28255Scgd op = OP(s);
1180592d6d61Sfreza printf("%2td%s", s-r->program, regprop(s)); /* Where, what. */
118161f28255Scgd next = regnext(s);
118261f28255Scgd if (next == NULL) /* Next ptr. */
118361f28255Scgd printf("(0)");
118461f28255Scgd else
1185592d6d61Sfreza printf("(%td)", (s-r->program)+(next-s));
118661f28255Scgd s += 3;
118761f28255Scgd if (op == ANYOF || op == ANYBUT || op == EXACTLY) {
118861f28255Scgd /* Literal string, where present. */
118961f28255Scgd while (*s != '\0') {
119061f28255Scgd putchar(*s);
119161f28255Scgd s++;
119261f28255Scgd }
119361f28255Scgd s++;
119461f28255Scgd }
119561f28255Scgd putchar('\n');
119661f28255Scgd }
119761f28255Scgd
119861f28255Scgd /* Header fields of interest. */
119961f28255Scgd if (r->regstart != '\0')
120061f28255Scgd printf("start `%c' ", r->regstart);
120161f28255Scgd if (r->reganch)
120261f28255Scgd printf("anchored ");
120361f28255Scgd if (r->regmust != NULL)
120461f28255Scgd printf("must have \"%s\"", r->regmust);
120561f28255Scgd printf("\n");
120661f28255Scgd }
120761f28255Scgd
120861f28255Scgd /*
120961f28255Scgd - regprop - printable representation of opcode
121061f28255Scgd */
121161f28255Scgd static char *
regprop(op)121261f28255Scgd regprop(op)
121361f28255Scgd char *op;
121461f28255Scgd {
121574174020Sperry char *p;
121661f28255Scgd static char buf[50];
121761f28255Scgd
12189cd5492cSmrg (void)strncpy(buf, ":", sizeof(buf) - 1);
121961f28255Scgd
122061f28255Scgd switch (OP(op)) {
122161f28255Scgd case BOL:
122261f28255Scgd p = "BOL";
122361f28255Scgd break;
122461f28255Scgd case EOL:
122561f28255Scgd p = "EOL";
122661f28255Scgd break;
122761f28255Scgd case ANY:
122861f28255Scgd p = "ANY";
122961f28255Scgd break;
123061f28255Scgd case ANYOF:
123161f28255Scgd p = "ANYOF";
123261f28255Scgd break;
123361f28255Scgd case ANYBUT:
123461f28255Scgd p = "ANYBUT";
123561f28255Scgd break;
123661f28255Scgd case BRANCH:
123761f28255Scgd p = "BRANCH";
123861f28255Scgd break;
123961f28255Scgd case EXACTLY:
124061f28255Scgd p = "EXACTLY";
124161f28255Scgd break;
124261f28255Scgd case NOTHING:
124361f28255Scgd p = "NOTHING";
124461f28255Scgd break;
124561f28255Scgd case BACK:
124661f28255Scgd p = "BACK";
124761f28255Scgd break;
124861f28255Scgd case END:
124961f28255Scgd p = "END";
125061f28255Scgd break;
125161f28255Scgd case OPEN+1:
125261f28255Scgd case OPEN+2:
125361f28255Scgd case OPEN+3:
125461f28255Scgd case OPEN+4:
125561f28255Scgd case OPEN+5:
125661f28255Scgd case OPEN+6:
125761f28255Scgd case OPEN+7:
125861f28255Scgd case OPEN+8:
125961f28255Scgd case OPEN+9:
12609cd5492cSmrg (void)snprintf(buf+strlen(buf), sizeof(buf) - strlen(buf),
12619cd5492cSmrg "OPEN%d", OP(op)-OPEN);
126261f28255Scgd p = NULL;
126361f28255Scgd break;
126461f28255Scgd case CLOSE+1:
126561f28255Scgd case CLOSE+2:
126661f28255Scgd case CLOSE+3:
126761f28255Scgd case CLOSE+4:
126861f28255Scgd case CLOSE+5:
126961f28255Scgd case CLOSE+6:
127061f28255Scgd case CLOSE+7:
127161f28255Scgd case CLOSE+8:
127261f28255Scgd case CLOSE+9:
12739cd5492cSmrg (void)snprintf(buf+strlen(buf), sizeof(buf) - strlen(buf),
12749cd5492cSmrg "CLOSE%d", OP(op)-CLOSE);
127561f28255Scgd p = NULL;
127661f28255Scgd break;
127761f28255Scgd case STAR:
127861f28255Scgd p = "STAR";
127961f28255Scgd break;
128061f28255Scgd case PLUS:
128161f28255Scgd p = "PLUS";
128261f28255Scgd break;
128361f28255Scgd case WORDA:
128461f28255Scgd p = "WORDA";
128561f28255Scgd break;
128661f28255Scgd case WORDZ:
128761f28255Scgd p = "WORDZ";
128861f28255Scgd break;
128961f28255Scgd default:
1290f5804cdeSchristos p = NULL;
129161f28255Scgd regerror("corrupted opcode");
129261f28255Scgd break;
129361f28255Scgd }
129461f28255Scgd if (p != NULL)
12959cd5492cSmrg (void)strncat(buf, p, sizeof(buf) - strlen(buf) - 1);
129661f28255Scgd return(buf);
129761f28255Scgd }
129861f28255Scgd #endif
129961f28255Scgd
130061f28255Scgd /*
130161f28255Scgd * The following is provided for those people who do not have strcspn() in
130261f28255Scgd * their C libraries. They should get off their butts and do something
130361f28255Scgd * about it; at least one public-domain implementation of those (highly
130461f28255Scgd * useful) string routines has been published on Usenet.
130561f28255Scgd */
130661f28255Scgd #ifdef STRCSPN
130761f28255Scgd /*
130861f28255Scgd * strcspn - find length of initial segment of s1 consisting entirely
130961f28255Scgd * of characters not from s2
131061f28255Scgd */
131161f28255Scgd
131261f28255Scgd static int
strcspn(s1,s2)131361f28255Scgd strcspn(s1, s2)
131461f28255Scgd char *s1;
131561f28255Scgd char *s2;
131661f28255Scgd {
131774174020Sperry char *scan1;
131874174020Sperry char *scan2;
131974174020Sperry int count;
132061f28255Scgd
132161f28255Scgd count = 0;
132261f28255Scgd for (scan1 = s1; *scan1 != '\0'; scan1++) {
132361f28255Scgd for (scan2 = s2; *scan2 != '\0';) /* ++ moved down. */
132461f28255Scgd if (*scan1 == *scan2++)
132561f28255Scgd return(count);
132661f28255Scgd count++;
132761f28255Scgd }
132861f28255Scgd return(count);
132961f28255Scgd }
133061f28255Scgd #endif
1331