14fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*	$OpenBSD: regex2.h,v 1.7 2004/11/30 17:04:23 otto Exp $	*/
24fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
34fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*-
44fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Copyright (c) 1992, 1993, 1994 Henry Spencer.
54fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Copyright (c) 1992, 1993, 1994
64fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *	The Regents of the University of California.  All rights reserved.
74fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
84fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * This code is derived from software contributed to Berkeley by
94fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Henry Spencer.
104fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
114fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Redistribution and use in source and binary forms, with or without
124fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * modification, are permitted provided that the following conditions
134fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * are met:
144fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * 1. Redistributions of source code must retain the above copyright
154fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    notice, this list of conditions and the following disclaimer.
164fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * 2. Redistributions in binary form must reproduce the above copyright
174fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    notice, this list of conditions and the following disclaimer in the
184fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    documentation and/or other materials provided with the distribution.
194fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * 3. Neither the name of the University nor the names of its contributors
204fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    may be used to endorse or promote products derived from this software
214fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *    without specific prior written permission.
224fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
234fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
244fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
254fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
264fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
274fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
284fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
294fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
304fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
314fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
324fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
334fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * SUCH DAMAGE.
344fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
354fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
364fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
374fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
384fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*
394fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * internals of regex_t
404fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
414fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	MAGIC1	((('r'^0200)<<8) | 'e')
424fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
434fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*
444fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * The internal representation is a *strip*, a sequence of
454fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * operators ending with an endmarker.  (Some terminology etc. is a
464fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * historical relic of earlier versions which used multiple strips.)
474fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Certain oddities in the representation are there to permit running
484fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * the machinery backwards; in particular, any deviation from sequential
494fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * flow must be marked at both its source and its destination.  Some
504fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * fine points:
514fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
524fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * - OPLUS_ and O_PLUS are *inside* the loop they create.
534fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * - OQUEST_ and O_QUEST are *outside* the bypass they create.
544fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * - OCH_ and O_CH are *outside* the multi-way branch they create, while
554fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *   OOR1 and OOR2 are respectively the end and the beginning of one of
564fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *   the branches.  Note that there is an implicit OOR2 following OCH_
574fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *   and an implicit OOR1 preceding O_CH.
584fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
594fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * In state representations, an operator's bit is on to signify a state
604fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * immediately *preceding* "execution" of that operator.
614fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
624fa7b105644222d9b35347c9d226ca8e011072ebColin Crosstypedef unsigned long sop;	/* strip operator */
634fa7b105644222d9b35347c9d226ca8e011072ebColin Crosstypedef long sopno;
644fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OPRMASK	0xf8000000LU
654fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OPDMASK	0x07ffffffLU
664fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OPSHIFT	((unsigned)27)
674fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OP(n)	((n)&OPRMASK)
684fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OPND(n)	((n)&OPDMASK)
694fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	SOP(op, opnd)	((op)|(opnd))
704fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* operators			   meaning	operand			*/
714fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*						(back, fwd are offsets)	*/
724fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OEND	(1LU<<OPSHIFT)	/* endmarker	-			*/
734fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OCHAR	(2LU<<OPSHIFT)	/* character	unsigned char		*/
744fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OBOL	(3LU<<OPSHIFT)	/* left anchor	-			*/
754fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OEOL	(4LU<<OPSHIFT)	/* right anchor	-			*/
764fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OANY	(5LU<<OPSHIFT)	/* .		-			*/
774fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OANYOF	(6LU<<OPSHIFT)	/* [...]	set number		*/
784fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OBACK_	(7LU<<OPSHIFT)	/* begin \d	paren number		*/
794fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	O_BACK	(8LU<<OPSHIFT)	/* end \d	paren number		*/
804fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OPLUS_	(9LU<<OPSHIFT)	/* + prefix	fwd to suffix		*/
814fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	O_PLUS	(10LU<<OPSHIFT)	/* + suffix	back to prefix		*/
824fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OQUEST_	(11LU<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
834fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	O_QUEST	(12LU<<OPSHIFT)	/* ? suffix	back to prefix		*/
844fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OLPAREN	(13LU<<OPSHIFT)	/* (		fwd to )		*/
854fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ORPAREN	(14LU<<OPSHIFT)	/* )		back to (		*/
864fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OCH_	(15LU<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
874fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OOR1	(16LU<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
884fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OOR2	(17LU<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
894fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	O_CH	(18LU<<OPSHIFT)	/* end choice	back to OOR1		*/
904fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OBOW	(19LU<<OPSHIFT)	/* begin word	-			*/
914fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OEOW	(20LU<<OPSHIFT)	/* end word	-			*/
924fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
934fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*
944fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * Structure for [] character-set representation.  Character sets are
954fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * done as bit vectors, grouped 8 to a byte vector for compactness.
964fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * The individual set therefore has both a pointer to the byte vector
974fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * and a mask to pick out the relevant bit of each byte.  A hash code
984fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * simplifies testing whether two sets could be identical.
994fa7b105644222d9b35347c9d226ca8e011072ebColin Cross *
1004fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * This will get trickier for multicharacter collating elements.  As
1014fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * preliminary hooks for dealing with such things, we also carry along
1024fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * a string of multi-character elements, and decide the size of the
1034fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * vectors at run time.
1044fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
1054fa7b105644222d9b35347c9d226ca8e011072ebColin Crosstypedef struct {
1064fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	uch *ptr;		/* -> uch [csetsize] */
1074fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	uch mask;		/* bit within array */
1084fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	uch hash;		/* hash code */
1094fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	size_t smultis;
1104fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
1114fa7b105644222d9b35347c9d226ca8e011072ebColin Cross} cset;
1124fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
1134fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	CHadd(cs, c)	((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (c))
1144fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	CHsub(cs, c)	((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (c))
1154fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
1164fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
1174fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
1184fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	MCin(p, cs, cp)	mcin(p, cs, cp)
1194fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1204fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* stuff for character categories */
1214fa7b105644222d9b35347c9d226ca8e011072ebColin Crosstypedef unsigned char cat_t;
1224fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1234fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/*
1244fa7b105644222d9b35347c9d226ca8e011072ebColin Cross * main compiled-expression structure
1254fa7b105644222d9b35347c9d226ca8e011072ebColin Cross */
1264fa7b105644222d9b35347c9d226ca8e011072ebColin Crossstruct re_guts {
1274fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int magic;
1284fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#		define	MAGIC2	((('R'^0200)<<8)|'E')
1294fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	sop *strip;		/* malloced area for strip */
1304fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int csetsize;		/* number of bits in a cset vector */
1314fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int ncsets;		/* number of csets in use */
1324fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	cset *sets;		/* -> cset [ncsets] */
1334fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
1344fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int cflags;		/* copy of regcomp() cflags argument */
1354fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	sopno nstates;		/* = number of sops */
1364fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	sopno firststate;	/* the initial OEND (normally 0) */
1374fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	sopno laststate;	/* the final OEND */
1384fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int iflags;		/* internal flags */
1394fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#		define	USEBOL	01	/* used ^ */
1404fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#		define	USEEOL	02	/* used $ */
1414fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#		define	BAD	04	/* something wrong */
1424fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int nbol;		/* number of ^ used */
1434fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int neol;		/* number of $ used */
1444fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int ncategories;	/* how many character categories */
1454fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
1464fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	char *must;		/* match must contain this string */
1474fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int mlen;		/* length of must */
1484fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	size_t nsub;		/* copy of re_nsub */
1494fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	int backrefs;		/* does it use back references? */
1504fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	sopno nplus;		/* how deep does it nest +s? */
1514fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	/* catspace must be last */
1524fa7b105644222d9b35347c9d226ca8e011072ebColin Cross	cat_t catspace[1];	/* actually [NC] */
1534fa7b105644222d9b35347c9d226ca8e011072ebColin Cross};
1544fa7b105644222d9b35347c9d226ca8e011072ebColin Cross
1554fa7b105644222d9b35347c9d226ca8e011072ebColin Cross/* misc utilities */
1564fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	OUT	(CHAR_MAX+1)	/* a non-character value */
1574fa7b105644222d9b35347c9d226ca8e011072ebColin Cross#define	ISWORD(c)	(isalnum(c) || (c) == '_')
158