xref: /aosp_15_r20/external/libcups/vcnet/regex/regexec.c (revision 5e7646d21f1134fb0638875d812ef646c12ab91e)
1*5e7646d2SAndroid Build Coastguard Worker /*
2*5e7646d2SAndroid Build Coastguard Worker  * the outer shell of regexec()
3*5e7646d2SAndroid Build Coastguard Worker  *
4*5e7646d2SAndroid Build Coastguard Worker  * This file includes engine.c *twice*, after muchos fiddling with the
5*5e7646d2SAndroid Build Coastguard Worker  * macros that code uses.  This lets the same code operate on two different
6*5e7646d2SAndroid Build Coastguard Worker  * representations for state sets.
7*5e7646d2SAndroid Build Coastguard Worker  */
8*5e7646d2SAndroid Build Coastguard Worker #include <sys/types.h>
9*5e7646d2SAndroid Build Coastguard Worker #include <stdio.h>
10*5e7646d2SAndroid Build Coastguard Worker #include <stdlib.h>
11*5e7646d2SAndroid Build Coastguard Worker #include <string.h>
12*5e7646d2SAndroid Build Coastguard Worker #include <limits.h>
13*5e7646d2SAndroid Build Coastguard Worker #include <ctype.h>
14*5e7646d2SAndroid Build Coastguard Worker #include <regex.h>
15*5e7646d2SAndroid Build Coastguard Worker 
16*5e7646d2SAndroid Build Coastguard Worker #include "utils.h"
17*5e7646d2SAndroid Build Coastguard Worker #include "regex2.h"
18*5e7646d2SAndroid Build Coastguard Worker 
19*5e7646d2SAndroid Build Coastguard Worker static int nope = 0;		/* for use in asserts; shuts lint up */
20*5e7646d2SAndroid Build Coastguard Worker 
21*5e7646d2SAndroid Build Coastguard Worker /* macros for manipulating states, small version */
22*5e7646d2SAndroid Build Coastguard Worker #define	states	unsigned
23*5e7646d2SAndroid Build Coastguard Worker #define	states1	unsigned	/* for later use in regexec() decision */
24*5e7646d2SAndroid Build Coastguard Worker #define	CLEAR(v)	((v) = 0)
25*5e7646d2SAndroid Build Coastguard Worker #define	SET0(v, n)	((v) &= ~((unsigned)1 << (n)))
26*5e7646d2SAndroid Build Coastguard Worker #define	SET1(v, n)	((v) |= (unsigned)1 << (n))
27*5e7646d2SAndroid Build Coastguard Worker #define	ISSET(v, n)	((v) & ((unsigned)1 << (n)))
28*5e7646d2SAndroid Build Coastguard Worker #define	ASSIGN(d, s)	((d) = (s))
29*5e7646d2SAndroid Build Coastguard Worker #define	EQ(a, b)	((a) == (b))
30*5e7646d2SAndroid Build Coastguard Worker #define	STATEVARS	int dummy	/* dummy version */
31*5e7646d2SAndroid Build Coastguard Worker #define	STATESETUP(m, n)	/* nothing */
32*5e7646d2SAndroid Build Coastguard Worker #define	STATETEARDOWN(m)	/* nothing */
33*5e7646d2SAndroid Build Coastguard Worker #define	SETUP(v)	((v) = 0)
34*5e7646d2SAndroid Build Coastguard Worker #define	onestate	unsigned
35*5e7646d2SAndroid Build Coastguard Worker #define	INIT(o, n)	((o) = (unsigned)1 << (n))
36*5e7646d2SAndroid Build Coastguard Worker #define	INC(o)	((o) <<= 1)
37*5e7646d2SAndroid Build Coastguard Worker #define	ISSTATEIN(v, o)	((v) & (o))
38*5e7646d2SAndroid Build Coastguard Worker /* some abbreviations; note that some of these know variable names! */
39*5e7646d2SAndroid Build Coastguard Worker /* do "if I'm here, I can also be there" etc without branches */
40*5e7646d2SAndroid Build Coastguard Worker #define	FWD(dst, src, n)	((dst) |= ((unsigned)(src)&(here)) << (n))
41*5e7646d2SAndroid Build Coastguard Worker #define	BACK(dst, src, n)	((dst) |= ((unsigned)(src)&(here)) >> (n))
42*5e7646d2SAndroid Build Coastguard Worker #define	ISSETBACK(v, n)	((v) & ((unsigned)here >> (n)))
43*5e7646d2SAndroid Build Coastguard Worker /* function names */
44*5e7646d2SAndroid Build Coastguard Worker #define SNAMES			/* engine.c looks after details */
45*5e7646d2SAndroid Build Coastguard Worker 
46*5e7646d2SAndroid Build Coastguard Worker #include "engine.c"
47*5e7646d2SAndroid Build Coastguard Worker 
48*5e7646d2SAndroid Build Coastguard Worker /* now undo things */
49*5e7646d2SAndroid Build Coastguard Worker #undef	states
50*5e7646d2SAndroid Build Coastguard Worker #undef	CLEAR
51*5e7646d2SAndroid Build Coastguard Worker #undef	SET0
52*5e7646d2SAndroid Build Coastguard Worker #undef	SET1
53*5e7646d2SAndroid Build Coastguard Worker #undef	ISSET
54*5e7646d2SAndroid Build Coastguard Worker #undef	ASSIGN
55*5e7646d2SAndroid Build Coastguard Worker #undef	EQ
56*5e7646d2SAndroid Build Coastguard Worker #undef	STATEVARS
57*5e7646d2SAndroid Build Coastguard Worker #undef	STATESETUP
58*5e7646d2SAndroid Build Coastguard Worker #undef	STATETEARDOWN
59*5e7646d2SAndroid Build Coastguard Worker #undef	SETUP
60*5e7646d2SAndroid Build Coastguard Worker #undef	onestate
61*5e7646d2SAndroid Build Coastguard Worker #undef	INIT
62*5e7646d2SAndroid Build Coastguard Worker #undef	INC
63*5e7646d2SAndroid Build Coastguard Worker #undef	ISSTATEIN
64*5e7646d2SAndroid Build Coastguard Worker #undef	FWD
65*5e7646d2SAndroid Build Coastguard Worker #undef	BACK
66*5e7646d2SAndroid Build Coastguard Worker #undef	ISSETBACK
67*5e7646d2SAndroid Build Coastguard Worker #undef	SNAMES
68*5e7646d2SAndroid Build Coastguard Worker 
69*5e7646d2SAndroid Build Coastguard Worker /* macros for manipulating states, large version */
70*5e7646d2SAndroid Build Coastguard Worker #define	states	char *
71*5e7646d2SAndroid Build Coastguard Worker #define	CLEAR(v)	memset(v, 0, m->g->nstates)
72*5e7646d2SAndroid Build Coastguard Worker #define	SET0(v, n)	((v)[n] = 0)
73*5e7646d2SAndroid Build Coastguard Worker #define	SET1(v, n)	((v)[n] = 1)
74*5e7646d2SAndroid Build Coastguard Worker #define	ISSET(v, n)	((v)[n])
75*5e7646d2SAndroid Build Coastguard Worker #define	ASSIGN(d, s)	memcpy(d, s, m->g->nstates)
76*5e7646d2SAndroid Build Coastguard Worker #define	EQ(a, b)	(memcmp(a, b, m->g->nstates) == 0)
77*5e7646d2SAndroid Build Coastguard Worker #define	STATEVARS	int vn; char *space
78*5e7646d2SAndroid Build Coastguard Worker #define	STATESETUP(m, nv)	{ (m)->space = malloc((nv)*(m)->g->nstates); \
79*5e7646d2SAndroid Build Coastguard Worker 				if ((m)->space == NULL) return(REG_ESPACE); \
80*5e7646d2SAndroid Build Coastguard Worker 				(m)->vn = 0; }
81*5e7646d2SAndroid Build Coastguard Worker #define	STATETEARDOWN(m)	{ free((m)->space); }
82*5e7646d2SAndroid Build Coastguard Worker #define	SETUP(v)	((v) = &m->space[m->vn++ * m->g->nstates])
83*5e7646d2SAndroid Build Coastguard Worker #define	onestate	int
84*5e7646d2SAndroid Build Coastguard Worker #define	INIT(o, n)	((o) = (n))
85*5e7646d2SAndroid Build Coastguard Worker #define	INC(o)	((o)++)
86*5e7646d2SAndroid Build Coastguard Worker #define	ISSTATEIN(v, o)	((v)[o])
87*5e7646d2SAndroid Build Coastguard Worker /* some abbreviations; note that some of these know variable names! */
88*5e7646d2SAndroid Build Coastguard Worker /* do "if I'm here, I can also be there" etc without branches */
89*5e7646d2SAndroid Build Coastguard Worker #define	FWD(dst, src, n)	((dst)[here+(n)] |= (src)[here])
90*5e7646d2SAndroid Build Coastguard Worker #define	BACK(dst, src, n)	((dst)[here-(n)] |= (src)[here])
91*5e7646d2SAndroid Build Coastguard Worker #define	ISSETBACK(v, n)	((v)[here - (n)])
92*5e7646d2SAndroid Build Coastguard Worker /* function names */
93*5e7646d2SAndroid Build Coastguard Worker #define	LNAMES			/* flag */
94*5e7646d2SAndroid Build Coastguard Worker 
95*5e7646d2SAndroid Build Coastguard Worker #include "engine.c"
96*5e7646d2SAndroid Build Coastguard Worker 
97*5e7646d2SAndroid Build Coastguard Worker /*
98*5e7646d2SAndroid Build Coastguard Worker  - regexec - interface for matching
99*5e7646d2SAndroid Build Coastguard Worker  = extern int regexec(const regex_t *, const char *, size_t, \
100*5e7646d2SAndroid Build Coastguard Worker  =					regmatch_t [], int);
101*5e7646d2SAndroid Build Coastguard Worker  = #define	REG_NOTBOL	00001
102*5e7646d2SAndroid Build Coastguard Worker  = #define	REG_NOTEOL	00002
103*5e7646d2SAndroid Build Coastguard Worker  = #define	REG_STARTEND	00004
104*5e7646d2SAndroid Build Coastguard Worker  = #define	REG_TRACE	00400	// tracing of execution
105*5e7646d2SAndroid Build Coastguard Worker  = #define	REG_LARGE	01000	// force large representation
106*5e7646d2SAndroid Build Coastguard Worker  = #define	REG_BACKR	02000	// force use of backref code
107*5e7646d2SAndroid Build Coastguard Worker  *
108*5e7646d2SAndroid Build Coastguard Worker  * We put this here so we can exploit knowledge of the state representation
109*5e7646d2SAndroid Build Coastguard Worker  * when choosing which matcher to call.  Also, by this point the matchers
110*5e7646d2SAndroid Build Coastguard Worker  * have been prototyped.
111*5e7646d2SAndroid Build Coastguard Worker  */
112*5e7646d2SAndroid Build Coastguard Worker int				/* 0 success, REG_NOMATCH failure */
regexec(preg,string,nmatch,pmatch,eflags)113*5e7646d2SAndroid Build Coastguard Worker regexec(preg, string, nmatch, pmatch, eflags)
114*5e7646d2SAndroid Build Coastguard Worker const regex_t *preg;
115*5e7646d2SAndroid Build Coastguard Worker const char *string;
116*5e7646d2SAndroid Build Coastguard Worker size_t nmatch;
117*5e7646d2SAndroid Build Coastguard Worker regmatch_t pmatch[];
118*5e7646d2SAndroid Build Coastguard Worker int eflags;
119*5e7646d2SAndroid Build Coastguard Worker {
120*5e7646d2SAndroid Build Coastguard Worker 	register struct re_guts *g = preg->re_g;
121*5e7646d2SAndroid Build Coastguard Worker #ifdef REDEBUG
122*5e7646d2SAndroid Build Coastguard Worker #	define	GOODFLAGS(f)	(f)
123*5e7646d2SAndroid Build Coastguard Worker #else
124*5e7646d2SAndroid Build Coastguard Worker #	define	GOODFLAGS(f)	((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
125*5e7646d2SAndroid Build Coastguard Worker #endif
126*5e7646d2SAndroid Build Coastguard Worker 
127*5e7646d2SAndroid Build Coastguard Worker 	if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
128*5e7646d2SAndroid Build Coastguard Worker 		return(REG_BADPAT);
129*5e7646d2SAndroid Build Coastguard Worker 	assert(!(g->iflags&BAD));
130*5e7646d2SAndroid Build Coastguard Worker 	if (g->iflags&BAD)		/* backstop for no-debug case */
131*5e7646d2SAndroid Build Coastguard Worker 		return(REG_BADPAT);
132*5e7646d2SAndroid Build Coastguard Worker 	eflags = GOODFLAGS(eflags);
133*5e7646d2SAndroid Build Coastguard Worker 
134*5e7646d2SAndroid Build Coastguard Worker 	if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags&REG_LARGE))
135*5e7646d2SAndroid Build Coastguard Worker 		return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
136*5e7646d2SAndroid Build Coastguard Worker 	else
137*5e7646d2SAndroid Build Coastguard Worker 		return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
138*5e7646d2SAndroid Build Coastguard Worker }
139