11175Sbill /*
21175Sbill  * egrep -- print lines containing (or not containing) a regular expression
31175Sbill  *
41175Sbill  *	status returns:
51175Sbill  *		0 - ok, and some matches
61175Sbill  *		1 - ok, but no matches
71175Sbill  *		2 - some error
81175Sbill  */
91175Sbill %token CHAR DOT CCL NCCL OR CAT STAR PLUS QUEST
101175Sbill %left OR
111175Sbill %left CHAR DOT CCL NCCL '('
121175Sbill %left CAT
131175Sbill %left STAR PLUS QUEST
141175Sbill 
151175Sbill %{
1648265Sbostic /*-
17*62025Sbostic  * Copyright (c) 1991, 1993
18*62025Sbostic  *	The Regents of the University of California.  All rights reserved.
1948265Sbostic  *
2048265Sbostic  * %sccs.include.proprietary.c%
2148265Sbostic  */
2248265Sbostic 
2348265Sbostic #ifndef lint
24*62025Sbostic static char copyright[] =
25*62025Sbostic "@(#) Copyright (c) 1991, 1993\n\
26*62025Sbostic 	The Regents of the University of California.  All rights reserved.\n";
2748265Sbostic #endif /* not lint */
2848265Sbostic 
2948265Sbostic #ifndef lint
30*62025Sbostic static char sccsid[] = "@(#)old.egrep.y	8.1 (Berkeley) 06/06/93";
3148265Sbostic #endif /* not lint */
3248265Sbostic 
331175Sbill #include <stdio.h>
3421255Smckusick #include <sys/types.h>
3521255Smckusick #include <sys/stat.h>
3632331Sbostic #include <ctype.h>
371175Sbill 
3821255Smckusick #define BLKSIZE 8192
391175Sbill #define MAXLIN 350
401175Sbill #define MAXPOS 4000
411175Sbill #define NCHARS 128
421175Sbill #define NSTATES 128
431175Sbill #define FINAL -1
441175Sbill char gotofn[NSTATES][NCHARS];
4532331Sbostic char cmap[256];
461175Sbill int state[NSTATES];
471175Sbill char out[NSTATES];
481175Sbill int line = 1;
491175Sbill int name[MAXLIN];
501175Sbill int left[MAXLIN];
511175Sbill int right[MAXLIN];
521175Sbill int parent[MAXLIN];
531175Sbill int foll[MAXLIN];
541175Sbill int positions[MAXPOS];
551175Sbill char chars[MAXLIN];
561175Sbill int nxtpos;
571175Sbill int nxtchar = 0;
581175Sbill int tmpstat[MAXLIN];
591175Sbill int initstat[MAXLIN];
601175Sbill int xstate;
611175Sbill int count;
621175Sbill int icount;
631175Sbill char *input;
649117Srrh FILE *exprfile;
651175Sbill 
661175Sbill long	lnum;
671175Sbill int	bflag;
681175Sbill int	cflag;
691175Sbill int	fflag;
7032331Sbostic int	iflag;
711175Sbill int	lflag;
721175Sbill int	nflag;
731175Sbill int	hflag	= 1;
7432365Sbostic int	oflag;
751175Sbill int	sflag;
761175Sbill int	vflag;
7711561Sedward int	retcode = 0;
781175Sbill int	nfile;
791175Sbill int	blkno;
801175Sbill long	tln;
811175Sbill int	nsucc;
821175Sbill 
831175Sbill int	f;
849117Srrh char	*fname;
851175Sbill %}
861175Sbill 
871175Sbill %%
881175Sbill s:	t
891175Sbill 		={ unary(FINAL, $1);
901175Sbill 		  line--;
911175Sbill 		}
921175Sbill 	;
931175Sbill t:	b r
941175Sbill 		={ $$ = node(CAT, $1, $2); }
951175Sbill 	| OR b r OR
961175Sbill 		={ $$ = node(CAT, $2, $3); }
971175Sbill 	| OR b r
981175Sbill 		={ $$ = node(CAT, $2, $3); }
991175Sbill 	| b r OR
1001175Sbill 		={ $$ = node(CAT, $1, $2); }
1011175Sbill 	;
1021175Sbill b:
1031175Sbill 		={ $$ = enter(DOT);
1041175Sbill 		   $$ = unary(STAR, $$); }
1051175Sbill 	;
1061175Sbill r:	CHAR
1071175Sbill 		={ $$ = enter($1); }
1081175Sbill 	| DOT
1091175Sbill 		={ $$ = enter(DOT); }
1101175Sbill 	| CCL
1111175Sbill 		={ $$ = cclenter(CCL); }
1121175Sbill 	| NCCL
1131175Sbill 		={ $$ = cclenter(NCCL); }
1141175Sbill 	;
1151175Sbill 
1161175Sbill r:	r OR r
1171175Sbill 		={ $$ = node(OR, $1, $3); }
1181175Sbill 	| r r %prec CAT
1191175Sbill 		={ $$ = node(CAT, $1, $2); }
1201175Sbill 	| r STAR
1211175Sbill 		={ $$ = unary(STAR, $1); }
1221175Sbill 	| r PLUS
1231175Sbill 		={ $$ = unary(PLUS, $1); }
1241175Sbill 	| r QUEST
1251175Sbill 		={ $$ = unary(QUEST, $1); }
1261175Sbill 	| '(' r ')'
1271175Sbill 		={ $$ = $2; }
1281175Sbill 	| error
1291175Sbill 	;
1301175Sbill 
1311175Sbill %%
1321175Sbill yyerror(s) {
1331175Sbill 	fprintf(stderr, "egrep: %s\n", s);
1341175Sbill 	exit(2);
1351175Sbill }
1361175Sbill 
yylex()1371175Sbill yylex() {
1381175Sbill 	extern int yylval;
1391175Sbill 	int cclcnt, x;
1401175Sbill 	register char c, d;
1411175Sbill 	switch(c = nextch()) {
1421175Sbill 		case '$':
1431175Sbill 		case '^': c = '\n';
1441175Sbill 			goto defchar;
1451175Sbill 		case '|': return (OR);
1461175Sbill 		case '*': return (STAR);
1471175Sbill 		case '+': return (PLUS);
1481175Sbill 		case '?': return (QUEST);
1491175Sbill 		case '(': return (c);
1501175Sbill 		case ')': return (c);
1511175Sbill 		case '.': return (DOT);
1521175Sbill 		case '\0': return (0);
1531175Sbill 		case '\n': return (OR);
1541175Sbill 		case '[':
1551175Sbill 			x = CCL;
1561175Sbill 			cclcnt = 0;
1571175Sbill 			count = nxtchar++;
1581175Sbill 			if ((c = nextch()) == '^') {
1591175Sbill 				x = NCCL;
1601175Sbill 				c = nextch();
1611175Sbill 			}
1621175Sbill 			do {
1631175Sbill 				if (c == '\0') synerror();
1641175Sbill 				if (c == '-' && cclcnt > 0 && chars[nxtchar-1] != 0) {
1651175Sbill 					if ((d = nextch()) != 0) {
1661175Sbill 						c = chars[nxtchar-1];
1671175Sbill 						while (c < d) {
1681175Sbill 							if (nxtchar >= MAXLIN) overflo();
1691175Sbill 							chars[nxtchar++] = ++c;
1701175Sbill 							cclcnt++;
1711175Sbill 						}
1721175Sbill 						continue;
1731175Sbill 					}
1741175Sbill 				}
1751175Sbill 				if (nxtchar >= MAXLIN) overflo();
1761175Sbill 				chars[nxtchar++] = c;
1771175Sbill 				cclcnt++;
1781175Sbill 			} while ((c = nextch()) != ']');
1791175Sbill 			chars[count] = cclcnt;
1801175Sbill 			return (x);
1811175Sbill 		case '\\':
1821175Sbill 			if ((c = nextch()) == '\0') synerror();
1831175Sbill 		defchar:
1841175Sbill 		default: yylval = c; return (CHAR);
1851175Sbill 	}
1861175Sbill }
nextch()1871175Sbill nextch() {
18832331Sbostic 	register int c;
1891175Sbill 	if (fflag) {
1909117Srrh 		if ((c = getc(exprfile)) == EOF) {
1919117Srrh 			fclose(exprfile);
1929117Srrh 			return(0);
1939117Srrh 		}
1941175Sbill 	}
1951175Sbill 	else c = *input++;
1961175Sbill 	return(c);
1971175Sbill }
1981175Sbill 
synerror()1991175Sbill synerror() {
2001175Sbill 	fprintf(stderr, "egrep: syntax error\n");
2011175Sbill 	exit(2);
2021175Sbill }
2031175Sbill 
enter(x)2041175Sbill enter(x) int x; {
2051175Sbill 	if(line >= MAXLIN) overflo();
2061175Sbill 	name[line] = x;
2071175Sbill 	left[line] = 0;
2081175Sbill 	right[line] = 0;
2091175Sbill 	return(line++);
2101175Sbill }
2111175Sbill 
cclenter(x)2121175Sbill cclenter(x) int x; {
2131175Sbill 	register linno;
2141175Sbill 	linno = enter(x);
2151175Sbill 	right[linno] = count;
2161175Sbill 	return (linno);
2171175Sbill }
2181175Sbill 
node(x,l,r)2191175Sbill node(x, l, r) {
2201175Sbill 	if(line >= MAXLIN) overflo();
2211175Sbill 	name[line] = x;
2221175Sbill 	left[line] = l;
2231175Sbill 	right[line] = r;
2241175Sbill 	parent[l] = line;
2251175Sbill 	parent[r] = line;
2261175Sbill 	return(line++);
2271175Sbill }
2281175Sbill 
unary(x,d)2291175Sbill unary(x, d) {
2301175Sbill 	if(line >= MAXLIN) overflo();
2311175Sbill 	name[line] = x;
2321175Sbill 	left[line] = d;
2331175Sbill 	right[line] = 0;
2341175Sbill 	parent[d] = line;
2351175Sbill 	return(line++);
2361175Sbill }
overflo()2371175Sbill overflo() {
2381175Sbill 	fprintf(stderr, "egrep: regular expression too long\n");
2391175Sbill 	exit(2);
2401175Sbill }
2411175Sbill 
cfoll(v)2421175Sbill cfoll(v) {
2431175Sbill 	register i;
2441175Sbill 	if (left[v] == 0) {
2451175Sbill 		count = 0;
2461175Sbill 		for (i=1; i<=line; i++) tmpstat[i] = 0;
2471175Sbill 		follow(v);
2481175Sbill 		add(foll, v);
2491175Sbill 	}
2501175Sbill 	else if (right[v] == 0) cfoll(left[v]);
2511175Sbill 	else {
2521175Sbill 		cfoll(left[v]);
2531175Sbill 		cfoll(right[v]);
2541175Sbill 	}
2551175Sbill }
cgotofn()2561175Sbill cgotofn() {
2571175Sbill 	register c, i, k;
2581175Sbill 	int n, s;
2591175Sbill 	char symbol[NCHARS];
2601175Sbill 	int j, nc, pc, pos;
2611175Sbill 	int curpos, num;
2621175Sbill 	int number, newpos;
2631175Sbill 	count = 0;
2641175Sbill 	for (n=3; n<=line; n++) tmpstat[n] = 0;
2651175Sbill 	if (cstate(line-1)==0) {
2661175Sbill 		tmpstat[line] = 1;
2671175Sbill 		count++;
2681175Sbill 		out[0] = 1;
2691175Sbill 	}
2701175Sbill 	for (n=3; n<=line; n++) initstat[n] = tmpstat[n];
2711175Sbill 	count--;		/*leave out position 1 */
2721175Sbill 	icount = count;
2731175Sbill 	tmpstat[1] = 0;
2741175Sbill 	add(state, 0);
2751175Sbill 	n = 0;
2761175Sbill 	for (s=0; s<=n; s++)  {
2771175Sbill 		if (out[s] == 1) continue;
2781175Sbill 		for (i=0; i<NCHARS; i++) symbol[i] = 0;
2791175Sbill 		num = positions[state[s]];
2801175Sbill 		count = icount;
2811175Sbill 		for (i=3; i<=line; i++) tmpstat[i] = initstat[i];
2821175Sbill 		pos = state[s] + 1;
2831175Sbill 		for (i=0; i<num; i++) {
2841175Sbill 			curpos = positions[pos];
2851175Sbill 			if ((c = name[curpos]) >= 0) {
2861175Sbill 				if (c < NCHARS) symbol[c] = 1;
2871175Sbill 				else if (c == DOT) {
2881175Sbill 					for (k=0; k<NCHARS; k++)
2891175Sbill 						if (k!='\n') symbol[k] = 1;
2901175Sbill 				}
2911175Sbill 				else if (c == CCL) {
2921175Sbill 					nc = chars[right[curpos]];
2931175Sbill 					pc = right[curpos] + 1;
2941175Sbill 					for (k=0; k<nc; k++) symbol[chars[pc++]] = 1;
2951175Sbill 				}
2961175Sbill 				else if (c == NCCL) {
2971175Sbill 					nc = chars[right[curpos]];
2981175Sbill 					for (j = 0; j < NCHARS; j++) {
2991175Sbill 						pc = right[curpos] + 1;
3001175Sbill 						for (k = 0; k < nc; k++)
3011175Sbill 							if (j==chars[pc++]) goto cont;
3021175Sbill 						if (j!='\n') symbol[j] = 1;
3031175Sbill 						cont:;
3041175Sbill 					}
3051175Sbill 				}
3061175Sbill 				else printf("something's funny\n");
3071175Sbill 			}
3081175Sbill 			pos++;
3091175Sbill 		}
3101175Sbill 		for (c=0; c<NCHARS; c++) {
3111175Sbill 			if (symbol[c] == 1) { /* nextstate(s,c) */
3121175Sbill 				count = icount;
3131175Sbill 				for (i=3; i <= line; i++) tmpstat[i] = initstat[i];
3141175Sbill 				pos = state[s] + 1;
3151175Sbill 				for (i=0; i<num; i++) {
3161175Sbill 					curpos = positions[pos];
3171175Sbill 					if ((k = name[curpos]) >= 0)
3181175Sbill 						if (
3191175Sbill 							(k == c)
3201175Sbill 							| (k == DOT)
3211175Sbill 							| (k == CCL && member(c, right[curpos], 1))
3221175Sbill 							| (k == NCCL && member(c, right[curpos], 0))
3231175Sbill 						) {
3241175Sbill 							number = positions[foll[curpos]];
3251175Sbill 							newpos = foll[curpos] + 1;
3261175Sbill 							for (k=0; k<number; k++) {
3271175Sbill 								if (tmpstat[positions[newpos]] != 1) {
3281175Sbill 									tmpstat[positions[newpos]] = 1;
3291175Sbill 									count++;
3301175Sbill 								}
3311175Sbill 								newpos++;
3321175Sbill 							}
3331175Sbill 						}
3341175Sbill 					pos++;
3351175Sbill 				} /* end nextstate */
3361175Sbill 				if (notin(n)) {
3371175Sbill 					if (n >= NSTATES) overflo();
3381175Sbill 					add(state, ++n);
3391175Sbill 					if (tmpstat[line] == 1) out[n] = 1;
3401175Sbill 					gotofn[s][c] = n;
3411175Sbill 				}
3421175Sbill 				else {
3431175Sbill 					gotofn[s][c] = xstate;
3441175Sbill 				}
3451175Sbill 			}
3461175Sbill 		}
3471175Sbill 	}
3481175Sbill }
3491175Sbill 
cstate(v)3501175Sbill cstate(v) {
3511175Sbill 	register b;
3521175Sbill 	if (left[v] == 0) {
3531175Sbill 		if (tmpstat[v] != 1) {
3541175Sbill 			tmpstat[v] = 1;
3551175Sbill 			count++;
3561175Sbill 		}
3571175Sbill 		return(1);
3581175Sbill 	}
3591175Sbill 	else if (right[v] == 0) {
3601175Sbill 		if (cstate(left[v]) == 0) return (0);
3611175Sbill 		else if (name[v] == PLUS) return (1);
3621175Sbill 		else return (0);
3631175Sbill 	}
3641175Sbill 	else if (name[v] == CAT) {
3651175Sbill 		if (cstate(left[v]) == 0 && cstate(right[v]) == 0) return (0);
3661175Sbill 		else return (1);
3671175Sbill 	}
3681175Sbill 	else { /* name[v] == OR */
3691175Sbill 		b = cstate(right[v]);
3701175Sbill 		if (cstate(left[v]) == 0 || b == 0) return (0);
3711175Sbill 		else return (1);
3721175Sbill 	}
3731175Sbill }
3741175Sbill 
3751175Sbill 
member(symb,set,torf)3761175Sbill member(symb, set, torf) {
3771175Sbill 	register i, num, pos;
3781175Sbill 	num = chars[set];
3791175Sbill 	pos = set + 1;
3801175Sbill 	for (i=0; i<num; i++)
3811175Sbill 		if (symb == chars[pos++]) return (torf);
3821175Sbill 	return (!torf);
3831175Sbill }
3841175Sbill 
notin(n)3851175Sbill notin(n) {
3861175Sbill 	register i, j, pos;
3871175Sbill 	for (i=0; i<=n; i++) {
3881175Sbill 		if (positions[state[i]] == count) {
3891175Sbill 			pos = state[i] + 1;
3901175Sbill 			for (j=0; j < count; j++)
3911175Sbill 				if (tmpstat[positions[pos++]] != 1) goto nxt;
3921175Sbill 			xstate = i;
3931175Sbill 			return (0);
3941175Sbill 		}
3951175Sbill 		nxt: ;
3961175Sbill 	}
3971175Sbill 	return (1);
3981175Sbill }
3991175Sbill 
add(array,n)4001175Sbill add(array, n) int *array; {
4011175Sbill 	register i;
4021175Sbill 	if (nxtpos + count > MAXPOS) overflo();
4031175Sbill 	array[n] = nxtpos;
4041175Sbill 	positions[nxtpos++] = count;
4051175Sbill 	for (i=3; i <= line; i++) {
4061175Sbill 		if (tmpstat[i] == 1) {
4071175Sbill 			positions[nxtpos++] = i;
4081175Sbill 		}
4091175Sbill 	}
4101175Sbill }
4111175Sbill 
follow(v)4121175Sbill follow(v) int v; {
4131175Sbill 	int p;
4141175Sbill 	if (v == line) return;
4151175Sbill 	p = parent[v];
4161175Sbill 	switch(name[p]) {
4171175Sbill 		case STAR:
4181175Sbill 		case PLUS:	cstate(v);
4191175Sbill 				follow(p);
4201175Sbill 				return;
4211175Sbill 
4221175Sbill 		case OR:
4231175Sbill 		case QUEST:	follow(p);
4241175Sbill 				return;
4251175Sbill 
4261175Sbill 		case CAT:	if (v == left[p]) {
4271175Sbill 					if (cstate(right[p]) == 0) {
4281175Sbill 						follow(p);
4291175Sbill 						return;
4301175Sbill 					}
4311175Sbill 				}
4321175Sbill 				else follow(p);
4331175Sbill 				return;
4341175Sbill 		case FINAL:	if (tmpstat[line] != 1) {
4351175Sbill 					tmpstat[line] = 1;
4361175Sbill 					count++;
4371175Sbill 				}
4381175Sbill 				return;
4391175Sbill 	}
4401175Sbill }
4411175Sbill 
4421175Sbill 
main(argc,argv)4431175Sbill main(argc, argv)
4441175Sbill char **argv;
4451175Sbill {
44632331Sbostic 	register int i;
44732331Sbostic 
4481175Sbill 	while (--argc > 0 && (++argv)[0][0]=='-')
4491175Sbill 		switch (argv[0][1]) {
4501175Sbill 
4511175Sbill 		case 's':
4521175Sbill 			sflag++;
4531175Sbill 			continue;
4541175Sbill 
4551175Sbill 		case 'h':
4561175Sbill 			hflag = 0;
4571175Sbill 			continue;
4581175Sbill 
45932365Sbostic 		case 'o':
46032365Sbostic 			oflag++;
46132365Sbostic 			continue;
46232365Sbostic 
4631175Sbill 		case 'b':
4641175Sbill 			bflag++;
4651175Sbill 			continue;
4661175Sbill 
4671175Sbill 		case 'c':
4681175Sbill 			cflag++;
4691175Sbill 			continue;
4701175Sbill 
4711175Sbill 		case 'e':
4721175Sbill 			argc--;
4731175Sbill 			argv++;
4741175Sbill 			goto out;
4751175Sbill 
4761175Sbill 		case 'f':
4771175Sbill 			fflag++;
4781175Sbill 			continue;
4791175Sbill 
48032331Sbostic 		case 'i':
48132331Sbostic 			iflag++;
48232331Sbostic 			for ( i = 'A'; i <= 'Z'; i++ )
48332331Sbostic 				cmap[i] = (char) tolower ( i );
48432331Sbostic 			continue;
48532331Sbostic 
4861175Sbill 		case 'l':
4871175Sbill 			lflag++;
4881175Sbill 			continue;
4891175Sbill 
4901175Sbill 		case 'n':
4911175Sbill 			nflag++;
4921175Sbill 			continue;
4931175Sbill 
4941175Sbill 		case 'v':
4951175Sbill 			vflag++;
4961175Sbill 			continue;
4971175Sbill 
4981175Sbill 		default:
4991175Sbill 			fprintf(stderr, "egrep: unknown flag\n");
5001175Sbill 			continue;
5011175Sbill 		}
5021175Sbill out:
5031175Sbill 	if (argc<=0)
5041175Sbill 		exit(2);
50532331Sbostic 
50632331Sbostic 	for (i = 0; i < 256; ++i)
50732331Sbostic 		cmap[i] = (char)i;
50832331Sbostic 
5091175Sbill 	if (fflag) {
5109117Srrh 		fname = *argv;
5119117Srrh 		exprfile = fopen(fname, "r");
5129117Srrh 		if (exprfile == (FILE *)NULL) {
5131175Sbill 			fprintf(stderr, "egrep: can't open %s\n", fname);
5141175Sbill 			exit(2);
5151175Sbill 		}
5161175Sbill 	}
5171175Sbill 	else input = *argv;
51832331Sbostic 	if ( iflag ) {
51932331Sbostic 		register char *s;
52032331Sbostic 		for ( s = input; *s != '\0'; s++ )
52132331Sbostic 			if ( isupper ( (int)(*s) ) )
52232331Sbostic 				*s = (char) tolower ( (int)(*s) );
52332331Sbostic 	}
5241175Sbill 	argc--;
5251175Sbill 	argv++;
5261175Sbill 
5271175Sbill 	yyparse();
5281175Sbill 
5291175Sbill 	cfoll(line-1);
5301175Sbill 	cgotofn();
5311175Sbill 	nfile = argc;
5321175Sbill 	if (argc<=0) {
5331175Sbill 		if (lflag) exit(1);
5341175Sbill 		execute(0);
5351175Sbill 	}
5361175Sbill 	else while (--argc >= 0) {
5371175Sbill 		execute(*argv);
5381175Sbill 		argv++;
5391175Sbill 	}
54011561Sedward 	exit(retcode != 0 ? retcode : nsucc == 0);
5411175Sbill }
5421175Sbill 
execute(file)5431175Sbill execute(file)
5441175Sbill char *file;
5451175Sbill {
5461175Sbill 	register char *p;
5471175Sbill 	register cstat;
5481175Sbill 	register ccount;
54932331Sbostic 	register char *cmapr = cmap;
55021255Smckusick 	static char *buf;
55121255Smckusick 	static int blksize;
55221255Smckusick 	struct stat stb;
5531175Sbill 	char *nlp;
5541175Sbill 	int istat;
5551175Sbill 	if (file) {
5561175Sbill 		if ((f = open(file, 0)) < 0) {
5571175Sbill 			fprintf(stderr, "egrep: can't open %s\n", file);
55811561Sedward 			retcode = 2;
55911561Sedward 			return;
5601175Sbill 		}
5611175Sbill 	}
5621175Sbill 	else f = 0;
56321255Smckusick 	if (buf == NULL) {
56421255Smckusick 		if (fstat(f, &stb) > 0 && stb.st_blksize > 0)
56521255Smckusick 			blksize = stb.st_blksize;
56621255Smckusick 		else
56721255Smckusick 			blksize = BLKSIZE;
56821255Smckusick 		buf = (char *)malloc(2*blksize);
56921255Smckusick 		if (buf == NULL) {
57021255Smckusick 			fprintf(stderr, "egrep: no memory for %s\n", file);
57121255Smckusick 			retcode = 2;
57221255Smckusick 			return;
57321255Smckusick 		}
57421255Smckusick 	}
5751175Sbill 	ccount = 0;
5761175Sbill 	lnum = 1;
5771175Sbill 	tln = 0;
5781175Sbill 	blkno = 0;
5791175Sbill 	p = buf;
5801175Sbill 	nlp = p;
58121255Smckusick 	if ((ccount = read(f,p,blksize))<=0) goto done;
5821175Sbill 	istat = cstat = gotofn[0]['\n'];
5831175Sbill 	if (out[cstat]) goto found;
5841175Sbill 	for (;;) {
58532331Sbostic 		cstat = gotofn[cstat][(unsigned char)cmapr[*(unsigned char *)p]];
5861175Sbill 		if (out[cstat]) {
5871175Sbill 		found:	for(;;) {
5881175Sbill 				if (*p++ == '\n') {
5891175Sbill 					if (vflag == 0) {
5901175Sbill 				succeed:	nsucc = 1;
5911175Sbill 						if (cflag) tln++;
5921175Sbill 						else if (sflag)
5931175Sbill 							;	/* ugh */
5941175Sbill 						else if (lflag) {
5951175Sbill 							printf("%s\n", file);
5961175Sbill 							close(f);
5971175Sbill 							return;
5981175Sbill 						}
5991175Sbill 						else {
60032365Sbostic 							if (nfile > 1 && hflag || oflag) printf("%s:", file);
6011175Sbill 							if (bflag) printf("%d:", blkno);
6021175Sbill 							if (nflag) printf("%ld:", lnum);
6031175Sbill 							if (p <= nlp) {
60421255Smckusick 								while (nlp < &buf[2*blksize]) putchar(*nlp++);
6051175Sbill 								nlp = buf;
6061175Sbill 							}
6071175Sbill 							while (nlp < p) putchar(*nlp++);
6081175Sbill 						}
6091175Sbill 					}
6101175Sbill 					lnum++;
6111175Sbill 					nlp = p;
6121175Sbill 					if ((out[(cstat=istat)]) == 0) goto brk2;
6131175Sbill 				}
6141175Sbill 				cfound:
6151175Sbill 				if (--ccount <= 0) {
61621255Smckusick 					if (p <= &buf[blksize]) {
61721255Smckusick 						if ((ccount = read(f, p, blksize)) <= 0) goto done;
6181175Sbill 					}
61921255Smckusick 					else if (p == &buf[2*blksize]) {
6201175Sbill 						p = buf;
62121255Smckusick 						if ((ccount = read(f, p, blksize)) <= 0) goto done;
6221175Sbill 					}
6231175Sbill 					else {
62421255Smckusick 						if ((ccount = read(f, p, &buf[2*blksize]-p)) <= 0) goto done;
6251175Sbill 					}
62621255Smckusick 					blkno += ccount / 512;
6271175Sbill 				}
6281175Sbill 			}
6291175Sbill 		}
6301175Sbill 		if (*p++ == '\n') {
6311175Sbill 			if (vflag) goto succeed;
6321175Sbill 			else {
6331175Sbill 				lnum++;
6341175Sbill 				nlp = p;
6351175Sbill 				if (out[(cstat=istat)]) goto cfound;
6361175Sbill 			}
6371175Sbill 		}
6381175Sbill 		brk2:
6391175Sbill 		if (--ccount <= 0) {
64021255Smckusick 			if (p <= &buf[blksize]) {
64121255Smckusick 				if ((ccount = read(f, p, blksize)) <= 0) break;
6421175Sbill 			}
64321255Smckusick 			else if (p == &buf[2*blksize]) {
6441175Sbill 				p = buf;
64521255Smckusick 				if ((ccount = read(f, p, blksize)) <= 0) break;
6461175Sbill 			}
6471175Sbill 			else {
64821255Smckusick 				if ((ccount = read(f, p, &buf[2*blksize] - p)) <= 0) break;
6491175Sbill 			}
65021255Smckusick 			blkno += ccount / 512;
6511175Sbill 		}
6521175Sbill 	}
6531175Sbill done:	close(f);
6541175Sbill 	if (cflag) {
6551175Sbill 		if (nfile > 1)
6561175Sbill 			printf("%s:", file);
6571175Sbill 		printf("%ld\n", tln);
6581175Sbill 	}
6591175Sbill }
660