1b04caf0cSbostic /*-
2fdc51b3dSbostic * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3fdc51b3dSbostic * Copyright (c) 1992, 1993, 1994
4bb43379cSbostic * The Regents of the University of California. All rights reserved.
5b04caf0cSbostic *
6b04caf0cSbostic * This code is derived from software contributed to Berkeley by
7*13950b19Sbostic * Henry Spencer.
8b04caf0cSbostic *
9b04caf0cSbostic * %sccs.include.redist.c%
10b04caf0cSbostic *
11*13950b19Sbostic * @(#)regexec.c 8.3 (Berkeley) 03/20/94
12b04caf0cSbostic */
13b04caf0cSbostic
14b04caf0cSbostic #if defined(LIBC_SCCS) && !defined(lint)
15*13950b19Sbostic static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 03/20/94";
16b04caf0cSbostic #endif /* LIBC_SCCS and not lint */
17b04caf0cSbostic
18e51f590cSbostic /*
19e51f590cSbostic * the outer shell of regexec()
20e51f590cSbostic *
21e51f590cSbostic * This file includes engine.c *twice*, after muchos fiddling with the
22e51f590cSbostic * macros that code uses. This lets the same code operate on two different
23e51f590cSbostic * representations for state sets.
24e51f590cSbostic */
25b04caf0cSbostic #include <sys/types.h>
26b04caf0cSbostic #include <stdio.h>
27b04caf0cSbostic #include <stdlib.h>
28b04caf0cSbostic #include <string.h>
29b04caf0cSbostic #include <limits.h>
30e51f590cSbostic #include <ctype.h>
31b04caf0cSbostic #include <regex.h>
32b04caf0cSbostic
33b04caf0cSbostic #include "utils.h"
34b04caf0cSbostic #include "regex2.h"
35b04caf0cSbostic
36188b6717Sbostic static int nope = 0; /* for use in asserts; shuts lint up */
37188b6717Sbostic
38b04caf0cSbostic /* macros for manipulating states, small version */
39b04caf0cSbostic #define states long
40e51f590cSbostic #define states1 states /* for later use in regexec() decision */
41b04caf0cSbostic #define CLEAR(v) ((v) = 0)
42b04caf0cSbostic #define SET0(v, n) ((v) &= ~(1 << (n)))
43b04caf0cSbostic #define SET1(v, n) ((v) |= 1 << (n))
44b04caf0cSbostic #define ISSET(v, n) ((v) & (1 << (n)))
45b04caf0cSbostic #define ASSIGN(d, s) ((d) = (s))
46b04caf0cSbostic #define EQ(a, b) ((a) == (b))
47b04caf0cSbostic #define STATEVARS int dummy /* dummy version */
48b04caf0cSbostic #define STATESETUP(m, n) /* nothing */
49b04caf0cSbostic #define STATETEARDOWN(m) /* nothing */
50e51f590cSbostic #define SETUP(v) ((v) = 0)
51b04caf0cSbostic #define onestate int
52188b6717Sbostic #define INIT(o, n) ((o) = (unsigned)1 << (n))
53b04caf0cSbostic #define INC(o) ((o) <<= 1)
54b04caf0cSbostic #define ISSTATEIN(v, o) ((v) & (o))
55b04caf0cSbostic /* some abbreviations; note that some of these know variable names! */
56b04caf0cSbostic /* do "if I'm here, I can also be there" etc without branches */
57188b6717Sbostic #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n))
58188b6717Sbostic #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n))
59188b6717Sbostic #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
60b04caf0cSbostic /* function names */
61b04caf0cSbostic #define SNAMES /* engine.c looks after details */
62b04caf0cSbostic
63b04caf0cSbostic #include "engine.c"
64b04caf0cSbostic
65b04caf0cSbostic /* now undo things */
66b04caf0cSbostic #undef states
67b04caf0cSbostic #undef CLEAR
68b04caf0cSbostic #undef SET0
69b04caf0cSbostic #undef SET1
70b04caf0cSbostic #undef ISSET
71b04caf0cSbostic #undef ASSIGN
72b04caf0cSbostic #undef EQ
73b04caf0cSbostic #undef STATEVARS
74b04caf0cSbostic #undef STATESETUP
75b04caf0cSbostic #undef STATETEARDOWN
76b04caf0cSbostic #undef SETUP
77b04caf0cSbostic #undef onestate
78b04caf0cSbostic #undef INIT
79b04caf0cSbostic #undef INC
80b04caf0cSbostic #undef ISSTATEIN
81b04caf0cSbostic #undef FWD
82b04caf0cSbostic #undef BACK
83b04caf0cSbostic #undef ISSETBACK
84b04caf0cSbostic #undef SNAMES
85b04caf0cSbostic
86b04caf0cSbostic /* macros for manipulating states, large version */
87b04caf0cSbostic #define states char *
88b04caf0cSbostic #define CLEAR(v) memset(v, 0, m->g->nstates)
89b04caf0cSbostic #define SET0(v, n) ((v)[n] = 0)
90b04caf0cSbostic #define SET1(v, n) ((v)[n] = 1)
91b04caf0cSbostic #define ISSET(v, n) ((v)[n])
92b04caf0cSbostic #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
93b04caf0cSbostic #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
94b04caf0cSbostic #define STATEVARS int vn; char *space
95b04caf0cSbostic #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
96b04caf0cSbostic if ((m)->space == NULL) return(REG_ESPACE); \
97b04caf0cSbostic (m)->vn = 0; }
98b04caf0cSbostic #define STATETEARDOWN(m) { free((m)->space); }
99b04caf0cSbostic #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
100b04caf0cSbostic #define onestate int
101b04caf0cSbostic #define INIT(o, n) ((o) = (n))
102b04caf0cSbostic #define INC(o) ((o)++)
103b04caf0cSbostic #define ISSTATEIN(v, o) ((v)[o])
104b04caf0cSbostic /* some abbreviations; note that some of these know variable names! */
105b04caf0cSbostic /* do "if I'm here, I can also be there" etc without branches */
106b04caf0cSbostic #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
107b04caf0cSbostic #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
108b04caf0cSbostic #define ISSETBACK(v, n) ((v)[here - (n)])
109b04caf0cSbostic /* function names */
110b04caf0cSbostic #define LNAMES /* flag */
111b04caf0cSbostic
112b04caf0cSbostic #include "engine.c"
113e51f590cSbostic
114e51f590cSbostic /*
115e51f590cSbostic - regexec - interface for matching
116fdc51b3dSbostic = extern int regexec(const regex_t *, const char *, size_t, \
117fdc51b3dSbostic = regmatch_t [], int);
118188b6717Sbostic = #define REG_NOTBOL 00001
119188b6717Sbostic = #define REG_NOTEOL 00002
120188b6717Sbostic = #define REG_STARTEND 00004
121188b6717Sbostic = #define REG_TRACE 00400 // tracing of execution
122188b6717Sbostic = #define REG_LARGE 01000 // force large representation
123188b6717Sbostic = #define REG_BACKR 02000 // force use of backref code
124e51f590cSbostic *
125e51f590cSbostic * We put this here so we can exploit knowledge of the state representation
126e51f590cSbostic * when choosing which matcher to call. Also, by this point the matchers
127e51f590cSbostic * have been prototyped.
128e51f590cSbostic */
129e51f590cSbostic int /* 0 success, REG_NOMATCH failure */
regexec(preg,string,nmatch,pmatch,eflags)130e51f590cSbostic regexec(preg, string, nmatch, pmatch, eflags)
131e51f590cSbostic const regex_t *preg;
132e51f590cSbostic const char *string;
133e51f590cSbostic size_t nmatch;
134e51f590cSbostic regmatch_t pmatch[];
135e51f590cSbostic int eflags;
136e51f590cSbostic {
137e51f590cSbostic register struct re_guts *g = preg->re_g;
138e51f590cSbostic #ifdef REDEBUG
139e51f590cSbostic # define GOODFLAGS(f) (f)
140e51f590cSbostic #else
141e51f590cSbostic # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
142e51f590cSbostic #endif
143e51f590cSbostic
144e51f590cSbostic if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
145e51f590cSbostic return(REG_BADPAT);
146e51f590cSbostic assert(!(g->iflags&BAD));
147e51f590cSbostic if (g->iflags&BAD) /* backstop for no-debug case */
148e51f590cSbostic return(REG_BADPAT);
149fdc51b3dSbostic eflags = GOODFLAGS(eflags);
150e51f590cSbostic
151e51f590cSbostic if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags®_LARGE))
152188b6717Sbostic return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
153e51f590cSbostic else
154188b6717Sbostic return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
155e51f590cSbostic }
156