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