xref: /freebsd/lib/libc/regex/regexec.c (revision 035e325c81b75ea3b5c878c55cc133672b6ac06d)
1 /*-
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.
5  *
6  * This code is derived from software contributed to Berkeley by
7  * Henry Spencer.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
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.
24  *
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
35  * SUCH DAMAGE.
36  *
37  *	@(#)regexec.c	8.3 (Berkeley) 3/20/94
38  */
39 
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$");
45 
46 /*
47  * the outer shell of regexec()
48  *
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.
52  */
53 #include <sys/types.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <limits.h>
58 #include <ctype.h>
59 #include <regex.h>
60 
61 #include "utils.h"
62 #include "regex2.h"
63 
64 static int nope __unused = 0;	/* for use in asserts; shuts lint up */
65 
66 /* macros for manipulating states, small version */
67 #define	states	long
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)
79 #define	onestate	long
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)
88 /* function names */
89 #define SNAMES			/* engine.c looks after details */
90 
91 #include "engine.c"
92 
93 /* now undo things */
94 #undef	states
95 #undef	CLEAR
96 #undef	SET0
97 #undef	SET1
98 #undef	ISSET
99 #undef	ASSIGN
100 #undef	EQ
101 #undef	STATEVARS
102 #undef	STATESETUP
103 #undef	STATETEARDOWN
104 #undef	SETUP
105 #undef	onestate
106 #undef	INIT
107 #undef	INC
108 #undef	ISSTATEIN
109 #undef	FWD
110 #undef	BACK
111 #undef	ISSETBACK
112 #undef	SNAMES
113 
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); \
125 				(m)->vn = 0; }
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)])
137 /* function names */
138 #define	LNAMES			/* flag */
139 
140 #include "engine.c"
141 
142 /*
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
152  *
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.
156  */
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;
161 size_t nmatch;
162 regmatch_t pmatch[__restrict];
163 int eflags;
164 {
165 	struct re_guts *g = preg->re_g;
166 #ifdef REDEBUG
167 #	define	GOODFLAGS(f)	(f)
168 #else
169 #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
170 #endif
171 
172 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
173 		return(REG_BADPAT);
174 	assert(!(g->iflags&BAD));
175 	if (g->iflags&BAD)		/* backstop for no-debug case */
176 		return(REG_BADPAT);
177 	eflags = GOODFLAGS(eflags);
178 
179 	if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
180 		return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
181 	else
182 		return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
183 }
184