xref: /freebsd/contrib/nvi/regex/regex2.h (revision 5f4c09dd85bff675e0ca63c55ea3c517e0fddfcc)
1 /*	$NetBSD: regex2.h,v 1.5 2011/11/23 15:43:39 tnozaki 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 of the University of Toronto.
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.3 (Berkeley) 3/16/94
36  */
37 
38 /*
39  * First, the stuff that ends up in the outside-world include file
40  = typedef off_t regoff_t;
41  = typedef struct {
42  = 	int re_magic;
43  = 	size_t re_nsub;		// number of parenthesized subexpressions
44  = 	const char *re_endp;	// end pointer for REG_PEND
45  = 	struct re_guts *re_g;	// none of your business :-)
46  = } regex_t;
47  = typedef struct {
48  = 	regoff_t rm_so;		// start of match
49  = 	regoff_t rm_eo;		// end of match
50  = } regmatch_t;
51  */
52 /*
53  * internals of regex_t
54  */
55 #define	MAGIC1	((('r'^0200)<<8) | 'e')
56 
57 /*
58  * The internal representation is a *strip*, a sequence of
59  * operators ending with an endmarker.  (Some terminology etc. is a
60  * historical relic of earlier versions which used multiple strips.)
61  * Certain oddities in the representation are there to permit running
62  * the machinery backwards; in particular, any deviation from sequential
63  * flow must be marked at both its source and its destination.  Some
64  * fine points:
65  *
66  * - OPLUS_ and O_PLUS are *inside* the loop they create.
67  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
68  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
69  *   OOR1 and OOR2 are respectively the end and the beginning of one of
70  *   the branches.  Note that there is an implicit OOR2 following OCH_
71  *   and an implicit OOR1 preceding O_CH.
72  *
73  * In state representations, an operator's bit is on to signify a state
74  * immediately *preceding* "execution" of that operator.
75  */
76 typedef char sop;	/* strip operator */
77 typedef size_t sopno;
78 /* operators			   meaning	operand			*/
79 /*						(back, fwd are offsets)	*/
80 #define	OEND	(1)		/* endmarker	-			*/
81 #define	OCHAR	(2)		/* character	unsigned char		*/
82 #define	OBOL	(3)		/* left anchor	-			*/
83 #define	OEOL	(4)		/* right anchor	-			*/
84 #define	OANY	(5)		/* .		-			*/
85 #define	OANYOF	(6)		/* [...]	set number		*/
86 #define	OBACK_	(7)		/* begin \d	paren number		*/
87 #define	O_BACK	(8)		/* end \d	paren number		*/
88 #define	OPLUS_	(9)		/* + prefix	fwd to suffix		*/
89 #define	O_PLUS	(10)		/* + suffix	back to prefix		*/
90 #define	OQUEST_	(11)		/* ? prefix	fwd to suffix		*/
91 #define	O_QUEST	(12)		/* ? suffix	back to prefix		*/
92 #define	OLPAREN	(13)		/* (		fwd to )		*/
93 #define	ORPAREN	(14)		/* )		back to (		*/
94 #define	OCH_	(15)		/* begin choice	fwd to OOR2		*/
95 #define	OOR1	(16)		/* | pt. 1	back to OOR1 or OCH_	*/
96 #define	OOR2	(17)		/* | pt. 2	fwd to OOR2 or O_CH	*/
97 #define	O_CH	(18)		/* end choice	back to OOR1		*/
98 #define	OBOW	(19)		/* begin word	-			*/
99 #define	OEOW	(20)		/* end word	-			*/
100 
101 /*
102  * Structure for [] character-set representation.  Character sets are
103  * done as bit vectors, grouped 8 to a byte vector for compactness.
104  * The individual set therefore has both a pointer to the byte vector
105  * and a mask to pick out the relevant bit of each byte.  A hash code
106  * simplifies testing whether two sets could be identical.
107  *
108  * This will get trickier for multicharacter collating elements.  As
109  * preliminary hooks for dealing with such things, we also carry along
110  * a string of multi-character elements, and decide the size of the
111  * vectors at run time.
112  */
113 typedef struct {
114 	uch *ptr;		/* -> uch [csetsize] */
115 	uch mask;		/* bit within array */
116 	uch hash;		/* hash code */
117 	size_t smultis;
118 	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
119 } cset;
120 /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
121 #define	CHadd(cs, c)	((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
122 #define	CHsub(cs, c)	((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
123 #define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
124 #define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
125 #define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
126 #define	MCin(p, cs, cp)	mcin(p, cs, cp)
127 
128 /* stuff for character categories */
129 typedef RCHAR_T cat_t;
130 
131 /*
132  * main compiled-expression structure
133  */
134 struct re_guts {
135 	int magic;
136 #		define	MAGIC2	((('R'^0200)<<8)|'E')
137 	sop *strip;		/* malloced area for strip */
138 	RCHAR_T *stripdata;	/* malloced area for stripdata */
139 	size_t csetsize;	/* number of bits in a cset vector */
140 	size_t ncsets;		/* number of csets in use */
141 	cset *sets;		/* -> cset [ncsets] */
142 	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
143 	int cflags;		/* copy of regcomp() cflags argument */
144 	sopno nstates;		/* = number of sops */
145 	sopno firststate;	/* the initial OEND (normally 0) */
146 	sopno laststate;	/* the final OEND */
147 	int iflags;		/* internal flags */
148 #		define	USEBOL	01	/* used ^ */
149 #		define	USEEOL	02	/* used $ */
150 #		define	BAD	04	/* something wrong */
151 	size_t nbol;		/* number of ^ used */
152 	size_t neol;		/* number of $ used */
153 #if 0
154 	size_t ncategories;	/* how many character categories */
155 	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
156 #endif
157 	RCHAR_T *must;		/* match must contain this string */
158 	size_t mlen;		/* length of must */
159 	size_t nsub;		/* copy of re_nsub */
160 	int backrefs;		/* does it use back references? */
161 	sopno nplus;		/* how deep does it nest +s? */
162 	/* catspace must be last */
163 #if 0
164 	cat_t catspace[1];	/* actually [NC] */
165 #endif
166 };
167 
168 /* misc utilities */
169 #define OUT	REOF	/* a non-character value */
170 #define	ISWORD(c) ((c) == '_' || (ISGRAPH((UCHAR_T)c) && !ISPUNCT((UCHAR_T)c)))
171