--- ray/src/common/rexpr.c 1990/12/08 09:28:17 1.1 +++ ray/src/common/rexpr.c 2003/06/30 14:59:11 2.8 @@ -1,11 +1,19 @@ -/* Copyright (c) 1990 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id: rexpr.c,v 2.8 2003/06/30 14:59:11 schorsch Exp $"; #endif +/* + * Regular expression parsing routines. + * + * External symbols declared in standard.h + */ +#include "copyright.h" + #include +#include #include +#include + /* * rexpr.c - regular expression parser (ala grep) */ @@ -24,6 +32,9 @@ static char SCCSid[] = "$SunId$ LBL"; #define same(a,b) (a==b || (iflag && (a^b)==' ' && isalpha(a))) + +static int advance(), cclass(); + static char expbuf[ESIZE]; static int iflag; static int circf; @@ -101,7 +112,8 @@ int iflg, wflag; if ((c = *sp++) == '\0') return(-1); if (c == '<') { - *ep++ = CBRC; + if (ep == expbuf || ep[-1] != CBRC) + *ep++ = CBRC; continue; } if (c == '>') { @@ -119,12 +131,11 @@ int iflg, wflag; char * expsave() /* save compiled string */ { - extern char *malloc(); register char *ep; if (explen == 0) return(NULL); - if ((ep = malloc(explen+3)) == NULL) + if ((ep = (char *)malloc(explen+3)) == NULL) return(NULL); ep[0] = iflag; ep[1] = circf; @@ -133,6 +144,7 @@ expsave() /* save compiled string */ return(ep); } +void expset(ep) /* install saved string */ register char *ep; { @@ -142,28 +154,26 @@ register char *ep; } char * -eindex(sp) /* find the expression in str */ +eindex(sp) /* find the expression in string sp */ register char *sp; { - if (circf) { - if (advance(sp, expbuf)) - return(sp); + /* check for match at beginning of line, watch CBRC */ + if (advance(sp, expbuf[0]==CBRC ? expbuf+1 : expbuf)) + return(sp); + if (circf) return(NULL); - } /* fast check for first character */ if (expbuf[0]==CCHR) { register c = expbuf[1]; - do { + while (*++sp) if (same(*sp, c) && advance(sp, expbuf)) return(sp); - } while (*sp++); return(NULL); } /* regular algorithm */ - do { + while (*++sp) if (advance(sp, expbuf)) return(sp); - } while (*sp++); return(NULL); } @@ -241,8 +251,6 @@ register char *ep; return(0); case CBRC: - if (lp == expbuf) - continue; if ((isalnum(*lp) || *lp == '_') && !(isalnum(lp[-1]) || lp[-1] == '_')) continue; return (0); @@ -261,6 +269,7 @@ static int cclass(set, c, af) register char *set; register c; +int af; { register n;