]> git.saurik.com Git - apple/libc.git/blob - regex/regexec-fbsd.c
38c7281717111bbf26d188d3ad546ca4be5b3d9c
[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 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
34 */
35
36 #if defined(LIBC_SCCS) && !defined(lint)
37 static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
38 #endif /* LIBC_SCCS and not lint */
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD: src/lib/libc/regex/regexec.c,v 1.8 2007/06/11 03:05:54 delphij Exp $");
41
42 #include "xlocale_private.h"
43
44 /*
45 * the outer shell of regexec()
46 *
47 * This file includes engine.c three times, after muchos fiddling with the
48 * macros that code uses. This lets the same code operate on two different
49 * representations for state sets and characters.
50 */
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>
58 #include <wchar.h>
59 #include <wctype.h>
60
61 #include "utils.h"
62 #include "regex2.h"
63
64 static int nope __unused = 0; /* for use in asserts; shuts lint up */
65
66 static __inline size_t
67 xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy, locale_t loc)
68 {
69 size_t nr;
70 wchar_t wc;
71
72 nr = mbrtowc_l(&wc, s, n, mbs, loc);
73 if (wi != NULL)
74 *wi = wc;
75 if (nr == 0)
76 return (1);
77 else if (nr == (size_t)-1 || nr == (size_t)-2) {
78 memset(mbs, 0, sizeof(*mbs));
79 if (wi != NULL)
80 *wi = dummy;
81 return (1);
82 } else
83 return (nr);
84 }
85
86 static __inline size_t
87 xmbrtowc_dummy(wint_t *wi,
88 const char *s,
89 size_t n __unused,
90 mbstate_t *mbs __unused,
91 wint_t dummy __unused,
92 locale_t loc __unused)
93 {
94
95 if (wi != NULL)
96 *wi = (unsigned char)*s;
97 return (1);
98 }
99
100 /* macros for manipulating states, small version */
101 #define states long
102 #define states1 states /* for later use in regexec() decision */
103 #define CLEAR(v) ((v) = 0)
104 #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
105 #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
106 #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
107 #define ASSIGN(d, s) ((d) = (s))
108 #define EQ(a, b) ((a) == (b))
109 #define STATEVARS long dummy /* dummy version */
110 #define STATESETUP(m, n) /* nothing */
111 #define STATETEARDOWN(m) /* nothing */
112 #define SETUP(v) ((v) = 0)
113 #define onestate long
114 #define INIT(o, n) ((o) = (unsigned long)1 << (n))
115 #define INC(o) ((o) <<= 1)
116 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
117 /* some abbreviations; note that some of these know variable names! */
118 /* do "if I'm here, I can also be there" etc without branches */
119 #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
120 #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
121 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
122 /* no multibyte support */
123 #define XMBRTOWC xmbrtowc_dummy
124 #define ZAPSTATE(mbs) ((void)(mbs))
125 /* function names */
126 #define SNAMES /* engine.c looks after details */
127
128 #include "engine.c"
129
130 /* now undo things */
131 #undef states
132 #undef CLEAR
133 #undef SET0
134 #undef SET1
135 #undef ISSET
136 #undef ASSIGN
137 #undef EQ
138 #undef STATEVARS
139 #undef STATESETUP
140 #undef STATETEARDOWN
141 #undef SETUP
142 #undef onestate
143 #undef INIT
144 #undef INC
145 #undef ISSTATEIN
146 #undef FWD
147 #undef BACK
148 #undef ISSETBACK
149 #undef SNAMES
150 #undef XMBRTOWC
151 #undef ZAPSTATE
152
153 /* macros for manipulating states, large version */
154 #define states char *
155 #define CLEAR(v) memset(v, 0, m->g->nstates)
156 #define SET0(v, n) ((v)[n] = 0)
157 #define SET1(v, n) ((v)[n] = 1)
158 #define ISSET(v, n) ((v)[n])
159 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
160 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
161 #define STATEVARS long vn; char *space
162 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
163 if ((m)->space == NULL) return(REG_ESPACE); \
164 (m)->vn = 0; }
165 #define STATETEARDOWN(m) { free((m)->space); }
166 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
167 #define onestate long
168 #define INIT(o, n) ((o) = (n))
169 #define INC(o) ((o)++)
170 #define ISSTATEIN(v, o) ((v)[o])
171 /* some abbreviations; note that some of these know variable names! */
172 /* do "if I'm here, I can also be there" etc without branches */
173 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
174 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
175 #define ISSETBACK(v, n) ((v)[here - (n)])
176 /* no multibyte support */
177 #define XMBRTOWC xmbrtowc_dummy
178 #define ZAPSTATE(mbs) ((void)(mbs))
179 /* function names */
180 #define LNAMES /* flag */
181
182 #undef __FBSDID
183 #define __FBSDID(x)
184 #include "engine.c"
185
186 /* multibyte character & large states version */
187 #undef LNAMES
188 #undef XMBRTOWC
189 #undef ZAPSTATE
190 #define XMBRTOWC xmbrtowc
191 #define ZAPSTATE(mbs) memset((mbs), 0, sizeof(*(mbs)))
192 #define MNAMES
193
194 #include "engine.c"
195
196 /*
197 - regexec - interface for matching
198 = extern int regexec(const regex_t *, const char *, size_t, \
199 = regmatch_t [], int);
200 = #define REG_NOTBOL 00001
201 = #define REG_NOTEOL 00002
202 = #define REG_STARTEND 00004
203 = #define REG_TRACE 00400 // tracing of execution
204 = #define REG_LARGE 01000 // force large representation
205 = #define REG_BACKR 02000 // force use of backref code
206 *
207 * We put this here so we can exploit knowledge of the state representation
208 * when choosing which matcher to call. Also, by this point the matchers
209 * have been prototyped.
210 */
211 int /* 0 success, REG_NOMATCH failure */
212 regexec(const regex_t * __restrict preg,
213 const char * __restrict string,
214 size_t nmatch,
215 regmatch_t pmatch[__restrict],
216 int eflags)
217 {
218 struct re_guts *g = preg->re_g;
219 #ifdef REDEBUG
220 # define GOODFLAGS(f) (f)
221 #else
222 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
223 #endif
224
225 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
226 return(REG_BADPAT);
227 assert(!(g->iflags&BAD));
228 if (g->iflags&BAD) /* backstop for no-debug case */
229 return(REG_BADPAT);
230 eflags = GOODFLAGS(eflags);
231
232 g->loc = __current_locale();
233 if (MB_CUR_MAX_L(g->loc) > 1)
234 return(mmatcher(g, (char *)string, nmatch, pmatch, eflags));
235 else if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
236 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
237 else
238 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
239 }