112308Stut #ifndef lint
2*32129Sbostic static char *sccsid = "@(#)what2.c	4.2 (Berkeley) 09/11/87";
312308Stut #endif
412308Stut 
512308Stut #include "stdio.h"
612308Stut #include "ctype.h"
712308Stut #define NS 5
812308Stut 
912308Stut struct sf {
1012308Stut 	char *text;
1112308Stut 	int olap;
1212308Stut }
1312308Stut sents[NS];
1412308Stut struct sf *sp;
1512308Stut char stext[NS][500];
1612308Stut 
1712308Stut describe (file, argc, argv, rf)
1812308Stut char *file, *argv[];
1912308Stut FILE *rf;
2012308Stut {
21*32129Sbostic 	int ns = 0;
2212308Stut 	char linbuf[BUFSIZ], *line, *p;
23*32129Sbostic 	int i, wrflg = 0, wrote = 0, ln = 0;
2412308Stut 	FILE *fi;
2512308Stut 	fi = fopen(file, "r");
2612308Stut 	if (fi==NULL) return;
2712308Stut 	for(i=1; i<argc; i++)
2812308Stut 		lcase(argv[i]);
2912308Stut 	while (gsent(linbuf, BUFSIZ, fi))
3012308Stut 	{
3112308Stut 		wrote=0;
3212308Stut 		for(line=linbuf; *line==' '; line++);
3312308Stut 		if (line[0]==0) continue;
3412308Stut 		for(p=line; *p; p++)
3512308Stut 			if (*p=='\t') *p= ' ';
3612308Stut 		if (wrflg && line[0]=='.' && isupper(line[1]))
3712308Stut 			wrflg=0;
3812308Stut 		if (wrflg)
3912308Stut 		{
4012308Stut 			output(line, ln, rf);
4112308Stut 			wrote=1;
4212308Stut 		}
4312308Stut 		if (prefix(".TL", line))
4412308Stut 			wrflg=1;
4512308Stut 		if (prefix(".AU", line))
4612308Stut 			wrflg = ln = 1;
4712308Stut 		if (prefix(".DA", line) || prefix(".ND", line))
4812308Stut 			output(line+4, 1, rf);
4912308Stut 		if (line[0]=='.')
5012308Stut 			continue;
5112308Stut 		if (wrote) continue;
5212308Stut 		ns=update(ns, line, count(line,argc,argv));
5312308Stut 	}
5412308Stut 	fclose(fi);
5512308Stut 	for(sp=sents; sp<sents+ns; sp++)
5612308Stut 		output(sp->text, 0, rf);
5712308Stut }
5812308Stut 
59*32129Sbostic int state = 0;
60*32129Sbostic int oldc = '\n';
6112308Stut 
6212308Stut gsent(buf, bsize, fi)
6312308Stut char *buf;
6412308Stut FILE *fi;
6512308Stut {
6612308Stut 	char *s;
67*32129Sbostic 	int c, leng = 0;
6812308Stut 	/* state
6912308Stut 		0: looking for '.'
7012308Stut 		1: looking for nl or space aftter '.'
7112308Stut 		2: looking for nl after line with dot.
7212308Stut 		*/
7312308Stut 	s=buf;
7412308Stut 	if (state==2)
7512308Stut 		*s++='.';
7612308Stut 	while ( (c = getc(fi)) > 0 )
7712308Stut 	{
7812308Stut 		switch(state)
7912308Stut 		{
8012308Stut 		case 0: /* normal */
8112308Stut 			if (c=='.' && oldc == '\n')
8212308Stut 			{
8312308Stut 				*s=0;
8412308Stut 				state=2;
8512308Stut 				oldc='\n';
8612308Stut 				return(1);
8712308Stut 			}
8812308Stut 			*s++ = (c=='\n'? ' ': c);
8912308Stut 			if (s>=buf+bsize)
9012308Stut 			{
9112308Stut 				*--s = 0;
9212308Stut 				return(1);
9312308Stut 			}
9412308Stut 			if (c=='.' || c == '?' || c=='!')
9512308Stut 				if (leng>1)
9612308Stut 					state=1;
9712308Stut 			leng = (isalpha(c) ? leng+1 : 0);
9812308Stut 			break;
9912308Stut 		case 1: /* found ., want nl or space */
10012308Stut 			if (c==' ' || c == '\n')
10112308Stut 			{
10212308Stut 				*s=0;
10312308Stut 				state=0;
10412308Stut 				oldc=c;
10512308Stut 				return(1);
10612308Stut 			}
10712308Stut 			*s++ = (c=='\n' ? ' ' : c);
10812308Stut 			state=0;
10912308Stut 			leng = 0;
11012308Stut 			break;
11112308Stut 		case 2: /* found trof line, want nl */
11212308Stut 			if (c == '\n')
11312308Stut 			{
11412308Stut 				*s=0;
11512308Stut 				state=0;
11612308Stut 				oldc='\n';
11712308Stut 				return(1);
11812308Stut 			}
11912308Stut 			*s++ = c;
12012308Stut 			break;
12112308Stut 		}
12212308Stut 		oldc=c;
12312308Stut 	}
12412308Stut 	*s=0;
12512308Stut 	return(0);
12612308Stut }
12712308Stut 
12812308Stut prefix( p, s)
12912308Stut char *p, *s;
13012308Stut {
13112308Stut 	int c;
13212308Stut 	while ( (c= *p++) == *s++)
13312308Stut 		if (c==0)
13412308Stut 			return(1);
13512308Stut 	return(c==0);
13612308Stut }
13712308Stut 
13812308Stut output (s, ln, rf)
13912308Stut char *s;
14012308Stut FILE *rf;
14112308Stut {
14212308Stut 	char *t;
143*32129Sbostic 	int more = 1;
14412308Stut 	t=s;
14512308Stut 	while (more)
14612308Stut 	{
14712308Stut 		while (t<s+72 && *t)
14812308Stut 			t++;
14912308Stut 		if (*t)
15012308Stut 		{
15112308Stut 			while (*t != ' ' && t>(s+25))
15212308Stut 				t--;
15312308Stut 			*t=0;
15412308Stut 			more=1;
15512308Stut 		}
15612308Stut 		else
15712308Stut 			more=0;
15812308Stut 		printf("%s%s\n",ln++ ? "     " : "   ", s);
15912308Stut 		if (rf!=NULL)
16012308Stut 			fprintf(rf, "%s\n", s);
16112308Stut 		s= ++t;
16212308Stut 	}
16312308Stut }
16412308Stut 
16512308Stut count(isent, nw, wds)
16612308Stut char *wds[], *isent;
16712308Stut {
16812308Stut 	int saw[50], ct;
169*32129Sbostic 	char sb[BUFSIZ], *s = sb;
17012308Stut 	int i, c;
17112308Stut 	for(i=1; i<nw; i++)
17212308Stut 		saw[i]=0;
17312308Stut 	while (c = *isent++)
17412308Stut 	{
17512308Stut 		*s++ = isupper(c) ? tolower(c) : c;
17612308Stut 	}
17712308Stut 	*s=0;
17812308Stut 	s=sb;
17912308Stut 	while (*s++)
18012308Stut 	{
18112308Stut 		if (s[-1]!=' ') continue;
18212308Stut 		for(i=1; i<nw; i++)
18312308Stut 		{
18412308Stut 			if (saw[i])continue;
18512308Stut 			if (prefix(wds[i], s))
18612308Stut 				saw[i]=1;
18712308Stut 		}
18812308Stut 	}
18912308Stut 	ct=0;
19012308Stut 	for(i=1; i<nw; i++)
19112308Stut 		if (saw[i])
19212308Stut 			ct++;
19312308Stut 	return(ct);
19412308Stut }
19512308Stut 
19612308Stut lcase(s)
19712308Stut char *s;
19812308Stut {
19912308Stut 	register int c;
20012308Stut 	for(; c= *s; s++)
20112308Stut 	{
20212308Stut 		if (isupper(c))
20312308Stut 			*s= tolower(c);
20412308Stut 	}
20512308Stut }
20612308Stut 
20712308Stut update( ns, line, kov)
20812308Stut char *line;
20912308Stut {
21012308Stut 	/* see if sentence array should be updated */
211*32129Sbostic 	int lval = 100;
21212308Stut 	char *ob;
213*32129Sbostic 	struct sf *sp, *least = NULL;
21412308Stut 	if (kov<=0) return (ns) ; /* no*/
21512308Stut 	if (ns<NS)
21612308Stut 	{
21712308Stut 		sp=sents+ns;
21812308Stut 		strcpy (sp->text = stext[ns], line);
21912308Stut 		sp->olap = kov;
22012308Stut 		return(ns+1);
22112308Stut 	}
22212308Stut 	for(sp=sents+ns-1; sp>=sents; sp--)
22312308Stut 	{
22412308Stut 		if (sp->olap < lval)
22512308Stut 		{
22612308Stut 			least = sp;
22712308Stut 			lval = sp->olap;
22812308Stut 		}
22912308Stut 	}
23012308Stut 	if (kov <= lval) return(ns);
23112308Stut 	ob = least->text;
23212308Stut 	while (++least < sents+NS)
23312308Stut 	{
23412308Stut 		(least-1)->text = least->text;
23512308Stut 		(least-1)->olap = least->olap;
23612308Stut 	}
23712308Stut 	sp = sents+NS-1;
23812308Stut 	strcpy (sp->text=ob, line);
23912308Stut 	sp->olap = kov;
24012308Stut 	return(NS);
24112308Stut }
242