xref: /freebsd/lib/libc/regex/regex2.h (revision 8ddb146abcdf061be9f2c0db7e391697dafad85c)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5  * Copyright (c) 1992, 1993, 1994
6  *	The Regents of the University of California.  All rights reserved.
7  *
8  * This code is derived from software contributed to Berkeley by
9  * Henry Spencer.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  *
35  *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
36  * $FreeBSD$
37  */
38 
39 /*
40  * First, the stuff that ends up in the outside-world include file
41  = typedef off_t regoff_t;
42  = typedef struct {
43  = 	int re_magic;
44  = 	size_t re_nsub;		// number of parenthesized subexpressions
45  = 	const char *re_endp;	// end pointer for REG_PEND
46  = 	struct re_guts *re_g;	// none of your business :-)
47  = } regex_t;
48  = typedef struct {
49  = 	regoff_t rm_so;		// start of match
50  = 	regoff_t rm_eo;		// end of match
51  = } regmatch_t;
52  */
53 /*
54  * internals of regex_t
55  */
56 #define	MAGIC1	((('r'^0200)<<8) | 'e')
57 
58 /*
59  * The internal representation is a *strip*, a sequence of
60  * operators ending with an endmarker.  (Some terminology etc. is a
61  * historical relic of earlier versions which used multiple strips.)
62  * Certain oddities in the representation are there to permit running
63  * the machinery backwards; in particular, any deviation from sequential
64  * flow must be marked at both its source and its destination.  Some
65  * fine points:
66  *
67  * - OPLUS_ and O_PLUS are *inside* the loop they create.
68  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
69  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
70  *   OOR1 and OOR2 are respectively the end and the beginning of one of
71  *   the branches.  Note that there is an implicit OOR2 following OCH_
72  *   and an implicit OOR1 preceding O_CH.
73  *
74  * In state representations, an operator's bit is on to signify a state
75  * immediately *preceding* "execution" of that operator.
76  */
77 typedef unsigned long sop;	/* strip operator */
78 typedef unsigned long sopno;
79 #define	OPRMASK	0xf8000000L
80 #define	OPDMASK	0x07ffffffL
81 #define	OPSHIFT	((unsigned)27)
82 #define	OP(n)	((n)&OPRMASK)
83 #define	OPND(n)	((n)&OPDMASK)
84 #define	SOP(op, opnd)	((op)|(opnd))
85 /* operators			   meaning	operand			*/
86 /*						(back, fwd are offsets)	*/
87 #define	OEND	(1L<<OPSHIFT)	/* endmarker	-			*/
88 #define	OCHAR	(2L<<OPSHIFT)	/* character	wide character		*/
89 #define	OBOL	(3L<<OPSHIFT)	/* left anchor	-			*/
90 #define	OEOL	(4L<<OPSHIFT)	/* right anchor	-			*/
91 #define	OANY	(5L<<OPSHIFT)	/* .		-			*/
92 #define	OANYOF	(6L<<OPSHIFT)	/* [...]	set number		*/
93 #define	OBACK_	(7L<<OPSHIFT)	/* begin \d	paren number		*/
94 #define	O_BACK	(8L<<OPSHIFT)	/* end \d	paren number		*/
95 #define	OPLUS_	(9L<<OPSHIFT)	/* + prefix	fwd to suffix		*/
96 #define	O_PLUS	(10L<<OPSHIFT)	/* + suffix	back to prefix		*/
97 #define	OQUEST_	(11L<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
98 #define	O_QUEST	(12L<<OPSHIFT)	/* ? suffix	back to prefix		*/
99 #define	OLPAREN	(13L<<OPSHIFT)	/* (		fwd to )		*/
100 #define	ORPAREN	(14L<<OPSHIFT)	/* )		back to (		*/
101 #define	OCH_	(15L<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
102 #define	OOR1	(16L<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
103 #define	OOR2	(17L<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
104 #define	O_CH	(18L<<OPSHIFT)	/* end choice	back to OOR1		*/
105 #define	OBOW	(19L<<OPSHIFT)	/* begin word	-			*/
106 #define	OEOW	(20L<<OPSHIFT)	/* end word	-			*/
107 #define	OBOS	(21L<<OPSHIFT)	/* begin subj.  -			*/
108 #define	OEOS	(22L<<OPSHIFT)	/* end subj.	-			*/
109 #define	OWBND	(23L<<OPSHIFT)	/* word bound	-			*/
110 #define	ONWBND	(24L<<OPSHIFT)	/* not bound	-			*/
111 
112 /*
113  * Structures for [] character-set representation.
114  */
115 typedef struct {
116 	wint_t		min;
117 	wint_t		max;
118 } crange;
119 typedef struct {
120 	unsigned char	bmp[NC_MAX / 8];
121 	wctype_t	*types;
122 	unsigned int	ntypes;
123 	wint_t		*wides;
124 	unsigned int	nwides;
125 	crange		*ranges;
126 	unsigned int	nranges;
127 	int		invert;
128 	int		icase;
129 } cset;
130 
131 static int
132 CHIN1(cset *cs, wint_t ch)
133 {
134 	unsigned int i;
135 
136 	assert(ch >= 0);
137 	if (ch < NC)
138 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
139 		    cs->invert);
140 	for (i = 0; i < cs->nwides; i++) {
141 		if (cs->icase) {
142 			if (ch == towlower(cs->wides[i]) ||
143 			    ch == towupper(cs->wides[i]))
144 				return (!cs->invert);
145 		} else if (ch == cs->wides[i])
146 			return (!cs->invert);
147 	}
148 	for (i = 0; i < cs->nranges; i++)
149 		if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
150 			return (!cs->invert);
151 	for (i = 0; i < cs->ntypes; i++)
152 		if (iswctype(ch, cs->types[i]))
153 			return (!cs->invert);
154 	return (cs->invert);
155 }
156 
157 static __inline int
158 CHIN(cset *cs, wint_t ch)
159 {
160 
161 	assert(ch >= 0);
162 	if (ch < NC)
163 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
164 		    cs->invert);
165 	else if (cs->icase)
166 		return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
167 		    CHIN1(cs, towupper(ch)));
168 	else
169 		return (CHIN1(cs, ch));
170 }
171 
172 /*
173  * main compiled-expression structure
174  */
175 struct re_guts {
176 	int magic;
177 #		define	MAGIC2	((('R'^0200)<<8)|'E')
178 	sop *strip;		/* malloced area for strip */
179 	unsigned int ncsets;	/* number of csets in use */
180 	cset *sets;		/* -> cset [ncsets] */
181 	int cflags;		/* copy of regcomp() cflags argument */
182 	sopno nstates;		/* = number of sops */
183 	sopno firststate;	/* the initial OEND (normally 0) */
184 	sopno laststate;	/* the final OEND */
185 	int iflags;		/* internal flags */
186 #		define	USEBOL	01	/* used ^ */
187 #		define	USEEOL	02	/* used $ */
188 #		define	BAD	04	/* something wrong */
189 	int nbol;		/* number of ^ used */
190 	int neol;		/* number of $ used */
191 	char *must;		/* match must contain this string */
192 	int moffset;		/* latest point at which must may be located */
193 	int *charjump;		/* Boyer-Moore char jump table */
194 	int *matchjump;		/* Boyer-Moore match jump table */
195 	int mlen;		/* length of must */
196 	size_t nsub;		/* copy of re_nsub */
197 	int backrefs;		/* does it use back references? */
198 	sopno nplus;		/* how deep does it nest +s? */
199 };
200 
201 /* misc utilities */
202 #define	OUT	(CHAR_MIN - 1)	/* a non-character value */
203 #define	IGN	(CHAR_MIN - 2)
204 #define ISWORD(c)       (iswalnum((uch)(c)) || (c) == '_')
205