Annotation of 43BSD/usr.bin/refer/what2.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.