11573Srgrimes/*-
21573Srgrimes * Copyright (c) 1992, 1993, 1994 Henry Spencer.
31573Srgrimes * Copyright (c) 1992, 1993, 1994
41573Srgrimes *	The Regents of the University of California.  All rights reserved.
51573Srgrimes *
61573Srgrimes * This code is derived from software contributed to Berkeley by
71573Srgrimes * Henry Spencer.
81573Srgrimes *
91573Srgrimes * Redistribution and use in source and binary forms, with or without
101573Srgrimes * modification, are permitted provided that the following conditions
111573Srgrimes * are met:
121573Srgrimes * 1. Redistributions of source code must retain the above copyright
131573Srgrimes *    notice, this list of conditions and the following disclaimer.
141573Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
151573Srgrimes *    notice, this list of conditions and the following disclaimer in the
161573Srgrimes *    documentation and/or other materials provided with the distribution.
171573Srgrimes * 4. Neither the name of the University nor the names of its contributors
181573Srgrimes *    may be used to endorse or promote products derived from this software
191573Srgrimes *    without specific prior written permission.
201573Srgrimes *
211573Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221573Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231573Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241573Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251573Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261573Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271573Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281573Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291573Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301573Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311573Srgrimes * SUCH DAMAGE.
321573Srgrimes *
331573Srgrimes *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
3462232Sdcs * $FreeBSD$
351573Srgrimes */
361573Srgrimes
371573Srgrimes/*
381573Srgrimes * First, the stuff that ends up in the outside-world include file
391573Srgrimes = typedef off_t regoff_t;
401573Srgrimes = typedef struct {
411573Srgrimes = 	int re_magic;
421573Srgrimes = 	size_t re_nsub;		// number of parenthesized subexpressions
431573Srgrimes = 	const char *re_endp;	// end pointer for REG_PEND
441573Srgrimes = 	struct re_guts *re_g;	// none of your business :-)
451573Srgrimes = } regex_t;
461573Srgrimes = typedef struct {
471573Srgrimes = 	regoff_t rm_so;		// start of match
481573Srgrimes = 	regoff_t rm_eo;		// end of match
491573Srgrimes = } regmatch_t;
501573Srgrimes */
511573Srgrimes/*
521573Srgrimes * internals of regex_t
531573Srgrimes */
541573Srgrimes#define	MAGIC1	((('r'^0200)<<8) | 'e')
551573Srgrimes
561573Srgrimes/*
571573Srgrimes * The internal representation is a *strip*, a sequence of
581573Srgrimes * operators ending with an endmarker.  (Some terminology etc. is a
591573Srgrimes * historical relic of earlier versions which used multiple strips.)
601573Srgrimes * Certain oddities in the representation are there to permit running
611573Srgrimes * the machinery backwards; in particular, any deviation from sequential
621573Srgrimes * flow must be marked at both its source and its destination.  Some
631573Srgrimes * fine points:
641573Srgrimes *
651573Srgrimes * - OPLUS_ and O_PLUS are *inside* the loop they create.
661573Srgrimes * - OQUEST_ and O_QUEST are *outside* the bypass they create.
671573Srgrimes * - OCH_ and O_CH are *outside* the multi-way branch they create, while
681573Srgrimes *   OOR1 and OOR2 are respectively the end and the beginning of one of
691573Srgrimes *   the branches.  Note that there is an implicit OOR2 following OCH_
701573Srgrimes *   and an implicit OOR1 preceding O_CH.
711573Srgrimes *
721573Srgrimes * In state representations, an operator's bit is on to signify a state
731573Srgrimes * immediately *preceding* "execution" of that operator.
741573Srgrimes */
751573Srgrimestypedef unsigned long sop;	/* strip operator */
761573Srgrimestypedef long sopno;
7736043Sjb#define	OPRMASK	0xf8000000L
7836043Sjb#define	OPDMASK	0x07ffffffL
791573Srgrimes#define	OPSHIFT	((unsigned)27)
801573Srgrimes#define	OP(n)	((n)&OPRMASK)
811573Srgrimes#define	OPND(n)	((n)&OPDMASK)
821573Srgrimes#define	SOP(op, opnd)	((op)|(opnd))
831573Srgrimes/* operators			   meaning	operand			*/
841573Srgrimes/*						(back, fwd are offsets)	*/
8536043Sjb#define	OEND	(1L<<OPSHIFT)	/* endmarker	-			*/
86132019Stjr#define	OCHAR	(2L<<OPSHIFT)	/* character	wide character		*/
8736043Sjb#define	OBOL	(3L<<OPSHIFT)	/* left anchor	-			*/
8836043Sjb#define	OEOL	(4L<<OPSHIFT)	/* right anchor	-			*/
8936043Sjb#define	OANY	(5L<<OPSHIFT)	/* .		-			*/
9036043Sjb#define	OANYOF	(6L<<OPSHIFT)	/* [...]	set number		*/
9136043Sjb#define	OBACK_	(7L<<OPSHIFT)	/* begin \d	paren number		*/
9236043Sjb#define	O_BACK	(8L<<OPSHIFT)	/* end \d	paren number		*/
9336043Sjb#define	OPLUS_	(9L<<OPSHIFT)	/* + prefix	fwd to suffix		*/
9436043Sjb#define	O_PLUS	(10L<<OPSHIFT)	/* + suffix	back to prefix		*/
9536043Sjb#define	OQUEST_	(11L<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
9636043Sjb#define	O_QUEST	(12L<<OPSHIFT)	/* ? suffix	back to prefix		*/
9736043Sjb#define	OLPAREN	(13L<<OPSHIFT)	/* (		fwd to )		*/
9836043Sjb#define	ORPAREN	(14L<<OPSHIFT)	/* )		back to (		*/
9936043Sjb#define	OCH_	(15L<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
10036043Sjb#define	OOR1	(16L<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
10136043Sjb#define	OOR2	(17L<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
10236043Sjb#define	O_CH	(18L<<OPSHIFT)	/* end choice	back to OOR1		*/
10336043Sjb#define	OBOW	(19L<<OPSHIFT)	/* begin word	-			*/
10436043Sjb#define	OEOW	(20L<<OPSHIFT)	/* end word	-			*/
1051573Srgrimes
1061573Srgrimes/*
107132019Stjr * Structures for [] character-set representation.
1081573Srgrimes */
1091573Srgrimestypedef struct {
110132019Stjr	wint_t		min;
111132019Stjr	wint_t		max;
112132019Stjr} crange;
113132019Stjrtypedef struct {
114132019Stjr	unsigned char	bmp[NC / 8];
115132019Stjr	wctype_t	*types;
116132019Stjr	int		ntypes;
117132019Stjr	wint_t		*wides;
118132019Stjr	int		nwides;
119132019Stjr	crange		*ranges;
120132019Stjr	int		nranges;
121132019Stjr	int		invert;
122132019Stjr	int		icase;
1231573Srgrimes} cset;
1241573Srgrimes
125132019Stjrstatic int
126150053SstefanfCHIN1(cset *cs, wint_t ch)
127132019Stjr{
128132019Stjr	int i;
129132019Stjr
130132019Stjr	assert(ch >= 0);
131132019Stjr	if (ch < NC)
132132019Stjr		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
133132019Stjr		    cs->invert);
134132019Stjr	for (i = 0; i < cs->nwides; i++)
135132019Stjr		if (ch == cs->wides[i])
136132019Stjr			return (!cs->invert);
137132019Stjr	for (i = 0; i < cs->nranges; i++)
138132019Stjr		if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
139132019Stjr			return (!cs->invert);
140132019Stjr	for (i = 0; i < cs->ntypes; i++)
141132019Stjr		if (iswctype(ch, cs->types[i]))
142132019Stjr			return (!cs->invert);
143132019Stjr	return (cs->invert);
144132019Stjr}
145132019Stjr
146132019Stjrstatic __inline int
147150053SstefanfCHIN(cset *cs, wint_t ch)
148132019Stjr{
149132019Stjr
150132019Stjr	assert(ch >= 0);
151132019Stjr	if (ch < NC)
152132019Stjr		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
153132019Stjr		    cs->invert);
154132019Stjr	else if (cs->icase)
155132019Stjr		return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
156132019Stjr		    CHIN1(cs, towupper(ch)));
157132019Stjr	else
158132019Stjr		return (CHIN1(cs, ch));
159132019Stjr}
160132019Stjr
1611573Srgrimes/*
1621573Srgrimes * main compiled-expression structure
1631573Srgrimes */
1641573Srgrimesstruct re_guts {
1651573Srgrimes	int magic;
1661573Srgrimes#		define	MAGIC2	((('R'^0200)<<8)|'E')
1671573Srgrimes	sop *strip;		/* malloced area for strip */
1681573Srgrimes	int ncsets;		/* number of csets in use */
1691573Srgrimes	cset *sets;		/* -> cset [ncsets] */
1701573Srgrimes	int cflags;		/* copy of regcomp() cflags argument */
1711573Srgrimes	sopno nstates;		/* = number of sops */
1721573Srgrimes	sopno firststate;	/* the initial OEND (normally 0) */
1731573Srgrimes	sopno laststate;	/* the final OEND */
1741573Srgrimes	int iflags;		/* internal flags */
1751573Srgrimes#		define	USEBOL	01	/* used ^ */
1761573Srgrimes#		define	USEEOL	02	/* used $ */
1771573Srgrimes#		define	BAD	04	/* something wrong */
1781573Srgrimes	int nbol;		/* number of ^ used */
1791573Srgrimes	int neol;		/* number of $ used */
1801573Srgrimes	char *must;		/* match must contain this string */
18162391Sdcs	int moffset;		/* latest point at which must may be located */
18262232Sdcs	int *charjump;		/* Boyer-Moore char jump table */
18362232Sdcs	int *matchjump;		/* Boyer-Moore match jump table */
1841573Srgrimes	int mlen;		/* length of must */
1851573Srgrimes	size_t nsub;		/* copy of re_nsub */
1861573Srgrimes	int backrefs;		/* does it use back references? */
1871573Srgrimes	sopno nplus;		/* how deep does it nest +s? */
1881573Srgrimes};
1891573Srgrimes
1901573Srgrimes/* misc utilities */
191149009Stjr#define	OUT	(CHAR_MIN - 1)	/* a non-character value */
192132019Stjr#define ISWORD(c)       (iswalnum((uch)(c)) || (c) == '_')
193