xref: /netbsd-src/lib/libc/regex/regex2.h (revision 3896c310d9fbd002ddca01ae787de3109f873065)
1*3896c310Schristos /*	$NetBSD: regex2.h,v 1.16 2025/01/01 18:19:50 christos Exp $	*/
2eb7c1594Sagc 
3eb7c1594Sagc /*-
41ee269c3Schristos  * SPDX-License-Identifier: BSD-3-Clause
51ee269c3Schristos  *
61ee269c3Schristos  * Copyright (c) 1992, 1993, 1994 Henry Spencer.
7eb7c1594Sagc  * Copyright (c) 1992, 1993, 1994
8eb7c1594Sagc  *	The Regents of the University of California.  All rights reserved.
9eb7c1594Sagc  *
10eb7c1594Sagc  * This code is derived from software contributed to Berkeley by
11eb7c1594Sagc  * Henry Spencer.
12eb7c1594Sagc  *
13eb7c1594Sagc  * Redistribution and use in source and binary forms, with or without
14eb7c1594Sagc  * modification, are permitted provided that the following conditions
15eb7c1594Sagc  * are met:
16eb7c1594Sagc  * 1. Redistributions of source code must retain the above copyright
17eb7c1594Sagc  *    notice, this list of conditions and the following disclaimer.
18eb7c1594Sagc  * 2. Redistributions in binary form must reproduce the above copyright
19eb7c1594Sagc  *    notice, this list of conditions and the following disclaimer in the
20eb7c1594Sagc  *    documentation and/or other materials provided with the distribution.
21eb7c1594Sagc  * 3. Neither the name of the University nor the names of its contributors
22eb7c1594Sagc  *    may be used to endorse or promote products derived from this software
23eb7c1594Sagc  *    without specific prior written permission.
24eb7c1594Sagc  *
25eb7c1594Sagc  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26eb7c1594Sagc  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27eb7c1594Sagc  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28eb7c1594Sagc  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29eb7c1594Sagc  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30eb7c1594Sagc  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31eb7c1594Sagc  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32eb7c1594Sagc  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33eb7c1594Sagc  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34eb7c1594Sagc  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35eb7c1594Sagc  * SUCH DAMAGE.
36eb7c1594Sagc  *
37eb7c1594Sagc  *	@(#)regex2.h	8.4 (Berkeley) 3/20/94
381ee269c3Schristos  * $FreeBSD: head/lib/libc/regex/regex2.h 368359 2020-12-05 03:18:48Z kevans $
397c6ed81dScgd  */
407c6ed81dScgd 
41b90ff831Sjtc /*
42b90ff831Sjtc  * First, the stuff that ends up in the outside-world include file
43b90ff831Sjtc  = typedef off_t regoff_t;
44b90ff831Sjtc  = typedef struct {
45b90ff831Sjtc  = 	int re_magic;
46b90ff831Sjtc  = 	size_t re_nsub;		// number of parenthesized subexpressions
47b90ff831Sjtc  = 	const char *re_endp;	// end pointer for REG_PEND
48b90ff831Sjtc  = 	struct re_guts *re_g;	// none of your business :-)
49b90ff831Sjtc  = } regex_t;
50b90ff831Sjtc  = typedef struct {
51b90ff831Sjtc  = 	regoff_t rm_so;		// start of match
52b90ff831Sjtc  = 	regoff_t rm_eo;		// end of match
53b90ff831Sjtc  = } regmatch_t;
54b90ff831Sjtc  */
55b90ff831Sjtc /*
56b90ff831Sjtc  * internals of regex_t
57b90ff831Sjtc  */
58b90ff831Sjtc #define	MAGIC1	((('r'^0200)<<8) | 'e')
59b90ff831Sjtc 
60b90ff831Sjtc /*
61b90ff831Sjtc  * The internal representation is a *strip*, a sequence of
62b90ff831Sjtc  * operators ending with an endmarker.  (Some terminology etc. is a
63b90ff831Sjtc  * historical relic of earlier versions which used multiple strips.)
64b90ff831Sjtc  * Certain oddities in the representation are there to permit running
65b90ff831Sjtc  * the machinery backwards; in particular, any deviation from sequential
66b90ff831Sjtc  * flow must be marked at both its source and its destination.  Some
67b90ff831Sjtc  * fine points:
68b90ff831Sjtc  *
69b90ff831Sjtc  * - OPLUS_ and O_PLUS are *inside* the loop they create.
70b90ff831Sjtc  * - OQUEST_ and O_QUEST are *outside* the bypass they create.
71b90ff831Sjtc  * - OCH_ and O_CH are *outside* the multi-way branch they create, while
72b90ff831Sjtc  *   OOR1 and OOR2 are respectively the end and the beginning of one of
73b90ff831Sjtc  *   the branches.  Note that there is an implicit OOR2 following OCH_
74b90ff831Sjtc  *   and an implicit OOR1 preceding O_CH.
75b90ff831Sjtc  *
76b90ff831Sjtc  * In state representations, an operator's bit is on to signify a state
77b90ff831Sjtc  * immediately *preceding* "execution" of that operator.
78b90ff831Sjtc  */
792cf99de6Schristos typedef uint32_t sop;	/* strip operator */
802cf99de6Schristos typedef uint32_t sopno;
812cf99de6Schristos #define	OPRMASK	0xf8000000U
822cf99de6Schristos #define	OPDMASK	0x07ffffffU
832cf99de6Schristos #define	OPSHIFT	(27U)
84b90ff831Sjtc #define	OP(n)	((n)&OPRMASK)
851ee269c3Schristos #define	OPND(n)	((n)&OPDMASK)
86b90ff831Sjtc #define	SOP(op, opnd)	((op)|(opnd))
87b90ff831Sjtc /* operators			   meaning	operand			*/
88b90ff831Sjtc /*						(back, fwd are offsets)	*/
892cf99de6Schristos #define	OEND	(1U<<OPSHIFT)	/* endmarker	-			*/
902cf99de6Schristos #define	OCHAR	(2U<<OPSHIFT)	/* character	wide character		*/
912cf99de6Schristos #define	OBOL	(3U<<OPSHIFT)	/* left anchor	-			*/
922cf99de6Schristos #define	OEOL	(4U<<OPSHIFT)	/* right anchor	-			*/
932cf99de6Schristos #define	OANY	(5U<<OPSHIFT)	/* .		-			*/
942cf99de6Schristos #define	OANYOF	(6U<<OPSHIFT)	/* [...]	set number		*/
952cf99de6Schristos #define	OBACK_	(7U<<OPSHIFT)	/* begin \d	paren number		*/
962cf99de6Schristos #define	O_BACK	(8U<<OPSHIFT)	/* end \d	paren number		*/
972cf99de6Schristos #define	OPLUS_	(9U<<OPSHIFT)	/* + prefix	fwd to suffix		*/
982cf99de6Schristos #define	O_PLUS	(10U<<OPSHIFT)	/* + suffix	back to prefix		*/
992cf99de6Schristos #define	OQUEST_	(11U<<OPSHIFT)	/* ? prefix	fwd to suffix		*/
1002cf99de6Schristos #define	O_QUEST	(12U<<OPSHIFT)	/* ? suffix	back to prefix		*/
1012cf99de6Schristos #define	OLPAREN	(13U<<OPSHIFT)	/* (		fwd to )		*/
1022cf99de6Schristos #define	ORPAREN	(14U<<OPSHIFT)	/* )		back to (		*/
1032cf99de6Schristos #define	OCH_	(15U<<OPSHIFT)	/* begin choice	fwd to OOR2		*/
1042cf99de6Schristos #define	OOR1	(16U<<OPSHIFT)	/* | pt. 1	back to OOR1 or OCH_	*/
1052cf99de6Schristos #define	OOR2	(17U<<OPSHIFT)	/* | pt. 2	fwd to OOR2 or O_CH	*/
1062cf99de6Schristos #define	O_CH	(18U<<OPSHIFT)	/* end choice	back to OOR1		*/
1072cf99de6Schristos #define	OBOW	(19U<<OPSHIFT)	/* begin word	-			*/
1082cf99de6Schristos #define	OEOW	(20U<<OPSHIFT)	/* end word	-			*/
1092cf99de6Schristos #define	OBOS	(21U<<OPSHIFT)	/* begin subj.  -			*/
1102cf99de6Schristos #define	OEOS	(22U<<OPSHIFT)	/* end subj.	-			*/
1112cf99de6Schristos #define	OWBND	(23U<<OPSHIFT)	/* word bound	-			*/
1122cf99de6Schristos #define	ONWBND	(24U<<OPSHIFT)	/* not bound	-			*/
113b90ff831Sjtc 
114b90ff831Sjtc /*
1151ee269c3Schristos  * Structures for [] character-set representation.
116b90ff831Sjtc  */
117b90ff831Sjtc typedef struct {
1181ee269c3Schristos 	wint_t		min;
1191ee269c3Schristos 	wint_t		max;
1201ee269c3Schristos } crange;
1211ee269c3Schristos typedef struct {
1221ee269c3Schristos 	unsigned char	bmp[NC_MAX / 8];
1231ee269c3Schristos 	wctype_t	*types;
1241ee269c3Schristos 	unsigned int	ntypes;
1251ee269c3Schristos 	wint_t		*wides;
1261ee269c3Schristos 	unsigned int	nwides;
1271ee269c3Schristos 	crange		*ranges;
1281ee269c3Schristos 	unsigned int	nranges;
1291ee269c3Schristos 	int		invert;
1301ee269c3Schristos 	int		icase;
131b90ff831Sjtc } cset;
132b90ff831Sjtc 
1331ee269c3Schristos static int
1341ee269c3Schristos CHIN1(cset *cs, wint_t ch)
1351ee269c3Schristos {
1361ee269c3Schristos 	unsigned int i;
1371ee269c3Schristos 
138*3896c310Schristos 	if ((unsigned)ch < NC)
1392cf99de6Schristos 		return (((cs->bmp[(unsigned)ch >> 3] & (1 << (ch & 7))) != 0) ^
1401ee269c3Schristos 		    cs->invert);
1411ee269c3Schristos 	for (i = 0; i < cs->nwides; i++) {
1421ee269c3Schristos 		if (cs->icase) {
1431ee269c3Schristos 			if (ch == towlower(cs->wides[i]) ||
1441ee269c3Schristos 			    ch == towupper(cs->wides[i]))
1451ee269c3Schristos 				return (!cs->invert);
1461ee269c3Schristos 		} else if (ch == cs->wides[i])
1471ee269c3Schristos 			return (!cs->invert);
1481ee269c3Schristos 	}
1491ee269c3Schristos 	for (i = 0; i < cs->nranges; i++)
1501ee269c3Schristos 		if (cs->ranges[i].min <= ch && ch <= cs->ranges[i].max)
1511ee269c3Schristos 			return (!cs->invert);
1521ee269c3Schristos 	for (i = 0; i < cs->ntypes; i++)
1531ee269c3Schristos 		if (iswctype(ch, cs->types[i]))
1541ee269c3Schristos 			return (!cs->invert);
1551ee269c3Schristos 	return (cs->invert);
1561ee269c3Schristos }
1571ee269c3Schristos 
1581ee269c3Schristos static __inline int
1591ee269c3Schristos CHIN(cset *cs, wint_t ch)
1601ee269c3Schristos {
1611ee269c3Schristos 
162*3896c310Schristos 	if ((unsigned)ch < NC)
1632cf99de6Schristos 		return (((cs->bmp[(unsigned)ch >> 3] & (1 << (ch & 7))) != 0) ^
1641ee269c3Schristos 		    cs->invert);
1651ee269c3Schristos 	else if (cs->icase)
1661ee269c3Schristos 		return (CHIN1(cs, ch) || CHIN1(cs, towlower(ch)) ||
1671ee269c3Schristos 		    CHIN1(cs, towupper(ch)));
1681ee269c3Schristos 	else
1691ee269c3Schristos 		return (CHIN1(cs, ch));
1701ee269c3Schristos }
171b90ff831Sjtc 
172b90ff831Sjtc /*
173b90ff831Sjtc  * main compiled-expression structure
174b90ff831Sjtc  */
175b90ff831Sjtc struct re_guts {
176b90ff831Sjtc 	int magic;
177b90ff831Sjtc #		define	MAGIC2	((('R'^0200)<<8)|'E')
178b90ff831Sjtc 	sop *strip;		/* malloced area for strip */
179407d8594Schristos 	size_t ncsets;		/* number of csets in use */
180b90ff831Sjtc 	cset *sets;		/* -> cset [ncsets] */
181b90ff831Sjtc 	int cflags;		/* copy of regcomp() cflags argument */
182b90ff831Sjtc 	sopno nstates;		/* = number of sops */
183b90ff831Sjtc 	sopno firststate;	/* the initial OEND (normally 0) */
184b90ff831Sjtc 	sopno laststate;	/* the final OEND */
185b90ff831Sjtc 	int iflags;		/* internal flags */
186b90ff831Sjtc #		define	USEBOL	01	/* used ^ */
187b90ff831Sjtc #		define	USEEOL	02	/* used $ */
188b90ff831Sjtc #		define	BAD	04	/* something wrong */
189407d8594Schristos 	size_t nbol;		/* number of ^ used */
190407d8594Schristos 	size_t neol;		/* number of $ used */
191b90ff831Sjtc 	char *must;		/* match must contain this string */
1921ee269c3Schristos 	int moffset;		/* latest point at which must may be located */
1931ee269c3Schristos 	size_t *charjump;	/* Boyer-Moore char jump table */
1941ee269c3Schristos 	size_t *matchjump;	/* Boyer-Moore match jump table */
195407d8594Schristos 	size_t mlen;		/* length of must */
19664d3d6deSlukem 	size_t nsub;		/* copy of re_nsub */
197b90ff831Sjtc 	int backrefs;		/* does it use back references? */
198b90ff831Sjtc 	sopno nplus;		/* how deep does it nest +s? */
199b90ff831Sjtc };
200b90ff831Sjtc 
201b90ff831Sjtc /* misc utilities */
2021ee269c3Schristos #define	OUT	(CHAR_MIN - 1)	/* a non-character value */
2031ee269c3Schristos #define	IGN	(CHAR_MIN - 2)
2041ee269c3Schristos #define ISWORD(c)       (iswalnum((uch)(c)) || (c) == '_')
205