Annotation of 41BSD/cmd/refer/what2.c, revision 1.1

1.1     ! root        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: 
        !            12: describe (file, argc, argv, rf)
        !            13:        char *file, *argv[];
        !            14:        FILE *rf;
        !            15: {
        !            16:        int ns 0;
        !            17:        char linbuf[BUFSIZ], *line, *p;
        !            18:        int i, wrflg 0, wrote 0, ln 0;
        !            19:        FILE *fi;
        !            20: fi = fopen(file, "r");
        !            21: if (fi==NULL) return;
        !            22: for(i=1; i<argc; i++)
        !            23:        lcase(argv[i]);
        !            24: while (gsent(linbuf, BUFSIZ, 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:        }
        !            49: fclose(fi);
        !            50: for(sp=sents; sp<sents+ns; sp++)
        !            51:        output(sp->text, 0, rf);
        !            52: }
        !            53: 
        !            54: int state 0;
        !            55: int oldc '\n';
        !            56: gsent(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:        */
        !            67: s=buf;
        !            68: if (state==2)
        !            69:        *s++='.';
        !            70: while ( (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;
        !           119: return(0);
        !           120: }
        !           121: prefix( p, s)
        !           122:        char *p, *s;
        !           123: {
        !           124: int c;
        !           125: while ( (c= *p++) == *s++)
        !           126:        if (c==0)
        !           127:                return(1);
        !           128: return(c==0);
        !           129: }
        !           130: output (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: }
        !           156: count(isent, nw, wds)
        !           157:        char *wds[], *isent;
        !           158: {
        !           159: int saw[50], ct;
        !           160: char sb[BUFSIZ], *s sb;
        !           161: int i, c;
        !           162: for(i=1; i<nw; i++)
        !           163:        saw[i]=0;
        !           164: while (c = *isent++)
        !           165:        {
        !           166:        *s++ = isupper(c) ? tolower(c) : c;
        !           167:        }
        !           168: *s=0;
        !           169: s=sb;
        !           170: while (*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:        }
        !           180: ct=0;
        !           181: for(i=1; i<nw; i++)
        !           182:        if (saw[i])
        !           183:                ct++;
        !           184: return(ct);
        !           185: }
        !           186: lcase(s)
        !           187:        char *s;
        !           188: {
        !           189:        register int c;
        !           190: for(; c= *s; s++)
        !           191:        {
        !           192:        if (isupper(c))
        !           193:                *s= tolower(c);
        !           194:        }
        !           195: }
        !           196: update( ns, line, kov)
        !           197:        char *line;
        !           198: {
        !           199: /* see if sentence array should be updated */
        !           200: int lval 100; char *ob;
        !           201: struct sf *sp, *least NULL;
        !           202: if (kov<=0) return (ns) ; /* no*/
        !           203: if (ns<NS)
        !           204:        {
        !           205:        sp=sents+ns;
        !           206:        strcpy (sp->text = stext[ns], line);
        !           207:        sp->olap = kov;
        !           208:        return(ns+1);
        !           209:        }
        !           210: for(sp=sents+ns-1; sp>=sents; sp--)
        !           211:        {
        !           212:        if (sp->olap < lval)
        !           213:                {
        !           214:                least = sp;
        !           215:                lval = sp->olap;
        !           216:                }
        !           217:        }
        !           218: if (kov <= lval) return(ns);
        !           219: ob = least->text;
        !           220: while (++least < sents+NS)
        !           221:        {
        !           222:        (least-1)->text = least->text;
        !           223:        (least-1)->olap = least->olap;
        !           224:        }
        !           225: sp = sents+NS-1;
        !           226: strcpy (sp->text=ob, line);
        !           227: sp->olap = kov;
        !           228: return(NS);
        !           229: }

unix.superglobalmegacorp.com

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