xref: /illumos-gate/usr/src/lib/libc/port/regex/regex2.h (revision 14247eb6)
14297a3b0SGarrett D'Amore /*
2*14247eb6SBill Sommerfeld  * Copyright 2023 Bill Sommerfeld <sommerfeld@hamachi.org>
34297a3b0SGarrett D'Amore  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
44297a3b0SGarrett D'Amore  * Copyright (c) 1992, 1993, 1994
54297a3b0SGarrett D'Amore  *	The Regents of the University of California.  All rights reserved.
64297a3b0SGarrett D'Amore  *
74297a3b0SGarrett D'Amore  * This code is derived from software contributed to Berkeley by
84297a3b0SGarrett D'Amore  * Henry Spencer.
94297a3b0SGarrett D'Amore  *
104297a3b0SGarrett D'Amore  * Redistribution and use in source and binary forms, with or without
114297a3b0SGarrett D'Amore  * modification, are permitted provided that the following conditions
124297a3b0SGarrett D'Amore  * are met:
134297a3b0SGarrett D'Amore  * 1. Redistributions of source code must retain the above copyright
144297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer.
154297a3b0SGarrett D'Amore  * 2. Redistributions in binary form must reproduce the above copyright
164297a3b0SGarrett D'Amore  *    notice, this list of conditions and the following disclaimer in the
174297a3b0SGarrett D'Amore  *    documentation and/or other materials provided with the distribution.
187641c5eaSYuri Pankov  * 3. Neither the name of the University nor the names of its contributors
194297a3b0SGarrett D'Amore  *    may be used to endorse or promote products derived from this software
204297a3b0SGarrett D'Amore  *    without specific prior written permission.
214297a3b0SGarrett D'Amore  *
224297a3b0SGarrett D'Amore  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
234297a3b0SGarrett D'Amore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
244297a3b0SGarrett D'Amore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
254297a3b0SGarrett D'Amore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
264297a3b0SGarrett D'Amore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
274297a3b0SGarrett D'Amore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
284297a3b0SGarrett D'Amore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
294297a3b0SGarrett D'Amore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
304297a3b0SGarrett D'Amore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
314297a3b0SGarrett D'Amore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
324297a3b0SGarrett D'Amore  * SUCH DAMAGE.
334297a3b0SGarrett D'Amore  */
344297a3b0SGarrett D'Amore 
354297a3b0SGarrett D'Amore /*
364297a3b0SGarrett D'Amore  * First, the stuff that ends up in the outside-world include file
374297a3b0SGarrett D'Amore  * typedef off_t regoff_t;
384297a3b0SGarrett D'Amore  * typedef struct {
391603eda2SYuri Pankov  *	int re_magic;
401603eda2SYuri Pankov  *	size_t re_nsub;		// number of parenthesized subexpressions
411603eda2SYuri Pankov  *	const char *re_endp;	// end pointer for REG_PEND
421603eda2SYuri Pankov  *	struct re_guts *re_g;	// none of your business :-)
434297a3b0SGarrett D'Amore  * } regex_t;
444297a3b0SGarrett D'Amore  * typedef struct {
451603eda2SYuri Pankov  *	regoff_t rm_so;		// start of match
461603eda2SYuri Pankov  *	regoff_t rm_eo;		// end of match
474297a3b0SGarrett D'Amore  * } regmatch_t;
484297a3b0SGarrett D'Amore  */
494297a3b0SGarrett D'Amore /*
504297a3b0SGarrett D'Amore  * internals of regex_t
514297a3b0SGarrett D'Amore  */
524297a3b0SGarrett D'Amore #define	MAGIC1	((('r'^0200)<<8) | 'e')
534297a3b0SGarrett D'Amore 
544297a3b0SGarrett D'Amore /*
554297a3b0SGarrett D'Amore  * The internal representation is a *strip*, a sequence of
564297a3b0SGarrett D'Amore  * operators ending with an endmarker.  (Some terminology etc. is a
574297a3b0SGarrett D'Amore  * historical relic of earlier versions which used multiple strips.)
584297a3b0SGarrett D'Amore  * Certain oddities in the representation are there to permit running
594297a3b0SGarrett D'Amore  * the machinery backwards; in particular, any deviation from sequential
604297a3b0SGarrett D'Amore  * flow must be marked at both its source and its destination.  Some
614297a3b0SGarrett D'Amore  * fine points:
624297a3b0SGarrett D'Amore  *
634297a3b0SGarrett D'Amore  * - OPLUS_ and O_PLUS are *inside* the loop they create.
644297a3b0SGarrett D'Amore  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
654297a3b0SGarrett D'Amore  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
664297a3b0SGarrett D'Amore  *   OOR1 and OOR2 are respectively the end and the beginning of one of
674297a3b0SGarrett D'Amore  *   the branches.  Note that there is an implicit OOR2 following OCH_
684297a3b0SGarrett D'Amore  *   and an implicit OOR1 preceding O_CH.
694297a3b0SGarrett D'Amore  *
704297a3b0SGarrett D'Amore  * In state representations, an operator's bit is on to signify a state
714297a3b0SGarrett D'Amore  * immediately *preceding* "execution" of that operator.
724297a3b0SGarrett D'Amore  */
73eda71b4aSGarrett D'Amore typedef unsigned int sop;	/* strip operator */
747641c5eaSYuri Pankov typedef unsigned int sopno;
754297a3b0SGarrett D'Amore #define	OPRMASK	0xf8000000U
764297a3b0SGarrett D'Amore #define	OPDMASK	0x07ffffffU
774297a3b0SGarrett D'Amore #define	OPSHIFT	((unsigned)27)
784297a3b0SGarrett D'Amore #define	OP(n)	((n)&OPRMASK)
794297a3b0SGarrett D'Amore #define	OPND(n)	((n)&OPDMASK)
804297a3b0SGarrett D'Amore #define	SOP(op, opnd)	((op)|(opnd))
814297a3b0SGarrett D'Amore /* operators			   meaning	operand			*/
824297a3b0SGarrett D'Amore /*						(back, fwd are offsets)	*/
834297a3b0SGarrett D'Amore #define	OEND	(1U<<OPSHIFT)	/* endmarker	-			*/
844297a3b0SGarrett D'Amore #define	OCHAR	(2U<<OPSHIFT)	/* character	wide character		*/
854297a3b0SGarrett D'Amore #define	OBOL	(3U<<OPSHIFT)	/* left anchor	-			*/
864297a3b0SGarrett D'Amore #define	OEOL	(4U<<OPSHIFT)	/* right anchor	-			*/
874297a3b0SGarrett D'Amore #define	OANY	(5U<<OPSHIFT)	/* .		-			*/
884297a3b0SGarrett D'Amore #define	OANYOF	(6U<<OPSHIFT)	/* [...]	set number		*/
894297a3b0SGarrett D'Amore #define	OBACK_	(7U<<OPSHIFT)	/* begin \d	paren number		*/
904297a3b0SGarrett D'Amore #define	O_BACK	(8U<<OPSHIFT)	/* end \d	paren number		*/
914297a3b0SGarrett D'Amore #define	OPLUS_	(9U<<OPSHIFT)	/* + prefix	fwd to suffix		*/
924297a3b0SGarrett D'Amore #define	O_PLUS	(10U<<OPSHIFT)	/* + suffix	back to prefix		*/
934297a3b0SGarrett D'Amore #define	OQUEST_	(11U<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
944297a3b0SGarrett D'Amore #define	O_QUEST	(12U<<OPSHIFT)	/* ? suffix	back to prefix		*/
954297a3b0SGarrett D'Amore #define	OLPAREN	(13U<<OPSHIFT)	/* (		fwd to )		*/
964297a3b0SGarrett D'Amore #define	ORPAREN	(14U<<OPSHIFT)	/* )		back to (		*/
974297a3b0SGarrett D'Amore #define	OCH_	(15U<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
984297a3b0SGarrett D'Amore #define	OOR1	(16U<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
994297a3b0SGarrett D'Amore #define	OOR2	(17U<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
1004297a3b0SGarrett D'Amore #define	O_CH	(18U<<OPSHIFT)	/* end choice	back to OOR1		*/
1014297a3b0SGarrett D'Amore #define	OBOW	(19U<<OPSHIFT)	/* begin word	-			*/
1024297a3b0SGarrett D'Amore #define	OEOW	(20U<<OPSHIFT)	/* end word	-			*/
1034297a3b0SGarrett D'Amore 
1044297a3b0SGarrett D'Amore /*
1054297a3b0SGarrett D'Amore  * Structures for [] character-set representation.
1064297a3b0SGarrett D'Amore  */
1074297a3b0SGarrett D'Amore typedef struct {
1084297a3b0SGarrett D'Amore 	wint_t		min;
1094297a3b0SGarrett D'Amore 	wint_t		max;
1104297a3b0SGarrett D'Amore } crange;
1114297a3b0SGarrett D'Amore typedef struct {
1121603eda2SYuri Pankov 	unsigned char	bmp[NC_MAX / 8];
1134297a3b0SGarrett D'Amore 	wctype_t	*types;
1147641c5eaSYuri Pankov 	unsigned int	ntypes;
1154297a3b0SGarrett D'Amore 	wint_t		*wides;
1167641c5eaSYuri Pankov 	unsigned int	nwides;
1174297a3b0SGarrett D'Amore 	crange		*ranges;
1187641c5eaSYuri Pankov 	unsigned int	nranges;
1194297a3b0SGarrett D'Amore 	int		invert;
1204297a3b0SGarrett D'Amore 	int		icase;
1214297a3b0SGarrett D'Amore } cset;
1224297a3b0SGarrett D'Amore 
123*14247eb6SBill Sommerfeld static inline int
CHIN1(int nc,cset * cs,wint_t ch)124*14247eb6SBill Sommerfeld CHIN1(int nc, cset *cs, wint_t ch)
1254297a3b0SGarrett D'Amore {
1267641c5eaSYuri Pankov 	unsigned int i;
1274297a3b0SGarrett D'Amore 
1284297a3b0SGarrett D'Amore 	assert(ch >= 0);
129*14247eb6SBill Sommerfeld 	if (ch < nc)
1304297a3b0SGarrett D'Amore 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
1314297a3b0SGarrett D'Amore 		    cs->invert);
1321603eda2SYuri Pankov 	for (i = 0; i < cs->nwides; i++) {
1331603eda2SYuri Pankov 		if (cs->icase) {
1341603eda2SYuri Pankov 			if (ch == towlower(cs->wides[i]) ||
1351603eda2SYuri Pankov 			    ch == towupper(cs->wides[i]))
1361603eda2SYuri Pankov 				return (!cs->invert);
1371603eda2SYuri Pankov 		} else if (ch == cs->wides[i])
1384297a3b0SGarrett D'Amore 			return (!cs->invert);
1391603eda2SYuri Pankov 	}
1404297a3b0SGarrett D'Amore 	for (i = 0; i < cs->nranges; i++)
1414297a3b0SGarrett D'Amore 		if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
1424297a3b0SGarrett D'Amore 			return (!cs->invert);
1434297a3b0SGarrett D'Amore 	for (i = 0; i < cs->ntypes; i++)
1444297a3b0SGarrett D'Amore 		if (iswctype(ch, cs->types[i]))
1454297a3b0SGarrett D'Amore 			return (!cs->invert);
1464297a3b0SGarrett D'Amore 	return (cs->invert);
1474297a3b0SGarrett D'Amore }
1484297a3b0SGarrett D'Amore 
149*14247eb6SBill Sommerfeld static inline int
CHIN(int nc,cset * cs,wint_t ch)150*14247eb6SBill Sommerfeld CHIN(int nc, cset *cs, wint_t ch)
1514297a3b0SGarrett D'Amore {
1524297a3b0SGarrett D'Amore 
1534297a3b0SGarrett D'Amore 	assert(ch >= 0);
154*14247eb6SBill Sommerfeld 	if (ch < nc)
1554297a3b0SGarrett D'Amore 		return (((cs->bmp[ch >> 3] & (1 << (ch & 7))) != 0) ^
1564297a3b0SGarrett D'Amore 		    cs->invert);
1574297a3b0SGarrett D'Amore 	else if (cs->icase)
158*14247eb6SBill Sommerfeld 		return (CHIN1(nc, cs, ch) || CHIN1(nc, cs, towlower(ch)) ||
159*14247eb6SBill Sommerfeld 		    CHIN1(nc, cs, towupper(ch)));
1604297a3b0SGarrett D'Amore 	else
161*14247eb6SBill Sommerfeld 		return (CHIN1(nc, cs, ch));
1624297a3b0SGarrett D'Amore }
1634297a3b0SGarrett D'Amore 
1644297a3b0SGarrett D'Amore /*
1654297a3b0SGarrett D'Amore  * main compiled-expression structure
1664297a3b0SGarrett D'Amore  */
1674297a3b0SGarrett D'Amore struct re_guts {
1684297a3b0SGarrett D'Amore 	int magic;
1694297a3b0SGarrett D'Amore #define	MAGIC2	((('R'^0200)<<8)|'E')
1704297a3b0SGarrett D'Amore 	sop *strip;		/* malloced area for strip */
1717641c5eaSYuri Pankov 	unsigned int ncsets;	/* number of csets in use */
1724297a3b0SGarrett D'Amore 	cset *sets;		/* -> cset [ncsets] */
1734297a3b0SGarrett D'Amore 	int cflags;		/* copy of regcomp() cflags argument */
1744297a3b0SGarrett D'Amore 	sopno nstates;		/* = number of sops */
1754297a3b0SGarrett D'Amore 	sopno firststate;	/* the initial OEND (normally 0) */
1764297a3b0SGarrett D'Amore 	sopno laststate;	/* the final OEND */
1774297a3b0SGarrett D'Amore 	int iflags;		/* internal flags */
1784297a3b0SGarrett D'Amore #define	USEBOL	01	/* used ^ */
1794297a3b0SGarrett D'Amore #define	USEEOL	02	/* used $ */
1804297a3b0SGarrett D'Amore #define	BAD	04	/* something wrong */
1814297a3b0SGarrett D'Amore 	int nbol;		/* number of ^ used */
1824297a3b0SGarrett D'Amore 	int neol;		/* number of $ used */
1834297a3b0SGarrett D'Amore 	char *must;		/* match must contain this string */
1844297a3b0SGarrett D'Amore 	int moffset;		/* latest point at which must may be located */
1854297a3b0SGarrett D'Amore 	int *charjump;		/* Boyer-Moore char jump table */
1864297a3b0SGarrett D'Amore 	int *matchjump;		/* Boyer-Moore match jump table */
1874297a3b0SGarrett D'Amore 	int mlen;		/* length of must */
1884297a3b0SGarrett D'Amore 	size_t nsub;		/* copy of re_nsub */
1894297a3b0SGarrett D'Amore 	int backrefs;		/* does it use back references? */
1904297a3b0SGarrett D'Amore 	sopno nplus;		/* how deep does it nest +s? */
191*14247eb6SBill Sommerfeld 	unsigned int mb_cur_max;
1924297a3b0SGarrett D'Amore };
1934297a3b0SGarrett D'Amore 
1944297a3b0SGarrett D'Amore /* misc utilities */
1954297a3b0SGarrett D'Amore #define	OUT	(CHAR_MIN - 1)	/* a non-character value */
19662e74980SKyle Evans #define	IGN	(CHAR_MIN - 2)
1974297a3b0SGarrett D'Amore #define	ISWORD(c)	(iswalnum((uch)(c)) || (c) == '_')
198