xref: /netbsd/lib/libc/regex/regex2.h (revision bf9ec67e)
1 /*	$NetBSD: regex2.h,v 1.9 1998/12/13 12:21:47 drochner Exp $	*/
2 
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. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the University of
22  *	California, Berkeley and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
40  */
41 
42 /*
43  * First, the stuff that ends up in the outside-world include file
44  = typedef off_t regoff_t;
45  = typedef struct {
46  = 	int re_magic;
47  = 	size_t re_nsub;		// number of parenthesized subexpressions
48  = 	const char *re_endp;	// end pointer for REG_PEND
49  = 	struct re_guts *re_g;	// none of your business :-)
50  = } regex_t;
51  = typedef struct {
52  = 	regoff_t rm_so;		// start of match
53  = 	regoff_t rm_eo;		// end of match
54  = } regmatch_t;
55  */
56 /*
57  * internals of regex_t
58  */
59 #define	MAGIC1	((('r'^0200)<<8) | 'e')
60 
61 /*
62  * The internal representation is a *strip*, a sequence of
63  * operators ending with an endmarker.  (Some terminology etc. is a
64  * historical relic of earlier versions which used multiple strips.)
65  * Certain oddities in the representation are there to permit running
66  * the machinery backwards; in particular, any deviation from sequential
67  * flow must be marked at both its source and its destination.  Some
68  * fine points:
69  *
70  * - OPLUS_ and O_PLUS are *inside* the loop they create.
71  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
72  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
73  *   OOR1 and OOR2 are respectively the end and the beginning of one of
74  *   the branches.  Note that there is an implicit OOR2 following OCH_
75  *   and an implicit OOR1 preceding O_CH.
76  *
77  * In state representations, an operator's bit is on to signify a state
78  * immediately *preceding* "execution" of that operator.
79  */
80 typedef u_int32_t sop;	/* strip operator */
81 typedef int sopno;
82 #define	OPRMASK	((u_int32_t)0xf8000000UL)
83 #define	OPDMASK	((u_int32_t)0x07ffffffUL)
84 #define	OPSHIFT	((unsigned)27)
85 #define	OP(n)	((n)&OPRMASK)
86 #define	OPND(n)	((int)((n)&OPDMASK))
87 #define	SOP(op, opnd)	((op)|(opnd))
88 
89 #define OPC(n)	(((u_int32_t)(n))<<OPSHIFT)
90 /* operators		   meaning	operand			*/
91 /*					(back, fwd are offsets)	*/
92 #define	OEND	OPC(1)	/* endmarker	-			*/
93 #define	OCHAR	OPC(2)	/* character	unsigned char		*/
94 #define	OBOL	OPC(3)	/* left anchor	-			*/
95 #define	OEOL	OPC(4)	/* right anchor	-			*/
96 #define	OANY	OPC(5)	/* .		-			*/
97 #define	OANYOF	OPC(6)	/* [...]	set number		*/
98 #define	OBACK_	OPC(7)	/* begin \d	paren number		*/
99 #define	O_BACK	OPC(8)	/* end \d	paren number		*/
100 #define	OPLUS_	OPC(9)	/* + prefix	fwd to suffix		*/
101 #define	O_PLUS	OPC(10)	/* + suffix	back to prefix		*/
102 #define	OQUEST_	OPC(11)	/* ? prefix	fwd to suffix		*/
103 #define	O_QUEST	OPC(12)	/* ? suffix	back to prefix		*/
104 #define	OLPAREN	OPC(13)	/* (		fwd to )		*/
105 #define	ORPAREN	OPC(14)	/* )		back to (		*/
106 #define	OCH_	OPC(15)	/* begin choice	fwd to OOR2		*/
107 #define	OOR1	OPC(16)	/* | pt. 1	back to OOR1 or OCH_	*/
108 #define	OOR2	OPC(17)	/* | pt. 2	fwd to OOR2 or O_CH	*/
109 #define	O_CH	OPC(18)	/* end choice	back to OOR1		*/
110 #define	OBOW	OPC(19)	/* begin word	-			*/
111 #define	OEOW	OPC(20)	/* end word	-			*/
112 
113 /*
114  * Structure for [] character-set representation.  Character sets are
115  * done as bit vectors, grouped 8 to a byte vector for compactness.
116  * The individual set therefore has both a pointer to the byte vector
117  * and a mask to pick out the relevant bit of each byte.  A hash code
118  * simplifies testing whether two sets could be identical.
119  *
120  * This will get trickier for multicharacter collating elements.  As
121  * preliminary hooks for dealing with such things, we also carry along
122  * a string of multi-character elements, and decide the size of the
123  * vectors at run time.
124  */
125 typedef struct {
126 	uch *ptr;		/* -> uch [csetsize] */
127 	uch mask;		/* bit within array */
128 	uch hash;		/* hash code */
129 	size_t smultis;
130 	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
131 } cset;
132 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
133 #define	CHadd(cs, c)	((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
134 #define	CHsub(cs, c)	((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
135 #define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
136 #define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
137 #define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
138 #define	MCin(p, cs, cp)	mcin(p, cs, cp)
139 
140 /* stuff for character categories */
141 typedef unsigned char cat_t;
142 
143 /*
144  * main compiled-expression structure
145  */
146 struct re_guts {
147 	int magic;
148 #		define	MAGIC2	((('R'^0200)<<8)|'E')
149 	sop *strip;		/* malloced area for strip */
150 	int csetsize;		/* number of bits in a cset vector */
151 	int ncsets;		/* number of csets in use */
152 	cset *sets;		/* -> cset [ncsets] */
153 	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
154 	int cflags;		/* copy of regcomp() cflags argument */
155 	sopno nstates;		/* = number of sops */
156 	sopno firststate;	/* the initial OEND (normally 0) */
157 	sopno laststate;	/* the final OEND */
158 	int iflags;		/* internal flags */
159 #		define	USEBOL	01	/* used ^ */
160 #		define	USEEOL	02	/* used $ */
161 #		define	BAD	04	/* something wrong */
162 	int nbol;		/* number of ^ used */
163 	int neol;		/* number of $ used */
164 	int ncategories;	/* how many character categories */
165 	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
166 	char *must;		/* match must contain this string */
167 	int mlen;		/* length of must */
168 	sopno nsub;		/* copy of re_nsub */
169 	int backrefs;		/* does it use back references? */
170 	sopno nplus;		/* how deep does it nest +s? */
171 	/* catspace must be last */
172 	cat_t catspace[1];	/* actually [NC] */
173 };
174 
175 /* misc utilities */
176 #define	OUT	(CHAR_MAX+1)	/* a non-character value */
177 #define	ISWORD(c)	(isalnum(c) || (c) == '_')
178