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