--- ray/src/common/rexpr.c 1990/12/08 09:28:17 1.1 +++ ray/src/common/rexpr.c 2003/07/17 09:21:29 2.9 @@ -1,11 +1,21 @@ -/* 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.9 2003/07/17 09:21:29 schorsch Exp $"; #endif +/* + * Regular expression parsing routines. + * + * External symbols declared in standard.h + */ +#include "copyright.h" + #include +#include #include +#include + +#include "rtio.h" + /* * rexpr.c - regular expression parser (ala grep) */ @@ -24,6 +34,10 @@ static char SCCSid[] = "$SunId$ LBL"; #define same(a,b) (a==b || (iflag && (a^b)==' ' && isalpha(a))) + +static int advance(char *, char *); +static int cclass(char *, int c, int af); + static char expbuf[ESIZE]; static int iflag; static int circf; @@ -35,9 +49,9 @@ ecompile(sp, iflg, wflag) /* compile the register char *sp; int iflg, wflag; { - register c; + register int c; register char *ep; - char *lastep; + char *lastep = NULL; int cclcnt; iflag = iflg; @@ -101,7 +115,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 +134,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 +147,7 @@ expsave() /* save compiled string */ return(ep); } +void expset(ep) /* install saved string */ register char *ep; { @@ -142,28 +157,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 { + register int c = expbuf[1]; + 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 +254,6 @@ register char *ep; return(0); case CBRC: - if (lp == expbuf) - continue; if ((isalnum(*lp) || *lp == '_') && !(isalnum(lp[-1]) || lp[-1] == '_')) continue; return (0); @@ -260,9 +271,10 @@ register char *ep; static int cclass(set, c, af) register char *set; -register c; +register int c; +int af; { - register n; + register int n; if (c == 0) return(0);