xref: /csrg-svn/lib/libc/regex/regex2.h (revision 55850)
1*55850Sbostic /*-
2*55850Sbostic  * Copyright (c) 1992 Henry Spencer.
3*55850Sbostic  * Copyright (c) 1992 The Regents of the University of California.
4*55850Sbostic  * All rights reserved.
5*55850Sbostic  *
6*55850Sbostic  * This code is derived from software contributed to Berkeley by
7*55850Sbostic  * Henry Spencer of the University of Toronto.
8*55850Sbostic  *
9*55850Sbostic  * %sccs.include.redist.c%
10*55850Sbostic  *
11*55850Sbostic  *	@(#)regex2.h	5.1 (Berkeley) 08/06/92
12*55850Sbostic  */
13*55850Sbostic 
14*55850Sbostic /*
15*55850Sbostic  * internals of regex_t
16*55850Sbostic  */
17*55850Sbostic #define	MAGIC1	((('r'^0200)<<8) | 'e')
18*55850Sbostic 
19*55850Sbostic /*
20*55850Sbostic  * The internal representation is a *strip*, a sequence of
21*55850Sbostic  * operators ending with an endmarker.  (Some terminology etc. is a
22*55850Sbostic  * historical relic of earlier versions which used multiple strips.)
23*55850Sbostic  * Certain oddities in the representation are there to permit running
24*55850Sbostic  * the machinery backwards; in particular, any deviation from sequential
25*55850Sbostic  * flow must be marked at both its source and its destination.  Some
26*55850Sbostic  * fine points:
27*55850Sbostic  *
28*55850Sbostic  * - OPLUS_ and O_PLUS are *inside* the loop they create.
29*55850Sbostic  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
30*55850Sbostic  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
31*55850Sbostic  *   OOR1 and OOR2 are respectively the end and the beginning of one of
32*55850Sbostic  *   the branches.  Note that there is an implicit OOR2 following OCH_
33*55850Sbostic  *   and an implicit OOR1 preceding O_CH.
34*55850Sbostic  *
35*55850Sbostic  * In state representations, an operator's bit is on to signify a state
36*55850Sbostic  * immediately *preceding* "execution" of that operator.
37*55850Sbostic  */
38*55850Sbostic typedef unsigned long sop;	/* strip operator */
39*55850Sbostic typedef long sopno;
40*55850Sbostic #define	OPRMASK	0xf8000000
41*55850Sbostic #define	OPDMASK	0x07ffffff
42*55850Sbostic #define	OPSHIFT	27
43*55850Sbostic #define	OP(n)	((n)&OPRMASK)
44*55850Sbostic #define	OPND(n)	((n)&OPDMASK)
45*55850Sbostic #define	SOP(op, opnd)	((op)|(opnd))
46*55850Sbostic /* operators			   meaning	operand			*/
47*55850Sbostic /*						(back, fwd are offsets)	*/
48*55850Sbostic #define	OEND	(1<<OPSHIFT)	/* endmarker	-			*/
49*55850Sbostic #define	OCHAR	(2<<OPSHIFT)	/* character	unsigned char		*/
50*55850Sbostic #define	OBOL	(3<<OPSHIFT)	/* left anchor	-			*/
51*55850Sbostic #define	OEOL	(4<<OPSHIFT)	/* right anchor	-			*/
52*55850Sbostic #define	OANY	(5<<OPSHIFT)	/* .		-			*/
53*55850Sbostic #define	OANYOF	(6<<OPSHIFT)	/* [...]	set number		*/
54*55850Sbostic #define	OBACK_	(7<<OPSHIFT)	/* begin \d	paren number		*/
55*55850Sbostic #define	O_BACK	(8<<OPSHIFT)	/* end \d	paren number		*/
56*55850Sbostic #define	OPLUS_	(9<<OPSHIFT)	/* + prefix	fwd to suffix		*/
57*55850Sbostic #define	O_PLUS	(10<<OPSHIFT)	/* + suffix	back to prefix		*/
58*55850Sbostic #define	OQUEST_	(11<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
59*55850Sbostic #define	O_QUEST	(12<<OPSHIFT)	/* ? suffix	back to prefix		*/
60*55850Sbostic #define	OLPAREN	(13<<OPSHIFT)	/* (		fwd to )		*/
61*55850Sbostic #define	ORPAREN	(14<<OPSHIFT)	/* )		back to (		*/
62*55850Sbostic #define	OCH_	(15<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
63*55850Sbostic #define	OOR1	(16<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
64*55850Sbostic #define	OOR2	(17<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
65*55850Sbostic #define	O_CH	(18<<OPSHIFT)	/* end choice	back to OOR1		*/
66*55850Sbostic 
67*55850Sbostic /*
68*55850Sbostic  * Structure for [] character-set representation.  Character sets are
69*55850Sbostic  * done as bit vectors, grouped 8 to a byte vector for compactness.
70*55850Sbostic  * The individual set therefore has both a pointer to the byte vector
71*55850Sbostic  * and a mask to pick out the relevant bit of each byte.  A hash code
72*55850Sbostic  * simplifies testing whether two sets could be identical.
73*55850Sbostic  *
74*55850Sbostic  * This will get trickier for multicharacter collating elements.  As
75*55850Sbostic  * preliminary hooks for dealing with such things, we also carry along
76*55850Sbostic  * a string of multi-character elements, and decide the size of the
77*55850Sbostic  * vectors at run time.
78*55850Sbostic  */
79*55850Sbostic typedef struct {
80*55850Sbostic 	uchar *ptr;		/* -> uchar [csetsize] */
81*55850Sbostic 	uchar mask;		/* bit within array */
82*55850Sbostic 	uchar hash;		/* hash code */
83*55850Sbostic 	size_t smultis;
84*55850Sbostic 	uchar *multis;		/* -> uchar[smulti]  ab\0cd\0ef\0\0 */
85*55850Sbostic } cset;
86*55850Sbostic /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
87*55850Sbostic #define	CHadd(cs, c)	((cs)->ptr[(c)] |= (cs)->mask, (cs)->hash += (c))
88*55850Sbostic #define	CHsub(cs, c)	((cs)->ptr[(c)] &= ~(cs)->mask, (cs)->hash -= (c))
89*55850Sbostic #define	CHIN(cs, c)	((cs)->ptr[(c)] & (cs)->mask)
90*55850Sbostic #define	MCadd(cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
91*55850Sbostic #define	MCsub(cs, cp)	mcsub(p, cs, cp)
92*55850Sbostic #define	MCin(cs, cp)	mcin(p, cs, cp)
93*55850Sbostic 
94*55850Sbostic /*
95*55850Sbostic  * main compiled-expression structure
96*55850Sbostic  */
97*55850Sbostic struct re_guts {
98*55850Sbostic 	int magic;
99*55850Sbostic #		define	MAGIC2	((('R'^0200)<<8)|'E')
100*55850Sbostic 	sop *strip;		/* malloced area for strip */
101*55850Sbostic 	int csetsize;		/* number of bits in a cset vector */
102*55850Sbostic 	int ncsets;		/* number of csets in use */
103*55850Sbostic 	cset *sets;		/* -> cset [ncsets] */
104*55850Sbostic 	uchar *setbits;		/* -> uchar[csetsize][ncsets/CHAR_BIT] */
105*55850Sbostic 	int cflags;		/* copy of regcomp() cflags argument */
106*55850Sbostic 	sopno nstates;		/* = number of sops */
107*55850Sbostic 	sopno firststate;	/* the initial OEND (normally 0) */
108*55850Sbostic 	sopno laststate;	/* the final OEND */
109*55850Sbostic 	int iflags;		/* internal flags */
110*55850Sbostic #		define	USEBOL	01	/* used ^ */
111*55850Sbostic #		define	USEEOL	02	/* used $ */
112*55850Sbostic #		define	BAD	04	/* something wrong */
113*55850Sbostic 	int ncategories;	/* how many character categories */
114*55850Sbostic 	uchar *categories;	/* -> uchar[NUC] */
115*55850Sbostic 	char *must;		/* match must contain this string */
116*55850Sbostic 	int mlen;		/* length of must */
117*55850Sbostic 	size_t nsub;		/* copy of re_nsub */
118*55850Sbostic 	int backrefs;		/* does it use back references? */
119*55850Sbostic 	sopno nplus;		/* how deep does it nest +s? */
120*55850Sbostic };
121