]> git.saurik.com Git - apple/libc.git/blob - regex/regexec-fbsd.c
Libc-583.tar.gz
[apple/libc.git] / regex / regexec-fbsd.c
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.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Henry Spencer.
8 *
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.
24 *
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.
36 *
37 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
38 */
39
40 #if defined(LIBC_SCCS) && !defined(lint)
41 static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
42 #endif /* LIBC_SCCS and not lint */
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD: src/lib/libc/regex/regexec.c,v 1.6 2004/07/12 07:35:59 tjr Exp $");
45
46 #include "xlocale_private.h"
47
48 /*
49 * the outer shell of regexec()
50 *
51 * This file includes engine.c three times, after muchos fiddling with the
52 * macros that code uses. This lets the same code operate on two different
53 * representations for state sets and characters.
54 */
55 #include <sys/types.h>
56 #include <stdio.h>
57 #include <stdlib.h>
58 #include <string.h>
59 #include <limits.h>
60 #include <ctype.h>
61 #include <regex.h>
62 #include <wchar.h>
63 #include <wctype.h>
64
65 #include "utils.h"
66 #include "regex2.h"
67
68 static int nope __unused = 0; /* for use in asserts; shuts lint up */
69
70 static __inline size_t
71 xmbrtowc(wi, s, n, mbs, dummy, loc)
72 wint_t *wi;
73 const char *s;
74 size_t n;
75 mbstate_t *mbs;
76 wint_t dummy;
77 locale_t loc;
78 {
79 size_t nr;
80 wchar_t wc;
81
82 nr = mbrtowc_l(&wc, s, n, mbs, loc);
83 if (wi != NULL)
84 *wi = wc;
85 if (nr == 0)
86 return (1);
87 else if (nr == (size_t)-1 || nr == (size_t)-2) {
88 memset(mbs, 0, sizeof(*mbs));
89 if (wi != NULL)
90 *wi = dummy;
91 return (1);
92 } else
93 return (nr);
94 }
95
96 static __inline size_t
97 xmbrtowc_dummy(wi, s, n, mbs, dummy, loc)
98 wint_t *wi;
99 const char *s;
100 size_t n __unused;
101 mbstate_t *mbs __unused;
102 wint_t dummy __unused;
103 locale_t loc;
104 {
105
106 if (wi != NULL)
107 *wi = (unsigned char)*s;
108 return (1);
109 }
110
111 /* macros for manipulating states, small version */
112 #define states long
113 #define states1 states /* for later use in regexec() decision */
114 #define CLEAR(v) ((v) = 0)
115 #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
116 #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
117 #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
118 #define ASSIGN(d, s) ((d) = (s))
119 #define EQ(a, b) ((a) == (b))
120 #define STATEVARS long dummy /* dummy version */
121 #define STATESETUP(m, n) /* nothing */
122 #define STATETEARDOWN(m) /* nothing */
123 #define SETUP(v) ((v) = 0)
124 #define onestate long
125 #define INIT(o, n) ((o) = (unsigned long)1 << (n))
126 #define INC(o) ((o) <<= 1)
127 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
128 /* some abbreviations; note that some of these know variable names! */
129 /* do "if I'm here, I can also be there" etc without branches */
130 #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
131 #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
132 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
133 /* no multibyte support */
134 #define XMBRTOWC xmbrtowc_dummy
135 #define ZAPSTATE(mbs) ((void)(mbs))
136 /* function names */
137 #define SNAMES /* engine.c looks after details */
138
139 #include "engine.c"
140
141 /* now undo things */
142 #undef states
143 #undef CLEAR
144 #undef SET0
145 #undef SET1
146 #undef ISSET
147 #undef ASSIGN
148 #undef EQ
149 #undef STATEVARS
150 #undef STATESETUP
151 #undef STATETEARDOWN
152 #undef SETUP
153 #undef onestate
154 #undef INIT
155 #undef INC
156 #undef ISSTATEIN
157 #undef FWD
158 #undef BACK
159 #undef ISSETBACK
160 #undef SNAMES
161 #undef XMBRTOWC
162 #undef ZAPSTATE
163
164 /* macros for manipulating states, large version */
165 #define states char *
166 #define CLEAR(v) memset(v, 0, m->g->nstates)
167 #define SET0(v, n) ((v)[n] = 0)
168 #define SET1(v, n) ((v)[n] = 1)
169 #define ISSET(v, n) ((v)[n])
170 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
171 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
172 #define STATEVARS long vn; char *space
173 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
174 if ((m)->space == NULL) return(REG_ESPACE); \
175 (m)->vn = 0; }
176 #define STATETEARDOWN(m) { free((m)->space); }
177 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
178 #define onestate long
179 #define INIT(o, n) ((o) = (n))
180 #define INC(o) ((o)++)
181 #define ISSTATEIN(v, o) ((v)[o])
182 /* some abbreviations; note that some of these know variable names! */
183 /* do "if I'm here, I can also be there" etc without branches */
184 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
185 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
186 #define ISSETBACK(v, n) ((v)[here - (n)])
187 /* no multibyte support */
188 #define XMBRTOWC xmbrtowc_dummy
189 #define ZAPSTATE(mbs) ((void)(mbs))
190 /* function names */
191 #define LNAMES /* flag */
192
193 #undef __FBSDID
194 #define __FBSDID(x)
195 #include "engine.c"
196
197 /* multibyte character & large states version */
198 #undef LNAMES
199 #undef XMBRTOWC
200 #undef ZAPSTATE
201 #define XMBRTOWC xmbrtowc
202 #define ZAPSTATE(mbs) memset((mbs), 0, sizeof(*(mbs)))
203 #define MNAMES
204
205 #include "engine.c"
206
207 /*
208 - regexec - interface for matching
209 = extern int regexec(const regex_t *, const char *, size_t, \
210 = regmatch_t [], int);
211 = #define REG_NOTBOL 00001
212 = #define REG_NOTEOL 00002
213 = #define REG_STARTEND 00004
214 = #define REG_TRACE 00400 // tracing of execution
215 = #define REG_LARGE 01000 // force large representation
216 = #define REG_BACKR 02000 // force use of backref code
217 *
218 * We put this here so we can exploit knowledge of the state representation
219 * when choosing which matcher to call. Also, by this point the matchers
220 * have been prototyped.
221 */
222 int /* 0 success, REG_NOMATCH failure */
223 regexec(preg, string, nmatch, pmatch, eflags)
224 const regex_t * __restrict preg;
225 const char * __restrict string;
226 size_t nmatch;
227 regmatch_t pmatch[__restrict];
228 int eflags;
229 {
230 struct re_guts *g = preg->re_g;
231 #ifdef REDEBUG
232 # define GOODFLAGS(f) (f)
233 #else
234 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
235 #endif
236
237 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
238 return(REG_BADPAT);
239 assert(!(g->iflags&BAD));
240 if (g->iflags&BAD) /* backstop for no-debug case */
241 return(REG_BADPAT);
242 eflags = GOODFLAGS(eflags);
243
244 g->loc = __current_locale();
245 if (MB_CUR_MAX_L(g->loc) > 1)
246 return(mmatcher(g, (char *)string, nmatch, pmatch, eflags));
247 else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
248 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
249 else
250 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
251 }