protoize most of the function prototypes
[nvi.git] / regex / regexec.c
blob8294b70d36b8c9453ded1d92f3ef00d3a4442ccc
1 /*-
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
11 * are met:
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
35 * SUCH DAMAGE.
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>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <limits.h>
56 #include <ctype.h>
57 #include <regex.h>
59 #include "utils.h"
60 #include "regex2.h"
62 static int nope = 0; /* for use in asserts; shuts lint up */
64 /* macros for manipulating states, small version */
65 #define states int
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)
77 #define onestate int
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)))
86 /* function names */
87 #define SNAMES /* engine.c looks after details */
89 #include "engine.c"
91 /* now undo things */
92 #undef states
93 #undef CLEAR
94 #undef SET0
95 #undef SET1
96 #undef ISSET
97 #undef ASSIGN
98 #undef EQ
99 #undef STATEVARS
100 #undef STATESETUP
101 #undef STATETEARDOWN
102 #undef SETUP
103 #undef onestate
104 #undef INIT
105 #undef INC
106 #undef ISSTATEIN
107 #undef FWD
108 #undef BACK
109 #undef ISSETBACK
110 #undef SNAMES
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); \
123 (m)->vn = 0; }
124 #define STATETEARDOWN(m) { free((m)->space); }
125 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
126 #define onestate int
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)])
135 /* function names */
136 #define LNAMES /* flag */
138 #include "engine.c"
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;
159 #ifdef REDEBUG
160 # define GOODFLAGS(f) (f)
161 #else
162 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
163 #endif
165 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
166 return(REG_BADPAT);
167 assert(!(g->iflags&BAD));
168 if (g->iflags&BAD) /* backstop for no-debug case */
169 return(REG_BADPAT);
170 eflags = GOODFLAGS(eflags);
172 if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
173 return(smatcher(g, (RCHAR_T *)string, nmatch, pmatch, eflags));
174 else
175 return(lmatcher(g, (RCHAR_T *)string, nmatch, pmatch, eflags));