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  *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
34  * $FreeBSD: src/lib/libc/regex/regex2.h,v 1.6 2002/03/22 23:41:56 obrien Exp $
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)	((sop)(n)&OPRMASK)
81 #define	OPND(n)	((sop)(n)&OPDMASK)
82 #define	SOP(op, opnd)	((sop)(op)|(sop)(opnd))
83 /* operators			   meaning	operand			*/
84 /*						(back, fwd are offsets)	*/
85 #define	OEND	(1UL<<OPSHIFT)	/* endmarker	-			*/
86 #define	OCHAR	(2UL<<OPSHIFT)	/* character	unsigned char		*/
87 #define	OBOL	(3UL<<OPSHIFT)	/* left anchor	-			*/
88 #define	OEOL	(4UL<<OPSHIFT)	/* right anchor	-			*/
89 #define	OANY	(5UL<<OPSHIFT)	/* .		-			*/
90 #define	OANYOF	(6UL<<OPSHIFT)	/* [...]	set number		*/
91 #define	OBACK_	(7UL<<OPSHIFT)	/* begin \d	paren number		*/
92 #define	O_BACK	(8UL<<OPSHIFT)	/* end \d	paren number		*/
93 #define	OPLUS_	(9UL<<OPSHIFT)	/* + prefix	fwd to suffix		*/
94 #define	O_PLUS	(10UL<<OPSHIFT)	/* + suffix	back to prefix		*/
95 #define	OQUEST_	(11UL<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
96 #define	O_QUEST	(12UL<<OPSHIFT)	/* ? suffix	back to prefix		*/
97 #define	OLPAREN	(13UL<<OPSHIFT)	/* (		fwd to )		*/
98 #define	ORPAREN	(14UL<<OPSHIFT)	/* )		back to (		*/
99 #define	OCH_	(15UL<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
100 #define	OOR1	(16UL<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
101 #define	OOR2	(17UL<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
102 #define	O_CH	(18UL<<OPSHIFT)	/* end choice	back to OOR1		*/
103 #define	OBOW	(19UL<<OPSHIFT)	/* begin word	-			*/
104 #define	OEOW	(20UL<<OPSHIFT)	/* end word	-			*/
105 
106 /*
107  * Structure for [] character-set representation.  Character sets are
108  * done as bit vectors, grouped 8 to a byte vector for compactness.
109  * The individual set therefore has both a pointer to the byte vector
110  * and a mask to pick out the relevant bit of each byte.  A hash code
111  * simplifies testing whether two sets could be identical.
112  *
113  * This will get trickier for multicharacter collating elements.  As
114  * preliminary hooks for dealing with such things, we also carry along
115  * a string of multi-character elements, and decide the size of the
116  * vectors at run time.
117  */
118 typedef struct {
119 	uch *ptr;		/* -> uch [csetsize] */
120 	uch mask;		/* bit within array */
121 	short hash;             /* hash code */
122 	size_t smultis;
123 	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
124 } cset;
125 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
126 #define CHadd(cs, c)    ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (uch)(c))
127 #define CHsub(cs, c)    ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (uch)(c))
128 #define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
129 #define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
130 #define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
131 #define	MCin(p, cs, cp)	mcin(p, cs, cp)
132 
133 /* stuff for character categories */
134 typedef unsigned char cat_t;
135 
136 /*
137  * main compiled-expression structure
138  */
139 struct re_guts {
140 	int magic;
141 #		define	MAGIC2	((('R'^0200)<<8)|'E')
142 	sop *strip;		/* malloced area for strip */
143 	int csetsize;		/* number of bits in a cset vector */
144 	int ncsets;		/* number of csets in use */
145 	cset *sets;		/* -> cset [ncsets] */
146 	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
147 	int cflags;		/* copy of regcomp() cflags argument */
148 	sopno nstates;		/* = number of sops */
149 	sopno firststate;	/* the initial OEND (normally 0) */
150 	sopno laststate;	/* the final OEND */
151 	int iflags;		/* internal flags */
152 #		define	USEBOL	01	/* used ^ */
153 #		define	USEEOL	02	/* used $ */
154 #		define	BAD	04	/* something wrong */
155 	int nbol;		/* number of ^ used */
156 	int neol;		/* number of $ used */
157 	int ncategories;	/* how many character categories */
158 	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
159 	char *must;		/* match must contain this string */
160 	int moffset;		/* latest point at which must may be located */
161 	int *charjump;		/* Boyer-Moore char jump table */
162 	int *matchjump;		/* Boyer-Moore match jump table */
163 	int mlen;		/* length of must */
164 	size_t nsub;		/* copy of re_nsub */
165 	int backrefs;		/* does it use back references? */
166 	sopno nplus;		/* how deep does it nest +s? */
167 	/* catspace must be last */
168 	cat_t catspace[NC];	/* categories */
169 };
170 
171 /* misc utilities */
172 #define	OUT	(CHAR_MAX+1)	/* a non-character value */
173 #define ISWORD(c)       (isalnum((uch)(c)) || (c) == '_')
174