xref: /titanic_52/usr/src/cmd/refer/glue5.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
2*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
3*7c478bd9Sstevel@tonic-gate 
4*7c478bd9Sstevel@tonic-gate 
5*7c478bd9Sstevel@tonic-gate /*
6*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
7*7c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
8*7c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate /*
12*7c478bd9Sstevel@tonic-gate  * Copyright (c) 1983, 1984 1985, 1986, 1987, 1988, Sun Microsystems, Inc.
13*7c478bd9Sstevel@tonic-gate  * All Rights Reserved.
14*7c478bd9Sstevel@tonic-gate  */
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
17*7c478bd9Sstevel@tonic-gate 
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #include <stdio.h>
20*7c478bd9Sstevel@tonic-gate #include <ctype.h>
21*7c478bd9Sstevel@tonic-gate /*
22*7c478bd9Sstevel@tonic-gate  * fgrep -- print all lines containing any of a set of keywords
23*7c478bd9Sstevel@tonic-gate  *
24*7c478bd9Sstevel@tonic-gate  *	status returns:
25*7c478bd9Sstevel@tonic-gate  *		0 - ok, and some matches
26*7c478bd9Sstevel@tonic-gate  *		1 - ok, but no matches
27*7c478bd9Sstevel@tonic-gate  *		2 - some error
28*7c478bd9Sstevel@tonic-gate  */
29*7c478bd9Sstevel@tonic-gate #define	MAXSIZ 700
30*7c478bd9Sstevel@tonic-gate #define QSIZE 400
31*7c478bd9Sstevel@tonic-gate struct words {
32*7c478bd9Sstevel@tonic-gate 	char 	inp;
33*7c478bd9Sstevel@tonic-gate 	char	out;
34*7c478bd9Sstevel@tonic-gate 	struct	words *nst;
35*7c478bd9Sstevel@tonic-gate 	struct	words *link;
36*7c478bd9Sstevel@tonic-gate 	struct	words *fail;
37*7c478bd9Sstevel@tonic-gate }
38*7c478bd9Sstevel@tonic-gate *www, *smax, *q;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate char	buf[2*BUFSIZ];
41*7c478bd9Sstevel@tonic-gate int	nsucc;
42*7c478bd9Sstevel@tonic-gate int	need;
43*7c478bd9Sstevel@tonic-gate char	*instr;
44*7c478bd9Sstevel@tonic-gate int	inct;
45*7c478bd9Sstevel@tonic-gate int	rflag;
46*7c478bd9Sstevel@tonic-gate int	xargc;
47*7c478bd9Sstevel@tonic-gate char	**xargv;
48*7c478bd9Sstevel@tonic-gate int	numwords;
49*7c478bd9Sstevel@tonic-gate int	nfound;
50*7c478bd9Sstevel@tonic-gate static int flag = 0;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate fgrep(argc, argv)
53*7c478bd9Sstevel@tonic-gate char **argv;
54*7c478bd9Sstevel@tonic-gate {
55*7c478bd9Sstevel@tonic-gate 	nsucc = need = inct = rflag = numwords = nfound = 0;
56*7c478bd9Sstevel@tonic-gate 	instr = 0;
57*7c478bd9Sstevel@tonic-gate 	flag = 0;
58*7c478bd9Sstevel@tonic-gate 	if (www==0)
59*7c478bd9Sstevel@tonic-gate 		www = (struct words *) zalloc(MAXSIZ, sizeof (*www));
60*7c478bd9Sstevel@tonic-gate 	if (www==NULL)
61*7c478bd9Sstevel@tonic-gate 		err(gettext("Can't get space for machines"), 0);
62*7c478bd9Sstevel@tonic-gate 	for (q=www; q<www+MAXSIZ; q++) {
63*7c478bd9Sstevel@tonic-gate 		q->inp =0; q->out =0; q->nst =0; q->link =0; q->fail =0;
64*7c478bd9Sstevel@tonic-gate 	}
65*7c478bd9Sstevel@tonic-gate 	xargc = argc-1;
66*7c478bd9Sstevel@tonic-gate 	xargv = argv+1;
67*7c478bd9Sstevel@tonic-gate 	while (xargc>0 && xargv[0][0]=='-')
68*7c478bd9Sstevel@tonic-gate 		{
69*7c478bd9Sstevel@tonic-gate 		switch(xargv[0][1])
70*7c478bd9Sstevel@tonic-gate 			{
71*7c478bd9Sstevel@tonic-gate 			case 'r': /* return value only */
72*7c478bd9Sstevel@tonic-gate 				rflag++;
73*7c478bd9Sstevel@tonic-gate 				break;
74*7c478bd9Sstevel@tonic-gate 			case 'n': /* number of answers needed */
75*7c478bd9Sstevel@tonic-gate 				need = (int) xargv[1];
76*7c478bd9Sstevel@tonic-gate 				xargv++; xargc--;
77*7c478bd9Sstevel@tonic-gate 				break;
78*7c478bd9Sstevel@tonic-gate 			case 'i':
79*7c478bd9Sstevel@tonic-gate 				instr = xargv[1];
80*7c478bd9Sstevel@tonic-gate 				inct = (int) xargv[2]+2;
81*7c478bd9Sstevel@tonic-gate # if D2
82*7c478bd9Sstevel@tonic-gate fprintf(stderr,"inct %d xargv.2. %o %d\n",inct, xargv[2],xargv[2]);
83*7c478bd9Sstevel@tonic-gate # endif
84*7c478bd9Sstevel@tonic-gate 				xargv += 2; xargc -= 2;
85*7c478bd9Sstevel@tonic-gate 				break;
86*7c478bd9Sstevel@tonic-gate 			}
87*7c478bd9Sstevel@tonic-gate 		xargv++; xargc--;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 	if (xargc<=0)
90*7c478bd9Sstevel@tonic-gate 		{
91*7c478bd9Sstevel@tonic-gate 		write (2, "bad fgrep call\n", 15);
92*7c478bd9Sstevel@tonic-gate 		exit(2);
93*7c478bd9Sstevel@tonic-gate 		}
94*7c478bd9Sstevel@tonic-gate # if D1
95*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "before cgoto\n");
96*7c478bd9Sstevel@tonic-gate # endif
97*7c478bd9Sstevel@tonic-gate 	cgotofn();
98*7c478bd9Sstevel@tonic-gate # if D1
99*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "before cfail\n");
100*7c478bd9Sstevel@tonic-gate # endif
101*7c478bd9Sstevel@tonic-gate 	cfail();
102*7c478bd9Sstevel@tonic-gate # if D1
103*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "before execute instr %.20s\n", instr? instr: "");
104*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "end of string %d %c %c %c\n", inct,
105*7c478bd9Sstevel@tonic-gate 		instr ? instr[inct-3] : '\0',
106*7c478bd9Sstevel@tonic-gate 		instr ? instr[inct-2] : '\0',
107*7c478bd9Sstevel@tonic-gate 		instr ? instr[inct-1] : '\0');
108*7c478bd9Sstevel@tonic-gate # endif
109*7c478bd9Sstevel@tonic-gate 	execute();
110*7c478bd9Sstevel@tonic-gate # if D1
111*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "returning nsucc %d\n", nsucc);
112*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "fgrep done www %o\n",www);
113*7c478bd9Sstevel@tonic-gate # endif
114*7c478bd9Sstevel@tonic-gate 	return(nsucc == 0);
115*7c478bd9Sstevel@tonic-gate }
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate execute()
118*7c478bd9Sstevel@tonic-gate {
119*7c478bd9Sstevel@tonic-gate 	register char *p;
120*7c478bd9Sstevel@tonic-gate 	register struct words *c;
121*7c478bd9Sstevel@tonic-gate 	register ch;
122*7c478bd9Sstevel@tonic-gate 	register ccount;
123*7c478bd9Sstevel@tonic-gate 	int f;
124*7c478bd9Sstevel@tonic-gate 	char *nlp;
125*7c478bd9Sstevel@tonic-gate 	f=0;
126*7c478bd9Sstevel@tonic-gate 	ccount = instr ? inct : 0;
127*7c478bd9Sstevel@tonic-gate 	nfound=0;
128*7c478bd9Sstevel@tonic-gate 	p = instr ? instr : buf;
129*7c478bd9Sstevel@tonic-gate 	if (need == 0) need = numwords;
130*7c478bd9Sstevel@tonic-gate 	nlp = p;
131*7c478bd9Sstevel@tonic-gate 	c = www;
132*7c478bd9Sstevel@tonic-gate # if D2
133*7c478bd9Sstevel@tonic-gate fprintf(stderr, "in execute ccount %d inct %d\n",ccount, inct );
134*7c478bd9Sstevel@tonic-gate # endif
135*7c478bd9Sstevel@tonic-gate 	for (;;) {
136*7c478bd9Sstevel@tonic-gate # if D3
137*7c478bd9Sstevel@tonic-gate fprintf(stderr, "down ccount\n");
138*7c478bd9Sstevel@tonic-gate # endif
139*7c478bd9Sstevel@tonic-gate 		if (--ccount <= 0) {
140*7c478bd9Sstevel@tonic-gate # if D2
141*7c478bd9Sstevel@tonic-gate fprintf(stderr, "ex loop ccount %d instr %o\n",ccount, instr);
142*7c478bd9Sstevel@tonic-gate # endif
143*7c478bd9Sstevel@tonic-gate 			if (instr) break;
144*7c478bd9Sstevel@tonic-gate 			if (p == &buf[2*BUFSIZ]) p = buf;
145*7c478bd9Sstevel@tonic-gate 			if (p > &buf[BUFSIZ]) {
146*7c478bd9Sstevel@tonic-gate 				if ((ccount = read(f, p, &buf[2*BUFSIZ] - p)) <= 0) break;
147*7c478bd9Sstevel@tonic-gate 			}
148*7c478bd9Sstevel@tonic-gate 			else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
149*7c478bd9Sstevel@tonic-gate # if D2
150*7c478bd9Sstevel@tonic-gate fprintf(stderr, " normal read %d bytres\n", ccount);
151*7c478bd9Sstevel@tonic-gate {char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
152*7c478bd9Sstevel@tonic-gate fprintf(stderr, xx, p);
153*7c478bd9Sstevel@tonic-gate }
154*7c478bd9Sstevel@tonic-gate # endif
155*7c478bd9Sstevel@tonic-gate 		}
156*7c478bd9Sstevel@tonic-gate nstate:
157*7c478bd9Sstevel@tonic-gate 		ch = *p;
158*7c478bd9Sstevel@tonic-gate # if D2
159*7c478bd9Sstevel@tonic-gate fprintf(stderr, "roaming along in ex ch %c c %o\n",ch,c);
160*7c478bd9Sstevel@tonic-gate # endif
161*7c478bd9Sstevel@tonic-gate 		if (isupper(ch)) ch |= 040;
162*7c478bd9Sstevel@tonic-gate 		if (c->inp == ch) {
163*7c478bd9Sstevel@tonic-gate 			c = c->nst;
164*7c478bd9Sstevel@tonic-gate 		}
165*7c478bd9Sstevel@tonic-gate 		else if (c->link != 0) {
166*7c478bd9Sstevel@tonic-gate 			c = c->link;
167*7c478bd9Sstevel@tonic-gate 			goto nstate;
168*7c478bd9Sstevel@tonic-gate 		}
169*7c478bd9Sstevel@tonic-gate 		else {
170*7c478bd9Sstevel@tonic-gate 			c = c->fail;
171*7c478bd9Sstevel@tonic-gate 			if (c==0) {
172*7c478bd9Sstevel@tonic-gate 				c = www;
173*7c478bd9Sstevel@tonic-gate istate:
174*7c478bd9Sstevel@tonic-gate 				if (c->inp == ch) {
175*7c478bd9Sstevel@tonic-gate 					c = c->nst;
176*7c478bd9Sstevel@tonic-gate 				}
177*7c478bd9Sstevel@tonic-gate 				else if (c->link != 0) {
178*7c478bd9Sstevel@tonic-gate 					c = c->link;
179*7c478bd9Sstevel@tonic-gate 					goto istate;
180*7c478bd9Sstevel@tonic-gate 				}
181*7c478bd9Sstevel@tonic-gate 			}
182*7c478bd9Sstevel@tonic-gate 			else goto nstate;
183*7c478bd9Sstevel@tonic-gate 		}
184*7c478bd9Sstevel@tonic-gate 		if (c->out && new (c)) {
185*7c478bd9Sstevel@tonic-gate # if D2
186*7c478bd9Sstevel@tonic-gate fprintf(stderr, " found: nfound %d need %d\n",nfound,need);
187*7c478bd9Sstevel@tonic-gate # endif
188*7c478bd9Sstevel@tonic-gate 			if (++nfound >= need)
189*7c478bd9Sstevel@tonic-gate 			{
190*7c478bd9Sstevel@tonic-gate # if D1
191*7c478bd9Sstevel@tonic-gate fprintf(stderr, "found, p %o nlp %o ccount %d buf %o buf[2*BUFSIZ] %o\n",p,nlp,ccount,buf,buf+2*BUFSIZ);
192*7c478bd9Sstevel@tonic-gate # endif
193*7c478bd9Sstevel@tonic-gate 				if (instr==0)
194*7c478bd9Sstevel@tonic-gate 				while (*p++ != '\n') {
195*7c478bd9Sstevel@tonic-gate # if D3
196*7c478bd9Sstevel@tonic-gate fprintf(stderr, "down ccount2\n");
197*7c478bd9Sstevel@tonic-gate # endif
198*7c478bd9Sstevel@tonic-gate 					if (--ccount <= 0) {
199*7c478bd9Sstevel@tonic-gate 						if (p == &buf[2*BUFSIZ]) p = buf;
200*7c478bd9Sstevel@tonic-gate 						if (p > &buf[BUFSIZ]) {
201*7c478bd9Sstevel@tonic-gate 							if ((ccount = read(f, p, &buf[2*BUFSIZ] - p)) <= 0) break;
202*7c478bd9Sstevel@tonic-gate 						}
203*7c478bd9Sstevel@tonic-gate 						else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
204*7c478bd9Sstevel@tonic-gate # if D2
205*7c478bd9Sstevel@tonic-gate fprintf(stderr, " read %d bytes\n",ccount);
206*7c478bd9Sstevel@tonic-gate { char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
207*7c478bd9Sstevel@tonic-gate fprintf(stderr, xx, p);
208*7c478bd9Sstevel@tonic-gate }
209*7c478bd9Sstevel@tonic-gate # endif
210*7c478bd9Sstevel@tonic-gate 					}
211*7c478bd9Sstevel@tonic-gate 				}
212*7c478bd9Sstevel@tonic-gate 				nsucc = 1;
213*7c478bd9Sstevel@tonic-gate 				if (rflag==0)
214*7c478bd9Sstevel@tonic-gate 					{
215*7c478bd9Sstevel@tonic-gate # if D2
216*7c478bd9Sstevel@tonic-gate fprintf(stderr, "p %o nlp %o buf %o\n",p,nlp,buf);
217*7c478bd9Sstevel@tonic-gate if (p>nlp)
218*7c478bd9Sstevel@tonic-gate {write (2, "XX\n", 3); write (2, nlp, p-nlp); write (2, "XX\n", 3);}
219*7c478bd9Sstevel@tonic-gate # endif
220*7c478bd9Sstevel@tonic-gate 					if (p > nlp) write(1, nlp, p-nlp);
221*7c478bd9Sstevel@tonic-gate 					else {
222*7c478bd9Sstevel@tonic-gate 						write(1, nlp, &buf[2*BUFSIZ] - nlp);
223*7c478bd9Sstevel@tonic-gate 						write(1, buf, p-&buf[0]);
224*7c478bd9Sstevel@tonic-gate 						}
225*7c478bd9Sstevel@tonic-gate 					if (p[-1]!= '\n') write (1, "\n", 1);
226*7c478bd9Sstevel@tonic-gate 					}
227*7c478bd9Sstevel@tonic-gate 				if (instr==0)
228*7c478bd9Sstevel@tonic-gate 					{
229*7c478bd9Sstevel@tonic-gate 					nlp = p;
230*7c478bd9Sstevel@tonic-gate 					c = www;
231*7c478bd9Sstevel@tonic-gate 					nfound=0;
232*7c478bd9Sstevel@tonic-gate 					}
233*7c478bd9Sstevel@tonic-gate 			}
234*7c478bd9Sstevel@tonic-gate 			else
235*7c478bd9Sstevel@tonic-gate 				ccount++;
236*7c478bd9Sstevel@tonic-gate 			continue;
237*7c478bd9Sstevel@tonic-gate 		}
238*7c478bd9Sstevel@tonic-gate # if D2
239*7c478bd9Sstevel@tonic-gate fprintf(stderr, "nr end loop p %o\n",p);
240*7c478bd9Sstevel@tonic-gate # endif
241*7c478bd9Sstevel@tonic-gate 		if (instr)
242*7c478bd9Sstevel@tonic-gate 			p++;
243*7c478bd9Sstevel@tonic-gate 		else
244*7c478bd9Sstevel@tonic-gate 		if (*p++ == '\n')
245*7c478bd9Sstevel@tonic-gate 		{
246*7c478bd9Sstevel@tonic-gate 			nlp = p;
247*7c478bd9Sstevel@tonic-gate 			c = www;
248*7c478bd9Sstevel@tonic-gate 			nfound=0;
249*7c478bd9Sstevel@tonic-gate 		}
250*7c478bd9Sstevel@tonic-gate 	}
251*7c478bd9Sstevel@tonic-gate 	if (instr==0)
252*7c478bd9Sstevel@tonic-gate 		close(f);
253*7c478bd9Sstevel@tonic-gate }
254*7c478bd9Sstevel@tonic-gate 
255*7c478bd9Sstevel@tonic-gate cgotofn() {
256*7c478bd9Sstevel@tonic-gate 	register c;
257*7c478bd9Sstevel@tonic-gate 	register struct words *s;
258*7c478bd9Sstevel@tonic-gate 	s = smax = www;
259*7c478bd9Sstevel@tonic-gate nword:
260*7c478bd9Sstevel@tonic-gate 	for(;;) {
261*7c478bd9Sstevel@tonic-gate # if D1
262*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, " in for loop c now %o %c\n",c, c>' ' ? c : ' ');
263*7c478bd9Sstevel@tonic-gate # endif
264*7c478bd9Sstevel@tonic-gate 		if ((c = gch())==0) return;
265*7c478bd9Sstevel@tonic-gate 		else if (c == '\n') {
266*7c478bd9Sstevel@tonic-gate 			s->out = 1;
267*7c478bd9Sstevel@tonic-gate 			s = www;
268*7c478bd9Sstevel@tonic-gate 		}
269*7c478bd9Sstevel@tonic-gate 		else {
270*7c478bd9Sstevel@tonic-gate loop:
271*7c478bd9Sstevel@tonic-gate 			if (s->inp == c) {
272*7c478bd9Sstevel@tonic-gate 				s = s->nst;
273*7c478bd9Sstevel@tonic-gate 				continue;
274*7c478bd9Sstevel@tonic-gate 			}
275*7c478bd9Sstevel@tonic-gate 			if (s->inp == 0) goto enter;
276*7c478bd9Sstevel@tonic-gate 			if (s->link == 0) {
277*7c478bd9Sstevel@tonic-gate 				if (smax >= &www[MAXSIZ - 1]) overflo();
278*7c478bd9Sstevel@tonic-gate 				s->link = ++smax;
279*7c478bd9Sstevel@tonic-gate 				s = smax;
280*7c478bd9Sstevel@tonic-gate 				goto enter;
281*7c478bd9Sstevel@tonic-gate 			}
282*7c478bd9Sstevel@tonic-gate 			s = s->link;
283*7c478bd9Sstevel@tonic-gate 			goto loop;
284*7c478bd9Sstevel@tonic-gate 		}
285*7c478bd9Sstevel@tonic-gate 	}
286*7c478bd9Sstevel@tonic-gate 
287*7c478bd9Sstevel@tonic-gate enter:
288*7c478bd9Sstevel@tonic-gate 	do {
289*7c478bd9Sstevel@tonic-gate 		s->inp = c;
290*7c478bd9Sstevel@tonic-gate 		if (smax >= &www[MAXSIZ - 1]) overflo();
291*7c478bd9Sstevel@tonic-gate 		s->nst = ++smax;
292*7c478bd9Sstevel@tonic-gate 		s = smax;
293*7c478bd9Sstevel@tonic-gate 	}
294*7c478bd9Sstevel@tonic-gate 	while ((c = gch()) != '\n');
295*7c478bd9Sstevel@tonic-gate 	smax->out = 1;
296*7c478bd9Sstevel@tonic-gate 	s = www;
297*7c478bd9Sstevel@tonic-gate 	numwords++;
298*7c478bd9Sstevel@tonic-gate 	goto nword;
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate }
301*7c478bd9Sstevel@tonic-gate 
302*7c478bd9Sstevel@tonic-gate gch()
303*7c478bd9Sstevel@tonic-gate {
304*7c478bd9Sstevel@tonic-gate 	static char *s;
305*7c478bd9Sstevel@tonic-gate 	if (flag==0)
306*7c478bd9Sstevel@tonic-gate 	{
307*7c478bd9Sstevel@tonic-gate 		flag=1;
308*7c478bd9Sstevel@tonic-gate 		s = *xargv++;
309*7c478bd9Sstevel@tonic-gate # if D1
310*7c478bd9Sstevel@tonic-gate 	fprintf(stderr, "next arg is %s xargc %d\n", xargc > 0 ? s : "", xargc);
311*7c478bd9Sstevel@tonic-gate # endif
312*7c478bd9Sstevel@tonic-gate 		if (xargc-- <=0) return(0);
313*7c478bd9Sstevel@tonic-gate 	}
314*7c478bd9Sstevel@tonic-gate 	if (*s) return(*s++);
315*7c478bd9Sstevel@tonic-gate 	for(flag=0; flag<2*BUFSIZ; flag++)
316*7c478bd9Sstevel@tonic-gate 		buf[flag]=0;
317*7c478bd9Sstevel@tonic-gate 	flag=0;
318*7c478bd9Sstevel@tonic-gate 	return('\n');
319*7c478bd9Sstevel@tonic-gate }
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate overflo() {
322*7c478bd9Sstevel@tonic-gate 	write(2,"wordlist too large\n", 19);
323*7c478bd9Sstevel@tonic-gate 	exit(2);
324*7c478bd9Sstevel@tonic-gate }
325*7c478bd9Sstevel@tonic-gate cfail() {
326*7c478bd9Sstevel@tonic-gate 	struct words *queue[QSIZE];
327*7c478bd9Sstevel@tonic-gate 	struct words **front, **rear;
328*7c478bd9Sstevel@tonic-gate 	struct words *state;
329*7c478bd9Sstevel@tonic-gate 	register char c;
330*7c478bd9Sstevel@tonic-gate 	register struct words *s;
331*7c478bd9Sstevel@tonic-gate 	s = www;
332*7c478bd9Sstevel@tonic-gate 	front = rear = queue;
333*7c478bd9Sstevel@tonic-gate init:
334*7c478bd9Sstevel@tonic-gate 	if ((s->inp) != 0) {
335*7c478bd9Sstevel@tonic-gate 		*rear++ = s->nst;
336*7c478bd9Sstevel@tonic-gate 		if (rear >= &queue[QSIZE - 1]) overflo();
337*7c478bd9Sstevel@tonic-gate 	}
338*7c478bd9Sstevel@tonic-gate 	if ((s = s->link) != 0) {
339*7c478bd9Sstevel@tonic-gate 		goto init;
340*7c478bd9Sstevel@tonic-gate 	}
341*7c478bd9Sstevel@tonic-gate 
342*7c478bd9Sstevel@tonic-gate 	while (rear!=front) {
343*7c478bd9Sstevel@tonic-gate 		s = *front;
344*7c478bd9Sstevel@tonic-gate 		if (front == &queue[QSIZE-1])
345*7c478bd9Sstevel@tonic-gate 			front = queue;
346*7c478bd9Sstevel@tonic-gate 		else front++;
347*7c478bd9Sstevel@tonic-gate cloop:
348*7c478bd9Sstevel@tonic-gate 		if ((c = s->inp) != 0) {
349*7c478bd9Sstevel@tonic-gate 			*rear = (q = s->nst);
350*7c478bd9Sstevel@tonic-gate 			if (front < rear)
351*7c478bd9Sstevel@tonic-gate 				if (rear >= &queue[QSIZE-1])
352*7c478bd9Sstevel@tonic-gate 					if (front == queue) overflo();
353*7c478bd9Sstevel@tonic-gate 					else rear = queue;
354*7c478bd9Sstevel@tonic-gate 			else rear++;
355*7c478bd9Sstevel@tonic-gate 			else
356*7c478bd9Sstevel@tonic-gate 				if (++rear == front) overflo();
357*7c478bd9Sstevel@tonic-gate 			state = s->fail;
358*7c478bd9Sstevel@tonic-gate floop:
359*7c478bd9Sstevel@tonic-gate 			if (state == 0) state = www;
360*7c478bd9Sstevel@tonic-gate 			if (state->inp == c) {
361*7c478bd9Sstevel@tonic-gate 				q->fail = state->nst;
362*7c478bd9Sstevel@tonic-gate 				if ((state->nst)->out == 1) q->out = 1;
363*7c478bd9Sstevel@tonic-gate 				continue;
364*7c478bd9Sstevel@tonic-gate 			}
365*7c478bd9Sstevel@tonic-gate 			else if ((state = state->link) != 0)
366*7c478bd9Sstevel@tonic-gate 				goto floop;
367*7c478bd9Sstevel@tonic-gate 		}
368*7c478bd9Sstevel@tonic-gate 		if ((s = s->link) != 0)
369*7c478bd9Sstevel@tonic-gate 			goto cloop;
370*7c478bd9Sstevel@tonic-gate 	}
371*7c478bd9Sstevel@tonic-gate }
372*7c478bd9Sstevel@tonic-gate 
373*7c478bd9Sstevel@tonic-gate static int seen[50];
374*7c478bd9Sstevel@tonic-gate new (x)
375*7c478bd9Sstevel@tonic-gate {
376*7c478bd9Sstevel@tonic-gate 	int i;
377*7c478bd9Sstevel@tonic-gate 	for(i=0; i<nfound; i++)
378*7c478bd9Sstevel@tonic-gate 		if (seen[i]==x)
379*7c478bd9Sstevel@tonic-gate 			return(0);
380*7c478bd9Sstevel@tonic-gate 	seen[i]=x;
381*7c478bd9Sstevel@tonic-gate 	return(1);
382*7c478bd9Sstevel@tonic-gate }
383