2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
7 * Commodore Business Machines.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
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.
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
40 static char sccsid[] = "@(#)rxp.c 8.1 (Berkeley) 5/31/93";
42 static const char rcsid[] =
43 "$FreeBSD: src/games/quiz/rxp.c,v 1.5 1999/12/12 02:29:54 billf Exp $";
47 * regular expression parser
49 * external functions and return values are:
52 * FALSE parse failure; error message will be in char rxperr[]
54 * {...} optional pattern, equialent to [...|]
56 * [...] pattern delimiters
59 * TRUE string s matches compiled pattern
60 * FALSE match failure or regexp error
63 * char * reverse-engineered regular expression string
70 /* regexp tokens, arg */
71 #define LIT (-1) /* literal character, char */
72 #define SOT (-2) /* start text anchor, - */
73 #define EOT (-3) /* end text anchor, - */
74 #define GRP_S (-4) /* start alternate grp, ptr_to_end */
75 #define GRP_E (-5) /* end group, - */
76 #define ALT_S (-6) /* alternate starts, ptr_to_next */
77 #define ALT_E (-7) /* alternate ends, - */
78 #define END (-8) /* end of regexp, - */
80 typedef short Rxp_t; /* type for regexp tokens */
82 static Rxp_t rxpbuf[RXP_LINE_SZ]; /* compiled regular expression buffer */
83 char rxperr[128]; /* parser error message */
85 static int rxp__compile __P((char *, int));
86 static char *rxp__expand __P((int));
87 static int rxp__match __P((char *, int, Rxp_t *, Rxp_t *, char *));
93 return (rxp__compile(s, TRUE));
97 rxp__compile(s, first)
111 *rp++ = SOT; /* auto-anchor: pat is really ^pat$ */
112 *rp++ = GRP_S; /* auto-group: ^pat$ is really ^[pat]$ */
119 if (rp - rxpbuf >= RXP_LINE_SZ - 4) {
120 (void)snprintf(rxperr, sizeof(rxperr),
121 "regular expression too long %s", s);
124 if (*sp == ':' && !esc)
141 if ((err = rxp__compile(s, FALSE)) != TRUE)
144 *grp_ptr = rp - rxpbuf;
150 *alt_ptr = rp - rxpbuf;
159 *alt_ptr = rp - rxpbuf;
162 (void)snprintf(rxperr, sizeof(rxperr),
163 "unmatched alternator in regexp %s",
178 (void)snprintf(rxperr, sizeof(rxperr),
179 "unmatched alternator in regexp %s", s);
183 *alt_ptr = rp - rxpbuf;
185 *(rxpbuf + 2) = rp - rxpbuf;
192 * match string against compiled regular expression
198 return (rxp__match(s, TRUE, NULL, NULL, NULL));
202 rxp__match(s, first, j_succ, j_fail, sp_fail)
205 Rxp_t *j_succ; /* jump here on successful alt match */
206 Rxp_t *j_fail; /* jump here on failed match */
207 char *sp_fail; /* reset sp to here on failed match */
220 while (rp < rxpbuf + RXP_LINE_SZ && *rp != END)
224 ch = isascii(*rp) && isupper(*rp) ? tolower(*rp) : *rp;
244 grp_end = rxpbuf + *rp++;
248 if ((err = rxp__match(sp,
249 FALSE, grp_end, rxpbuf + *rp++, sp)) != TRUE)
259 return (*rp != END ? FALSE : TRUE);
263 * Reverse engineer the regular expression, by picking first of all alternates.
268 return (rxp__expand(TRUE));
275 static char buf[RXP_LINE_SZ/2];
285 while (rp < rxpbuf + RXP_LINE_SZ && *rp != END)
293 grp_ptr = rxpbuf + *rp;
295 if ((err = rxp__expand(FALSE)) == NULL)