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