]>
git.saurik.com Git - apple/libc.git/blob - regex/FreeBSD/regexec.c
e91938e45f3ec8c96e66cee950474e60f93e3ab9
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
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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
37 * @(#)regexec.c 8.3 (Berkeley) 3/20/94
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.5 2003/02/16 17:29:10 nectar Exp $");
47 * the outer shell of regexec()
49 * This file includes engine.c *twice*, after muchos fiddling with the
50 * macros that code uses. This lets the same code operate on two different
51 * representations for state sets.
53 #include <sys/types.h>
64 static int nope __unused
= 0; /* for use in asserts; shuts lint up */
66 /* macros for manipulating states, small version */
68 #define states1 states /* for later use in regexec() decision */
69 #define CLEAR(v) ((v) = 0)
70 #define SET0(v, n) ((v) &= ~((unsigned long)1 << (n)))
71 #define SET1(v, n) ((v) |= (unsigned long)1 << (n))
72 #define ISSET(v, n) (((v) & ((unsigned long)1 << (n))) != 0)
73 #define ASSIGN(d, s) ((d) = (s))
74 #define EQ(a, b) ((a) == (b))
75 #define STATEVARS long dummy /* dummy version */
76 #define STATESETUP(m, n) /* nothing */
77 #define STATETEARDOWN(m) /* nothing */
78 #define SETUP(v) ((v) = 0)
80 #define INIT(o, n) ((o) = (unsigned long)1 << (n))
81 #define INC(o) ((o) <<= 1)
82 #define ISSTATEIN(v, o) (((v) & (o)) != 0)
83 /* some abbreviations; note that some of these know variable names! */
84 /* do "if I'm here, I can also be there" etc without branches */
85 #define FWD(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) << (n))
86 #define BACK(dst, src, n) ((dst) |= ((unsigned long)(src)&(here)) >> (n))
87 #define ISSETBACK(v, n) (((v) & ((unsigned long)here >> (n))) != 0)
89 #define SNAMES /* engine.c looks after details */
114 /* macros for manipulating states, large version */
115 #define states char *
116 #define CLEAR(v) memset(v, 0, m->g->nstates)
117 #define SET0(v, n) ((v)[n] = 0)
118 #define SET1(v, n) ((v)[n] = 1)
119 #define ISSET(v, n) ((v)[n])
120 #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
121 #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
122 #define STATEVARS long vn; char *space
123 #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
124 if ((m)->space == NULL) return(REG_ESPACE); \
126 #define STATETEARDOWN(m) { free((m)->space); }
127 #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
128 #define onestate long
129 #define INIT(o, n) ((o) = (n))
130 #define INC(o) ((o)++)
131 #define ISSTATEIN(v, o) ((v)[o])
132 /* some abbreviations; note that some of these know variable names! */
133 /* do "if I'm here, I can also be there" etc without branches */
134 #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
135 #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
136 #define ISSETBACK(v, n) ((v)[here - (n)])
138 #define LNAMES /* flag */
143 - regexec - interface for matching
144 = extern int regexec(const regex_t *, const char *, size_t, \
145 = regmatch_t [], int);
146 = #define REG_NOTBOL 00001
147 = #define REG_NOTEOL 00002
148 = #define REG_STARTEND 00004
149 = #define REG_TRACE 00400 // tracing of execution
150 = #define REG_LARGE 01000 // force large representation
151 = #define REG_BACKR 02000 // force use of backref code
153 * We put this here so we can exploit knowledge of the state representation
154 * when choosing which matcher to call. Also, by this point the matchers
155 * have been prototyped.
157 int /* 0 success, REG_NOMATCH failure */
158 regexec(preg
, string
, nmatch
, pmatch
, eflags
)
159 const regex_t
* __restrict preg
;
160 const char * __restrict string
;
162 regmatch_t pmatch
[__restrict
];
165 struct re_guts
*g
= preg
->re_g
;
167 # define GOODFLAGS(f) (f)
169 # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
172 if (preg
->re_magic
!= MAGIC1
|| g
->magic
!= MAGIC2
)
174 assert(!(g
->iflags
&BAD
));
175 if (g
->iflags
&BAD
) /* backstop for no-debug case */
177 eflags
= GOODFLAGS(eflags
);
179 if (g
->nstates
<= CHAR_BIT
*sizeof(states1
) && !(eflags
®_LARGE
))
180 return(smatcher(g
, (char *)string
, nmatch
, pmatch
, eflags
));
182 return(lmatcher(g
, (char *)string
, nmatch
, pmatch
, eflags
));