1198090Srdivacky/*-
2198090Srdivacky * This code is derived from OpenBSD's libc/regex, original license follows:
3198090Srdivacky *
4198090Srdivacky * Copyright (c) 1992, 1993, 1994 Henry Spencer.
5198090Srdivacky * Copyright (c) 1992, 1993, 1994
6198090Srdivacky *	The Regents of the University of California.  All rights reserved.
7198090Srdivacky *
8198090Srdivacky * This code is derived from software contributed to Berkeley by
9198090Srdivacky * Henry Spencer.
10198090Srdivacky *
11198090Srdivacky * Redistribution and use in source and binary forms, with or without
12198090Srdivacky * modification, are permitted provided that the following conditions
13198090Srdivacky * are met:
14198090Srdivacky * 1. Redistributions of source code must retain the above copyright
15198090Srdivacky *    notice, this list of conditions and the following disclaimer.
16198090Srdivacky * 2. Redistributions in binary form must reproduce the above copyright
17198090Srdivacky *    notice, this list of conditions and the following disclaimer in the
18198090Srdivacky *    documentation and/or other materials provided with the distribution.
19198090Srdivacky * 3. Neither the name of the University nor the names of its contributors
20198090Srdivacky *    may be used to endorse or promote products derived from this software
21198090Srdivacky *    without specific prior written permission.
22198090Srdivacky *
23198090Srdivacky * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24198090Srdivacky * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25198090Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26198090Srdivacky * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27198090Srdivacky * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28198090Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29198090Srdivacky * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30198090Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31198090Srdivacky * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32198090Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33198090Srdivacky * SUCH DAMAGE.
34198090Srdivacky *
35198090Srdivacky *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
36198090Srdivacky */
37198090Srdivacky
38198090Srdivacky/*
39198090Srdivacky * internals of regex_t
40198090Srdivacky */
41198090Srdivacky#define	MAGIC1	((('r'^0200)<<8) | 'e')
42198090Srdivacky
43198090Srdivacky/*
44198090Srdivacky * The internal representation is a *strip*, a sequence of
45198090Srdivacky * operators ending with an endmarker.  (Some terminology etc. is a
46198090Srdivacky * historical relic of earlier versions which used multiple strips.)
47198090Srdivacky * Certain oddities in the representation are there to permit running
48198090Srdivacky * the machinery backwards; in particular, any deviation from sequential
49198090Srdivacky * flow must be marked at both its source and its destination.  Some
50198090Srdivacky * fine points:
51198090Srdivacky *
52198090Srdivacky * - OPLUS_ and O_PLUS are *inside* the loop they create.
53198090Srdivacky * - OQUEST_ and O_QUEST are *outside* the bypass they create.
54198090Srdivacky * - OCH_ and O_CH are *outside* the multi-way branch they create, while
55198090Srdivacky *   OOR1 and OOR2 are respectively the end and the beginning of one of
56198090Srdivacky *   the branches.  Note that there is an implicit OOR2 following OCH_
57198090Srdivacky *   and an implicit OOR1 preceding O_CH.
58198090Srdivacky *
59198090Srdivacky * In state representations, an operator's bit is on to signify a state
60198090Srdivacky * immediately *preceding* "execution" of that operator.
61198090Srdivacky */
62198090Srdivackytypedef unsigned long sop;	/* strip operator */
63198090Srdivackytypedef long sopno;
64198090Srdivacky#define	OPRMASK	0xf8000000LU
65198090Srdivacky#define	OPDMASK	0x07ffffffLU
66198090Srdivacky#define	OPSHIFT	((unsigned)27)
67198090Srdivacky#define	OP(n)	((n)&OPRMASK)
68198090Srdivacky#define	OPND(n)	((n)&OPDMASK)
69198090Srdivacky#define	SOP(op, opnd)	((op)|(opnd))
70198090Srdivacky/* operators			   meaning	operand			*/
71198090Srdivacky/*						(back, fwd are offsets)	*/
72198090Srdivacky#define	OEND	(1LU<<OPSHIFT)	/* endmarker	-			*/
73198090Srdivacky#define	OCHAR	(2LU<<OPSHIFT)	/* character	unsigned char		*/
74198090Srdivacky#define	OBOL	(3LU<<OPSHIFT)	/* left anchor	-			*/
75198090Srdivacky#define	OEOL	(4LU<<OPSHIFT)	/* right anchor	-			*/
76198090Srdivacky#define	OANY	(5LU<<OPSHIFT)	/* .		-			*/
77198090Srdivacky#define	OANYOF	(6LU<<OPSHIFT)	/* [...]	set number		*/
78198090Srdivacky#define	OBACK_	(7LU<<OPSHIFT)	/* begin \d	paren number		*/
79198090Srdivacky#define	O_BACK	(8LU<<OPSHIFT)	/* end \d	paren number		*/
80198090Srdivacky#define	OPLUS_	(9LU<<OPSHIFT)	/* + prefix	fwd to suffix		*/
81198090Srdivacky#define	O_PLUS	(10LU<<OPSHIFT)	/* + suffix	back to prefix		*/
82198090Srdivacky#define	OQUEST_	(11LU<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
83198090Srdivacky#define	O_QUEST	(12LU<<OPSHIFT)	/* ? suffix	back to prefix		*/
84198090Srdivacky#define	OLPAREN	(13LU<<OPSHIFT)	/* (		fwd to )		*/
85198090Srdivacky#define	ORPAREN	(14LU<<OPSHIFT)	/* )		back to (		*/
86198090Srdivacky#define	OCH_	(15LU<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
87198090Srdivacky#define	OOR1	(16LU<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
88198090Srdivacky#define	OOR2	(17LU<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
89198090Srdivacky#define	O_CH	(18LU<<OPSHIFT)	/* end choice	back to OOR1		*/
90198090Srdivacky#define	OBOW	(19LU<<OPSHIFT)	/* begin word	-			*/
91198090Srdivacky#define	OEOW	(20LU<<OPSHIFT)	/* end word	-			*/
92198090Srdivacky
93198090Srdivacky/*
94198090Srdivacky * Structure for [] character-set representation.  Character sets are
95198090Srdivacky * done as bit vectors, grouped 8 to a byte vector for compactness.
96198090Srdivacky * The individual set therefore has both a pointer to the byte vector
97198090Srdivacky * and a mask to pick out the relevant bit of each byte.  A hash code
98198090Srdivacky * simplifies testing whether two sets could be identical.
99198090Srdivacky *
100198090Srdivacky * This will get trickier for multicharacter collating elements.  As
101198090Srdivacky * preliminary hooks for dealing with such things, we also carry along
102198090Srdivacky * a string of multi-character elements, and decide the size of the
103198090Srdivacky * vectors at run time.
104198090Srdivacky */
105198090Srdivackytypedef struct {
106198090Srdivacky	uch *ptr;		/* -> uch [csetsize] */
107198090Srdivacky	uch mask;		/* bit within array */
108198090Srdivacky	uch hash;		/* hash code */
109198090Srdivacky	size_t smultis;
110198090Srdivacky	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
111198090Srdivacky} cset;
112198090Srdivacky/* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
113198090Srdivacky#define	CHadd(cs, c)	((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
114198090Srdivacky#define	CHsub(cs, c)	((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
115198090Srdivacky#define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
116198090Srdivacky#define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* llvm_regcomp() internal fns */
117198090Srdivacky#define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
118198090Srdivacky#define	MCin(p, cs, cp)	mcin(p, cs, cp)
119198090Srdivacky
120198090Srdivacky/* stuff for character categories */
121198090Srdivackytypedef unsigned char cat_t;
122198090Srdivacky
123198090Srdivacky/*
124198090Srdivacky * main compiled-expression structure
125198090Srdivacky */
126198090Srdivackystruct re_guts {
127198090Srdivacky	int magic;
128198090Srdivacky#		define	MAGIC2	((('R'^0200)<<8)|'E')
129198090Srdivacky	sop *strip;		/* malloced area for strip */
130198090Srdivacky	int csetsize;		/* number of bits in a cset vector */
131198090Srdivacky	int ncsets;		/* number of csets in use */
132198090Srdivacky	cset *sets;		/* -> cset [ncsets] */
133198090Srdivacky	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
134198090Srdivacky	int cflags;		/* copy of llvm_regcomp() cflags argument */
135198090Srdivacky	sopno nstates;		/* = number of sops */
136198090Srdivacky	sopno firststate;	/* the initial OEND (normally 0) */
137198090Srdivacky	sopno laststate;	/* the final OEND */
138198090Srdivacky	int iflags;		/* internal flags */
139198090Srdivacky#		define	USEBOL	01	/* used ^ */
140198090Srdivacky#		define	USEEOL	02	/* used $ */
141198090Srdivacky#		define	REGEX_BAD	04	/* something wrong */
142198090Srdivacky	int nbol;		/* number of ^ used */
143198090Srdivacky	int neol;		/* number of $ used */
144198090Srdivacky	int ncategories;	/* how many character categories */
145198090Srdivacky	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
146198090Srdivacky	char *must;		/* match must contain this string */
147198090Srdivacky	int mlen;		/* length of must */
148198090Srdivacky	size_t nsub;		/* copy of re_nsub */
149198090Srdivacky	int backrefs;		/* does it use back references? */
150198090Srdivacky	sopno nplus;		/* how deep does it nest +s? */
151198090Srdivacky	/* catspace must be last */
152198090Srdivacky	cat_t catspace[1];	/* actually [NC] */
153198090Srdivacky};
154198090Srdivacky
155198090Srdivacky/* misc utilities */
156198090Srdivacky#define	OUT	(CHAR_MAX+1)	/* a non-character value */
157198090Srdivacky#define	ISWORD(c)	(isalnum(c&0xff) || (c) == '_')
158