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
37 * @(#)rxp.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/games/quiz/rxp.c,v 1.5 1999/12/12 02:29:54 billf Exp $
39 * $DragonFly: src/games/quiz/rxp.c,v 1.4 2005/04/25 16:10:24 liamfoy Exp $
43 * regular expression parser
45 * external functions and return values are:
48 * FALSE parse failure; error message will be in char rxperr[]
50 * {...} optional pattern, equialent to [...|]
52 * [...] pattern delimiters
55 * TRUE string s matches compiled pattern
56 * FALSE match failure or regexp error
59 * char * reverse-engineered regular expression string
66 /* regexp tokens, arg */
67 #define LIT (-1) /* literal character, char */
68 #define SOT (-2) /* start text anchor, - */
69 #define EOT (-3) /* end text anchor, - */
70 #define GRP_S (-4) /* start alternate grp, ptr_to_end */
71 #define GRP_E (-5) /* end group, - */
72 #define ALT_S (-6) /* alternate starts, ptr_to_next */
73 #define ALT_E (-7) /* alternate ends, - */
74 #define END (-8) /* end of regexp, - */
76 typedef short Rxp_t
; /* type for regexp tokens */
78 static Rxp_t rxpbuf
[RXP_LINE_SZ
]; /* compiled regular expression buffer */
79 char rxperr
[128]; /* parser error message */
81 static int rxp__compile (char *, int);
82 static char *rxp__expand (int);
83 static int rxp__match (char *, int, Rxp_t
*, Rxp_t
*, char *);
88 return (rxp__compile(s
, TRUE
));
92 rxp__compile(char *s
, int first
)
104 *rp
++ = SOT
; /* auto-anchor: pat is really ^pat$ */
105 *rp
++ = GRP_S
; /* auto-group: ^pat$ is really ^[pat]$ */
112 if (rp
- rxpbuf
>= RXP_LINE_SZ
- 4) {
113 (void)snprintf(rxperr
, sizeof(rxperr
),
114 "regular expression too long %s", s
);
117 if (*sp
== ':' && !esc
)
134 if ((err
= rxp__compile(s
, FALSE
)) != TRUE
)
137 *grp_ptr
= rp
- rxpbuf
;
143 *alt_ptr
= rp
- rxpbuf
;
152 *alt_ptr
= rp
- rxpbuf
;
155 (void)snprintf(rxperr
, sizeof(rxperr
),
156 "unmatched alternator in regexp %s",
171 (void)snprintf(rxperr
, sizeof(rxperr
),
172 "unmatched alternator in regexp %s", s
);
176 *alt_ptr
= rp
- rxpbuf
;
178 *(rxpbuf
+ 2) = rp
- rxpbuf
;
185 * match string against compiled regular expression
190 return (rxp__match(s
, TRUE
, NULL
, NULL
, NULL
));
194 * jump to j_succ on successful alt match
195 * jump to j_fail on failed match
196 * reset sp to sp_fail on failed match
199 rxp__match(char *s
, int first
, Rxp_t
*j_succ
, Rxp_t
*j_fail
, char *sp_fail
)
212 while (rp
< rxpbuf
+ RXP_LINE_SZ
&& *rp
!= END
)
216 ch
= isascii(*rp
) && isupper(*rp
) ? tolower(*rp
) : *rp
;
236 grp_end
= rxpbuf
+ *rp
++;
240 if ((err
= rxp__match(sp
,
241 FALSE
, grp_end
, rxpbuf
+ *rp
++, sp
)) != TRUE
)
251 return (*rp
!= END
? FALSE
: TRUE
);
255 * Reverse engineer the regular expression, by picking first of all alternates.
260 return (rxp__expand(TRUE
));
264 rxp__expand(int first
)
266 static char buf
[RXP_LINE_SZ
/2];
276 while (rp
< rxpbuf
+ RXP_LINE_SZ
&& *rp
!= END
)
284 grp_ptr
= rxpbuf
+ *rp
;
286 if ((err
= rxp__expand(FALSE
)) == NULL
)