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