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