1f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/*	$OpenBSD: regexec.c,v 1.11 2005/08/05 13:03:00 espie Exp $ */
2f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/*-
3f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * Copyright (c) 1992, 1993, 1994
5f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *	The Regents of the University of California.  All rights reserved.
6f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *
7f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * This code is derived from software contributed to Berkeley by
8f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * Henry Spencer.
9f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *
10f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * Redistribution and use in source and binary forms, with or without
11f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * modification, are permitted provided that the following conditions
12f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * are met:
13f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * 1. Redistributions of source code must retain the above copyright
14f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *    notice, this list of conditions and the following disclaimer.
15f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * 2. Redistributions in binary form must reproduce the above copyright
16f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *    notice, this list of conditions and the following disclaimer in the
17f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *    documentation and/or other materials provided with the distribution.
18f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * 3. Neither the name of the University nor the names of its contributors
19f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *    may be used to endorse or promote products derived from this software
20f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *    without specific prior written permission.
21f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *
22f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * SUCH DAMAGE.
33f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *
34f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
35f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine */
36f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
37f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/*
38f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * the outer shell of regexec()
39f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *
40f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * This file includes engine.c *twice*, after muchos fiddling with the
41f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * macros that code uses.  This lets the same code operate on two different
42f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * representations for state sets.
43f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine */
44f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include <sys/types.h>
45f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include <stdio.h>
46f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include <stdlib.h>
47f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include <string.h>
48f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include <limits.h>
49f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include <ctype.h>
50f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include "./regex.h"
51f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
52f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include "utils.h"
53f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include "./regex2.h"
54f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
55f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* macros for manipulating states, small version */
56f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	states	long
57f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	states1	states		/* for later use in regexec() decision */
58f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	CLEAR(v)	((v) = 0)
59f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
60f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
61f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
62f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ASSIGN(d, s)	((d) = (s))
63f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	EQ(a, b)	((a) == (b))
64f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	STATEVARS	long dummy	/* dummy version */
65f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	STATESETUP(m, n)	/* nothing */
66f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	STATETEARDOWN(m)	/* nothing */
67f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	SETUP(v)	((v) = 0)
68f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	onestate	long
69f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	INIT(o, n)	((o) = (unsigned long)1 << (n))
70f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	INC(o)		((o) <<= 1)
71f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
72f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* some abbreviations; note that some of these know variable names! */
73f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* do "if I'm here, I can also be there" etc without branches */
74f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
75f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
76f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ISSETBACK(v, n)		(((v) & ((unsigned long)here >> (n))) != 0)
77f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* function names */
78f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define SNAMES			/* engine.c looks after details */
79f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
80f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include "engine.c"
81f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
82f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* now undo things */
83f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	states
84f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	CLEAR
85f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	SET0
86f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	SET1
87f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	ISSET
88f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	ASSIGN
89f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	EQ
90f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	STATEVARS
91f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	STATESETUP
92f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	STATETEARDOWN
93f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	SETUP
94f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	onestate
95f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	INIT
96f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	INC
97f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	ISSTATEIN
98f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	FWD
99f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	BACK
100f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	ISSETBACK
101f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#undef	SNAMES
102f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
103f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* macros for manipulating states, large version */
104f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	states	char *
105f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	CLEAR(v)	memset(v, 0, m->g->nstates)
106f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	SET0(v, n)	((v)[n] = 0)
107f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	SET1(v, n)	((v)[n] = 1)
108f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ISSET(v, n)	((v)[n])
109f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ASSIGN(d, s)	memcpy(d, s, m->g->nstates)
110f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
111f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	STATEVARS	long vn; char *space
112f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	STATESETUP(m, nv)	{ (m)->space = (char*)malloc((nv)*(m)->g->nstates); \
113f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine				if ((m)->space == NULL) return(REG_ESPACE); \
114f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine				(m)->vn = 0; }
115f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	STATETEARDOWN(m)	{ free((m)->space); }
116f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
117f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	onestate	long
118f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	INIT(o, n)	((o) = (n))
119f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	INC(o)	((o)++)
120f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ISSTATEIN(v, o)	((v)[o])
121f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* some abbreviations; note that some of these know variable names! */
122f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* do "if I'm here, I can also be there" etc without branches */
123f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
124f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
125f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	ISSETBACK(v, n)	((v)[here - (n)])
126f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/* function names */
127f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#define	LNAMES			/* flag */
128f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
129f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#include "engine.c"
130f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
131f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine/*
132f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine - regexec - interface for matching
133f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine *
134f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * We put this here so we can exploit knowledge of the state representation
135f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * when choosing which matcher to call.  Also, by this point the matchers
136f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine * have been prototyped.
137f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine */
138f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkineint				/* 0 success, REG_NOMATCH failure */
139f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkineregexec(const regex_t *preg, const char *string, size_t nmatch,
140f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine    regmatch_t pmatch[], int eflags)
141f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine{
142f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	struct re_guts *g = preg->re_g;
143f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#ifdef REDEBUG
144f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#	define	GOODFLAGS(f)	(f)
145f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#else
146f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
147f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine#endif
148f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
149f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
150f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine		return(REG_BADPAT);
151f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	assert(!(g->iflags&BAD));
152f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	if (g->iflags&BAD)		/* backstop for no-debug case */
153f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine		return(REG_BADPAT);
154f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	eflags = GOODFLAGS(eflags);
155f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine
156f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	if (g->nstates <= (int)(CHAR_BIT*sizeof(states1)) && !(eflags&REG_LARGE))
157f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine		return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
158f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine	else
159f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine		return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
160f9e88edc21c894886747e899dc29c02a28728d65Vladimir Chtchetkine}
161