1cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes/*	$NetBSD: regexec.c,v 1.22 2012/03/13 21:13:43 christos Exp $	*/
2cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
34fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*-
44fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Copyright (c) 1992, 1993, 1994
54fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *	The Regents of the University of California.  All rights reserved.
64fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
74fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * This code is derived from software contributed to Berkeley by
84fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Henry Spencer.
94fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
104fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Redistribution and use in source and binary forms, with or without
114fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * modification, are permitted provided that the following conditions
124fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * are met:
134fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * 1. Redistributions of source code must retain the above copyright
144fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    notice, this list of conditions and the following disclaimer.
154fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * 2. Redistributions in binary form must reproduce the above copyright
164fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    notice, this list of conditions and the following disclaimer in the
174fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    documentation and/or other materials provided with the distribution.
184fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * 3. Neither the name of the University nor the names of its contributors
194fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    may be used to endorse or promote products derived from this software
204fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    without specific prior written permission.
214fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
224fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * SUCH DAMAGE.
334fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
344fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
354fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
364fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
37cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes/*-
38cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * Copyright (c) 1992, 1993, 1994 Henry Spencer.
39cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *
40cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * This code is derived from software contributed to Berkeley by
41cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * Henry Spencer.
42cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *
43cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * Redistribution and use in source and binary forms, with or without
44cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * modification, are permitted provided that the following conditions
45cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * are met:
46cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * 1. Redistributions of source code must retain the above copyright
47cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *    notice, this list of conditions and the following disclaimer.
48cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * 2. Redistributions in binary form must reproduce the above copyright
49cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *    notice, this list of conditions and the following disclaimer in the
50cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *    documentation and/or other materials provided with the distribution.
51cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * 3. All advertising materials mentioning features or use of this software
52cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *    must display the following acknowledgement:
53cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *	This product includes software developed by the University of
54cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *	California, Berkeley and its contributors.
55cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * 4. Neither the name of the University nor the names of its contributors
56cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *    may be used to endorse or promote products derived from this software
57cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *    without specific prior written permission.
58cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *
59cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes * SUCH DAMAGE.
70cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *
71cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
72cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes */
73cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
74cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#include <sys/cdefs.h>
75cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#if defined(LIBC_SCCS) && !defined(lint)
76cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#if 0
77cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughesstatic char sccsid[] = "@(#)regexec.c	8.3 (Berkeley) 3/20/94";
78cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#else
79cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes__RCSID("$NetBSD: regexec.c,v 1.22 2012/03/13 21:13:43 christos Exp $");
80cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#endif
81cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#endif /* LIBC_SCCS and not lint */
82cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
834fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*
844fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * the outer shell of regexec()
854fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
864fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * This file includes engine.c *twice*, after muchos fiddling with the
874fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * macros that code uses.  This lets the same code operate on two different
884fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * representations for state sets.
894fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
90cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#include "namespace.h"
914fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include <sys/types.h>
92cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
93cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#include <assert.h>
94cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#include <ctype.h>
95cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#include <limits.h>
964fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include <stdio.h>
974fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include <stdlib.h>
984fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include <string.h>
994fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include <regex.h>
1004fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
101cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#ifdef __weak_alias
102cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes__weak_alias(regexec,_regexec)
103cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#endif
104cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
1054fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include "utils.h"
1064fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include "regex2.h"
1074fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1084fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* macros for manipulating states, small version */
109cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	states	unsigned long
110cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	states1	unsigned long	/* for later use in regexec() decision */
1114fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	CLEAR(v)	((v) = 0)
1124fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
1134fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
1144fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
1154fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ASSIGN(d, s)	((d) = (s))
1164fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	EQ(a, b)	((a) == (b))
117cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	STATEVARS	int dummy	/* dummy version */
1184fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	STATESETUP(m, n)	/* nothing */
1194fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	STATETEARDOWN(m)	/* nothing */
1204fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	SETUP(v)	((v) = 0)
121cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	onestate	unsigned long
1224fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	INIT(o, n)	((o) = (unsigned long)1 << (n))
123cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	INC(o)	((o) <<= 1)
1244fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
1254fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* some abbreviations; note that some of these know variable names! */
1264fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* do "if I'm here, I can also be there" etc without branches */
1274fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
1284fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
129cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
1304fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* function names */
1314fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define SNAMES			/* engine.c looks after details */
1324fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1334fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include "engine.c"
1344fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1354fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* now undo things */
1364fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	states
1374fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	CLEAR
1384fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	SET0
1394fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	SET1
1404fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	ISSET
1414fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	ASSIGN
1424fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	EQ
1434fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	STATEVARS
1444fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	STATESETUP
1454fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	STATETEARDOWN
1464fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	SETUP
1474fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	onestate
1484fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	INIT
1494fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	INC
1504fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	ISSTATEIN
1514fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	FWD
1524fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	BACK
1534fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	ISSETBACK
1544fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#undef	SNAMES
1554fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1564fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* macros for manipulating states, large version */
1574fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	states	char *
158cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	CLEAR(v)	memset(v, 0, (size_t)m->g->nstates)
1594fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	SET0(v, n)	((v)[n] = 0)
1604fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	SET1(v, n)	((v)[n] = 1)
1614fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ISSET(v, n)	((v)[n])
162cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	ASSIGN(d, s)	memcpy(d, s, (size_t)m->g->nstates)
163cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	EQ(a, b)	(memcmp(a, b, (size_t)m->g->nstates) == 0)
164cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	STATEVARS	int vn; char *space
165cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	STATESETUP(m, nv) \
166cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    if (((m)->space = malloc((size_t)((nv)*(m)->g->nstates))) == NULL) \
167cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	return(REG_ESPACE); \
168cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    else \
169cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	(m)->vn = 0
170cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
171cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	STATETEARDOWN(m)	{ free((m)->space); m->space = NULL; }
172cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	SETUP(v)	((v) = &m->space[(size_t)(m->vn++ * m->g->nstates)])
173cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	onestate	int
174cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes#define	INIT(o, n)	((o) = (int)(n))
1754fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	INC(o)	((o)++)
1764fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ISSTATEIN(v, o)	((v)[o])
1774fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* some abbreviations; note that some of these know variable names! */
1784fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* do "if I'm here, I can also be there" etc without branches */
1794fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
1804fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
1814fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ISSETBACK(v, n)	((v)[here - (n)])
1824fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* function names */
1834fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	LNAMES			/* flag */
1844fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1854fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#include "engine.c"
1864fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1874fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*
1884fa7b105644222d9b35347c9d226ca8e011072ebColin Cross - regexec - interface for matching
189cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = extern int regexec(const regex_t *, const char *, size_t, \
190cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes =					regmatch_t [], int);
191cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = #define	REG_NOTBOL	00001
192cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = #define	REG_NOTEOL	00002
193cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = #define	REG_STARTEND	00004
194cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = #define	REG_TRACE	00400	// tracing of execution
195cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = #define	REG_LARGE	01000	// force large representation
196cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes = #define	REG_BACKR	02000	// force use of backref code
1974fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
1984fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * We put this here so we can exploit knowledge of the state representation
1994fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * when choosing which matcher to call.  Also, by this point the matchers
2004fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * have been prototyped.
2014fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
2024fa7b105644222d9b35347c9d226ca8e011072ebColin Crossint				/* 0 success, REG_NOMATCH failure */
203cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughesregexec(
204cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    const regex_t *preg,
205cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    const char *string,
206cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    size_t nmatch,
207cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    regmatch_t pmatch[],
208cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes    int eflags)
2094fa7b105644222d9b35347c9d226ca8e011072ebColin Cross{
2104fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	struct re_guts *g = preg->re_g;
211cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	char *s;
2124fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#ifdef REDEBUG
2134fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#	define	GOODFLAGS(f)	(f)
2144fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#else
2154fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
2164fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#endif
2174fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
218cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	_DIAGASSERT(preg != NULL);
219cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	_DIAGASSERT(string != NULL);
220cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
2214fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
2224fa7b105644222d9b35347c9d226ca8e011072ebColin Cross		return(REG_BADPAT);
2234fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	assert(!(g->iflags&BAD));
2244fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	if (g->iflags&BAD)		/* backstop for no-debug case */
2254fa7b105644222d9b35347c9d226ca8e011072ebColin Cross		return(REG_BADPAT);
2264fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	eflags = GOODFLAGS(eflags);
2274fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
228cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	s = __UNCONST(string);
229cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes
230cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes	if (g->nstates <= (sopno)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
231cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes		return(smatcher(g, s, nmatch, pmatch, eflags));
2324fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	else
233cc213f871bf4c5329eb5eb7a80a0ce9d4a880af8Elliott Hughes		return(lmatcher(g, s, nmatch, pmatch, eflags));
2344fa7b105644222d9b35347c9d226ca8e011072ebColin Cross}
235