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