xref: /openbsd-src/lib/libc/regex/regexec.c (revision a4afd6dad3fba28f80e70208181c06c482259988)
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 
38 #if defined(LIBC_SCCS) && !defined(lint)
39 static char rcsid[] = "$OpenBSD: regexec.c,v 1.4 1996/09/15 09:31:27 tholo Exp $";
40 #endif /* LIBC_SCCS and not lint */
41 
42 /*
43  * the outer shell of regexec()
44  *
45  * This file includes engine.c *twice*, after muchos fiddling with the
46  * macros that code uses.  This lets the same code operate on two different
47  * representations for state sets.
48  */
49 #include <sys/types.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <limits.h>
54 #include <ctype.h>
55 #include <regex.h>
56 
57 #include "utils.h"
58 #include "regex2.h"
59 
60 /* macros for manipulating states, small version */
61 #define	states	long
62 #define	states1	states		/* for later use in regexec() decision */
63 #define	CLEAR(v)	((v) = 0)
64 #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
65 #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
66 #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
67 #define	ASSIGN(d, s)	((d) = (s))
68 #define	EQ(a, b)	((a) == (b))
69 #define	STATEVARS	long dummy	/* dummy version */
70 #define	STATESETUP(m, n)	/* nothing */
71 #define	STATETEARDOWN(m)	/* nothing */
72 #define	SETUP(v)	((v) = 0)
73 #define	onestate	long
74 #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
75 #define	INC(o)		((o) = (unsigned long)(o) << 1)
76 #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
77 /* some abbreviations; note that some of these know variable names! */
78 /* do "if I'm here, I can also be there" etc without branches */
79 #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
80 #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
81 #define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
82 /* function names */
83 #define SNAMES			/* engine.c looks after details */
84 
85 #include "engine.c"
86 
87 /* now undo things */
88 #undef	states
89 #undef	CLEAR
90 #undef	SET0
91 #undef	SET1
92 #undef	ISSET
93 #undef	ASSIGN
94 #undef	EQ
95 #undef	STATEVARS
96 #undef	STATESETUP
97 #undef	STATETEARDOWN
98 #undef	SETUP
99 #undef	onestate
100 #undef	INIT
101 #undef	INC
102 #undef	ISSTATEIN
103 #undef	FWD
104 #undef	BACK
105 #undef	ISSETBACK
106 #undef	SNAMES
107 
108 /* macros for manipulating states, large version */
109 #define	states	char *
110 #define	CLEAR(v)	memset(v, 0, m->g->nstates)
111 #define	SET0(v, n)	((v)[n] = 0)
112 #define	SET1(v, n)	((v)[n] = 1)
113 #define	ISSET(v, n)	((v)[n])
114 #define	ASSIGN(d, s)	memcpy(d, s, m->g->nstates)
115 #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
116 #define	STATEVARS	long vn; char *space
117 #define	STATESETUP(m, nv)	{ (m)->space = malloc((nv)*(m)->g->nstates); \
118 				if ((m)->space == NULL) return(REG_ESPACE); \
119 				(m)->vn = 0; }
120 #define	STATETEARDOWN(m)	{ free((m)->space); }
121 #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
122 #define	onestate	long
123 #define	INIT(o, n)	((o) = (n))
124 #define	INC(o)	((o)++)
125 #define	ISSTATEIN(v, o)	((v)[o])
126 /* some abbreviations; note that some of these know variable names! */
127 /* do "if I'm here, I can also be there" etc without branches */
128 #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
129 #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
130 #define	ISSETBACK(v, n)	((v)[here - (n)])
131 /* function names */
132 #define	LNAMES			/* flag */
133 
134 #include "engine.c"
135 
136 /*
137  - regexec - interface for matching
138  = extern int regexec(const regex_t *, const char *, size_t, \
139  =					regmatch_t [], int);
140  = #define	REG_NOTBOL	00001
141  = #define	REG_NOTEOL	00002
142  = #define	REG_STARTEND	00004
143  = #define	REG_TRACE	00400	// tracing of execution
144  = #define	REG_LARGE	01000	// force large representation
145  = #define	REG_BACKR	02000	// force use of backref code
146  *
147  * We put this here so we can exploit knowledge of the state representation
148  * when choosing which matcher to call.  Also, by this point the matchers
149  * have been prototyped.
150  */
151 int				/* 0 success, REG_NOMATCH failure */
152 regexec(preg, string, nmatch, pmatch, eflags)
153 const regex_t *preg;
154 const char *string;
155 size_t nmatch;
156 regmatch_t pmatch[];
157 int eflags;
158 {
159 	register struct re_guts *g = preg->re_g;
160 #ifdef REDEBUG
161 #	define	GOODFLAGS(f)	(f)
162 #else
163 #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
164 #endif
165 
166 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
167 		return(REG_BADPAT);
168 	assert(!(g->iflags&BAD));
169 	if (g->iflags&BAD)		/* backstop for no-debug case */
170 		return(REG_BADPAT);
171 	eflags = GOODFLAGS(eflags);
172 
173 	if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
174 		return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
175 	else
176 		return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
177 }
178