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