Annotation of researchv10dc/vol2/index/tools/sdiction.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * diction -- print all sentences containing one of default phrases
        !             3:  *
        !             4:  *     status returns:
        !             5:  *             0 - ok, and some matches
        !             6:  *             1 - ok, but no matches
        !             7:  *             2 - some error
        !             8:  */
        !             9: 
        !            10: #include <stdio.h>
        !            11: #include <ctype.h>
        !            12: 
        !            13: #define        MAXSIZ 6500
        !            14: #define QSIZE 1000
        !            15: #define DICT ""
        !            16: int linemsg;
        !            17: long olcount;
        !            18: long lcount;
        !            19: struct words {
        !            20:        char    inp;
        !            21:        char    out;
        !            22:        struct  words *nst;
        !            23:        struct  words *link;
        !            24:        struct  words *fail;
        !            25: } w[MAXSIZ], *smax, *q;
        !            26: 
        !            27: char table[128] = {
        !            28:        0, 0, 0, 0, 0, 0, 0, 0,
        !            29:        0, 0, ' ', 0, 0, 0, 0, 0,
        !            30:        0, 0, 0, 0, 0, 0, 0, 0,
        !            31:        0, 0, 0, 0, 0, 0, 0, 0,
        !            32:        ' ', '.', ' ', ' ', '$', '%', '&', '\'',
        !            33:        '(', ')', '*', '+', ' ', '-', '.', '/',
        !            34:        '0', '1', '2', '3', '4', '5', '6', '7',
        !            35:        '8', '9', ':', ' ', ' ', '=', ' ', '?',
        !            36:        ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
        !            37:        'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        !            38:        'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        !            39:        'x', 'y', 'z', ' ', '\\', ' ', '^', '_',
        !            40:        ' ', 'a', 'b', 'c', 'd', 'e', 'f', 'g',
        !            41:        'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o',
        !            42:        'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
        !            43:        'x', 'y', 'z', '{', ' ', '}', ' ', ' '
        !            44:        };
        !            45: int    caps = 0;
        !            46: int    lineno = 1;
        !            47: int fflag= 0;
        !            48: int nflag      = 0; /*use default file*/
        !            49: char *filename;
        !            50: int    mflg    = 0;    /*don't catch output*/
        !            51: int    nfile;
        !            52: int    nsucc;
        !            53: long nsent = 0;
        !            54: long nhits = 0;
        !            55: char *nlp;
        !            56: char *begp, *endp;
        !            57: int beg, last;
        !            58: char *myst;
        !            59: int myct = 0;
        !            60: int oct = 0;
        !            61: FILE   *wordf;
        !            62: FILE *mine;
        !            63: FILE *fl;
        !            64: char *listn;
        !            65: int list = 0;
        !            66: char   *argptr;
        !            67: long tl = 0;
        !            68: long th = 0;
        !            69: 
        !            70: main(argc, argv)
        !            71: char *argv[];
        !            72: {
        !            73:        int sv;
        !            74:        char cc;
        !            75:        while (--argc > 0 && (++argv)[0][0]=='-')
        !            76:                switch (argv[0][1]) {
        !            77: 
        !            78:                case 'f':
        !            79:                        fflag++;
        !            80:                        filename = (++argv)[0];
        !            81:                        argc--;
        !            82:                        continue;
        !            83: 
        !            84:                case 'n':
        !            85:                        nflag = 0;
        !            86:                        continue;
        !            87:                case 'd':
        !            88:                        mflg=0;
        !            89:                        continue;
        !            90:                case 'c':
        !            91:                        caps++;
        !            92:                        continue;
        !            93:                case 'l':
        !            94:                        lineno++;
        !            95:                        continue;
        !            96:                case 'A':               /* for acro */
        !            97:                        for(cc='A';cc<='Z';cc++)
        !            98:                                table[cc] = cc;
        !            99:                        continue;
        !           100:                case 'o':               /*to put hits to file*/
        !           101:                        listn = (++argv)[0];
        !           102:                        argc--;
        !           103:                        list++;
        !           104:                        if((fl=fopen(listn,"w"))== NULL){
        !           105:                                fprintf(stderr,"diction: can't open file %s\n",
        !           106:                                        listn);
        !           107:                                exit(2);
        !           108:                        }
        !           109:                        continue;
        !           110:                default:
        !           111:                        fprintf(stderr, "diction: unknown flag\n");
        !           112:                        continue;
        !           113:                }
        !           114: out:
        !           115:        if(nflag){
        !           116:                wordf = fopen(DICT,"r");
        !           117:                if(wordf == NULL){
        !           118:                        fprintf(stderr,"diction: can't open default dictionary\n");
        !           119:                        exit(2);
        !           120:                }
        !           121:        }
        !           122:        else {
        !           123:                wordf = fopen(filename,"r");
        !           124:                if(wordf == NULL){
        !           125:                        fprintf(stderr,"diction: can't open %s\n",filename);
        !           126:                        exit(2);
        !           127:                }
        !           128:        }
        !           129: 
        !           130: #ifdef CATCH
        !           131:        if(fopen(CATCH,"r") != NULL){
        !           132:                if((mine=fopen(CATCH,"a"))==NULL)mflg=0;
        !           133:                else mflg = 1;
        !           134:        }
        !           135: #else
        !           136:        mflg = 0;
        !           137: #endif
        !           138: #ifdef MACS
        !           139:        if(caps){
        !           140:                printf(".so ");
        !           141:                printf(MACS);
        !           142:                printf("\n");
        !           143:        }
        !           144: #endif
        !           145:        cgotofn();
        !           146:        cfail();
        !           147:        nfile = argc;
        !           148:        if (argc<=0) {
        !           149:                execute((char *)NULL);
        !           150:        }
        !           151:        else while (--argc >= 0) {
        !           152:                execute(*argv);
        !           153:                if(lineno){
        !           154:                        printf("file %s: number of lines %ld number of phrases found %ld\n",
        !           155:                                *argv, lcount-1, nhits);
        !           156:                        tl += lcount-1;
        !           157:                        th += nhits;
        !           158:                        sv = lcount-1;
        !           159:                        lcount = nhits = 0;
        !           160:                }
        !           161:                argv++;
        !           162:        }
        !           163: fprintf(stderr,"lcount %ld\n",tl);
        !           164:        if(mflg)fprintf(mine,"number of sentences %ld %ld number of hits %ld %ld\n",nsent,tl,nhits,th);
        !           165:        if(!caps&& !lineno)printf("number of sentences %ld number of phrases found %ld\n",nsent,nhits);
        !           166:        else if(tl != sv)
        !           167:                 if(!caps)printf("totals: number of lines %ld number of phrases found %ld\n",tl,th);
        !           168:        exit(nsucc == 0);
        !           169: }
        !           170: 
        !           171: execute(file)
        !           172: char *file;
        !           173: {
        !           174:        register char *p;
        !           175:        register struct words *c;
        !           176:        register ccount;
        !           177:        int count1;
        !           178:        char *beg1;
        !           179:        struct words *savc;
        !           180:        char *savp, *seen;
        !           181:        int savct;
        !           182:        int scr;
        !           183:        char buf[1024];
        !           184:        int f;
        !           185:        int hit;
        !           186:        last = 0;
        !           187:        if (file) {
        !           188:                if ((f = open(file, 0)) < 0) {
        !           189:                        fprintf(stderr, "diction: can't open %s\n", file);
        !           190:                        exit(2);
        !           191:                }
        !           192:        }
        !           193:        else f = 0;
        !           194:        lcount = olcount = 1;
        !           195:        linemsg = 1;
        !           196:        ccount = 0;
        !           197:        count1 = -1;
        !           198:        p = buf;
        !           199:        nlp = p;
        !           200:        c = w;
        !           201:        oct = hit = 0;
        !           202:        savc = (struct words *) 0;
        !           203:        savp = (char *) 0;
        !           204:        for (;;) {
        !           205:                if(--ccount <= 0) {
        !           206:                        if (p == &buf[1024]) p = buf;
        !           207:                        if (p > &buf[512]) {
        !           208:                                if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
        !           209:                        }
        !           210:                        else if ((ccount = read(f, p, 512)) <= 0) break;
        !           211:                        if(caps && (count1 > 0))
        !           212:                                fwrite(beg1,sizeof(*beg1),count1,stdout);
        !           213:                        count1 = ccount;
        !           214:                        beg1 = p;
        !           215:                }
        !           216:                if(p == &buf[1024])p=buf;
        !           217:                nstate:
        !           218:                        if (c->inp == table[*p]) {
        !           219:                                c = c->nst;
        !           220:                        }
        !           221:                        else if (c->link != 0) {
        !           222:                                c = c->link;
        !           223:                                goto nstate;
        !           224:                        }
        !           225:                        else {
        !           226:                                if(savp != 0){
        !           227:                                        c=savc;
        !           228:                                        p=savp;
        !           229:                                        if(ccount > savct)ccount += savct;
        !           230:                                        else ccount = savct;
        !           231:                                        savc = (struct words *) 0;
        !           232:                                        savp = (char *) 0;
        !           233:                                        goto hadone;
        !           234:                                }
        !           235:                                c = c->fail;
        !           236:                                if (c==0) {
        !           237:                                        c = w;
        !           238:                                        istate:
        !           239:                                        if (c->inp == table[*p]) {
        !           240:                                                c = c->nst;
        !           241:                                        }
        !           242:                                        else if (c->link != 0) {
        !           243:                                                c = c->link;
        !           244:                                                goto istate;
        !           245:                                        }
        !           246:                                }
        !           247:                                else goto nstate;
        !           248:                        }
        !           249:                if(c->out){
        !           250:                        if((c->inp == table[*(p+1)]) && (c->nst != 0)){
        !           251:                                savp=p;
        !           252:                                savc=c;
        !           253:                                savct=ccount;
        !           254:                                goto cont;
        !           255:                        }
        !           256:                        else if(c->link != 0){
        !           257:                                savc=c;
        !           258:                                while((savc=savc->link)!= 0){
        !           259:                                        if(savc->inp == table[*(p+1)]){
        !           260:                                                savp=p;
        !           261:                                                savc=c;
        !           262:                                                savct=ccount;
        !           263:                                                goto cont;
        !           264:                                        }
        !           265:                                }
        !           266:                        }
        !           267:                hadone:
        !           268:                        savc = (struct words *) 0;
        !           269:                        savp = (char *) 0;
        !           270:                        if(c->out == (char)(0377)){
        !           271:                                c=w;
        !           272:                                goto nstate;
        !           273:                        }
        !           274:                        begp = p - (c->out);
        !           275:                        if(begp < &buf[0])begp = &buf[1024] - (&buf[0]-begp);
        !           276:                        endp=p;
        !           277:                        if(mflg){
        !           278:                                if(begp-20 < &buf[0]){
        !           279:                                        myst = &buf[1024]-20;
        !           280:                                        if(nlp < &buf[512])myst=nlp;
        !           281:                                }
        !           282:                                else myst = begp-20;
        !           283:                                if(myst < nlp)myst = nlp;
        !           284:                                beg = 0;
        !           285:                        }
        !           286:                        hit = 1;
        !           287:                        nhits++;
        !           288:                        if(*p == '\n'){lcount++;
        !           289:                        seen = p;
        !           290:                        }
        !           291:                        if (table[*p++] == '.') {
        !           292:                                linemsg = 1;
        !           293:                                if (--ccount <= 0) {
        !           294:                                        if (p == &buf[1024]) p = buf;
        !           295:                                        if (p > &buf[512]) {
        !           296:                                                if ((ccount = read(f, p, &buf[1024] - p)) <= 0) break;
        !           297:                                        }
        !           298:                                        else if ((ccount = read(f, p, 512)) <= 0) break;
        !           299:                                        if(caps && (count1 > 0))
        !           300:                                                fwrite(beg1,sizeof(*beg1),count1,stdout);
        !           301:                                        count1=ccount;
        !           302:                                        beg1=p;
        !           303:                                }
        !           304:                        }
        !           305:        succeed:        nsucc = 1;
        !           306:                        {
        !           307:                                if (p <= nlp) {
        !           308:                                        outc(&buf[1024],file);
        !           309:                                        nlp = buf;
        !           310:                                }
        !           311:                                outc(p,file);
        !           312:                        }
        !           313:                        if(mflg)last=1;
        !           314:        nomatch:
        !           315:                        nlp = p;
        !           316:                        c = w;
        !           317:                        begp = endp = 0;
        !           318:                        continue;
        !           319:                }
        !           320:        cont:
        !           321:                if(*p == '\n' && p != seen){lcount++;
        !           322:                        seen = p;
        !           323:                }
        !           324:                if (table[*p++] == '.'){
        !           325:                                if(hit){
        !           326:                                        if(p <= nlp){
        !           327:                                                outc(&buf[1024],file);
        !           328:                                                nlp = buf;
        !           329:                                        }
        !           330:                                        outc(p,file);
        !           331:                                        if(!caps)printf("\n\n");
        !           332:                                        if(mflg && last){putc('\n',mine);myct = 0;}
        !           333:                                        }
        !           334:                                linemsg = 1;
        !           335:                                if(*p == '\n')olcount = lcount+1;
        !           336:                                else
        !           337:                                        olcount=lcount;
        !           338:                                last = 0;
        !           339:                                hit = 0;
        !           340:                                oct = 0;
        !           341:                                nlp = p;
        !           342:                                c = w;
        !           343:                                begp = endp = 0;
        !           344:                                nsent++;
        !           345:                        }
        !           346:        }
        !           347:        if(caps && (count1 > 0))
        !           348:                fwrite(beg1,sizeof(*beg1),count1,stdout);
        !           349:        close(f);
        !           350: }
        !           351: 
        !           352: getargc()
        !           353: {
        !           354:        register c;
        !           355:        if (wordf){
        !           356:                if((c=getc(wordf))==EOF){
        !           357:                        fclose(wordf);
        !           358:                        if(nflag && fflag){
        !           359:                                nflag=0;
        !           360:                                wordf=fopen(filename,"r");
        !           361:                                if(wordf == NULL){
        !           362:                                        fprintf(stderr,"diction can't open %s\n",filename);
        !           363:                                        exit(2);
        !           364:                                }
        !           365:                                return(getc(wordf));
        !           366:                        }
        !           367:                        else return(EOF);
        !           368:                }
        !           369:                else return(c);
        !           370:        }
        !           371:        if ((c = *argptr++) == '\0')
        !           372:                return(EOF);
        !           373:        return(c);
        !           374: }
        !           375: 
        !           376: cgotofn() {
        !           377:        register c;
        !           378:        register struct words *s;
        !           379:        register ct;
        !           380:        int neg;
        !           381: 
        !           382:        s = smax = w;
        !           383:        neg = ct = 0;
        !           384: nword: for(;;) {
        !           385:                c = getargc();
        !           386:                if(c == '~'){
        !           387:                        neg++;
        !           388:                        c = getargc();
        !           389:                }
        !           390:                if (c==EOF)
        !           391:                        return;
        !           392:                if (c == '\n') {
        !           393:                        if(neg)s->out = 0377;
        !           394:                        else s->out = ct-1;
        !           395:                        neg = ct = 0;
        !           396:                        s = w;
        !           397:                } else {
        !           398:                loop:   if (s->inp == c) {
        !           399:                                s = s->nst;
        !           400:                                ct++;
        !           401:                                continue;
        !           402:                        }
        !           403:                        if (s->inp == 0) goto enter;
        !           404:                        if (s->link == 0) {
        !           405:                                if (smax >= &w[MAXSIZ - 1]) overflo("w1");
        !           406:                                s->link = ++smax;
        !           407:                                s = smax;
        !           408:                                goto enter;
        !           409:                        }
        !           410:                        s = s->link;
        !           411:                        goto loop;
        !           412:                }
        !           413:        }
        !           414: 
        !           415:        enter:
        !           416:        do {
        !           417:                s->inp = c;
        !           418:                ct++;
        !           419:                if (smax >= &w[MAXSIZ - 1]) overflo("w2");
        !           420:                s->nst = ++smax;
        !           421:                s = smax;
        !           422:        } while ((c = getargc()) != '\n' && c!=EOF);
        !           423:        if(neg)smax->out = 0377;
        !           424:        else smax->out = ct-1;
        !           425:        neg = ct = 0;
        !           426:        s = w;
        !           427:        if (c != EOF)
        !           428:                goto nword;
        !           429: }
        !           430: 
        !           431: overflo(s)
        !           432: char *s;
        !           433: {
        !           434:        fprintf(stderr, "wordlist too large %s\n",s);
        !           435:        exit(2);
        !           436: }
        !           437: cfail() {
        !           438:        struct words *queue[QSIZE];
        !           439:        struct words **front, **rear;
        !           440:        struct words *state;
        !           441:        int bstart;
        !           442:        register char c;
        !           443:        register struct words *s;
        !           444:        s = w;
        !           445:        front = rear = queue;
        !           446: init:  if ((s->inp) != 0) {
        !           447:                *rear++ = s->nst;
        !           448:                if (rear >= &queue[QSIZE - 1]) overflo("queue1");
        !           449:        }
        !           450:        if ((s = s->link) != 0) {
        !           451:                goto init;
        !           452:        }
        !           453: 
        !           454:        while (rear!=front) {
        !           455:                s = *front;
        !           456:                if (front == &queue[QSIZE-1])
        !           457:                        front = queue;
        !           458:                else front++;
        !           459:        cloop:  if ((c = s->inp) != 0) {
        !           460:                        bstart=0;
        !           461:                        *rear = (q = s->nst);
        !           462:                        if (front < rear)
        !           463:                                if (rear >= &queue[QSIZE-1])
        !           464:                                        if (front == queue) overflo("queue2");
        !           465:                                        else rear = queue;
        !           466:                                else rear++;
        !           467:                        else
        !           468:                                if (++rear == front) overflo("queue3");
        !           469:                        state = s->fail;
        !           470:                floop:  if (state == 0){ state = w;bstart=1;}
        !           471:                        if (state->inp == c) {
        !           472:                        qloop:  q->fail = state->nst;
        !           473:                                if ((state->nst)->out != 0 && q->out == 0) q->out = (state->nst)->out;
        !           474:                                if((q=q->link) != 0)goto qloop;
        !           475:                        }
        !           476:                        else if((state->link) != 0){
        !           477:                                state = state->link;
        !           478:                                goto floop;
        !           479:                        }
        !           480:                        else if((state = state->fail) != 0)
        !           481:                                goto floop;
        !           482:                        else if(bstart==0){state=0; goto floop;}
        !           483:                }
        !           484:                if ((s = s->link) != 0)
        !           485:                        goto cloop;
        !           486:        }
        !           487: /*     for(s=w;s<=smax;s++)
        !           488:                printf("s %d ch %c out %d nst %d link %d fail %d\n",s,
        !           489:                        s->inp,s->out,s->nst,s->link,s->fail);
        !           490: */
        !           491: }
        !           492: outc(addr,file)
        !           493: char *addr;
        !           494: char *file;
        !           495: {
        !           496:        static inside = 0;
        !           497: 
        !           498:        if(!caps && lineno && linemsg){
        !           499:                if(list)fprintf(fl,"%ld ",olcount);
        !           500:                printf("beginning line %ld",olcount);
        !           501:                if(file != (char *)NULL)printf(" %s\n",file);
        !           502:                else printf("\n");
        !           503:                linemsg = 0;
        !           504:        }
        !           505:        while(nlp < addr){
        !           506:                if(!caps && oct > 60 && table[*nlp] == ' ' && nlp != begp && nlp != endp){
        !           507:                        oct=0;
        !           508:                        putchar('\n');
        !           509:                }
        !           510:                if(nlp == begp){
        !           511:                        if(caps)inside++;
        !           512:                        else {
        !           513:                                if(list)inside++;
        !           514:                                if( oct >45){putchar('\n');
        !           515:                                        oct=0;
        !           516:                                }
        !           517:                                if( oct==0 || table[*nlp] != ' '){
        !           518:                                        printf("*[");
        !           519:                                        oct+=2;
        !           520:                                }
        !           521:                                else {printf(" *[");;
        !           522:                                        oct+=3;
        !           523:                                }
        !           524:                        }
        !           525:                        if(mflg)putc('[',mine);
        !           526:                }
        !           527:                if(inside && caps){
        !           528:                        if(islower(*nlp))*nlp = toupper(*nlp);
        !           529:                }
        !           530:                else {
        !           531:                        if(inside && list)putc(table[*nlp],fl);
        !           532:                        if(!caps && *nlp == '\n')*nlp = ' ';
        !           533:                        if(*nlp == ' ' && oct==0);
        !           534:                        else if(!caps) {putchar(*nlp); oct++;}
        !           535:                }
        !           536:                if(nlp == endp){
        !           537:                        if(caps)
        !           538:                                inside= 0;
        !           539:                        else {
        !           540:                                if(list && inside){
        !           541:                                        inside = 0;
        !           542:                                        putc('\n',fl);
        !           543:                                }
        !           544:                                if(*(nlp) != ' '){printf("]*");
        !           545:                                        oct+=2;
        !           546:                                }
        !           547:                                else {printf("]* ");
        !           548:                                        oct+=3;
        !           549:                                }
        !           550:                                if(oct >60){putchar('\n');
        !           551:                                        oct=0;
        !           552:                                }
        !           553:                        }
        !           554:                        if(mflg)putc(']',mine);
        !           555:                        beg = 0;
        !           556:                }
        !           557:                if(mflg){
        !           558:                        if(nlp == myst)beg = 1;
        !           559:                        if(beg || last){
        !           560:                                putc(*nlp,mine);
        !           561:                                if(myct++ >= 72 || last == 20){
        !           562:                                        putc('\n',mine);
        !           563:                                        if(last == 20)last=myct=0;
        !           564:                                        else myct=0;
        !           565:                                }
        !           566:                                if(last)last++;
        !           567:                        }
        !           568:                }
        !           569:                nlp++;
        !           570:        }
        !           571: }

unix.superglobalmegacorp.com

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