Annotation of researchv10no/cmd/ap/apkeys/dread.c, revision 1.1.1.1

1.1       root        1: # include "stdio.h"
                      2: # include "assert.h"
                      3: # include "key..c"
                      4: # include "sys/types.h"
                      5: # include "sys/stat.h"
                      6: int nseq =0;
                      7: int nsent=0;
                      8: int nstory=0;
                      9: int ndest= -1;
                     10: struct wordstr *wds;
                     11: int nwds = 0;
                     12: struct matchstr *seqs;
                     13: int *ok, *len, *wseen;
                     14: struct matchstr *sents;
                     15: struct matchstr *stories;
                     16: struct deststr *dests;
                     17: FILE * zfopen();
                     18: extern FILE *yyin;
                     19: extern char today[];
                     20: stread()
                     21: {
                     22: char line[400], *calloc(), buff[400];
                     23: int i, a, b, c, d, e;
                     24: gets(line);
                     25: nwds= atoi(line);
                     26: wds = (struct wordstr *)calloc(nwds+1, sizeof (*wds));
                     27: assert(wds!=0);
                     28: for(i=0; i<nwds; i++)
                     29:        {
                     30:        gets(line);
                     31:        sscanf(line, "%s %d %d %d %d", buff, &a, &b, &c, &d);
                     32:        wds[i].wch = svc(buff);
                     33:        wds[i].seq = a;
                     34:        wds[i].ifneg = b;
                     35:        wds[i].which = c;
                     36:        wds[i].mask = d;
                     37:        }
                     38: gets(line);
                     39: nseq=atoi(line);
                     40: seqs = (struct matchstr *)calloc(nseq+1, sizeof(*seqs));
                     41: ok = (int *)calloc(nseq+1, sizeof(*ok));
                     42: len = (int *)calloc(nseq+1, sizeof(*len));
                     43: wseen = (int *)calloc(nseq+1, sizeof(*wseen));
                     44: assert(seqs!=0);
                     45: for(i=0; i<nseq; i++)
                     46:        {
                     47:        gets(line);
                     48:        sscanf(line, "%d %d %d %d %d", &a, &b, &c, &d, &e);
                     49:        seqs[i].next = a;
                     50:        seqs[i].ntype = b;
                     51:        seqs[i].lim = c;
                     52:        seqs[i].smask = d;
                     53:        len[i]=e;
                     54:        }
                     55: gets(line);
                     56: nsent=atoi(line);
                     57: sents = (struct matchstr *)calloc(nsent+1, sizeof(*sents));
                     58: assert(sents!=0);
                     59: for(i=0; i<nsent; i++)
                     60:        {
                     61:        gets(line);
                     62:        sscanf(line, "%d %d %d %d", &a, &b, &c, &d);
                     63:        sents[i].next = a;
                     64:        sents[i].ntype = b;
                     65:        sents[i].lim =c;
                     66:        sents[i].smask=d;
                     67:        }
                     68: gets(line);
                     69: nstory=atoi(line);
                     70: stories = (struct matchstr *)calloc(nstory+1, sizeof(*stories));
                     71: assert(stories!=0);
                     72: for(i=0; i<nstory; i++)
                     73:        {
                     74:        gets(line);
                     75:        sscanf(line, "%d %d %d", &a, &b, &c);
                     76:        stories[i].next = a;
                     77:        stories[i].ntype = b;
                     78:        stories[i].lim = c;
                     79:        }
                     80: gets(line);
                     81: ndest=atoi(line);
                     82: dests = (struct deststr *)calloc(ndest+1, sizeof(*dests));
                     83: assert(dests!=0);
                     84: for(i=0; i<ndest; i++)
                     85:        {
                     86:        gets(line);
                     87:        dests[i].sndflg = atoi(line);
                     88:        assert(strlen(line+2)<400);
                     89:        dests[i].dstr = svc(line+2);
                     90:        }
                     91: fprintf(stderr, "nwds %d nseq %d nsent %d sents %o\n",nwds,nseq,nsent, sents);
                     92: fprintf(stderr, "nstory %d ndests %d dests %o\n",nstory, ndest, dests);
                     93: fprintf(stderr, "about to exit stread\n");
                     94: }
                     95: char *yylval;
                     96: # define WORD 1
                     97: # define DOT 2
                     98: extern char *fname;
                     99: process()
                    100: {
                    101: int k, i, j;
                    102: yylval= (char *) calloc(10,1); /* get it set up */
                    103: for(i=0; i<nseq; i++)
                    104:        seqs[i].ct=ok[i]=wseen[i]=0;
                    105: for(i=0; i<nsent; i++)
                    106:        sents[i].ct=0;
                    107: for(i=0; i<nstory; i++)
                    108:        stories[i].ct=0;
                    109: yyunput('#');
                    110: while (k = yylex())
                    111:        {
                    112:        switch(k)
                    113:                {
                    114:                case WORD:
                    115:                        i = search(yylval);
                    116:                        while (i<nwds && strcmp(wds[i].wch, yylval)==0)
                    117:                                hit(i++);
                    118:                        break;
                    119:                case DOT:
                    120:                        senthits();
                    121:                        for(i=0; i<nsent; i++)
                    122:                                sents[i].ct=0;
                    123:                        break;
                    124:                }
                    125:        for(i=0; i<nseq; i++)
                    126:                {
                    127:                if (wseen[i] ==len[i] && seqs[i].ct==seqs[i].lim)
                    128:                        seqhit(i);
                    129:                if (wseen[i]>0 && missing(i, wseen[i])) wseen[i]++;
                    130:                else
                    131:                if (ok[i]==0 || (seqs[i].ct==0) || (wseen[i]>len[i]))
                    132:                        seqs[i].ct=wseen[i]=0;
                    133:                ok[i]=0;
                    134:                }
                    135:        }
                    136: storyhits();
                    137: }
                    138: hit(i)
                    139: {
                    140: int j, m, n, ms;
                    141: j = wds[i].seq;
                    142: ms = wds[i].mask;
                    143: switch(wds[i].which)
                    144: {
                    145: case SEQ:
                    146:        ok[j]=1;
                    147:        while (missing(j, wseen[j])) wseen[j]++; /* anything we didn't see */
                    148:        wseen[j]++;
                    149:        seqs[j].ct |= ms;
                    150:        if (wseen[j]==len[j] && seqs[j].ct== seqs[j].lim)
                    151:                {
                    152:                seqhit(j);
                    153:                wseen[j]=0;
                    154:                }
                    155:        return;
                    156: case SENT:
                    157:        sents[j].ct |= ms;
                    158:        return;
                    159: case STORY:
                    160:        stories[j].ct |= ms;
                    161:        return;
                    162: case DEST:
                    163:        if (wds[i].ifneg==0)
                    164:                found(j);
                    165:        return;
                    166: }
                    167: }
                    168: seqhit(j)
                    169: {
                    170: int m, ms;
                    171: m = seqs[j].next;
                    172: ms = seqs[j].smask;
                    173: switch(seqs[j].ntype)
                    174:        {
                    175:        case SENT:
                    176:                sents[m].ct |= ms;
                    177:                break;
                    178:        case STORY:
                    179:                stories[m].ct |= ms;
                    180:                break;
                    181:        case DEST:
                    182:                found(m);
                    183:                break;
                    184:        }
                    185: }
                    186: senthits()
                    187: {
                    188: int m,n;
                    189: for(m=0; m<nsent; m++)
                    190:        if (sents[m].ct==sents[m].lim)
                    191:                {
                    192:                n = sents[m].next;
                    193:                switch(sents[m].ntype)
                    194:                        {
                    195:                        case STORY:
                    196:                                stories[n].ct |= sents[m].smask;
                    197:                                continue;
                    198:                        case DEST:
                    199:                                found(n);
                    200:                                continue;
                    201:                        }
                    202:                }
                    203: }
                    204: storyhits()
                    205: {
                    206: int n;
                    207: for(n=0; n<nstory; n++)
                    208:        if (stories[n].ct==stories[n].lim)
                    209:                found(stories[n].next);
                    210: }
                    211: int tosend[400];
                    212: int nts=0;
                    213: found(nx)
                    214: {
                    215: int i;
                    216: for(i=0; i<nts; i++)
                    217:        if (tosend[i]==nx)
                    218:                return;
                    219: tosend[nts++]=nx;
                    220: assert(nts<400);
                    221: }
                    222: extern int debug;
                    223: deliver()
                    224: {
                    225: char *d, sline[300];
                    226: int nx, para, i, sendf;
                    227: int gate;
                    228: FILE *fi, *fo;
                    229: for(i=0; i<nts; i++)
                    230:        {
                    231:        nx = tosend[i];
                    232:        d = dests[nx].dstr;
                    233:        if (debug) printf("Send %s to %s\n", fname, d);
                    234:        if (d[0]=='*')
                    235:                {
                    236:                printf("deliver %d to :%s:\n",nx,d+1);
                    237:                continue;
                    238:                }
                    239:        if (d[0]=='|')
                    240:                {
                    241:                pipeto(d+1, fname, dests[nx].sndflg);
                    242:                continue;
                    243:                }
                    244:        if (d[0]=='>' && d[1]=='>')
                    245:                fo = zfopen(d+2, "a");
                    246:        else if (d[0]=='>')
                    247:                fo = zfopen(d+1, "w");
                    248:        if (fo==NULL) /* may be a directory */
                    249:                {
                    250:                char temp[400];
                    251:                while (*d=='>' || *d==' ') d++;
                    252:                sprintf(temp, "%s/%s.%s", d, today, fname);
                    253:                fo = zfopen(temp, "a");
                    254:                }
                    255:        if (fo==NULL) continue;
                    256:        sendf = dests[nx].sndflg;
                    257:        fi = fopen(fname, "r");
                    258:        para=0;
                    259:        gate = debug ? 1 : 0; /* pass on first line */
                    260:        while (fgets(sline, 300, fi))
                    261:                {
                    262:                if (strncmp(sline, "\t   ", 4)==0) para++;
                    263:                if (para==2 && sendf==2) break;
                    264:                fputs(sline, fo);
                    265:                if (gate>0)
                    266:                        {
                    267:                        printf("%s", sline);
                    268:                        gate=0;
                    269:                        }
                    270:                if (sendf==1) break;
                    271:                }
                    272:        fclose(fo); fclose(fi);
                    273:        }
                    274: nts=0;
                    275: }
                    276: pipeto( command, file, sendf)
                    277:        char *command, *file;
                    278: {
                    279: int fp[2], fr, fw, para, gate;
                    280: char sline[300];
                    281: FILE *fi, *fo;
                    282: assert(pipe(fp)!= -1);
                    283: fr = fp[0]; fw = fp[1];
                    284: if (fork()==0)
                    285:        {
                    286:        close(fw);
                    287:        close(0);
                    288:        assert(dup(fr)==0);
                    289:        system(command);
                    290:        exit(0);
                    291:        }
                    292: close(fr);
                    293: fi = fopen(file, "r");
                    294: fo = fopen("/dev/null", "w");
                    295: assert(fi!=NULL);
                    296: assert(fo!=NULL);
                    297: close(fo->_file);
                    298: fo->_file = fw;
                    299: para=0;
                    300: gate = debug ? 1 : 0;
                    301: fprintf(fo, "(%s) ", file);
                    302: while (fgets(sline, 300, fi))
                    303:        {
                    304:        if (strncmp(sline, "\t   ", 4)==0) para++;
                    305:        if (para==2 && sendf == 2) break;
                    306:        fputs(sline, fo);
                    307:        if (gate>0)
                    308:                {
                    309:                printf("%s", sline);
                    310:                gate=0;
                    311:                }
                    312:        if (sendf==1) break;
                    313:        }
                    314: fclose(fo);
                    315: fclose(fi);
                    316: wait(0);
                    317: return;
                    318: }
                    319: search(s)
                    320:        char *s;
                    321: {
                    322: /* binarysearch from 1 to nwds thru wds[].wch */
                    323: /* find x such that [x-1]is <s and x big as possible */
                    324: int i, j, low, hi, lv, hv;
                    325: low=0; hi=nwds-1; i= -10;
                    326: while (low+1 < hi)
                    327:        {
                    328:        i = (low+hi)/2;
                    329:        j = strcmp (wds[i].wch, s);
                    330:        if (j>=0)
                    331:                {
                    332:                hi=i;
                    333:                continue;
                    334:                }
                    335:        low=i;
                    336:        }
                    337: if (i==hi)
                    338:        {
                    339:        hv = j;
                    340:        lv = strcmp(wds[low].wch, s);
                    341:        }
                    342: else if (i==low)
                    343:        {
                    344:        lv = j;
                    345:        hv = strcmp(wds[hi].wch, s);
                    346:        }
                    347: else
                    348:        {
                    349:        lv = strcmp(wds[low].wch, s);
                    350:        hv = strcmp(wds[hi].wch, s);
                    351:        }
                    352: if (lv==0) return(low);
                    353: if (hv==0) return(hi);
                    354: return(low);
                    355: }
                    356: missing( i, lni )
                    357: {
                    358: /* checks whether the next item in a sequence is deliberately missing */
                    359: if (lni<0) return(0);
                    360: if ((seqs[i].lim & (1<<lni))==0) return(1);
                    361: return(0);
                    362: }
                    363: FILE *
                    364: zfopen( fnam, w)
                    365:        char *fnam, *w;
                    366: {
                    367: /* this opens file fnam and if successful, tries to donate
                    368:    to the user who owns the including directory */
                    369: char td[100];
                    370: FILE *f;
                    371: struct stat buf;
                    372: char *x;
                    373: while (*fnam==' ') fnam++;
                    374: f = fopen(fnam, w);
                    375: if (f==NULL) return(NULL);
                    376: strcpy(td, fnam);
                    377: for (x=td; *x; x++);
                    378: while (x>td && *x!='/') x--;
                    379: *x=0;
                    380: if (x<=td) return(f);
                    381: stat (td, &buf);
                    382: chown (fnam, buf.st_uid, buf.st_gid);
                    383: return(f);
                    384: }

unix.superglobalmegacorp.com

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