rxp.c revision 1.12 1 1.12 jsm /* $NetBSD: rxp.c,v 1.12 2004/01/27 20:30:30 jsm Exp $ */
2 1.5 cgd
3 1.1 cgd /*-
4 1.4 cgd * Copyright (c) 1991, 1993
5 1.4 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * This code is derived from software contributed to Berkeley by
8 1.4 cgd * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
9 1.4 cgd * Commodore Business Machines.
10 1.1 cgd *
11 1.1 cgd * Redistribution and use in source and binary forms, with or without
12 1.1 cgd * modification, are permitted provided that the following conditions
13 1.1 cgd * are met:
14 1.1 cgd * 1. Redistributions of source code must retain the above copyright
15 1.1 cgd * notice, this list of conditions and the following disclaimer.
16 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 cgd * notice, this list of conditions and the following disclaimer in the
18 1.1 cgd * documentation and/or other materials provided with the distribution.
19 1.11 agc * 3. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.6 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.5 cgd #if 0
39 1.4 cgd static char sccsid[] = "@(#)rxp.c 8.1 (Berkeley) 5/31/93";
40 1.5 cgd #else
41 1.12 jsm __RCSID("$NetBSD: rxp.c,v 1.12 2004/01/27 20:30:30 jsm Exp $");
42 1.5 cgd #endif
43 1.1 cgd #endif /* not lint */
44 1.1 cgd
45 1.1 cgd /*
46 1.1 cgd * regular expression parser
47 1.1 cgd *
48 1.1 cgd * external functions and return values are:
49 1.1 cgd * rxp_compile(s)
50 1.1 cgd * TRUE success
51 1.1 cgd * FALSE parse failure; error message will be in char rxperr[]
52 1.1 cgd * metas are:
53 1.1 cgd * {...} optional pattern, equialent to [...|]
54 1.1 cgd * | alternate pattern
55 1.1 cgd * [...] pattern delimiters
56 1.1 cgd *
57 1.1 cgd * rxp_match(s)
58 1.1 cgd * TRUE string s matches compiled pattern
59 1.1 cgd * FALSE match failure or regexp error
60 1.1 cgd *
61 1.1 cgd * rxp_expand()
62 1.1 cgd * char * reverse-engineered regular expression string
63 1.1 cgd * NULL regexp error
64 1.1 cgd */
65 1.1 cgd
66 1.1 cgd #include <stdio.h>
67 1.10 thorpej #include <stdlib.h>
68 1.1 cgd #include <ctype.h>
69 1.1 cgd #include "quiz.h"
70 1.1 cgd /* regexp tokens, arg */
71 1.1 cgd #define LIT (-1) /* literal character, char */
72 1.1 cgd #define SOT (-2) /* start text anchor, - */
73 1.1 cgd #define EOT (-3) /* end text anchor, - */
74 1.1 cgd #define GRP_S (-4) /* start alternate grp, ptr_to_end */
75 1.1 cgd #define GRP_E (-5) /* end group, - */
76 1.1 cgd #define ALT_S (-6) /* alternate starts, ptr_to_next */
77 1.1 cgd #define ALT_E (-7) /* alternate ends, - */
78 1.1 cgd #define END (-8) /* end of regexp, - */
79 1.1 cgd
80 1.1 cgd typedef short Rxp_t; /* type for regexp tokens */
81 1.1 cgd
82 1.1 cgd static Rxp_t rxpbuf[RXP_LINE_SZ]; /* compiled regular expression buffer */
83 1.1 cgd char rxperr[128]; /* parser error message */
84 1.1 cgd
85 1.12 jsm static int rxp__compile(const char *, int);
86 1.12 jsm static char *rxp__expand(int);
87 1.12 jsm static int rxp__match(const char *, int, Rxp_t *, Rxp_t *, const char *);
88 1.1 cgd
89 1.1 cgd int
90 1.1 cgd rxp_compile(s)
91 1.7 jsm const char * s;
92 1.1 cgd {
93 1.1 cgd return (rxp__compile(s, TRUE));
94 1.1 cgd }
95 1.1 cgd
96 1.1 cgd static int
97 1.1 cgd rxp__compile(s, first)
98 1.7 jsm const char *s;
99 1.1 cgd int first;
100 1.1 cgd {
101 1.1 cgd static Rxp_t *rp;
102 1.7 jsm static const char *sp;
103 1.1 cgd Rxp_t *grp_ptr;
104 1.1 cgd Rxp_t *alt_ptr;
105 1.1 cgd int esc, err;
106 1.1 cgd
107 1.1 cgd esc = 0;
108 1.1 cgd if (first) {
109 1.1 cgd rp = rxpbuf;
110 1.1 cgd sp = s;
111 1.1 cgd *rp++ = SOT; /* auto-anchor: pat is really ^pat$ */
112 1.1 cgd *rp++ = GRP_S; /* auto-group: ^pat$ is really ^[pat]$ */
113 1.1 cgd *rp++ = 0;
114 1.1 cgd }
115 1.1 cgd *rp++ = ALT_S;
116 1.1 cgd alt_ptr = rp;
117 1.1 cgd *rp++ = 0;
118 1.1 cgd for (; *sp; ++sp) {
119 1.1 cgd if (rp - rxpbuf >= RXP_LINE_SZ - 4) {
120 1.1 cgd (void)snprintf(rxperr, sizeof(rxperr),
121 1.1 cgd "regular expression too long %s", s);
122 1.1 cgd return (FALSE);
123 1.1 cgd }
124 1.1 cgd if (*sp == ':' && !esc)
125 1.1 cgd break;
126 1.1 cgd if (esc) {
127 1.1 cgd *rp++ = LIT;
128 1.1 cgd *rp++ = *sp;
129 1.1 cgd esc = 0;
130 1.1 cgd }
131 1.1 cgd else switch (*sp) {
132 1.1 cgd case '\\':
133 1.1 cgd esc = 1;
134 1.1 cgd break;
135 1.1 cgd case '{':
136 1.1 cgd case '[':
137 1.1 cgd *rp++ = GRP_S;
138 1.1 cgd grp_ptr = rp;
139 1.1 cgd *rp++ = 0;
140 1.1 cgd sp++;
141 1.1 cgd if ((err = rxp__compile(s, FALSE)) != TRUE)
142 1.1 cgd return (err);
143 1.1 cgd *rp++ = GRP_E;
144 1.1 cgd *grp_ptr = rp - rxpbuf;
145 1.1 cgd break;
146 1.1 cgd case '}':
147 1.1 cgd case ']':
148 1.1 cgd case '|':
149 1.1 cgd *rp++ = ALT_E;
150 1.1 cgd *alt_ptr = rp - rxpbuf;
151 1.1 cgd if (*sp != ']') {
152 1.1 cgd *rp++ = ALT_S;
153 1.1 cgd alt_ptr = rp;
154 1.1 cgd *rp++ = 0;
155 1.1 cgd }
156 1.1 cgd if (*sp != '|') {
157 1.1 cgd if (*sp != ']') {
158 1.1 cgd *rp++ = ALT_E;
159 1.1 cgd *alt_ptr = rp - rxpbuf;
160 1.1 cgd }
161 1.1 cgd if (first) {
162 1.1 cgd (void)snprintf(rxperr, sizeof(rxperr),
163 1.1 cgd "unmatched alternator in regexp %s",
164 1.1 cgd s);
165 1.1 cgd return (FALSE);
166 1.1 cgd }
167 1.1 cgd return (TRUE);
168 1.1 cgd }
169 1.1 cgd break;
170 1.1 cgd default:
171 1.1 cgd *rp++ = LIT;
172 1.1 cgd *rp++ = *sp;
173 1.1 cgd esc = 0;
174 1.1 cgd break;
175 1.1 cgd }
176 1.1 cgd }
177 1.1 cgd if (!first) {
178 1.1 cgd (void)snprintf(rxperr, sizeof(rxperr),
179 1.1 cgd "unmatched alternator in regexp %s", s);
180 1.1 cgd return (FALSE);
181 1.1 cgd }
182 1.1 cgd *rp++ = ALT_E;
183 1.1 cgd *alt_ptr = rp - rxpbuf;
184 1.1 cgd *rp++ = GRP_E;
185 1.1 cgd *(rxpbuf + 2) = rp - rxpbuf;
186 1.1 cgd *rp++ = EOT;
187 1.1 cgd *rp = END;
188 1.1 cgd return (TRUE);
189 1.1 cgd }
190 1.1 cgd
191 1.1 cgd /*
192 1.1 cgd * match string against compiled regular expression
193 1.1 cgd */
194 1.1 cgd int
195 1.1 cgd rxp_match(s)
196 1.7 jsm const char * s;
197 1.1 cgd {
198 1.1 cgd return (rxp__match(s, TRUE, NULL, NULL, NULL));
199 1.1 cgd }
200 1.1 cgd
201 1.1 cgd static int
202 1.1 cgd rxp__match(s, first, j_succ, j_fail, sp_fail)
203 1.7 jsm const char *s;
204 1.1 cgd int first;
205 1.1 cgd Rxp_t *j_succ; /* jump here on successful alt match */
206 1.1 cgd Rxp_t *j_fail; /* jump here on failed match */
207 1.7 jsm const char *sp_fail; /* reset sp to here on failed match */
208 1.1 cgd {
209 1.1 cgd static Rxp_t *rp;
210 1.7 jsm static const char *sp;
211 1.6 lukem int ch;
212 1.6 lukem Rxp_t *grp_end = NULL;
213 1.1 cgd
214 1.1 cgd if (first) {
215 1.1 cgd rp = rxpbuf;
216 1.1 cgd sp = s;
217 1.1 cgd }
218 1.1 cgd while (rp < rxpbuf + RXP_LINE_SZ && *rp != END)
219 1.1 cgd switch(*rp) {
220 1.1 cgd case LIT:
221 1.1 cgd rp++;
222 1.1 cgd ch = isascii(*rp) && isupper(*rp) ? tolower(*rp) : *rp;
223 1.1 cgd if (ch != *sp++) {
224 1.1 cgd rp = j_fail;
225 1.1 cgd sp = sp_fail;
226 1.8 dbj return (FALSE);
227 1.1 cgd }
228 1.1 cgd rp++;
229 1.1 cgd break;
230 1.1 cgd case SOT:
231 1.1 cgd if (sp != s)
232 1.1 cgd return (FALSE);
233 1.1 cgd rp++;
234 1.1 cgd break;
235 1.1 cgd case EOT:
236 1.1 cgd if (*sp != 0)
237 1.1 cgd return (FALSE);
238 1.1 cgd rp++;
239 1.1 cgd break;
240 1.1 cgd case GRP_S:
241 1.1 cgd rp++;
242 1.1 cgd grp_end = rxpbuf + *rp++;
243 1.1 cgd break;
244 1.1 cgd case ALT_S:
245 1.1 cgd rp++;
246 1.8 dbj rxp__match(sp, FALSE, grp_end, rxpbuf + *rp++, sp);
247 1.1 cgd break;
248 1.1 cgd case ALT_E:
249 1.1 cgd rp = j_succ;
250 1.1 cgd return (TRUE);
251 1.1 cgd case GRP_E:
252 1.8 dbj rp = j_fail;
253 1.8 dbj sp = sp_fail;
254 1.8 dbj return (FALSE);
255 1.1 cgd default:
256 1.8 dbj abort();
257 1.1 cgd }
258 1.1 cgd return (*rp != END ? FALSE : TRUE);
259 1.1 cgd }
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * Reverse engineer the regular expression, by picking first of all alternates.
263 1.1 cgd */
264 1.1 cgd char *
265 1.1 cgd rxp_expand()
266 1.1 cgd {
267 1.1 cgd return (rxp__expand(TRUE));
268 1.1 cgd }
269 1.1 cgd
270 1.1 cgd static char *
271 1.1 cgd rxp__expand(first)
272 1.1 cgd int first;
273 1.1 cgd {
274 1.1 cgd static char buf[RXP_LINE_SZ/2];
275 1.1 cgd static Rxp_t *rp;
276 1.1 cgd static char *bp;
277 1.1 cgd Rxp_t *grp_ptr;
278 1.1 cgd char *err;
279 1.1 cgd
280 1.1 cgd if (first) {
281 1.1 cgd rp = rxpbuf;
282 1.1 cgd bp = buf;
283 1.1 cgd }
284 1.1 cgd while (rp < rxpbuf + RXP_LINE_SZ && *rp != END)
285 1.1 cgd switch(*rp) {
286 1.1 cgd case LIT:
287 1.1 cgd rp++;
288 1.1 cgd *bp++ = *rp++;
289 1.1 cgd break;
290 1.1 cgd case GRP_S:
291 1.1 cgd rp++;
292 1.1 cgd grp_ptr = rxpbuf + *rp;
293 1.1 cgd rp++;
294 1.1 cgd if ((err = rxp__expand(FALSE)) == NULL)
295 1.1 cgd return (err);
296 1.1 cgd rp = grp_ptr;
297 1.1 cgd break;
298 1.1 cgd case ALT_E:
299 1.1 cgd return (buf);
300 1.1 cgd case ALT_S:
301 1.1 cgd rp++;
302 1.1 cgd /* FALLTHROUGH */
303 1.1 cgd case SOT:
304 1.1 cgd case EOT:
305 1.1 cgd case GRP_E:
306 1.1 cgd rp++;
307 1.1 cgd break;
308 1.1 cgd default:
309 1.1 cgd return (NULL);
310 1.1 cgd }
311 1.1 cgd if (first) {
312 1.1 cgd if (*rp != END)
313 1.1 cgd return (NULL);
314 1.1 cgd *bp = '\0';
315 1.1 cgd }
316 1.1 cgd return (buf);
317 1.1 cgd }
318