]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - quiz/rxp.c
1 /* $NetBSD: rxp.c,v 1.5 1995/04/22 10:17:00 cgd 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
42 static char sccsid
[] = "@(#)rxp.c 8.1 (Berkeley) 5/31/93";
44 static char rcsid
[] = "$NetBSD: rxp.c,v 1.5 1995/04/22 10:17:00 cgd Exp $";
49 * regular expression parser
51 * external functions and return values are:
54 * FALSE parse failure; error message will be in char rxperr[]
56 * {...} optional pattern, equialent to [...|]
58 * [...] pattern delimiters
61 * TRUE string s matches compiled pattern
62 * FALSE match failure or regexp error
65 * char * reverse-engineered regular expression string
72 /* regexp tokens, arg */
73 #define LIT (-1) /* literal character, char */
74 #define SOT (-2) /* start text anchor, - */
75 #define EOT (-3) /* end text anchor, - */
76 #define GRP_S (-4) /* start alternate grp, ptr_to_end */
77 #define GRP_E (-5) /* end group, - */
78 #define ALT_S (-6) /* alternate starts, ptr_to_next */
79 #define ALT_E (-7) /* alternate ends, - */
80 #define END (-8) /* end of regexp, - */
82 typedef short Rxp_t
; /* type for regexp tokens */
84 static Rxp_t rxpbuf
[RXP_LINE_SZ
]; /* compiled regular expression buffer */
85 char rxperr
[128]; /* parser error message */
87 static int rxp__compile
__P((char *, int));
88 static char *rxp__expand
__P((int));
89 static int rxp__match
__P((char *, int, Rxp_t
*, Rxp_t
*, char *));
95 return (rxp__compile(s
, TRUE
));
99 rxp__compile(s
, first
)
113 *rp
++ = SOT
; /* auto-anchor: pat is really ^pat$ */
114 *rp
++ = GRP_S
; /* auto-group: ^pat$ is really ^[pat]$ */
121 if (rp
- rxpbuf
>= RXP_LINE_SZ
- 4) {
122 (void)snprintf(rxperr
, sizeof(rxperr
),
123 "regular expression too long %s", s
);
126 if (*sp
== ':' && !esc
)
143 if ((err
= rxp__compile(s
, FALSE
)) != TRUE
)
146 *grp_ptr
= rp
- rxpbuf
;
152 *alt_ptr
= rp
- rxpbuf
;
161 *alt_ptr
= rp
- rxpbuf
;
164 (void)snprintf(rxperr
, sizeof(rxperr
),
165 "unmatched alternator in regexp %s",
180 (void)snprintf(rxperr
, sizeof(rxperr
),
181 "unmatched alternator in regexp %s", s
);
185 *alt_ptr
= rp
- rxpbuf
;
187 *(rxpbuf
+ 2) = rp
- rxpbuf
;
194 * match string against compiled regular expression
200 return (rxp__match(s
, TRUE
, NULL
, NULL
, NULL
));
204 rxp__match(s
, first
, j_succ
, j_fail
, sp_fail
)
207 Rxp_t
*j_succ
; /* jump here on successful alt match */
208 Rxp_t
*j_fail
; /* jump here on failed match */
209 char *sp_fail
; /* reset sp to here on failed match */
221 while (rp
< rxpbuf
+ RXP_LINE_SZ
&& *rp
!= END
)
225 ch
= isascii(*rp
) && isupper(*rp
) ? tolower(*rp
) : *rp
;
245 grp_end
= rxpbuf
+ *rp
++;
249 if ((err
= rxp__match(sp
,
250 FALSE
, grp_end
, rxpbuf
+ *rp
++, sp
)) != TRUE
)
260 return (*rp
!= END
? FALSE
: TRUE
);
264 * Reverse engineer the regular expression, by picking first of all alternates.
269 return (rxp__expand(TRUE
));
276 static char buf
[RXP_LINE_SZ
/2];
286 while (rp
< rxpbuf
+ RXP_LINE_SZ
&& *rp
!= END
)
294 grp_ptr
= rxpbuf
+ *rp
;
296 if ((err
= rxp__expand(FALSE
)) == NULL
)