2 * Copyright (c) 1992, 1993, 1994 Henry Spencer.
3 * Copyright (c) 1992, 1993, 1994
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer of the University of Toronto.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the University of
20 * California, Berkeley and its contributors.
21 * 4. Neither the name of the University nor the names of its contributors
22 * may be used to endorse or promote products derived from this software
23 * without specific prior written permission.
25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * @(#)regexec.c 8.2 (Berkeley) 3/16/94
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid
[] = "@(#)regexec.c 8.2 (Berkeley) 3/16/94";
42 #endif /* LIBC_SCCS and not lint */
45 * the outer shell of regexec()
47 * This file includes engine.c *twice*, after muchos fiddling with the
48 * macros that code uses. This lets the same code operate on two different
49 * representations for state sets.
51 #include <sys/types.h>
62 static int nope
= 0; /* for use in asserts; shuts lint up */
64 /* macros for manipulating states, small version */
66 #define states1 states /* for later use in regexec() decision */
67 #define CLEAR(v) ((v) = 0)
68 #define SET0(v, n) ((v) &= ~(1 << (n)))
69 #define SET1(v, n) ((v) |= 1 << (n))
70 #define ISSET(v, n) ((v) & (1 << (n)))
71 #define ASSIGN(d, s) ((d) = (s))
72 #define EQ(a, b) ((a) == (b))
73 #define STATEVARS int dummy /* dummy version */
74 #define STATESETUP(m, n) /* nothing */
75 #define STATETEARDOWN(m) /* nothing */
76 #define SETUP(v) ((v) = 0)
78 #define INIT(o, n) ((o) = (unsigned)1 << (n))
79 #define INC(o) ((o) <<= 1)
80 #define ISSTATEIN(v, o) ((v) & (o))
81 /* some abbreviations; note that some of these know variable names! */
82 /* do "if I'm here, I can also be there" etc without branches */
83 #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n))
84 #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n))
85 #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
87 #define SNAMES /* engine.c looks after details */
112 /* macros for manipulating states, large version */
113 #define states char *
114 #define CLEAR(v) memset(v, 0, m->g->nstates)
115 #define SET0(v, n) ((v)[n] = 0)
116 #define SET1(v, n) ((v)[n] = 1)
117 #define ISSET(v, n) ((v)[n])
118 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
119 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
120 #define STATEVARS int vn; char *space
121 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
122 if ((m)->space == NULL) return(REG_ESPACE); \
124 #define STATETEARDOWN(m) { free((m)->space); }
125 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
127 #define INIT(o, n) ((o) = (n))
128 #define INC(o) ((o)++)
129 #define ISSTATEIN(v, o) ((v)[o])
130 /* some abbreviations; note that some of these know variable names! */
131 /* do "if I'm here, I can also be there" etc without branches */
132 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
133 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
134 #define ISSETBACK(v, n) ((v)[here - (n)])
136 #define LNAMES /* flag */
141 - regexec - interface for matching
142 = extern int regexec(const regex_t *, const char *, size_t, \
143 = regmatch_t [], int);
144 = #define REG_NOTBOL 00001
145 = #define REG_NOTEOL 00002
146 = #define REG_STARTEND 00004
147 = #define REG_TRACE 00400 // tracing of execution
148 = #define REG_LARGE 01000 // force large representation
149 = #define REG_BACKR 02000 // force use of backref code
151 * We put this here so we can exploit knowledge of the state representation
152 * when choosing which matcher to call. Also, by this point the matchers
153 * have been prototyped.
155 int /* 0 success, REG_NOMATCH failure */
156 regexec(const regex_t
*preg
, const RCHAR_T
*string
, size_t nmatch
, regmatch_t
*pmatch
, int eflags
)
158 register struct re_guts
*g
= preg
->re_g
;
160 # define GOODFLAGS(f) (f)
162 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
165 if (preg
->re_magic
!= MAGIC1
|| g
->magic
!= MAGIC2
)
167 assert(!(g
->iflags
&BAD
));
168 if (g
->iflags
&BAD
) /* backstop for no-debug case */
170 eflags
= GOODFLAGS(eflags
);
172 if (g
->nstates
<= CHAR_BIT
*sizeof(states1
) && !(eflags
®_LARGE
))
173 return(smatcher(g
, (RCHAR_T
*)string
, nmatch
, pmatch
, eflags
));
175 return(lmatcher(g
, (RCHAR_T
*)string
, nmatch
, pmatch
, eflags
));