Home | History | Annotate | Line # | Download | only in quiz
rxp.c revision 1.10
      1  1.10  thorpej /*	$NetBSD: rxp.c,v 1.10 2002/12/06 01:54:55 thorpej 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.10  thorpej __RCSID("$NetBSD: rxp.c,v 1.10 2002/12/06 01:54:55 thorpej 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.10  thorpej #include <stdlib.h>
     72   1.1      cgd #include <ctype.h>
     73   1.1      cgd #include "quiz.h"
     74   1.1      cgd 					/* regexp tokens,	arg */
     75   1.1      cgd #define	LIT	(-1)			/* literal character,	char */
     76   1.1      cgd #define	SOT	(-2)			/* start text anchor,	- */
     77   1.1      cgd #define	EOT	(-3)			/* end text anchor,	- */
     78   1.1      cgd #define	GRP_S	(-4)			/* start alternate grp,	ptr_to_end */
     79   1.1      cgd #define	GRP_E	(-5)			/* end group,		- */
     80   1.1      cgd #define	ALT_S	(-6)			/* alternate starts,	ptr_to_next */
     81   1.1      cgd #define	ALT_E	(-7)			/* alternate ends,	- */
     82   1.1      cgd #define	END	(-8)			/* end of regexp,	- */
     83   1.1      cgd 
     84   1.1      cgd typedef short Rxp_t;			/* type for regexp tokens */
     85   1.1      cgd 
     86   1.1      cgd static Rxp_t rxpbuf[RXP_LINE_SZ];	/* compiled regular expression buffer */
     87   1.1      cgd char rxperr[128];			/* parser error message */
     88   1.1      cgd 
     89   1.7      jsm static int	 rxp__compile __P((const char *, int));
     90   1.2  mycroft static char	*rxp__expand __P((int));
     91   1.7      jsm static int	 rxp__match __P((const char *, int, Rxp_t *, Rxp_t *, const char *));
     92   1.1      cgd 
     93   1.1      cgd int
     94   1.1      cgd rxp_compile(s)
     95   1.7      jsm 	const char *	s;
     96   1.1      cgd {
     97   1.1      cgd 	return (rxp__compile(s, TRUE));
     98   1.1      cgd }
     99   1.1      cgd 
    100   1.1      cgd static int
    101   1.1      cgd rxp__compile(s, first)
    102   1.7      jsm 	const char *s;
    103   1.1      cgd 	int first;
    104   1.1      cgd {
    105   1.1      cgd 	static Rxp_t *rp;
    106   1.7      jsm 	static const char *sp;
    107   1.1      cgd 	Rxp_t *grp_ptr;
    108   1.1      cgd 	Rxp_t *alt_ptr;
    109   1.1      cgd 	int esc, err;
    110   1.1      cgd 
    111   1.1      cgd 	esc = 0;
    112   1.1      cgd 	if (first) {
    113   1.1      cgd 		rp = rxpbuf;
    114   1.1      cgd 		sp = s;
    115   1.1      cgd 		*rp++ = SOT;	/* auto-anchor: pat is really ^pat$ */
    116   1.1      cgd 		*rp++ = GRP_S;	/* auto-group: ^pat$ is really ^[pat]$ */
    117   1.1      cgd 		*rp++ = 0;
    118   1.1      cgd 	}
    119   1.1      cgd 	*rp++ = ALT_S;
    120   1.1      cgd 	alt_ptr = rp;
    121   1.1      cgd 	*rp++ = 0;
    122   1.1      cgd 	for (; *sp; ++sp) {
    123   1.1      cgd 		if (rp - rxpbuf >= RXP_LINE_SZ - 4) {
    124   1.1      cgd 			(void)snprintf(rxperr, sizeof(rxperr),
    125   1.1      cgd 			    "regular expression too long %s", s);
    126   1.1      cgd 			return (FALSE);
    127   1.1      cgd 		}
    128   1.1      cgd 		if (*sp == ':' && !esc)
    129   1.1      cgd 			break;
    130   1.1      cgd 		if (esc) {
    131   1.1      cgd 			*rp++ = LIT;
    132   1.1      cgd 			*rp++ = *sp;
    133   1.1      cgd 			esc = 0;
    134   1.1      cgd 		}
    135   1.1      cgd 		else switch (*sp) {
    136   1.1      cgd 		case '\\':
    137   1.1      cgd 			esc = 1;
    138   1.1      cgd 			break;
    139   1.1      cgd 		case '{':
    140   1.1      cgd 		case '[':
    141   1.1      cgd 			*rp++ = GRP_S;
    142   1.1      cgd 			grp_ptr = rp;
    143   1.1      cgd 			*rp++ = 0;
    144   1.1      cgd 			sp++;
    145   1.1      cgd 			if ((err = rxp__compile(s, FALSE)) != TRUE)
    146   1.1      cgd 				return (err);
    147   1.1      cgd 			*rp++ = GRP_E;
    148   1.1      cgd 			*grp_ptr = rp - rxpbuf;
    149   1.1      cgd 			break;
    150   1.1      cgd 		case '}':
    151   1.1      cgd 		case ']':
    152   1.1      cgd 		case '|':
    153   1.1      cgd 			*rp++ = ALT_E;
    154   1.1      cgd 			*alt_ptr = rp - rxpbuf;
    155   1.1      cgd 			if (*sp != ']') {
    156   1.1      cgd 				*rp++ = ALT_S;
    157   1.1      cgd 				alt_ptr = rp;
    158   1.1      cgd 				*rp++ = 0;
    159   1.1      cgd 			}
    160   1.1      cgd 			if (*sp != '|') {
    161   1.1      cgd 				if (*sp != ']') {
    162   1.1      cgd 					*rp++ = ALT_E;
    163   1.1      cgd 					*alt_ptr = rp - rxpbuf;
    164   1.1      cgd 				}
    165   1.1      cgd 				if (first) {
    166   1.1      cgd 					(void)snprintf(rxperr, sizeof(rxperr),
    167   1.1      cgd 					    "unmatched alternator in regexp %s",
    168   1.1      cgd 					     s);
    169   1.1      cgd 					return (FALSE);
    170   1.1      cgd 				}
    171   1.1      cgd 				return (TRUE);
    172   1.1      cgd 			}
    173   1.1      cgd 			break;
    174   1.1      cgd 		default:
    175   1.1      cgd 			*rp++ = LIT;
    176   1.1      cgd 			*rp++ = *sp;
    177   1.1      cgd 			esc = 0;
    178   1.1      cgd 			break;
    179   1.1      cgd 		}
    180   1.1      cgd 	}
    181   1.1      cgd 	if (!first) {
    182   1.1      cgd 		(void)snprintf(rxperr, sizeof(rxperr),
    183   1.1      cgd 		    "unmatched alternator in regexp %s", s);
    184   1.1      cgd 		return (FALSE);
    185   1.1      cgd 	}
    186   1.1      cgd 	*rp++ = ALT_E;
    187   1.1      cgd 	*alt_ptr = rp - rxpbuf;
    188   1.1      cgd 	*rp++ = GRP_E;
    189   1.1      cgd 	*(rxpbuf + 2) = rp - rxpbuf;
    190   1.1      cgd 	*rp++ = EOT;
    191   1.1      cgd 	*rp = END;
    192   1.1      cgd 	return (TRUE);
    193   1.1      cgd }
    194   1.1      cgd 
    195   1.1      cgd /*
    196   1.1      cgd  * match string against compiled regular expression
    197   1.1      cgd  */
    198   1.1      cgd int
    199   1.1      cgd rxp_match(s)
    200   1.7      jsm 	const char *	s;
    201   1.1      cgd {
    202   1.1      cgd 	return (rxp__match(s, TRUE, NULL, NULL, NULL));
    203   1.1      cgd }
    204   1.1      cgd 
    205   1.1      cgd static int
    206   1.1      cgd rxp__match(s, first, j_succ, j_fail, sp_fail)
    207   1.7      jsm 	const char *s;
    208   1.1      cgd 	int first;
    209   1.1      cgd 	Rxp_t *j_succ;		/* jump here on successful alt match */
    210   1.1      cgd 	Rxp_t *j_fail;		/* jump here on failed match */
    211   1.7      jsm 	const char *sp_fail;		/* reset sp to here on failed match */
    212   1.1      cgd {
    213   1.1      cgd 	static Rxp_t *rp;
    214   1.7      jsm 	static const char *sp;
    215   1.6    lukem 	int ch;
    216   1.6    lukem 	Rxp_t *grp_end = NULL;
    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.8      dbj 				return (FALSE);
    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.8      dbj 			rxp__match(sp, FALSE, grp_end, rxpbuf + *rp++, sp);
    251   1.1      cgd 			break;
    252   1.1      cgd 		case ALT_E:
    253   1.1      cgd 			rp = j_succ;
    254   1.1      cgd 			return (TRUE);
    255   1.1      cgd 		case GRP_E:
    256   1.8      dbj 			rp = j_fail;
    257   1.8      dbj 			sp = sp_fail;
    258   1.8      dbj 			return (FALSE);
    259   1.1      cgd 		default:
    260   1.8      dbj 			abort();
    261   1.1      cgd 		}
    262   1.1      cgd 	return (*rp != END ? FALSE : TRUE);
    263   1.1      cgd }
    264   1.1      cgd 
    265   1.1      cgd /*
    266   1.1      cgd  * Reverse engineer the regular expression, by picking first of all alternates.
    267   1.1      cgd  */
    268   1.1      cgd char *
    269   1.1      cgd rxp_expand()
    270   1.1      cgd {
    271   1.1      cgd 	return (rxp__expand(TRUE));
    272   1.1      cgd }
    273   1.1      cgd 
    274   1.1      cgd static char *
    275   1.1      cgd rxp__expand(first)
    276   1.1      cgd 	int first;
    277   1.1      cgd {
    278   1.1      cgd 	static char buf[RXP_LINE_SZ/2];
    279   1.1      cgd 	static Rxp_t *rp;
    280   1.1      cgd 	static char *bp;
    281   1.1      cgd 	Rxp_t *grp_ptr;
    282   1.1      cgd 	char *err;
    283   1.1      cgd 
    284   1.1      cgd 	if (first) {
    285   1.1      cgd 		rp = rxpbuf;
    286   1.1      cgd 		bp = buf;
    287   1.1      cgd 	}
    288   1.1      cgd 	while (rp < rxpbuf + RXP_LINE_SZ && *rp != END)
    289   1.1      cgd 		switch(*rp) {
    290   1.1      cgd 		case LIT:
    291   1.1      cgd 			rp++;
    292   1.1      cgd 			*bp++ = *rp++;
    293   1.1      cgd 			break;
    294   1.1      cgd 		case GRP_S:
    295   1.1      cgd 			rp++;
    296   1.1      cgd 			grp_ptr = rxpbuf + *rp;
    297   1.1      cgd 			rp++;
    298   1.1      cgd 			if ((err = rxp__expand(FALSE)) == NULL)
    299   1.1      cgd 				return (err);
    300   1.1      cgd 			rp = grp_ptr;
    301   1.1      cgd 			break;
    302   1.1      cgd 		case ALT_E:
    303   1.1      cgd 			return (buf);
    304   1.1      cgd 		case ALT_S:
    305   1.1      cgd 			rp++;
    306   1.1      cgd 			/* FALLTHROUGH */
    307   1.1      cgd 		case SOT:
    308   1.1      cgd 		case EOT:
    309   1.1      cgd 		case GRP_E:
    310   1.1      cgd 			rp++;
    311   1.1      cgd 			break;
    312   1.1      cgd 		default:
    313   1.1      cgd 			return (NULL);
    314   1.1      cgd 		}
    315   1.1      cgd 	if (first) {
    316   1.1      cgd 		if (*rp != END)
    317   1.1      cgd 			return (NULL);
    318   1.1      cgd 		*bp = '\0';
    319   1.1      cgd 	}
    320   1.1      cgd 	return (buf);
    321   1.1      cgd }
    322