1 |
– |
/* Copyright (c) 1990 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
+ |
/* |
5 |
+ |
* Regular expression parsing routines. |
6 |
+ |
* |
7 |
+ |
* External symbols declared in standard.h |
8 |
+ |
*/ |
9 |
|
|
10 |
+ |
#include "copyright.h" |
11 |
+ |
|
12 |
|
#include <stdio.h> |
13 |
+ |
#include <stdlib.h> |
14 |
|
#include <ctype.h> |
15 |
+ |
#include <string.h> |
16 |
+ |
|
17 |
+ |
#include "rtio.h" |
18 |
+ |
|
19 |
|
/* |
20 |
|
* rexpr.c - regular expression parser (ala grep) |
21 |
|
*/ |
34 |
|
|
35 |
|
#define same(a,b) (a==b || (iflag && (a^b)==' ' && isalpha(a))) |
36 |
|
|
27 |
– |
#ifdef BSD |
28 |
– |
#define memcpy(to,from,len) bcopy(from,to,len) |
29 |
– |
#endif |
37 |
|
|
38 |
+ |
static int advance(char *, char *); |
39 |
+ |
static int cclass(char *, int c, int af); |
40 |
+ |
|
41 |
|
static char expbuf[ESIZE]; |
42 |
|
static int iflag; |
43 |
|
static int circf; |
49 |
|
register char *sp; |
50 |
|
int iflg, wflag; |
51 |
|
{ |
52 |
< |
register c; |
52 |
> |
register int c; |
53 |
|
register char *ep; |
54 |
< |
char *lastep; |
54 |
> |
char *lastep = NULL; |
55 |
|
int cclcnt; |
56 |
|
|
57 |
|
iflag = iflg; |
115 |
|
if ((c = *sp++) == '\0') |
116 |
|
return(-1); |
117 |
|
if (c == '<') { |
118 |
< |
*ep++ = CBRC; |
118 |
> |
if (ep == expbuf || ep[-1] != CBRC) |
119 |
> |
*ep++ = CBRC; |
120 |
|
continue; |
121 |
|
} |
122 |
|
if (c == '>') { |
134 |
|
char * |
135 |
|
expsave() /* save compiled string */ |
136 |
|
{ |
126 |
– |
extern char *malloc(); |
137 |
|
register char *ep; |
138 |
|
|
139 |
|
if (explen == 0) |
140 |
|
return(NULL); |
141 |
< |
if ((ep = malloc(explen+3)) == NULL) |
141 |
> |
if ((ep = (char *)malloc(explen+3)) == NULL) |
142 |
|
return(NULL); |
143 |
|
ep[0] = iflag; |
144 |
|
ep[1] = circf; |
147 |
|
return(ep); |
148 |
|
} |
149 |
|
|
150 |
+ |
void |
151 |
|
expset(ep) /* install saved string */ |
152 |
|
register char *ep; |
153 |
|
{ |
157 |
|
} |
158 |
|
|
159 |
|
char * |
160 |
< |
eindex(sp) /* find the expression in str */ |
160 |
> |
eindex(sp) /* find the expression in string sp */ |
161 |
|
register char *sp; |
162 |
|
{ |
163 |
< |
if (circf) { |
164 |
< |
if (advance(sp, expbuf)) |
165 |
< |
return(sp); |
163 |
> |
/* check for match at beginning of line, watch CBRC */ |
164 |
> |
if (advance(sp, expbuf[0]==CBRC ? expbuf+1 : expbuf)) |
165 |
> |
return(sp); |
166 |
> |
if (circf) |
167 |
|
return(NULL); |
156 |
– |
} |
168 |
|
/* fast check for first character */ |
169 |
|
if (expbuf[0]==CCHR) { |
170 |
< |
register c = expbuf[1]; |
171 |
< |
do { |
170 |
> |
register int c = expbuf[1]; |
171 |
> |
while (*++sp) |
172 |
|
if (same(*sp, c) && advance(sp, expbuf)) |
173 |
|
return(sp); |
163 |
– |
} while (*sp++); |
174 |
|
return(NULL); |
175 |
|
} |
176 |
|
/* regular algorithm */ |
177 |
< |
do { |
177 |
> |
while (*++sp) |
178 |
|
if (advance(sp, expbuf)) |
179 |
|
return(sp); |
170 |
– |
} while (*sp++); |
180 |
|
return(NULL); |
181 |
|
} |
182 |
|
|
254 |
|
return(0); |
255 |
|
|
256 |
|
case CBRC: |
248 |
– |
if (lp == expbuf) |
249 |
– |
continue; |
257 |
|
if ((isalnum(*lp) || *lp == '_') && !(isalnum(lp[-1]) || lp[-1] == '_')) |
258 |
|
continue; |
259 |
|
return (0); |
271 |
|
static int |
272 |
|
cclass(set, c, af) |
273 |
|
register char *set; |
274 |
< |
register c; |
274 |
> |
register int c; |
275 |
> |
int af; |
276 |
|
{ |
277 |
< |
register n; |
277 |
> |
register int n; |
278 |
|
|
279 |
|
if (c == 0) |
280 |
|
return(0); |