Research V7 development
[unix-history] / usr / src / cmd / refer / what2.c
CommitLineData
52960f2f
ML
1# include "stdio.h"
2# include "ctype.h"
3
4# define NS 5
5 struct sf {
6 char *text;
7 int olap;
8 } sents[NS];
9 struct sf *sp;
10 char stext[NS][500];
11
12describe (file, argc, argv, rf)
13 char *file, *argv[];
14 FILE *rf;
15{
16 int ns 0;
17 char linbuf[512], *line, *p;
18 int i, wrflg 0, wrote 0, ln 0;
19 FILE *fi;
20fi = fopen(file, "r");
21if (fi==NULL) return;
22for(i=1; i<argc; i++)
23 lcase(argv[i]);
24while (gsent(linbuf, 512, fi))
25 {
26 wrote=0;
27 for(line=linbuf; *line==' '; line++);
28 if (line[0]==0) continue;
29 for(p=line; *p; p++)
30 if (*p=='\t') *p= ' ';
31 if (wrflg && line[0]=='.' && isupper(line[1]))
32 wrflg=0;
33 if (wrflg)
34 {
35 output(line, ln, rf);
36 wrote=1;
37 }
38 if (prefix(".TL", line))
39 wrflg=1;
40 if (prefix(".AU", line))
41 wrflg = ln = 1;
42 if (prefix(".DA", line) || prefix(".ND", line))
43 output(line+4, 1, rf);
44 if (line[0]=='.')
45 continue;
46 if (wrote) continue;
47 ns=update(ns, line, count(line,argc,argv));
48 }
49fclose(fi);
50for(sp=sents; sp<sents+ns; sp++)
51 output(sp->text, 0, rf);
52}
53
54int state 0;
55int oldc '\n';
56gsent(buf, bsize, fi)
57 char *buf;
58 FILE *fi;
59{
60 char *s;
61 int c, leng 0;
62/* state
63 0: looking for '.'
64 1: looking for nl or space aftter '.'
65 2: looking for nl after line with dot.
66 */
67s=buf;
68if (state==2)
69 *s++='.';
70while ( (c = getc(fi)) > 0 )
71 {
72 switch(state)
73 {
74 case 0: /* normal */
75 if (c=='.' && oldc == '\n')
76 {
77 *s=0;
78 state=2;
79 oldc='\n';
80 return(1);
81 }
82 *s++ = (c=='\n'? ' ': c);
83 if (s>=buf+bsize)
84 {
85 *--s = 0;
86 return(1);
87 }
88 if (c=='.' || c == '?' || c=='!')
89 if (leng>1)
90 state=1;
91 leng = (isalpha(c) ? leng+1 : 0);
92 break;
93 case 1: /* found ., want nl or space */
94 if (c==' ' || c == '\n')
95 {
96 *s=0;
97 state=0;
98 oldc=c;
99 return(1);
100 }
101 *s++ = (c=='\n' ? ' ' : c);
102 state=0;
103 leng = 0;
104 break;
105 case 2: /* found trof line, want nl */
106 if (c == '\n')
107 {
108 *s=0;
109 state=0;
110 oldc='\n';
111 return(1);
112 }
113 *s++ = c;
114 break;
115 }
116 oldc=c;
117 }
118*s=0;
119return(0);
120}
121prefix( p, s)
122 char *p, *s;
123{
124int c;
125while ( (c= *p++) == *s++)
126 if (c==0)
127 return(1);
128return(c==0);
129}
130output (s, ln, rf)
131 char *s;
132 FILE *rf;
133{
134 char *t;
135 int more 1;
136 t=s;
137 while (more)
138 {
139 while (t<s+72 && *t)
140 t++;
141 if (*t)
142 {
143 while (*t != ' ' && t>(s+25))
144 t--;
145 *t=0;
146 more=1;
147 }
148 else
149 more=0;
150 printf("%s%s\n",ln++ ? " " : " ", s);
151 if (rf!=NULL)
152 fprintf(rf, "%s\n", s);
153 s= ++t;
154 }
155}
156count(isent, nw, wds)
157 char *wds[], *isent;
158{
159int saw[50], ct;
160char sb[512], *s sb;
161int i, c;
162for(i=1; i<nw; i++)
163 saw[i]=0;
164while (c = *isent++)
165 {
166 *s++ = isupper(c) ? tolower(c) : c;
167 }
168*s=0;
169s=sb;
170while (*s++)
171 {
172 if (s[-1]!=' ') continue;
173 for(i=1; i<nw; i++)
174 {
175 if (saw[i])continue;
176 if (prefix(wds[i], s))
177 saw[i]=1;
178 }
179 }
180ct=0;
181for(i=1; i<nw; i++)
182 if (saw[i])
183 ct++;
184return(ct);
185}
186lcase(s)
187 char *s;
188{
189 register int c;
190for(; c= *s; s++)
191 {
192 if (isupper(c))
193 *s= tolower(c);
194 }
195}
196update( ns, line, kov)
197 char *line;
198{
199/* see if sentence array should be updated */
200int lval 100; char *ob;
201struct sf *sp, *least NULL;
202if (kov<=0) return (ns) ; /* no*/
203if (ns<NS)
204 {
205 sp=sents+ns;
206 strcpy (sp->text = stext[ns], line);
207 sp->olap = kov;
208 return(ns+1);
209 }
210for(sp=sents+ns-1; sp>=sents; sp--)
211 {
212 if (sp->olap < lval)
213 {
214 least = sp;
215 lval = sp->olap;
216 }
217 }
218if (kov <= lval) return(ns);
219ob = least->text;
220while (++least < sents+NS)
221 {
222 (least-1)->text = least->text;
223 (least-1)->olap = least->olap;
224 }
225sp = sents+NS-1;
226strcpy (sp->text=ob, line);
227sp->olap = kov;
228return(NS);
229}