]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/rxp.c
1 /* $NetBSD: rxp.c,v 1.6 1997/09/20 14:28:19 lukem Exp $ */
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Jim R. Oldroyd at The Instruction Set and Keith Gabryelski at
9 * Commodore Business Machines.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #include <sys/cdefs.h>
43 static char sccsid
[] = "@(#)rxp.c 8.1 (Berkeley) 5/31/93";
45 __RCSID("$NetBSD: rxp.c,v 1.6 1997/09/20 14:28:19 lukem Exp $");
50 * regular expression parser
52 * external functions and return values are:
55 * FALSE parse failure; error message will be in char rxperr[]
57 * {...} optional pattern, equialent to [...|]
59 * [...] pattern delimiters
62 * TRUE string s matches compiled pattern
63 * FALSE match failure or regexp error
66 * char * reverse-engineered regular expression string
73 /* regexp tokens, arg */
74 #define LIT (-1) /* literal character, char */
75 #define SOT (-2) /* start text anchor, - */
76 #define EOT (-3) /* end text anchor, - */
77 #define GRP_S (-4) /* start alternate grp, ptr_to_end */
78 #define GRP_E (-5) /* end group, - */
79 #define ALT_S (-6) /* alternate starts, ptr_to_next */
80 #define ALT_E (-7) /* alternate ends, - */
81 #define END (-8) /* end of regexp, - */
83 typedef short Rxp_t
; /* type for regexp tokens */
85 static Rxp_t rxpbuf
[RXP_LINE_SZ
]; /* compiled regular expression buffer */
86 char rxperr
[128]; /* parser error message */
88 static int rxp__compile
__P((char *, int));
89 static char *rxp__expand
__P((int));
90 static int rxp__match
__P((char *, int, Rxp_t
*, Rxp_t
*, char *));
96 return (rxp__compile(s
, TRUE
));
100 rxp__compile(s
, first
)
114 *rp
++ = SOT
; /* auto-anchor: pat is really ^pat$ */
115 *rp
++ = GRP_S
; /* auto-group: ^pat$ is really ^[pat]$ */
122 if (rp
- rxpbuf
>= RXP_LINE_SZ
- 4) {
123 (void)snprintf(rxperr
, sizeof(rxperr
),
124 "regular expression too long %s", s
);
127 if (*sp
== ':' && !esc
)
144 if ((err
= rxp__compile(s
, FALSE
)) != TRUE
)
147 *grp_ptr
= rp
- rxpbuf
;
153 *alt_ptr
= rp
- rxpbuf
;
162 *alt_ptr
= rp
- rxpbuf
;
165 (void)snprintf(rxperr
, sizeof(rxperr
),
166 "unmatched alternator in regexp %s",
181 (void)snprintf(rxperr
, sizeof(rxperr
),
182 "unmatched alternator in regexp %s", s
);
186 *alt_ptr
= rp
- rxpbuf
;
188 *(rxpbuf
+ 2) = rp
- rxpbuf
;
195 * match string against compiled regular expression
201 return (rxp__match(s
, TRUE
, NULL
, NULL
, NULL
));
205 rxp__match(s
, first
, j_succ
, j_fail
, sp_fail
)
208 Rxp_t
*j_succ
; /* jump here on successful alt match */
209 Rxp_t
*j_fail
; /* jump here on failed match */
210 char *sp_fail
; /* reset sp to here on failed match */
215 Rxp_t
*grp_end
= NULL
;
222 while (rp
< rxpbuf
+ RXP_LINE_SZ
&& *rp
!= END
)
226 ch
= isascii(*rp
) && isupper(*rp
) ? tolower(*rp
) : *rp
;
246 grp_end
= rxpbuf
+ *rp
++;
250 if ((err
= rxp__match(sp
,
251 FALSE
, grp_end
, rxpbuf
+ *rp
++, sp
)) != TRUE
)
261 return (*rp
!= END
? FALSE
: TRUE
);
265 * Reverse engineer the regular expression, by picking first of all alternates.
270 return (rxp__expand(TRUE
));
277 static char buf
[RXP_LINE_SZ
/2];
287 while (rp
< rxpbuf
+ RXP_LINE_SZ
&& *rp
!= END
)
295 grp_ptr
= rxpbuf
+ *rp
;
297 if ((err
= rxp__expand(FALSE
)) == NULL
)