xref: /titanic_52/usr/src/cmd/refer/glue5.c (revision 11a8fa6cb17403e630122ac19b39a323c6e64142)
1*11a8fa6cSceastha /*
2*11a8fa6cSceastha  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
3*11a8fa6cSceastha  * Use is subject to license terms.
4*11a8fa6cSceastha  */
5*11a8fa6cSceastha 
67c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
77c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
87c478bd9Sstevel@tonic-gate 
97c478bd9Sstevel@tonic-gate /*
107c478bd9Sstevel@tonic-gate  * Copyright (c) 1980 Regents of the University of California.
117c478bd9Sstevel@tonic-gate  * All rights reserved. The Berkeley software License Agreement
127c478bd9Sstevel@tonic-gate  * specifies the terms and conditions for redistribution.
137c478bd9Sstevel@tonic-gate  */
147c478bd9Sstevel@tonic-gate 
157c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
167c478bd9Sstevel@tonic-gate 
177c478bd9Sstevel@tonic-gate 
187c478bd9Sstevel@tonic-gate #include <stdio.h>
197c478bd9Sstevel@tonic-gate #include <ctype.h>
207c478bd9Sstevel@tonic-gate /*
217c478bd9Sstevel@tonic-gate  * fgrep -- print all lines containing any of a set of keywords
227c478bd9Sstevel@tonic-gate  *
237c478bd9Sstevel@tonic-gate  *	status returns:
247c478bd9Sstevel@tonic-gate  *		0 - ok, and some matches
257c478bd9Sstevel@tonic-gate  *		1 - ok, but no matches
267c478bd9Sstevel@tonic-gate  *		2 - some error
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate #define	MAXSIZ 700
297c478bd9Sstevel@tonic-gate #define	QSIZE 400
307c478bd9Sstevel@tonic-gate struct words {
317c478bd9Sstevel@tonic-gate 	char 	inp;
327c478bd9Sstevel@tonic-gate 	char	out;
337c478bd9Sstevel@tonic-gate 	struct	words *nst;
347c478bd9Sstevel@tonic-gate 	struct	words *link;
357c478bd9Sstevel@tonic-gate 	struct	words *fail;
367c478bd9Sstevel@tonic-gate }
377c478bd9Sstevel@tonic-gate *www, *smax, *q;
387c478bd9Sstevel@tonic-gate 
397c478bd9Sstevel@tonic-gate char	buf[2*BUFSIZ];
407c478bd9Sstevel@tonic-gate int	nsucc;
417c478bd9Sstevel@tonic-gate int	need;
427c478bd9Sstevel@tonic-gate char	*instr;
437c478bd9Sstevel@tonic-gate int	inct;
447c478bd9Sstevel@tonic-gate int	rflag;
457c478bd9Sstevel@tonic-gate int	xargc;
467c478bd9Sstevel@tonic-gate char	**xargv;
477c478bd9Sstevel@tonic-gate int	numwords;
487c478bd9Sstevel@tonic-gate int	nfound;
497c478bd9Sstevel@tonic-gate static int flag = 0;
507c478bd9Sstevel@tonic-gate 
51*11a8fa6cSceastha extern void err();
52*11a8fa6cSceastha extern void *zalloc();
53*11a8fa6cSceastha 
54*11a8fa6cSceastha static void cfail(void);
55*11a8fa6cSceastha static void cgotofn(void);
56*11a8fa6cSceastha static void execute(void);
57*11a8fa6cSceastha static char gch(void);
58*11a8fa6cSceastha static int new(struct words *x);
59*11a8fa6cSceastha static void overflo(void);
60*11a8fa6cSceastha 
61*11a8fa6cSceastha int
62*11a8fa6cSceastha fgrep(int argc, char **argv)
637c478bd9Sstevel@tonic-gate {
647c478bd9Sstevel@tonic-gate 	nsucc = need = inct = rflag = numwords = nfound = 0;
657c478bd9Sstevel@tonic-gate 	instr = 0;
667c478bd9Sstevel@tonic-gate 	flag = 0;
677c478bd9Sstevel@tonic-gate 	if (www == 0)
687c478bd9Sstevel@tonic-gate 		www = (struct words *)zalloc(MAXSIZ, sizeof (*www));
697c478bd9Sstevel@tonic-gate 	if (www == NULL)
707c478bd9Sstevel@tonic-gate 		err(gettext("Can't get space for machines"), 0);
717c478bd9Sstevel@tonic-gate 	for (q = www; q < www+MAXSIZ; q++) {
727c478bd9Sstevel@tonic-gate 		q->inp = 0; q->out = 0; q->nst = 0; q->link = 0; q->fail = 0;
737c478bd9Sstevel@tonic-gate 	}
747c478bd9Sstevel@tonic-gate 	xargc = argc-1;
757c478bd9Sstevel@tonic-gate 	xargv = argv+1;
76*11a8fa6cSceastha 	while (xargc > 0 && xargv[0][0] == '-') {
77*11a8fa6cSceastha 		switch (xargv[0][1]) {
787c478bd9Sstevel@tonic-gate 			case 'r': /* return value only */
797c478bd9Sstevel@tonic-gate 				rflag++;
807c478bd9Sstevel@tonic-gate 				break;
817c478bd9Sstevel@tonic-gate 			case 'n': /* number of answers needed */
827c478bd9Sstevel@tonic-gate 				need = (int)xargv[1];
837c478bd9Sstevel@tonic-gate 				xargv++; xargc--;
847c478bd9Sstevel@tonic-gate 				break;
857c478bd9Sstevel@tonic-gate 			case 'i':
867c478bd9Sstevel@tonic-gate 				instr = xargv[1];
877c478bd9Sstevel@tonic-gate 				inct = (int)xargv[2]+2;
887c478bd9Sstevel@tonic-gate #if D2
897c478bd9Sstevel@tonic-gate fprintf(stderr, "inct %d xargv.2. %o %d\n", inct, xargv[2], xargv[2]);
907c478bd9Sstevel@tonic-gate #endif
917c478bd9Sstevel@tonic-gate 				xargv += 2; xargc -= 2;
927c478bd9Sstevel@tonic-gate 				break;
937c478bd9Sstevel@tonic-gate 		}
947c478bd9Sstevel@tonic-gate 		xargv++; xargc--;
957c478bd9Sstevel@tonic-gate 	}
96*11a8fa6cSceastha 	if (xargc <= 0) {
977c478bd9Sstevel@tonic-gate 		write(2, "bad fgrep call\n", 15);
987c478bd9Sstevel@tonic-gate 		exit(2);
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate #if D1
1017c478bd9Sstevel@tonic-gate 	fprintf(stderr, "before cgoto\n");
1027c478bd9Sstevel@tonic-gate #endif
1037c478bd9Sstevel@tonic-gate 	cgotofn();
1047c478bd9Sstevel@tonic-gate #if D1
1057c478bd9Sstevel@tonic-gate 	fprintf(stderr, "before cfail\n");
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate 	cfail();
1087c478bd9Sstevel@tonic-gate #if D1
1097c478bd9Sstevel@tonic-gate 	fprintf(stderr, "before execute instr %.20s\n", instr? instr: "");
1107c478bd9Sstevel@tonic-gate 	fprintf(stderr, "end of string %d %c %c %c\n", inct,
1117c478bd9Sstevel@tonic-gate 	    instr ? instr[inct-3] : '\0',
1127c478bd9Sstevel@tonic-gate 	    instr ? instr[inct-2] : '\0',
1137c478bd9Sstevel@tonic-gate 	    instr ? instr[inct-1] : '\0');
1147c478bd9Sstevel@tonic-gate #endif
1157c478bd9Sstevel@tonic-gate 	execute();
1167c478bd9Sstevel@tonic-gate #if D1
1177c478bd9Sstevel@tonic-gate 	fprintf(stderr, "returning nsucc %d\n", nsucc);
1187c478bd9Sstevel@tonic-gate 	fprintf(stderr, "fgrep done www %o\n", www);
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate 	return (nsucc == 0);
1217c478bd9Sstevel@tonic-gate }
1227c478bd9Sstevel@tonic-gate 
123*11a8fa6cSceastha static void
124*11a8fa6cSceastha execute(void)
1257c478bd9Sstevel@tonic-gate {
126*11a8fa6cSceastha 	char *p;
127*11a8fa6cSceastha 	struct words *c;
128*11a8fa6cSceastha 	char ch;
129*11a8fa6cSceastha 	int ccount;
1307c478bd9Sstevel@tonic-gate 	int f;
1317c478bd9Sstevel@tonic-gate 	char *nlp;
1327c478bd9Sstevel@tonic-gate 	f = 0;
1337c478bd9Sstevel@tonic-gate 	ccount = instr ? inct : 0;
1347c478bd9Sstevel@tonic-gate 	nfound = 0;
1357c478bd9Sstevel@tonic-gate 	p = instr ? instr : buf;
1367c478bd9Sstevel@tonic-gate 	if (need == 0) need = numwords;
1377c478bd9Sstevel@tonic-gate 	nlp = p;
1387c478bd9Sstevel@tonic-gate 	c = www;
1397c478bd9Sstevel@tonic-gate #if D2
1407c478bd9Sstevel@tonic-gate fprintf(stderr, "in execute ccount %d inct %d\n", ccount, inct);
1417c478bd9Sstevel@tonic-gate #endif
1427c478bd9Sstevel@tonic-gate 	for (;;) {
1437c478bd9Sstevel@tonic-gate #if D3
1447c478bd9Sstevel@tonic-gate fprintf(stderr, "down ccount\n");
1457c478bd9Sstevel@tonic-gate #endif
1467c478bd9Sstevel@tonic-gate 		if (--ccount <= 0) {
1477c478bd9Sstevel@tonic-gate #if D2
1487c478bd9Sstevel@tonic-gate fprintf(stderr, "ex loop ccount %d instr %o\n", ccount, instr);
1497c478bd9Sstevel@tonic-gate #endif
1507c478bd9Sstevel@tonic-gate 			if (instr) break;
1517c478bd9Sstevel@tonic-gate 			if (p == &buf[2*BUFSIZ]) p = buf;
1527c478bd9Sstevel@tonic-gate 			if (p > &buf[BUFSIZ]) {
153*11a8fa6cSceastha 				if ((ccount = read(f, p,
154*11a8fa6cSceastha 				    &buf[2*BUFSIZ] - p)) <= 0)
155*11a8fa6cSceastha 					break;
156*11a8fa6cSceastha 			} else if ((ccount = read(f, p, BUFSIZ)) <= 0) break;
1577c478bd9Sstevel@tonic-gate #if D2
1587c478bd9Sstevel@tonic-gate fprintf(stderr, " normal read %d bytres\n", ccount);
159*11a8fa6cSceastha {
160*11a8fa6cSceastha 	char xx[20];
161*11a8fa6cSceastha 	sprintf(xx, "they are %%.%ds\n", ccount);
1627c478bd9Sstevel@tonic-gate 	fprintf(stderr, xx, p);
1637c478bd9Sstevel@tonic-gate }
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 		}
1667c478bd9Sstevel@tonic-gate nstate:
1677c478bd9Sstevel@tonic-gate 		ch = *p;
1687c478bd9Sstevel@tonic-gate #if D2
1697c478bd9Sstevel@tonic-gate fprintf(stderr, "roaming along in ex ch %c c %o\n", ch, c);
1707c478bd9Sstevel@tonic-gate #endif
1717c478bd9Sstevel@tonic-gate 		if (isupper(ch)) ch |= 040;
1727c478bd9Sstevel@tonic-gate 		if (c->inp == ch) {
1737c478bd9Sstevel@tonic-gate 			c = c->nst;
174*11a8fa6cSceastha 		} else if (c->link != 0) {
1757c478bd9Sstevel@tonic-gate 			c = c->link;
1767c478bd9Sstevel@tonic-gate 			goto nstate;
177*11a8fa6cSceastha 		} else {
1787c478bd9Sstevel@tonic-gate 			c = c->fail;
1797c478bd9Sstevel@tonic-gate 			if (c == 0) {
1807c478bd9Sstevel@tonic-gate 				c = www;
1817c478bd9Sstevel@tonic-gate istate:
1827c478bd9Sstevel@tonic-gate 				if (c->inp == ch) {
1837c478bd9Sstevel@tonic-gate 					c = c->nst;
184*11a8fa6cSceastha 				} else if (c->link != 0) {
1857c478bd9Sstevel@tonic-gate 					c = c->link;
1867c478bd9Sstevel@tonic-gate 					goto istate;
1877c478bd9Sstevel@tonic-gate 				}
188*11a8fa6cSceastha 			} else goto nstate;
1897c478bd9Sstevel@tonic-gate 		}
1907c478bd9Sstevel@tonic-gate 		if (c->out && new(c)) {
1917c478bd9Sstevel@tonic-gate #if D2
1927c478bd9Sstevel@tonic-gate fprintf(stderr, " found: nfound %d need %d\n", nfound, need);
1937c478bd9Sstevel@tonic-gate #endif
194*11a8fa6cSceastha 			if (++nfound >= need) {
1957c478bd9Sstevel@tonic-gate #if D1
196*11a8fa6cSceastha fprintf(stderr, "found, p %o nlp %o ccount %d buf %o buf[2*BUFSIZ] %o\n",
197*11a8fa6cSceastha     p, nlp, ccount, buf, buf+2*BUFSIZ);
1987c478bd9Sstevel@tonic-gate #endif
1997c478bd9Sstevel@tonic-gate 				if (instr == 0)
2007c478bd9Sstevel@tonic-gate 				while (*p++ != '\n') {
2017c478bd9Sstevel@tonic-gate #if D3
2027c478bd9Sstevel@tonic-gate fprintf(stderr, "down ccount2\n");
2037c478bd9Sstevel@tonic-gate #endif
2047c478bd9Sstevel@tonic-gate 					if (--ccount <= 0) {
205*11a8fa6cSceastha 						if (p == &buf[2*BUFSIZ])
206*11a8fa6cSceastha 							p = buf;
2077c478bd9Sstevel@tonic-gate 						if (p > &buf[BUFSIZ]) {
208*11a8fa6cSceastha 							if ((ccount = read(f, p,
209*11a8fa6cSceastha 							    &buf[2*BUFSIZ] - p))
210*11a8fa6cSceastha 							    <= 0)
211*11a8fa6cSceastha 								break;
212*11a8fa6cSceastha 						} else if ((ccount = read(f, p,
213*11a8fa6cSceastha 						    BUFSIZ)) <= 0)
214*11a8fa6cSceastha 							break;
2157c478bd9Sstevel@tonic-gate #if D2
2167c478bd9Sstevel@tonic-gate fprintf(stderr, " read %d bytes\n", ccount);
2177c478bd9Sstevel@tonic-gate { char xx[20]; sprintf(xx, "they are %%.%ds\n", ccount);
2187c478bd9Sstevel@tonic-gate fprintf(stderr, xx, p);
2197c478bd9Sstevel@tonic-gate }
2207c478bd9Sstevel@tonic-gate #endif
2217c478bd9Sstevel@tonic-gate 					}
2227c478bd9Sstevel@tonic-gate 				}
2237c478bd9Sstevel@tonic-gate 				nsucc = 1;
224*11a8fa6cSceastha 				if (rflag == 0) {
2257c478bd9Sstevel@tonic-gate #if D2
2267c478bd9Sstevel@tonic-gate fprintf(stderr, "p %o nlp %o buf %o\n", p, nlp, buf);
2277c478bd9Sstevel@tonic-gate if (p > nlp)
2287c478bd9Sstevel@tonic-gate {write(2, "XX\n", 3); write(2, nlp, p-nlp); write(2, "XX\n", 3); }
2297c478bd9Sstevel@tonic-gate #endif
2307c478bd9Sstevel@tonic-gate 					if (p > nlp) write(1, nlp, p-nlp);
2317c478bd9Sstevel@tonic-gate 					else {
232*11a8fa6cSceastha 						write(1, nlp,
233*11a8fa6cSceastha 						    &buf[2*BUFSIZ] - nlp);
2347c478bd9Sstevel@tonic-gate 						write(1, buf, p-&buf[0]);
2357c478bd9Sstevel@tonic-gate 					}
2367c478bd9Sstevel@tonic-gate 					if (p[-1] != '\n') write(1, "\n", 1);
2377c478bd9Sstevel@tonic-gate 				}
238*11a8fa6cSceastha 				if (instr == 0) {
2397c478bd9Sstevel@tonic-gate 					nlp = p;
2407c478bd9Sstevel@tonic-gate 					c = www;
2417c478bd9Sstevel@tonic-gate 					nfound = 0;
2427c478bd9Sstevel@tonic-gate 				}
243*11a8fa6cSceastha 			} else
2447c478bd9Sstevel@tonic-gate 				ccount++;
2457c478bd9Sstevel@tonic-gate 			continue;
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate #if D2
2487c478bd9Sstevel@tonic-gate fprintf(stderr, "nr end loop p %o\n", p);
2497c478bd9Sstevel@tonic-gate #endif
2507c478bd9Sstevel@tonic-gate 		if (instr)
2517c478bd9Sstevel@tonic-gate 			p++;
2527c478bd9Sstevel@tonic-gate 		else
2537c478bd9Sstevel@tonic-gate 		if (*p++ == '\n')
2547c478bd9Sstevel@tonic-gate 		{
2557c478bd9Sstevel@tonic-gate 			nlp = p;
2567c478bd9Sstevel@tonic-gate 			c = www;
2577c478bd9Sstevel@tonic-gate 			nfound = 0;
2587c478bd9Sstevel@tonic-gate 		}
2597c478bd9Sstevel@tonic-gate 	}
2607c478bd9Sstevel@tonic-gate 	if (instr == 0)
2617c478bd9Sstevel@tonic-gate 		close(f);
2627c478bd9Sstevel@tonic-gate }
2637c478bd9Sstevel@tonic-gate 
264*11a8fa6cSceastha static void
265*11a8fa6cSceastha cgotofn(void)
266*11a8fa6cSceastha {
267*11a8fa6cSceastha 	char c;
268*11a8fa6cSceastha 	struct words *s;
2697c478bd9Sstevel@tonic-gate 	s = smax = www;
2707c478bd9Sstevel@tonic-gate nword:
2717c478bd9Sstevel@tonic-gate 	for (;;) {
2727c478bd9Sstevel@tonic-gate #if D1
2737c478bd9Sstevel@tonic-gate 	fprintf(stderr, " in for loop c now %o %c\n", c, c > ' ' ? c : ' ');
2747c478bd9Sstevel@tonic-gate #endif
275*11a8fa6cSceastha 		if ((c = gch()) == 0)
276*11a8fa6cSceastha 			return;
2777c478bd9Sstevel@tonic-gate 		else if (c == '\n') {
2787c478bd9Sstevel@tonic-gate 			s->out = 1;
2797c478bd9Sstevel@tonic-gate 			s = www;
280*11a8fa6cSceastha 		} else {
2817c478bd9Sstevel@tonic-gate loop:
2827c478bd9Sstevel@tonic-gate 			if (s->inp == c) {
2837c478bd9Sstevel@tonic-gate 				s = s->nst;
2847c478bd9Sstevel@tonic-gate 				continue;
2857c478bd9Sstevel@tonic-gate 			}
2867c478bd9Sstevel@tonic-gate 			if (s->inp == 0) goto enter;
2877c478bd9Sstevel@tonic-gate 			if (s->link == 0) {
2887c478bd9Sstevel@tonic-gate 				if (smax >= &www[MAXSIZ - 1]) overflo();
2897c478bd9Sstevel@tonic-gate 				s->link = ++smax;
2907c478bd9Sstevel@tonic-gate 				s = smax;
2917c478bd9Sstevel@tonic-gate 				goto enter;
2927c478bd9Sstevel@tonic-gate 			}
2937c478bd9Sstevel@tonic-gate 			s = s->link;
2947c478bd9Sstevel@tonic-gate 			goto loop;
2957c478bd9Sstevel@tonic-gate 		}
2967c478bd9Sstevel@tonic-gate 	}
2977c478bd9Sstevel@tonic-gate 
2987c478bd9Sstevel@tonic-gate enter:
2997c478bd9Sstevel@tonic-gate 	do {
3007c478bd9Sstevel@tonic-gate 		s->inp = c;
3017c478bd9Sstevel@tonic-gate 		if (smax >= &www[MAXSIZ - 1]) overflo();
3027c478bd9Sstevel@tonic-gate 		s->nst = ++smax;
3037c478bd9Sstevel@tonic-gate 		s = smax;
3047c478bd9Sstevel@tonic-gate 	}
305*11a8fa6cSceastha 	while ((c = gch()) != '\n')
306*11a8fa6cSceastha 		;
3077c478bd9Sstevel@tonic-gate 	smax->out = 1;
3087c478bd9Sstevel@tonic-gate 	s = www;
3097c478bd9Sstevel@tonic-gate 	numwords++;
3107c478bd9Sstevel@tonic-gate 	goto nword;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate }
3137c478bd9Sstevel@tonic-gate 
314*11a8fa6cSceastha static char
315*11a8fa6cSceastha gch(void)
3167c478bd9Sstevel@tonic-gate {
3177c478bd9Sstevel@tonic-gate 	static char *s;
318*11a8fa6cSceastha 	if (flag == 0) {
3197c478bd9Sstevel@tonic-gate 		flag = 1;
3207c478bd9Sstevel@tonic-gate 		s = *xargv++;
3217c478bd9Sstevel@tonic-gate #if D1
3227c478bd9Sstevel@tonic-gate 	fprintf(stderr, "next arg is %s xargc %d\n", xargc > 0 ? s : "", xargc);
3237c478bd9Sstevel@tonic-gate #endif
324*11a8fa6cSceastha 		if (xargc-- <= 0)
325*11a8fa6cSceastha 			return (0);
3267c478bd9Sstevel@tonic-gate 	}
327*11a8fa6cSceastha 	if (*s)
328*11a8fa6cSceastha 		return (*s++);
3297c478bd9Sstevel@tonic-gate 	for (flag = 0; flag < 2*BUFSIZ; flag++)
3307c478bd9Sstevel@tonic-gate 		buf[flag] = 0;
3317c478bd9Sstevel@tonic-gate 	flag = 0;
3327c478bd9Sstevel@tonic-gate 	return ('\n');
3337c478bd9Sstevel@tonic-gate }
3347c478bd9Sstevel@tonic-gate 
335*11a8fa6cSceastha static void
336*11a8fa6cSceastha overflo(void)
337*11a8fa6cSceastha {
3387c478bd9Sstevel@tonic-gate 	write(2, "wordlist too large\n", 19);
3397c478bd9Sstevel@tonic-gate 	exit(2);
3407c478bd9Sstevel@tonic-gate }
341*11a8fa6cSceastha 
342*11a8fa6cSceastha static void
343*11a8fa6cSceastha cfail(void)
344*11a8fa6cSceastha {
3457c478bd9Sstevel@tonic-gate 	struct words *queue[QSIZE];
3467c478bd9Sstevel@tonic-gate 	struct words **front, **rear;
3477c478bd9Sstevel@tonic-gate 	struct words *state;
348*11a8fa6cSceastha 	char c;
349*11a8fa6cSceastha 	struct words *s;
3507c478bd9Sstevel@tonic-gate 	s = www;
3517c478bd9Sstevel@tonic-gate 	front = rear = queue;
3527c478bd9Sstevel@tonic-gate init:
3537c478bd9Sstevel@tonic-gate 	if ((s->inp) != 0) {
3547c478bd9Sstevel@tonic-gate 		*rear++ = s->nst;
3557c478bd9Sstevel@tonic-gate 		if (rear >= &queue[QSIZE - 1]) overflo();
3567c478bd9Sstevel@tonic-gate 	}
3577c478bd9Sstevel@tonic-gate 	if ((s = s->link) != 0) {
3587c478bd9Sstevel@tonic-gate 		goto init;
3597c478bd9Sstevel@tonic-gate 	}
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	while (rear != front) {
3627c478bd9Sstevel@tonic-gate 		s = *front;
3637c478bd9Sstevel@tonic-gate 		if (front == &queue[QSIZE-1])
3647c478bd9Sstevel@tonic-gate 			front = queue;
3657c478bd9Sstevel@tonic-gate 		else front++;
3667c478bd9Sstevel@tonic-gate cloop:
3677c478bd9Sstevel@tonic-gate 		if ((c = s->inp) != 0) {
3687c478bd9Sstevel@tonic-gate 			*rear = (q = s->nst);
3697c478bd9Sstevel@tonic-gate 			if (front < rear)
3707c478bd9Sstevel@tonic-gate 				if (rear >= &queue[QSIZE-1])
3717c478bd9Sstevel@tonic-gate 					if (front == queue) overflo();
3727c478bd9Sstevel@tonic-gate 					else rear = queue;
3737c478bd9Sstevel@tonic-gate 			else rear++;
3747c478bd9Sstevel@tonic-gate 			else
3757c478bd9Sstevel@tonic-gate 				if (++rear == front) overflo();
3767c478bd9Sstevel@tonic-gate 			state = s->fail;
3777c478bd9Sstevel@tonic-gate floop:
3787c478bd9Sstevel@tonic-gate 			if (state == 0) state = www;
3797c478bd9Sstevel@tonic-gate 			if (state->inp == c) {
3807c478bd9Sstevel@tonic-gate 				q->fail = state->nst;
3817c478bd9Sstevel@tonic-gate 				if ((state->nst)->out == 1) q->out = 1;
3827c478bd9Sstevel@tonic-gate 				continue;
383*11a8fa6cSceastha 			} else if ((state = state->link) != 0)
3847c478bd9Sstevel@tonic-gate 				goto floop;
3857c478bd9Sstevel@tonic-gate 		}
3867c478bd9Sstevel@tonic-gate 		if ((s = s->link) != 0)
3877c478bd9Sstevel@tonic-gate 			goto cloop;
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
391*11a8fa6cSceastha static struct words *seen[50];
392*11a8fa6cSceastha 
393*11a8fa6cSceastha static int
394*11a8fa6cSceastha new(struct words *x)
3957c478bd9Sstevel@tonic-gate {
3967c478bd9Sstevel@tonic-gate 	int i;
3977c478bd9Sstevel@tonic-gate 	for (i = 0; i < nfound; i++)
3987c478bd9Sstevel@tonic-gate 		if (seen[i] == x)
3997c478bd9Sstevel@tonic-gate 			return (0);
4007c478bd9Sstevel@tonic-gate 	seen[i] = x;
4017c478bd9Sstevel@tonic-gate 	return (1);
4027c478bd9Sstevel@tonic-gate }
403