xref: /freebsd/lib/libc/regex/regex2.h (revision e6a886d8dbcf8e1f3e9a252369051f15af41f8ab)
158f0484fSRodney W. Grimes /*-
258f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
358f0484fSRodney W. Grimes  * Copyright (c) 1992, 1993, 1994
458f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
558f0484fSRodney W. Grimes  *
658f0484fSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
758f0484fSRodney W. Grimes  * Henry Spencer.
858f0484fSRodney W. Grimes  *
958f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
1058f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
1158f0484fSRodney W. Grimes  * are met:
1258f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
1358f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1458f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1558f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1658f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1758f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1858f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1958f0484fSRodney W. Grimes  *	This product includes software developed by the University of
2058f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
2158f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
2258f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
2358f0484fSRodney W. Grimes  *    without specific prior written permission.
2458f0484fSRodney W. Grimes  *
2558f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2658f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2758f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2858f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2958f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3058f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
3158f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
3258f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
3358f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3458f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3558f0484fSRodney W. Grimes  * SUCH DAMAGE.
3658f0484fSRodney W. Grimes  *
3758f0484fSRodney W. Grimes  *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
386049d9f0SDaniel C. Sobral  *
396049d9f0SDaniel C. Sobral  * $FreeBSD$
4058f0484fSRodney W. Grimes  */
4158f0484fSRodney W. Grimes 
4258f0484fSRodney W. Grimes /*
4358f0484fSRodney W. Grimes  * First, the stuff that ends up in the outside-world include file
4458f0484fSRodney W. Grimes  = typedef off_t regoff_t;
4558f0484fSRodney W. Grimes  = typedef struct {
4658f0484fSRodney W. Grimes  = 	int re_magic;
4758f0484fSRodney W. Grimes  = 	size_t re_nsub;		// number of parenthesized subexpressions
4858f0484fSRodney W. Grimes  = 	const char *re_endp;	// end pointer for REG_PEND
4958f0484fSRodney W. Grimes  = 	struct re_guts *re_g;	// none of your business :-)
5058f0484fSRodney W. Grimes  = } regex_t;
5158f0484fSRodney W. Grimes  = typedef struct {
5258f0484fSRodney W. Grimes  = 	regoff_t rm_so;		// start of match
5358f0484fSRodney W. Grimes  = 	regoff_t rm_eo;		// end of match
5458f0484fSRodney W. Grimes  = } regmatch_t;
5558f0484fSRodney W. Grimes  */
5658f0484fSRodney W. Grimes /*
5758f0484fSRodney W. Grimes  * internals of regex_t
5858f0484fSRodney W. Grimes  */
5958f0484fSRodney W. Grimes #define	MAGIC1	((('r'^0200)<<8) | 'e')
6058f0484fSRodney W. Grimes 
6158f0484fSRodney W. Grimes /*
6258f0484fSRodney W. Grimes  * The internal representation is a *strip*, a sequence of
6358f0484fSRodney W. Grimes  * operators ending with an endmarker.  (Some terminology etc. is a
6458f0484fSRodney W. Grimes  * historical relic of earlier versions which used multiple strips.)
6558f0484fSRodney W. Grimes  * Certain oddities in the representation are there to permit running
6658f0484fSRodney W. Grimes  * the machinery backwards; in particular, any deviation from sequential
6758f0484fSRodney W. Grimes  * flow must be marked at both its source and its destination.  Some
6858f0484fSRodney W. Grimes  * fine points:
6958f0484fSRodney W. Grimes  *
7058f0484fSRodney W. Grimes  * - OPLUS_ and O_PLUS are *inside* the loop they create.
7158f0484fSRodney W. Grimes  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
7258f0484fSRodney W. Grimes  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
7358f0484fSRodney W. Grimes  *   OOR1 and OOR2 are respectively the end and the beginning of one of
7458f0484fSRodney W. Grimes  *   the branches.  Note that there is an implicit OOR2 following OCH_
7558f0484fSRodney W. Grimes  *   and an implicit OOR1 preceding O_CH.
7658f0484fSRodney W. Grimes  *
7758f0484fSRodney W. Grimes  * In state representations, an operator's bit is on to signify a state
7858f0484fSRodney W. Grimes  * immediately *preceding* "execution" of that operator.
7958f0484fSRodney W. Grimes  */
8058f0484fSRodney W. Grimes typedef unsigned long sop;	/* strip operator */
8158f0484fSRodney W. Grimes typedef long sopno;
82cfc1614aSJohn Birrell #define	OPRMASK	0xf8000000L
83cfc1614aSJohn Birrell #define	OPDMASK	0x07ffffffL
8458f0484fSRodney W. Grimes #define	OPSHIFT	((unsigned)27)
8558f0484fSRodney W. Grimes #define	OP(n)	((n)&OPRMASK)
8658f0484fSRodney W. Grimes #define	OPND(n)	((n)&OPDMASK)
8758f0484fSRodney W. Grimes #define	SOP(op, opnd)	((op)|(opnd))
8858f0484fSRodney W. Grimes /* operators			   meaning	operand			*/
8958f0484fSRodney W. Grimes /*						(back, fwd are offsets)	*/
90cfc1614aSJohn Birrell #define	OEND	(1L<<OPSHIFT)	/* endmarker	-			*/
91cfc1614aSJohn Birrell #define	OCHAR	(2L<<OPSHIFT)	/* character	unsigned char		*/
92cfc1614aSJohn Birrell #define	OBOL	(3L<<OPSHIFT)	/* left anchor	-			*/
93cfc1614aSJohn Birrell #define	OEOL	(4L<<OPSHIFT)	/* right anchor	-			*/
94cfc1614aSJohn Birrell #define	OANY	(5L<<OPSHIFT)	/* .		-			*/
95cfc1614aSJohn Birrell #define	OANYOF	(6L<<OPSHIFT)	/* [...]	set number		*/
96cfc1614aSJohn Birrell #define	OBACK_	(7L<<OPSHIFT)	/* begin \d	paren number		*/
97cfc1614aSJohn Birrell #define	O_BACK	(8L<<OPSHIFT)	/* end \d	paren number		*/
98cfc1614aSJohn Birrell #define	OPLUS_	(9L<<OPSHIFT)	/* + prefix	fwd to suffix		*/
99cfc1614aSJohn Birrell #define	O_PLUS	(10L<<OPSHIFT)	/* + suffix	back to prefix		*/
100cfc1614aSJohn Birrell #define	OQUEST_	(11L<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
101cfc1614aSJohn Birrell #define	O_QUEST	(12L<<OPSHIFT)	/* ? suffix	back to prefix		*/
102cfc1614aSJohn Birrell #define	OLPAREN	(13L<<OPSHIFT)	/* (		fwd to )		*/
103cfc1614aSJohn Birrell #define	ORPAREN	(14L<<OPSHIFT)	/* )		back to (		*/
104cfc1614aSJohn Birrell #define	OCH_	(15L<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
105cfc1614aSJohn Birrell #define	OOR1	(16L<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
106cfc1614aSJohn Birrell #define	OOR2	(17L<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
107cfc1614aSJohn Birrell #define	O_CH	(18L<<OPSHIFT)	/* end choice	back to OOR1		*/
108cfc1614aSJohn Birrell #define	OBOW	(19L<<OPSHIFT)	/* begin word	-			*/
109cfc1614aSJohn Birrell #define	OEOW	(20L<<OPSHIFT)	/* end word	-			*/
11058f0484fSRodney W. Grimes 
11158f0484fSRodney W. Grimes /*
11258f0484fSRodney W. Grimes  * Structure for [] character-set representation.  Character sets are
11358f0484fSRodney W. Grimes  * done as bit vectors, grouped 8 to a byte vector for compactness.
11458f0484fSRodney W. Grimes  * The individual set therefore has both a pointer to the byte vector
11558f0484fSRodney W. Grimes  * and a mask to pick out the relevant bit of each byte.  A hash code
11658f0484fSRodney W. Grimes  * simplifies testing whether two sets could be identical.
11758f0484fSRodney W. Grimes  *
11858f0484fSRodney W. Grimes  * This will get trickier for multicharacter collating elements.  As
11958f0484fSRodney W. Grimes  * preliminary hooks for dealing with such things, we also carry along
12058f0484fSRodney W. Grimes  * a string of multi-character elements, and decide the size of the
12158f0484fSRodney W. Grimes  * vectors at run time.
12258f0484fSRodney W. Grimes  */
12358f0484fSRodney W. Grimes typedef struct {
12458f0484fSRodney W. Grimes 	uch *ptr;		/* -> uch [csetsize] */
12558f0484fSRodney W. Grimes 	uch mask;		/* bit within array */
126b5363c4aSAndrey A. Chernov 	short hash;             /* hash code */
12758f0484fSRodney W. Grimes 	size_t smultis;
12858f0484fSRodney W. Grimes 	char *multis;		/* -> char[smulti]  ab\0cd\0ef\0\0 */
12958f0484fSRodney W. Grimes } cset;
13058f0484fSRodney W. Grimes /* note that CHadd and CHsub are unsafe, and CHIN doesn't yield 0/1 */
131b5363c4aSAndrey A. Chernov #define CHadd(cs, c)    ((cs)->ptr[(uch)(c)] |= (cs)->mask, (cs)->hash += (uch)(c))
132b5363c4aSAndrey A. Chernov #define CHsub(cs, c)    ((cs)->ptr[(uch)(c)] &= ~(cs)->mask, (cs)->hash -= (uch)(c))
13358f0484fSRodney W. Grimes #define	CHIN(cs, c)	((cs)->ptr[(uch)(c)] & (cs)->mask)
13458f0484fSRodney W. Grimes #define	MCadd(p, cs, cp)	mcadd(p, cs, cp)	/* regcomp() internal fns */
13558f0484fSRodney W. Grimes #define	MCsub(p, cs, cp)	mcsub(p, cs, cp)
13658f0484fSRodney W. Grimes #define	MCin(p, cs, cp)	mcin(p, cs, cp)
13758f0484fSRodney W. Grimes 
13858f0484fSRodney W. Grimes /* stuff for character categories */
13958f0484fSRodney W. Grimes typedef unsigned char cat_t;
14058f0484fSRodney W. Grimes 
14158f0484fSRodney W. Grimes /*
14258f0484fSRodney W. Grimes  * main compiled-expression structure
14358f0484fSRodney W. Grimes  */
14458f0484fSRodney W. Grimes struct re_guts {
14558f0484fSRodney W. Grimes 	int magic;
14658f0484fSRodney W. Grimes #		define	MAGIC2	((('R'^0200)<<8)|'E')
14758f0484fSRodney W. Grimes 	sop *strip;		/* malloced area for strip */
14858f0484fSRodney W. Grimes 	int csetsize;		/* number of bits in a cset vector */
14958f0484fSRodney W. Grimes 	int ncsets;		/* number of csets in use */
15058f0484fSRodney W. Grimes 	cset *sets;		/* -> cset [ncsets] */
15158f0484fSRodney W. Grimes 	uch *setbits;		/* -> uch[csetsize][ncsets/CHAR_BIT] */
15258f0484fSRodney W. Grimes 	int cflags;		/* copy of regcomp() cflags argument */
15358f0484fSRodney W. Grimes 	sopno nstates;		/* = number of sops */
15458f0484fSRodney W. Grimes 	sopno firststate;	/* the initial OEND (normally 0) */
15558f0484fSRodney W. Grimes 	sopno laststate;	/* the final OEND */
15658f0484fSRodney W. Grimes 	int iflags;		/* internal flags */
15758f0484fSRodney W. Grimes #		define	USEBOL	01	/* used ^ */
15858f0484fSRodney W. Grimes #		define	USEEOL	02	/* used $ */
15958f0484fSRodney W. Grimes #		define	BAD	04	/* something wrong */
16058f0484fSRodney W. Grimes 	int nbol;		/* number of ^ used */
16158f0484fSRodney W. Grimes 	int neol;		/* number of $ used */
16258f0484fSRodney W. Grimes 	int ncategories;	/* how many character categories */
16358f0484fSRodney W. Grimes 	cat_t *categories;	/* ->catspace[-CHAR_MIN] */
16458f0484fSRodney W. Grimes 	char *must;		/* match must contain this string */
165e6a886d8SDaniel C. Sobral 	int moffset;		/* latest point at which must may be located */
1666049d9f0SDaniel C. Sobral 	int *charjump;		/* Boyer-Moore char jump table */
1676049d9f0SDaniel C. Sobral 	int *matchjump;		/* Boyer-Moore match jump table */
16858f0484fSRodney W. Grimes 	int mlen;		/* length of must */
16958f0484fSRodney W. Grimes 	size_t nsub;		/* copy of re_nsub */
17058f0484fSRodney W. Grimes 	int backrefs;		/* does it use back references? */
17158f0484fSRodney W. Grimes 	sopno nplus;		/* how deep does it nest +s? */
17258f0484fSRodney W. Grimes 	/* catspace must be last */
17358f0484fSRodney W. Grimes 	cat_t catspace[1];	/* actually [NC] */
17458f0484fSRodney W. Grimes };
17558f0484fSRodney W. Grimes 
17658f0484fSRodney W. Grimes /* misc utilities */
17758f0484fSRodney W. Grimes #define	OUT	(CHAR_MAX+1)	/* a non-character value */
178b5363c4aSAndrey A. Chernov #define ISWORD(c)       (isalnum((uch)(c)) || (c) == '_')
179