xref: /titanic_51/usr/src/lib/libc/port/locale/regexec.c (revision ce0d9371c4da921f289a8bc7fd846d3bf1b8cca7)
1 /*
2  * Copyright 2010 Nexenta Systems, Inc.  All rights reserved.
3  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
4  * Copyright (c) 1992, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * This code is derived from software contributed to Berkeley by
8  * Henry Spencer.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 4. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * the outer shell of regexec()
37  *
38  * This file includes engine.c three times, after muchos fiddling with the
39  * macros that code uses.  This lets the same code operate on two different
40  * representations for state sets and characters.
41  */
42 #include "lint.h"
43 #include "file64.h"
44 #include <sys/types.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <limits.h>
49 #include <ctype.h>
50 #include <regex.h>
51 #include <wchar.h>
52 #include <wctype.h>
53 #include <note.h>
54 #include <assert.h>
55 
56 #include "utils.h"
57 #include "regex2.h"
58 
59 /* we want _NOTE, but not NOTE (which collides with our own use) */
60 #undef	NOTE
61 
62 static size_t
63 xmbrtowc(wint_t *wi, const char *s, size_t n, mbstate_t *mbs, wint_t dummy)
64 {
65 	size_t nr;
66 	wchar_t wc;
67 
68 	nr = mbrtowc(&wc, s, n, mbs);
69 	if (wi != NULL)
70 		*wi = wc;
71 	if (nr == 0)
72 		return (1);
73 	else if (nr == (size_t)-1 || nr == (size_t)-2) {
74 		(void) memset(mbs, 0, sizeof (*mbs));
75 		if (wi != NULL)
76 			*wi = dummy;
77 		return (1);
78 	} else
79 		return (nr);
80 }
81 
82 static size_t
83 xmbrtowc_dummy(wint_t *wi, const char *s, size_t n, mbstate_t *mbs,
84     wint_t dummy)
85 {
86 	_NOTE(ARGUNUSED(n));
87 	_NOTE(ARGUNUSED(mbs));
88 	_NOTE(ARGUNUSED(dummy));
89 
90 	if (wi != NULL)
91 		*wi = (unsigned char)*s;
92 	return (1);
93 }
94 
95 /* macros for manipulating states, small version */
96 #define	states	long
97 #define	states1	states		/* for later use in regexec() decision */
98 #define	CLEAR(v)	((v) = 0)
99 #define	SET0(v, n)	((v) &= ~((unsigned long)1 << (n)))
100 #define	SET1(v, n)	((v) |= (unsigned long)1 << (n))
101 #define	ISSET(v, n)	(((v) & ((unsigned long)1 << (n))) != 0)
102 #define	ASSIGN(d, s)	((d) = (s))
103 #define	EQ(a, b)	((a) == (b))
104 #define	STATEVARS	long dummy	/* dummy version */
105 #define	STATESETUP(m, n)	/* nothing */
106 #define	STATETEARDOWN(m)	/* nothing */
107 #define	SETUP(v)	((v) = 0)
108 #define	onestate	long
109 #define	INIT(o, n)	((o) = (unsigned long)1 << (n))
110 #define	INC(o)	((o) <<= 1)
111 #define	ISSTATEIN(v, o)	(((v) & (o)) != 0)
112 /* some abbreviations; note that some of these know variable names! */
113 /* do "if I'm here, I can also be there" etc without branches */
114 #define	FWD(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) << (n))
115 #define	BACK(dst, src, n)	((dst) |= ((unsigned long)(src)&(here)) >> (n))
116 #define	ISSETBACK(v, n)	(((v) & ((unsigned long)here >> (n))) != 0)
117 /* no multibyte support */
118 #define	XMBRTOWC	xmbrtowc_dummy
119 #define	ZAPSTATE(mbs)	((void)(mbs))
120 /* function names */
121 #define	SNAMES			/* engine.c looks after details */
122 
123 #include "engine.c"
124 
125 /* now undo things */
126 #undef	states
127 #undef	CLEAR
128 #undef	SET0
129 #undef	SET1
130 #undef	ISSET
131 #undef	ASSIGN
132 #undef	EQ
133 #undef	STATEVARS
134 #undef	STATESETUP
135 #undef	STATETEARDOWN
136 #undef	SETUP
137 #undef	onestate
138 #undef	INIT
139 #undef	INC
140 #undef	ISSTATEIN
141 #undef	FWD
142 #undef	BACK
143 #undef	ISSETBACK
144 #undef	SNAMES
145 #undef	XMBRTOWC
146 #undef	ZAPSTATE
147 
148 /* macros for manipulating states, large version */
149 #define	states	char *
150 #define	CLEAR(v)	(void) memset(v, 0, m->g->nstates)
151 #define	SET0(v, n)	((v)[n] = 0)
152 #define	SET1(v, n)	((v)[n] = 1)
153 #define	ISSET(v, n)	((v)[n])
154 #define	ASSIGN(d, s)	(void) memcpy(d, s, m->g->nstates)
155 #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
156 #define	STATEVARS	long vn; char *space
157 #define	STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
158 	if ((m)->space == NULL) \
159 		return (REG_ESPACE); \
160 	(m)->vn = 0; }
161 #define	STATETEARDOWN(m)	{ free((m)->space); }
162 #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
163 #define	onestate	long
164 #define	INIT(o, n)	((o) = (n))
165 #define	INC(o)	((o)++)
166 #define	ISSTATEIN(v, o)	((v)[o])
167 /* some abbreviations; note that some of these know variable names! */
168 /* do "if I'm here, I can also be there" etc without branches */
169 #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
170 #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
171 #define	ISSETBACK(v, n)	((v)[here - (n)])
172 /* no multibyte support */
173 #define	XMBRTOWC	xmbrtowc_dummy
174 #define	ZAPSTATE(mbs)	((void)(mbs))
175 /* function names */
176 #define	LNAMES			/* flag */
177 
178 #include "engine.c"
179 
180 /* multibyte character & large states version */
181 #undef	LNAMES
182 #undef	XMBRTOWC
183 #undef	ZAPSTATE
184 #define	XMBRTOWC	xmbrtowc
185 #define	ZAPSTATE(mbs)	(void) memset((mbs), 0, sizeof (*(mbs)))
186 #define	MNAMES
187 
188 #include "engine.c"
189 
190 /*
191  * regexec - interface for matching
192  *
193  * We put this here so we can exploit knowledge of the state representation
194  * when choosing which matcher to call.  Also, by this point the matchers
195  * have been prototyped.
196  */
197 int				/* 0 success, REG_NOMATCH failure */
198 regexec(const regex_t *_RESTRICT_KYWD preg,
199     const char *_RESTRICT_KYWD string, size_t nmatch,
200 	regmatch_t pmatch[_RESTRICT_KYWD], int eflags)
201 {
202 	struct re_guts *g = preg->re_g;
203 #ifdef REDEBUG
204 #define	GOODFLAGS(f)	(f)
205 #else
206 #ifdef	REG_STARTEND
207 #define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
208 #else
209 #define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL))
210 #endif
211 #endif
212 
213 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
214 		return (REG_BADPAT);
215 	assert(!(g->iflags&BAD));
216 	if (g->iflags&BAD)		/* backstop for no-debug case */
217 		return (REG_BADPAT);
218 	eflags = GOODFLAGS(eflags);
219 
220 	if (MB_CUR_MAX > 1)
221 		return (mmatcher(g, (char *)string, nmatch, pmatch, eflags));
222 #ifdef	REG_LARGE
223 	else if (g->nstates <= CHAR_BIT*sizeof (states1) && !(eflags&REG_LARGE))
224 #else
225 	else if (g->nstates <= CHAR_BIT*sizeof (states1))
226 #endif
227 		return (smatcher(g, (char *)string, nmatch, pmatch, eflags));
228 	else
229 		return (lmatcher(g, (char *)string, nmatch, pmatch, eflags));
230 }
231