]> git.saurik.com Git - apple/libc.git/blob - regex/regexec.c
682550e9f5a9103c635dda8faa86cfedfeb95970
[apple/libc.git] / regex / regexec.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /*
26 * Copyright (c) 1992, 1993, 1994
27 * The Regents of the University of California. All rights reserved.
28 *
29 * This code is derived from software contributed to Berkeley by
30 * Henry Spencer.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61 /*
62 * the outer shell of regexec()
63 *
64 * This file includes engine.c *twice*, after muchos fiddling with the
65 * macros that code uses. This lets the same code operate on two different
66 * representations for state sets.
67 */
68 #include <sys/types.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <string.h>
72 #include <limits.h>
73 #include <ctype.h>
74 #include <regex.h>
75
76 #include "utils.h"
77 #include "regex2.h"
78
79 static int nope = 0; /* for use in asserts; shuts lint up */
80
81 /* macros for manipulating states, small version */
82 #define states long
83 #define states1 states /* for later use in regexec() decision */
84 #define CLEAR(v) ((v) = 0)
85 #define SET0(v, n) ((v) &= ~(1 << (n)))
86 #define SET1(v, n) ((v) |= 1 << (n))
87 #define ISSET(v, n) ((v) & (1 << (n)))
88 #define ASSIGN(d, s) ((d) = (s))
89 #define EQ(a, b) ((a) == (b))
90 #define STATEVARS int dummy /* dummy version */
91 #define STATESETUP(m, n) /* nothing */
92 #define STATETEARDOWN(m) /* nothing */
93 #define SETUP(v) ((v) = 0)
94 #define onestate int
95 #define INIT(o, n) ((o) = (unsigned)1 << (n))
96 #define INC(o) ((o) <<= 1)
97 #define ISSTATEIN(v, o) ((v) & (o))
98 /* some abbreviations; note that some of these know variable names! */
99 /* do "if I'm here, I can also be there" etc without branches */
100 #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n))
101 #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n))
102 #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
103 /* function names */
104 #define SNAMES /* engine.c looks after details */
105
106 #include "engine.c"
107
108 /* now undo things */
109 #undef states
110 #undef CLEAR
111 #undef SET0
112 #undef SET1
113 #undef ISSET
114 #undef ASSIGN
115 #undef EQ
116 #undef STATEVARS
117 #undef STATESETUP
118 #undef STATETEARDOWN
119 #undef SETUP
120 #undef onestate
121 #undef INIT
122 #undef INC
123 #undef ISSTATEIN
124 #undef FWD
125 #undef BACK
126 #undef ISSETBACK
127 #undef SNAMES
128
129 /* macros for manipulating states, large version */
130 #define states char *
131 #define CLEAR(v) memset(v, 0, m->g->nstates)
132 #define SET0(v, n) ((v)[n] = 0)
133 #define SET1(v, n) ((v)[n] = 1)
134 #define ISSET(v, n) ((v)[n])
135 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
136 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
137 #define STATEVARS int vn; char *space
138 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
139 if ((m)->space == NULL) return(REG_ESPACE); \
140 (m)->vn = 0; }
141 #define STATETEARDOWN(m) { free((m)->space); }
142 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
143 #define onestate int
144 #define INIT(o, n) ((o) = (n))
145 #define INC(o) ((o)++)
146 #define ISSTATEIN(v, o) ((v)[o])
147 /* some abbreviations; note that some of these know variable names! */
148 /* do "if I'm here, I can also be there" etc without branches */
149 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
150 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
151 #define ISSETBACK(v, n) ((v)[here - (n)])
152 /* function names */
153 #define LNAMES /* flag */
154
155 #include "engine.c"
156
157 /*
158 - regexec - interface for matching
159 = extern int regexec(const regex_t *, const char *, size_t, \
160 = regmatch_t [], int);
161 = #define REG_NOTBOL 00001
162 = #define REG_NOTEOL 00002
163 = #define REG_STARTEND 00004
164 = #define REG_TRACE 00400 // tracing of execution
165 = #define REG_LARGE 01000 // force large representation
166 = #define REG_BACKR 02000 // force use of backref code
167 *
168 * We put this here so we can exploit knowledge of the state representation
169 * when choosing which matcher to call. Also, by this point the matchers
170 * have been prototyped.
171 */
172 int /* 0 success, REG_NOMATCH failure */
173 regexec(preg, string, nmatch, pmatch, eflags)
174 const regex_t *preg;
175 const char *string;
176 size_t nmatch;
177 regmatch_t pmatch[];
178 int eflags;
179 {
180 register struct re_guts *g = preg->re_g;
181 #ifdef REDEBUG
182 # define GOODFLAGS(f) (f)
183 #else
184 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
185 #endif
186
187 if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
188 return(REG_BADPAT);
189 assert(!(g->iflags&BAD));
190 if (g->iflags&BAD) /* backstop for no-debug case */
191 return(REG_BADPAT);
192 eflags = GOODFLAGS(eflags);
193
194 if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
195 return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
196 else
197 return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
198 }