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.
|
---|
8 | *
|
---|
9 | * Redistribution and use in source and binary forms, with or without
|
---|
10 | * modification, are permitted provided that the following conditions
|
---|
11 | * are met:
|
---|
12 | * 1. Redistributions of source code must retain the above copyright
|
---|
13 | * notice, this list of conditions and the following disclaimer.
|
---|
14 | * 2. Redistributions in binary form must reproduce the above copyright
|
---|
15 | * notice, this list of conditions and the following disclaimer in the
|
---|
16 | * documentation and/or other materials provided with the distribution.
|
---|
17 | * 3. All advertising materials mentioning features or use of this software
|
---|
18 | * must display the following acknowledgement:
|
---|
19 | * This product includes software developed by the University of
|
---|
20 | * California, Berkeley and its contributors.
|
---|
21 | * 4. Neither the name of the University nor the names of its contributors
|
---|
22 | * may be used to endorse or promote products derived from this software
|
---|
23 | * without specific prior written permission.
|
---|
24 | *
|
---|
25 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
---|
26 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
---|
27 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
---|
28 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
---|
29 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
---|
30 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
---|
31 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
---|
32 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
---|
33 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
---|
34 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
---|
35 | * SUCH DAMAGE.
|
---|
36 | *
|
---|
37 | * @(#)regexec.c 8.3 (Berkeley) 3/20/94
|
---|
38 | */
|
---|
39 |
|
---|
40 | #if defined(LIBC_SCCS) && !defined(lint)
|
---|
41 | static char sccsid[] = "@(#)regexec.c 8.3 (Berkeley) 3/20/94";
|
---|
42 | #endif /* LIBC_SCCS and not lint */
|
---|
43 |
|
---|
44 | /*
|
---|
45 | * the outer shell of regexec()
|
---|
46 | *
|
---|
47 | * This file includes engine.c *twice*, after muchos fiddling with the
|
---|
48 | * macros that code uses. This lets the same code operate on two different
|
---|
49 | * representations for state sets.
|
---|
50 | */
|
---|
51 | #include <sys/types.h>
|
---|
52 | #include <stdio.h>
|
---|
53 | #include <stdlib.h>
|
---|
54 | #include <string.h>
|
---|
55 | #include <limits.h>
|
---|
56 | #ifdef __minix_vmd
|
---|
57 | #include <bsd/asciictype.h>
|
---|
58 | #else
|
---|
59 | #include <ctype.h>
|
---|
60 | #endif
|
---|
61 | #include <regex.h>
|
---|
62 |
|
---|
63 | #include "utils.h"
|
---|
64 | #include "regex2.h"
|
---|
65 |
|
---|
66 | static int nope = 0; /* for use in asserts; shuts lint up */
|
---|
67 |
|
---|
68 | /* macros for manipulating states, small version */
|
---|
69 | #define states long
|
---|
70 | #define states1 states /* for later use in regexec() decision */
|
---|
71 | #define CLEAR(v) ((v) = 0)
|
---|
72 | #define SET0(v, n) ((v) &= ~(1 << (n)))
|
---|
73 | #define SET1(v, n) ((v) |= 1 << (n))
|
---|
74 | #define ISSET(v, n) ((v) & (1 << (n)))
|
---|
75 | #define ASSIGN(d, s) ((d) = (s))
|
---|
76 | #define EQ(a, b) ((a) == (b))
|
---|
77 | #define STATEVARS int dummy /* dummy version */
|
---|
78 | #define STATESETUP(m, n) /* nothing */
|
---|
79 | #define STATETEARDOWN(m) /* nothing */
|
---|
80 | #define SETUP(v) ((v) = 0)
|
---|
81 | #define onestate int
|
---|
82 | #define INIT(o, n) ((o) = (unsigned)1 << (n))
|
---|
83 | #define INC(o) ((o) <<= 1)
|
---|
84 | #define ISSTATEIN(v, o) ((v) & (o))
|
---|
85 | /* some abbreviations; note that some of these know variable names! */
|
---|
86 | /* do "if I'm here, I can also be there" etc without branches */
|
---|
87 | #define FWD(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) << (n))
|
---|
88 | #define BACK(dst, src, n) ((dst) |= ((unsigned)(src)&(here)) >> (n))
|
---|
89 | #define ISSETBACK(v, n) ((v) & ((unsigned)here >> (n)))
|
---|
90 | /* function names */
|
---|
91 | #define SNAMES /* engine.c looks after details */
|
---|
92 |
|
---|
93 | #include "engine.c"
|
---|
94 |
|
---|
95 | /* now undo things */
|
---|
96 | #undef states
|
---|
97 | #undef CLEAR
|
---|
98 | #undef SET0
|
---|
99 | #undef SET1
|
---|
100 | #undef ISSET
|
---|
101 | #undef ASSIGN
|
---|
102 | #undef EQ
|
---|
103 | #undef STATEVARS
|
---|
104 | #undef STATESETUP
|
---|
105 | #undef STATETEARDOWN
|
---|
106 | #undef SETUP
|
---|
107 | #undef onestate
|
---|
108 | #undef INIT
|
---|
109 | #undef INC
|
---|
110 | #undef ISSTATEIN
|
---|
111 | #undef FWD
|
---|
112 | #undef BACK
|
---|
113 | #undef ISSETBACK
|
---|
114 | #undef SNAMES
|
---|
115 |
|
---|
116 | /* macros for manipulating states, large version */
|
---|
117 | #define states char *
|
---|
118 | #define CLEAR(v) memset(v, 0, m->g->nstates)
|
---|
119 | #define SET0(v, n) ((v)[n] = 0)
|
---|
120 | #define SET1(v, n) ((v)[n] = 1)
|
---|
121 | #define ISSET(v, n) ((v)[n])
|
---|
122 | #define ASSIGN(d, s) memcpy(d, s, m->g->nstates)
|
---|
123 | #define EQ(a, b) (memcmp(a, b, m->g->nstates) == 0)
|
---|
124 | #define STATEVARS int vn; char *space
|
---|
125 | #define STATESETUP(m, nv) { (m)->space = malloc((nv)*(m)->g->nstates); \
|
---|
126 | if ((m)->space == NULL) return(REG_ESPACE); \
|
---|
127 | (m)->vn = 0; }
|
---|
128 | #define STATETEARDOWN(m) { free((m)->space); }
|
---|
129 | #define SETUP(v) ((v) = &m->space[m->vn++ * m->g->nstates])
|
---|
130 | #define onestate int
|
---|
131 | #define INIT(o, n) ((o) = (n))
|
---|
132 | #define INC(o) ((o)++)
|
---|
133 | #define ISSTATEIN(v, o) ((v)[o])
|
---|
134 | /* some abbreviations; note that some of these know variable names! */
|
---|
135 | /* do "if I'm here, I can also be there" etc without branches */
|
---|
136 | #define FWD(dst, src, n) ((dst)[here+(n)] |= (src)[here])
|
---|
137 | #define BACK(dst, src, n) ((dst)[here-(n)] |= (src)[here])
|
---|
138 | #define ISSETBACK(v, n) ((v)[here - (n)])
|
---|
139 | /* function names */
|
---|
140 | #define LNAMES /* flag */
|
---|
141 |
|
---|
142 | #include "engine.c"
|
---|
143 |
|
---|
144 | /*
|
---|
145 | - regexec - interface for matching
|
---|
146 | = extern int regexec(const regex_t *, const char *, size_t, \
|
---|
147 | = regmatch_t [], int);
|
---|
148 | = #define REG_NOTBOL 00001
|
---|
149 | = #define REG_NOTEOL 00002
|
---|
150 | = #define REG_STARTEND 00004
|
---|
151 | = #define REG_TRACE 00400 // tracing of execution
|
---|
152 | = #define REG_LARGE 01000 // force large representation
|
---|
153 | = #define REG_BACKR 02000 // force use of backref code
|
---|
154 | *
|
---|
155 | * We put this here so we can exploit knowledge of the state representation
|
---|
156 | * when choosing which matcher to call. Also, by this point the matchers
|
---|
157 | * have been prototyped.
|
---|
158 | */
|
---|
159 | int /* 0 success, REG_NOMATCH failure */
|
---|
160 | regexec(preg, string, nmatch, pmatch, eflags)
|
---|
161 | const regex_t *preg;
|
---|
162 | const char *string;
|
---|
163 | size_t nmatch;
|
---|
164 | regmatch_t pmatch[];
|
---|
165 | int eflags;
|
---|
166 | {
|
---|
167 | register struct re_guts *g = preg->re_g;
|
---|
168 | #ifdef REDEBUG
|
---|
169 | # define GOODFLAGS(f) (f)
|
---|
170 | #else
|
---|
171 | # define GOODFLAGS(f) ((f)&(REG_NOTBOL|REG_NOTEOL|REG_STARTEND))
|
---|
172 | #endif
|
---|
173 |
|
---|
174 | if (preg->re_magic != MAGIC1 || g->magic != MAGIC2)
|
---|
175 | return(REG_BADPAT);
|
---|
176 | assert(!(g->iflags&BAD));
|
---|
177 | if (g->iflags&BAD) /* backstop for no-debug case */
|
---|
178 | return(REG_BADPAT);
|
---|
179 | eflags = GOODFLAGS(eflags);
|
---|
180 |
|
---|
181 | if (g->nstates <= CHAR_BIT*sizeof(states1) && !(eflags®_LARGE))
|
---|
182 | return(smatcher(g, (char *)string, nmatch, pmatch, eflags));
|
---|
183 | else
|
---|
184 | return(lmatcher(g, (char *)string, nmatch, pmatch, eflags));
|
---|
185 | }
|
---|
186 |
|
---|
187 | /*
|
---|
188 | * $PchId: regexec.c,v 1.2 1996/03/12 19:10:15 philip Exp $
|
---|
189 | */
|
---|