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