xref: /minix3/lib/libc/regex/regexec.c (revision f14fb602092e015ff630df58e17c2a9cd57d29b3)
1*f14fb602SLionel Sambuc /*	$NetBSD: regexec.c,v 1.22 2012/03/13 21:13:43 christos Exp $	*/
22fe8fb19SBen Gras 
3b7061124SArun Thomas /*-
4b7061124SArun Thomas  * Copyright (c) 1992, 1993, 1994
5b7061124SArun Thomas  *	The Regents of the University of California.  All rights reserved.
6b7061124SArun Thomas  *
7b7061124SArun Thomas  * This code is derived from software contributed to Berkeley by
8b7061124SArun Thomas  * Henry Spencer.
9b7061124SArun Thomas  *
10b7061124SArun Thomas  * Redistribution and use in source and binary forms, with or without
11b7061124SArun Thomas  * modification, are permitted provided that the following conditions
12b7061124SArun Thomas  * are met:
13b7061124SArun Thomas  * 1. Redistributions of source code must retain the above copyright
14b7061124SArun Thomas  *    notice, this list of conditions and the following disclaimer.
15b7061124SArun Thomas  * 2. Redistributions in binary form must reproduce the above copyright
16b7061124SArun Thomas  *    notice, this list of conditions and the following disclaimer in the
17b7061124SArun Thomas  *    documentation and/or other materials provided with the distribution.
182fe8fb19SBen Gras  * 3. Neither the name of the University nor the names of its contributors
192fe8fb19SBen Gras  *    may be used to endorse or promote products derived from this software
202fe8fb19SBen Gras  *    without specific prior written permission.
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
232fe8fb19SBen Gras  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
242fe8fb19SBen Gras  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
252fe8fb19SBen Gras  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
262fe8fb19SBen Gras  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
272fe8fb19SBen Gras  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
282fe8fb19SBen Gras  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
292fe8fb19SBen Gras  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
302fe8fb19SBen Gras  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
312fe8fb19SBen Gras  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
322fe8fb19SBen Gras  * SUCH DAMAGE.
332fe8fb19SBen Gras  *
342fe8fb19SBen Gras  *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
352fe8fb19SBen Gras  */
362fe8fb19SBen Gras 
372fe8fb19SBen Gras /*-
382fe8fb19SBen Gras  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
392fe8fb19SBen Gras  *
402fe8fb19SBen Gras  * This code is derived from software contributed to Berkeley by
412fe8fb19SBen Gras  * Henry Spencer.
422fe8fb19SBen Gras  *
432fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
442fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
452fe8fb19SBen Gras  * are met:
462fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
472fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
482fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
492fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
502fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
51b7061124SArun Thomas  * 3. All advertising materials mentioning features or use of this software
52b7061124SArun Thomas  *    must display the following acknowledgement:
53b7061124SArun Thomas  *	This product includes software developed by the University of
54b7061124SArun Thomas  *	California, Berkeley and its contributors.
55b7061124SArun Thomas  * 4. Neither the name of the University nor the names of its contributors
56b7061124SArun Thomas  *    may be used to endorse or promote products derived from this software
57b7061124SArun Thomas  *    without specific prior written permission.
58b7061124SArun Thomas  *
59b7061124SArun Thomas  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60b7061124SArun Thomas  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61b7061124SArun Thomas  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62b7061124SArun Thomas  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63b7061124SArun Thomas  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64b7061124SArun Thomas  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65b7061124SArun Thomas  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66b7061124SArun Thomas  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67b7061124SArun Thomas  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68b7061124SArun Thomas  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69b7061124SArun Thomas  * SUCH DAMAGE.
70b7061124SArun Thomas  *
71b7061124SArun Thomas  *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
72b7061124SArun Thomas  */
73b7061124SArun Thomas 
742fe8fb19SBen Gras #include <sys/cdefs.h>
75b7061124SArun Thomas #if defined(LIBC_SCCS) && !defined(lint)
762fe8fb19SBen Gras #if 0
77b7061124SArun Thomas static char sccsid[] = "@(#)regexec.c	8.3 (Berkeley) 3/20/94";
782fe8fb19SBen Gras #else
79*f14fb602SLionel Sambuc __RCSID("$NetBSD: regexec.c,v 1.22 2012/03/13 21:13:43 christos Exp $");
802fe8fb19SBen Gras #endif
81b7061124SArun Thomas #endif /* LIBC_SCCS and not lint */
82b7061124SArun Thomas 
83b7061124SArun Thomas /*
84b7061124SArun Thomas  * the outer shell of regexec()
85b7061124SArun Thomas  *
86b7061124SArun Thomas  * This file includes engine.c *twice*, after muchos fiddling with the
87b7061124SArun Thomas  * macros that code uses.  This lets the same code operate on two different
88b7061124SArun Thomas  * representations for state sets.
89b7061124SArun Thomas  */
902fe8fb19SBen Gras #include "namespace.h"
91b7061124SArun Thomas #include <sys/types.h>
922fe8fb19SBen Gras 
932fe8fb19SBen Gras #include <assert.h>
942fe8fb19SBen Gras #include <ctype.h>
952fe8fb19SBen Gras #include <limits.h>
96b7061124SArun Thomas #include <stdio.h>
97b7061124SArun Thomas #include <stdlib.h>
98b7061124SArun Thomas #include <string.h>
99b7061124SArun Thomas #include <regex.h>
100b7061124SArun Thomas 
1012fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(regexec,_regexec)1022fe8fb19SBen Gras __weak_alias(regexec,_regexec)
1032fe8fb19SBen Gras #endif
1042fe8fb19SBen Gras 
105b7061124SArun Thomas #include "utils.h"
106b7061124SArun Thomas #include "regex2.h"
107b7061124SArun Thomas 
108b7061124SArun Thomas /* macros for manipulating states, small version */
1092fe8fb19SBen Gras #define	states	unsigned long
1102fe8fb19SBen Gras #define	states1	unsigned long	/* for later use in regexec() decision */
111b7061124SArun Thomas #define	CLEAR(v)	((v) = 0)
1122fe8fb19SBen Gras #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
1132fe8fb19SBen Gras #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
1142fe8fb19SBen Gras #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
115b7061124SArun Thomas #define	ASSIGN(d, s)	((d) = (s))
116b7061124SArun Thomas #define	EQ(a, b)	((a) == (b))
117b7061124SArun Thomas #define	STATEVARS	int dummy	/* dummy version */
118b7061124SArun Thomas #define	STATESETUP(m, n)	/* nothing */
119b7061124SArun Thomas #define	STATETEARDOWN(m)	/* nothing */
120b7061124SArun Thomas #define	SETUP(v)	((v) = 0)
1212fe8fb19SBen Gras #define	onestate	unsigned long
1222fe8fb19SBen Gras #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
123b7061124SArun Thomas #define	INC(o)	((o) <<= 1)
1242fe8fb19SBen Gras #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
125b7061124SArun Thomas /* some abbreviations; note that some of these know variable names! */
126b7061124SArun Thomas /* do "if I'm here, I can also be there" etc without branches */
1272fe8fb19SBen Gras #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
1282fe8fb19SBen Gras #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
1292fe8fb19SBen Gras #define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
130b7061124SArun Thomas /* function names */
131b7061124SArun Thomas #define SNAMES			/* engine.c looks after details */
132b7061124SArun Thomas 
133b7061124SArun Thomas #include "engine.c"
134b7061124SArun Thomas 
135b7061124SArun Thomas /* now undo things */
136b7061124SArun Thomas #undef	states
137b7061124SArun Thomas #undef	CLEAR
138b7061124SArun Thomas #undef	SET0
139b7061124SArun Thomas #undef	SET1
140b7061124SArun Thomas #undef	ISSET
141b7061124SArun Thomas #undef	ASSIGN
142b7061124SArun Thomas #undef	EQ
143b7061124SArun Thomas #undef	STATEVARS
144b7061124SArun Thomas #undef	STATESETUP
145b7061124SArun Thomas #undef	STATETEARDOWN
146b7061124SArun Thomas #undef	SETUP
147b7061124SArun Thomas #undef	onestate
148b7061124SArun Thomas #undef	INIT
149b7061124SArun Thomas #undef	INC
150b7061124SArun Thomas #undef	ISSTATEIN
151b7061124SArun Thomas #undef	FWD
152b7061124SArun Thomas #undef	BACK
153b7061124SArun Thomas #undef	ISSETBACK
154b7061124SArun Thomas #undef	SNAMES
155b7061124SArun Thomas 
156b7061124SArun Thomas /* macros for manipulating states, large version */
157b7061124SArun Thomas #define	states	char *
1582fe8fb19SBen Gras #define	CLEAR(v)	memset(v, 0, (size_t)m->g->nstates)
159b7061124SArun Thomas #define	SET0(v, n)	((v)[n] = 0)
160b7061124SArun Thomas #define	SET1(v, n)	((v)[n] = 1)
161b7061124SArun Thomas #define	ISSET(v, n)	((v)[n])
1622fe8fb19SBen Gras #define	ASSIGN(d, s)	memcpy(d, s, (size_t)m->g->nstates)
1632fe8fb19SBen Gras #define	EQ(a, b)	(memcmp(a, b, (size_t)m->g->nstates) == 0)
164b7061124SArun Thomas #define	STATEVARS	int vn; char *space
1652fe8fb19SBen Gras #define	STATESETUP(m, nv) \
1662fe8fb19SBen Gras     if (((m)->space = malloc((size_t)((nv)*(m)->g->nstates))) == NULL) \
1672fe8fb19SBen Gras 	return(REG_ESPACE); \
1682fe8fb19SBen Gras     else \
1692fe8fb19SBen Gras 	(m)->vn = 0
1702fe8fb19SBen Gras 
1712fe8fb19SBen Gras #define	STATETEARDOWN(m)	{ free((m)->space); m->space = NULL; }
1722fe8fb19SBen Gras #define	SETUP(v)	((v) = &m->space[(size_t)(m->vn++ * m->g->nstates)])
173b7061124SArun Thomas #define	onestate	int
174*f14fb602SLionel Sambuc #define	INIT(o, n)	((o) = (int)(n))
175b7061124SArun Thomas #define	INC(o)	((o)++)
176b7061124SArun Thomas #define	ISSTATEIN(v, o)	((v)[o])
177b7061124SArun Thomas /* some abbreviations; note that some of these know variable names! */
178b7061124SArun Thomas /* do "if I'm here, I can also be there" etc without branches */
179b7061124SArun Thomas #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
180b7061124SArun Thomas #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
181b7061124SArun Thomas #define	ISSETBACK(v, n)	((v)[here - (n)])
182b7061124SArun Thomas /* function names */
183b7061124SArun Thomas #define	LNAMES			/* flag */
184b7061124SArun Thomas 
185b7061124SArun Thomas #include "engine.c"
186b7061124SArun Thomas 
187b7061124SArun Thomas /*
188b7061124SArun Thomas  - regexec - interface for matching
189b7061124SArun Thomas  = extern int regexec(const regex_t *, const char *, size_t, \
190b7061124SArun Thomas  =					regmatch_t [], int);
191b7061124SArun Thomas  = #define	REG_NOTBOL	00001
192b7061124SArun Thomas  = #define	REG_NOTEOL	00002
193b7061124SArun Thomas  = #define	REG_STARTEND	00004
194b7061124SArun Thomas  = #define	REG_TRACE	00400	// tracing of execution
195b7061124SArun Thomas  = #define	REG_LARGE	01000	// force large representation
196b7061124SArun Thomas  = #define	REG_BACKR	02000	// force use of backref code
197b7061124SArun Thomas  *
198b7061124SArun Thomas  * We put this here so we can exploit knowledge of the state representation
199b7061124SArun Thomas  * when choosing which matcher to call.  Also, by this point the matchers
200b7061124SArun Thomas  * have been prototyped.
201b7061124SArun Thomas  */
202b7061124SArun Thomas int				/* 0 success, REG_NOMATCH failure */
2032fe8fb19SBen Gras regexec(
2042fe8fb19SBen Gras     const regex_t *preg,
2052fe8fb19SBen Gras     const char *string,
2062fe8fb19SBen Gras     size_t nmatch,
2072fe8fb19SBen Gras     regmatch_t pmatch[],
2082fe8fb19SBen Gras     int eflags)
209b7061124SArun Thomas {
2102fe8fb19SBen Gras 	struct re_guts *g = preg->re_g;
2112fe8fb19SBen Gras 	char *s;
212b7061124SArun Thomas #ifdef REDEBUG
213b7061124SArun Thomas #	define	GOODFLAGS(f)	(f)
214b7061124SArun Thomas #else
215b7061124SArun Thomas #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
216b7061124SArun Thomas #endif
217b7061124SArun Thomas 
2182fe8fb19SBen Gras 	_DIAGASSERT(preg != NULL);
2192fe8fb19SBen Gras 	_DIAGASSERT(string != NULL);
2202fe8fb19SBen Gras 
221b7061124SArun Thomas 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
222b7061124SArun Thomas 		return(REG_BADPAT);
223b7061124SArun Thomas 	assert(!(g->iflags&BAD));
224b7061124SArun Thomas 	if (g->iflags&BAD)		/* backstop for no-debug case */
225b7061124SArun Thomas 		return(REG_BADPAT);
226b7061124SArun Thomas 	eflags = GOODFLAGS(eflags);
227b7061124SArun Thomas 
2282fe8fb19SBen Gras 	s = __UNCONST(string);
229b7061124SArun Thomas 
2302fe8fb19SBen Gras 	if (g->nstates <= (sopno)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
2312fe8fb19SBen Gras 		return(smatcher(g, s, nmatch, pmatch, eflags));
2322fe8fb19SBen Gras 	else
2332fe8fb19SBen Gras 		return(lmatcher(g, s, nmatch, pmatch, eflags));
2342fe8fb19SBen Gras }
235