xref: /illumos-gate/usr/src/lib/libc/port/regex/regex2.h (revision d48be21240dfd051b689384ce2b23479d757f2d8)
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. 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 
34 /*
35  * First, the stuff that ends up in the outside-world include file
36  * typedef off_t regoff_t;
37  * typedef struct {
38  *	int re_magic;
39  *	size_t re_nsub;		// number of parenthesized subexpressions
40  *	const char *re_endp;	// end pointer for REG_PEND
41  *	struct re_guts *re_g;	// none of your business :-)
42  * } regex_t;
43  * typedef struct {
44  *	regoff_t rm_so;		// start of match
45  *	regoff_t rm_eo;		// end of match
46  * } regmatch_t;
47  */
48 /*
49  * internals of regex_t
50  */
51 #define	MAGIC1	((('r'^0200)<<8) | 'e')
52 
53 /*
54  * The internal representation is a *strip*, a sequence of
55  * operators ending with an endmarker.  (Some terminology etc. is a
56  * historical relic of earlier versions which used multiple strips.)
57  * Certain oddities in the representation are there to permit running
58  * the machinery backwards; in particular, any deviation from sequential
59  * flow must be marked at both its source and its destination.  Some
60  * fine points:
61  *
62  * - OPLUS_ and O_PLUS are *inside* the loop they create.
63  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
64  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
65  *   OOR1 and OOR2 are respectively the end and the beginning of one of
66  *   the branches.  Note that there is an implicit OOR2 following OCH_
67  *   and an implicit OOR1 preceding O_CH.
68  *
69  * In state representations, an operator's bit is on to signify a state
70  * immediately *preceding* "execution" of that operator.
71  */
72 typedef unsigned int sop;	/* strip operator */
73 typedef unsigned int sopno;
74 #define	OPRMASK	0xf8000000U
75 #define	OPDMASK	0x07ffffffU
76 #define	OPSHIFT	((unsigned)27)
77 #define	OP(n)	((n)&OPRMASK)
78 #define	OPND(n)	((n)&OPDMASK)
79 #define	SOP(op, opnd)	((op)|(opnd))
80 /* operators			   meaning	operand			*/
81 /*						(back, fwd are offsets)	*/
82 #define	OEND	(1U<<OPSHIFT)	/* endmarker	-			*/
83 #define	OCHAR	(2U<<OPSHIFT)	/* character	wide character		*/
84 #define	OBOL	(3U<<OPSHIFT)	/* left anchor	-			*/
85 #define	OEOL	(4U<<OPSHIFT)	/* right anchor	-			*/
86 #define	OANY	(5U<<OPSHIFT)	/* .		-			*/
87 #define	OANYOF	(6U<<OPSHIFT)	/* [...]	set number		*/
88 #define	OBACK_	(7U<<OPSHIFT)	/* begin \d	paren number		*/
89 #define	O_BACK	(8U<<OPSHIFT)	/* end \d	paren number		*/
90 #define	OPLUS_	(9U<<OPSHIFT)	/* + prefix	fwd to suffix		*/
91 #define	O_PLUS	(10U<<OPSHIFT)	/* + suffix	back to prefix		*/
92 #define	OQUEST_	(11U<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
93 #define	O_QUEST	(12U<<OPSHIFT)	/* ? suffix	back to prefix		*/
94 #define	OLPAREN	(13U<<OPSHIFT)	/* (		fwd to )		*/
95 #define	ORPAREN	(14U<<OPSHIFT)	/* )		back to (		*/
96 #define	OCH_	(15U<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
97 #define	OOR1	(16U<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
98 #define	OOR2	(17U<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
99 #define	O_CH	(18U<<OPSHIFT)	/* end choice	back to OOR1		*/
100 #define	OBOW	(19U<<OPSHIFT)	/* begin word	-			*/
101 #define	OEOW	(20U<<OPSHIFT)	/* end word	-			*/
102 
103 /*
104  * Structures for [] character-set representation.
105  */
106 typedef struct {
107 	wint_t		min;
108 	wint_t		max;
109 } crange;
110 typedef struct {
111 	unsigned char	bmp[NC_MAX / 8];
112 	wctype_t	*types;
113 	unsigned int	ntypes;
114 	wint_t		*wides;
115 	unsigned int	nwides;
116 	crange		*ranges;
117 	unsigned int	nranges;
118 	int		invert;
119 	int		icase;
120 } cset;
121 
122 static int
123 CHIN1(cset *cs, wint_t ch)
124 {
125 	unsigned int i;
126 
127 	assert(ch >= 0);
128 	if (ch < NC)
129 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
130 		    cs->invert);
131 	for (i = 0; i < cs->nwides; i++) {
132 		if (cs->icase) {
133 			if (ch == towlower(cs->wides[i]) ||
134 			    ch == towupper(cs->wides[i]))
135 				return (!cs->invert);
136 		} else if (ch == cs->wides[i])
137 			return (!cs->invert);
138 	}
139 	for (i = 0; i < cs->nranges; i++)
140 		if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
141 			return (!cs->invert);
142 	for (i = 0; i < cs->ntypes; i++)
143 		if (iswctype(ch, cs->types[i]))
144 			return (!cs->invert);
145 	return (cs->invert);
146 }
147 
148 static int
149 CHIN(cset *cs, wint_t ch)
150 {
151 
152 	assert(ch >= 0);
153 	if (ch < NC)
154 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
155 		    cs->invert);
156 	else if (cs->icase)
157 		return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
158 		    CHIN1(cs, towupper(ch)));
159 	else
160 		return (CHIN1(cs, ch));
161 }
162 
163 /*
164  * main compiled-expression structure
165  */
166 struct re_guts {
167 	int magic;
168 #define	MAGIC2	((('R'^0200)<<8)|'E')
169 	sop *strip;		/* malloced area for strip */
170 	unsigned int ncsets;	/* number of csets in use */
171 	cset *sets;		/* -> cset [ncsets] */
172 	int cflags;		/* copy of regcomp() cflags argument */
173 	sopno nstates;		/* = number of sops */
174 	sopno firststate;	/* the initial OEND (normally 0) */
175 	sopno laststate;	/* the final OEND */
176 	int iflags;		/* internal flags */
177 #define	USEBOL	01	/* used ^ */
178 #define	USEEOL	02	/* used $ */
179 #define	BAD	04	/* something wrong */
180 	int nbol;		/* number of ^ used */
181 	int neol;		/* number of $ used */
182 	char *must;		/* match must contain this string */
183 	int moffset;		/* latest point at which must may be located */
184 	int *charjump;		/* Boyer-Moore char jump table */
185 	int *matchjump;		/* Boyer-Moore match jump table */
186 	int mlen;		/* length of must */
187 	size_t nsub;		/* copy of re_nsub */
188 	int backrefs;		/* does it use back references? */
189 	sopno nplus;		/* how deep does it nest +s? */
190 };
191 
192 /* misc utilities */
193 #define	OUT	(CHAR_MIN - 1)	/* a non-character value */
194 #define	IGN	(CHAR_MIN - 2)
195 #define	ISWORD(c)	(iswalnum((uch)(c)) || (c) == '_')
196