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