Annotation of researchv10no/cmd/ap/apnews/badidea1.c, revision 1.1.1.1

1.1       root        1: # include "stdio.h"
                      2: # include "ctype.h"
                      3: # include "assert.h"
                      4: # include "signal.h"
                      5: # include "sys/types.h"
                      6: # include "sys/stat.h"
                      7: # include "cbt.h"
                      8: # include "time.h"
                      9: char names[20][14];
                     10: char heads[20][50];
                     11: # define NST 200
                     12: char storylist[NST][12];
                     13: int stc=0;
                     14: int intr();
                     15: char *getlogin(), *ctime();
                     16: struct tm *localtime();
                     17: int day, yesterday;
                     18: int stp=0;
                     19: int inolim=0;
                     20: int inodes[NST+5];
                     21: char *getenv(), *getpwd();
                     22: char nowfile[20]="";
                     23: char nowhead[50];
                     24: char pwdir[50];
                     25: long filtim();
                     26: char *storydir = "/usr/spool/ap";
                     27: 
                     28: /* logging */
                     29: char *apnlog = "/usr/spool/ap/apnlog";
                     30: long tmstart, tmend;
                     31: int nslug, nstory, nsearch;
                     32: 
                     33: main(argc,argv)
                     34:        char *argv[];
                     35: {
                     36: char line[100];
                     37: int i, lc=1;
                     38: long l;
                     39: char *ctp, *userp;
                     40: char forcebuf[BUFSIZ];
                     41: setbuf(stdout, forcebuf);
                     42: for (argc--,argv++; argc>0 && argv[0][0]=='-'; argc--,argv++)
                     43:        switch(argv[0][1])
                     44:                {
                     45:                case 'f':
                     46:                        storydir = argv[1];
                     47:                        argv++; argc--;
                     48:                        break;
                     49:                }
                     50: tmstart = l = time(0);
                     51: day = localtime(&l)->tm_mday;
                     52: l -= 86400; /* go back one day */
                     53: yesterday = localtime(&l)->tm_mday;
                     54: signal (SIGINT, intr);
                     55: strcpy(pwdir, getpwd());
                     56: i = chdir(storydir);
                     57: if (i!=0)
                     58:        {
                     59:        fprintf(stderr, "no directory %s\n",storydir);
                     60:        exit(1);
                     61:        }
                     62: top();
                     63: extend(20);
                     64: display();
                     65: menu(lc);
                     66: fflush(stdout);
                     67: extend(30);
                     68: while (gets(line))
                     69:        {
                     70:        tpos(0,0);
                     71:        if (line[0]!=0)
                     72:                rate();
                     73:        switch(line[0])
                     74:                {
                     75:                case '0': case '1': case '2': case '3': case '4': 
                     76:                case '5': case '6': case '7': case '8': case '9': 
                     77:                        i = atoi(line)-1;
                     78:                        if (i<0 || i>19) continue;
                     79:                        pr(names[i], heads[i]);
                     80:                        nstory++;
                     81:                        lc=0;
                     82:                        break;
                     83:                case 0: /* continue this story */
                     84:                        if (lc==0)
                     85:                                pr(0,0);
                     86:                        else if (lc==1)
                     87:                                display();
                     88:                        else if (lc==2)
                     89:                                sums();
                     90:                        if (lc>0) menu(lc);
                     91:                        break;
                     92:                case '?':
                     93:                        menu(0);
                     94:                        break;
                     95:                case '!':
                     96:                        system(line+1);
                     97:                        break;
                     98:                case '.':
                     99:                        disprint();
                    100:                        break;
                    101:                case 'a':
                    102:                        pr(line, "");
                    103:                        nstory++;
                    104:                        lc=0;
                    105:                        break;
                    106:                case '+':
                    107:                case 'm':
                    108:                        shorten(line+1);
                    109:                        display();
                    110:                        lc=1;
                    111:                        break;
                    112:                case 'M':
                    113:                        sums();
                    114:                        menu(lc=2);
                    115:                        break;
                    116:                case 't':
                    117:                        top();
                    118:                        extend(20);
                    119:                        shorten(line+1);
                    120:                        display();
                    121:                        menu(lc=1);
                    122:                        break;
                    123:                case 'T':
                    124:                        top();
                    125:                        extend(20);
                    126:                        sums();
                    127:                        menu(lc=2);
                    128:                        break;
                    129:                case 'c':
                    130:                case 'C': /* copy file to my directory */
                    131:                        i = atoi(line+1);
                    132:                        if (i>0)
                    133:                                strcpy(nowfile, names[i-1]);
                    134:                        copyto(line+1, nowfile);
                    135:                        break;
                    136:                case 'D': /* delete file*/
                    137:                        i = atoi(line+1);
                    138:                        if (i>0)
                    139:                                strcpy(nowfile, names[i-1]);
                    140:                        printf("Deleting /%s/\n",nowfile);
                    141:                        unlink(nowfile);
                    142:                        break;
                    143:                case 'Y':
                    144:                case 'y': /* must get keyword list */
                    145:                        getkeys(line[0]=='Y');
                    146:                        break;
                    147:                case 'S':
                    148:                case 's': /* search for words */
                    149:                        wdsearch (line+1, line[0]=='S');
                    150:                        nsearch++;
                    151:                        break;
                    152:                case 'q':
                    153:                        tpos(23,0);
                    154:                        wrap();
                    155:                }
                    156:        fflush(stdout);
                    157:        }
                    158: tpos(23,0);
                    159: wrap();
                    160: }
                    161: intr()
                    162: {
                    163: tpos(23,0);
                    164: wrap();
                    165: exit(0);
                    166: }
                    167: int llens[10] ={70,70,70,70,70,70,70,70,70,70};
                    168: pr (s, h)
                    169:        char *s, *h;
                    170: {
                    171: static FILE *f=NULL;
                    172: char line[120];
                    173: int i, k, k1;
                    174: if (s && s[0]==0)
                    175:        return;
                    176: if (s!=0)
                    177:        {
                    178:        if (f!= NULL) fclose(f);
                    179:        f = fopen(s, "r");
                    180:        if (f==NULL)
                    181:                {
                    182:                printf("No file %s\n", s);
                    183:                return;
                    184:                }
                    185:        strcpy(nowfile, s);
                    186:        strcpy(nowhead, h);
                    187:        }
                    188: else
                    189:        if (f==NULL) return;
                    190: tpos(12,0);
                    191: if (ifclear())
                    192:        clr2end();
                    193: for(i=0; i<10; i++)
                    194:        {
                    195:        if(fgets(line, 100, f)==0)
                    196:                break;
                    197:        trimnl(line);
                    198:        k = strlen(line);
                    199:        if (line[0]=='\t') k+= 7;
                    200:        if (!ifclear() &&ifclrline())
                    201:                clrline();
                    202:        fputs(line, stdout);
                    203:        if (!ifclear() && !ifclrline())
                    204:                for(k1=llens[i]; k1>k; k1--)
                    205:                        putchar(' ');
                    206:        printf("\n");
                    207:        llens[i]=k+1;
                    208:        }
                    209: }
                    210: trimnl(s)
                    211:        char *s;
                    212: {
                    213: while (*s) s++;
                    214: if (*--s == '\n') *s=0;
                    215: }
                    216: static hfgate=0;
                    217: static char hfbuff[5000];
                    218: static char *hflab[400];
                    219: static int hftop=0;
                    220: hfast (out, snam)
                    221:        char *out, *snam;
                    222: {
                    223: char *s;
                    224: int i;
                    225: if (hfgate==0)
                    226:        {
                    227:        int f, n;
                    228:        char fn[40], *index();
                    229:        sprintf(fn, "%s/.S%d", storydir, day);
                    230:        f = open(fn, 0);
                    231:        if (f<0) return(0);
                    232:        n= read (f, hfbuff, 5000);
                    233:        hfbuff[n]=0;
                    234:        close(f);
                    235:        hfgate=1;
                    236:        s = hfbuff;
                    237:        for(hftop=0; hftop<400; hftop++)
                    238:                {
                    239:                if (*s==0) break;
                    240:                hflab[hftop] = s;
                    241:                s = index(s, '\n');
                    242:                if (s==NULL) break;
                    243:                *s++ = 0;
                    244:                }
                    245:        }
                    246: for(i=0; i<hftop; i++)
                    247:        {
                    248:        if (strncmp(snam, hflab[i], 5)==0 && hflab[i][6] !=0)
                    249:                {
                    250:                strcpy(out, hflab[i]+6);
                    251:                return(1);
                    252:                }
                    253:        }
                    254: return(0);
                    255: }
                    256: hget(s, f)
                    257:        char s[];
                    258:        FILE *f;
                    259: {
                    260: int i, c, nw;
                    261: char line[100], *wv[40], *p;
                    262: if (f==NULL)
                    263:        {
                    264:        *s=0;
                    265:        return;
                    266:        }
                    267: fgets(line, 100, f);
                    268: trimnl(line);
                    269: if (line[1]==' ' && line[3]==' ' && line[6]=='-') /* their heading */
                    270:        {
                    271:        for(p=line+7; (c = *p) && c!=' '; p++)
                    272:                *s++ = c;
                    273:        *s=0;
                    274:        return;
                    275:        }
                    276: nw = getargs(line, wv);
                    277: strcpy(s, wv[i=pickbig(wv, nw)]);
                    278: wv[i]="";
                    279: strcat(s, wv[pickbig(wv,nw)]);
                    280: return;
                    281: }
                    282: notnew(i, inodes, fname)
                    283:        char *fname;
                    284:        int *inodes;
                    285: {
                    286: int ino, j;
                    287: struct stat stbuff;
                    288: stat (fname, &stbuff);
                    289: ino = stbuff.st_ino;
                    290: for(j=0; j<i; j++)
                    291:        if (ino==inodes[j])
                    292:                return(1);
                    293: inodes[i]=ino;
                    294: return(0);
                    295: }
                    296: display()
                    297: {
                    298: getnam(20);
                    299: disprint();
                    300: nslug += 20;
                    301: }
                    302: disprint()
                    303: {
                    304: int i, kl, a;
                    305: char bform[30];
                    306: FILE *fl;
                    307: startsc();
                    308: for(i=0; i<20; i++)
                    309:        {
                    310:        if (names[i][0]== '\0')
                    311:                break;
                    312:        if (hfast(heads[i], names[i]))
                    313:                continue;
                    314:        fl = fopen(names[i], "r");
                    315:        if (fl!=NULL)
                    316:                {
                    317:                hget(heads[i],fl);
                    318:                fclose(fl);
                    319:                }
                    320:        else
                    321:                heads[i][0]=names[i][0]=0;
                    322:        }
                    323: for(kl=i=0; i<10; i++)
                    324:        {
                    325:        a = strlen(heads[i]);
                    326:        if (a>kl) kl=a;
                    327:        }
                    328: kl += 6;
                    329: if (kl>30) kl=30;
                    330: sprintf(bform, "%%3d: (%%5s) %%-%d.%ds ", kl, kl);
                    331: for(i=0; i<10; i++)
                    332:        {
                    333:        char xheads[50], yheads[50];
                    334:        clrline();
                    335: 
                    336:        sprintf(xheads, "%s [%c]", heads[i], scr(names[i]));
                    337:        sprintf(yheads, "%s [%c]", heads[i+10], scr(names[i+10]));
                    338:        if (strlen(names[i])<=5)
                    339:                printf(bform, i+1, names[i], xheads);
                    340:        else
                    341:                printf("%3d: (%11s) %-19.19s ",i+1,names[i],xheads);
                    342:        printf("%3d: (%5s) %.30s\n",i+11,names[i+10],yheads);
                    343:        }
                    344: nowfile[0]=0;
                    345: }
                    346: top()
                    347: {
                    348:        char tstories[NST][6];
                    349:        int f, n, i, j;
                    350:        stc = inolim = stp = 0;
                    351:        f = open("..tlist", 0);
                    352:        if (f<0) return;
                    353:        n = read(f, tstories, NST*6);
                    354:        stc = n/6;
                    355:        /* reverse tstories */
                    356:        for(i=0, j=stc-1; i<j; i++,j--)
                    357:                {
                    358:                char x[6];
                    359:                strncpy(x,tstories[i],6);
                    360:                strncpy(tstories[i],tstories[j],6);
                    361:                strncpy(tstories[j],x,6);
                    362:                }
                    363:        close(f);
                    364:        for(i=0; i<stc; i++)
                    365:                {
                    366:                strncpy(storylist[i],tstories[i],6);
                    367:                storylist[i][6]= 0;
                    368:                }
                    369: }
                    370: getnam(n)
                    371: {
                    372: int i;
                    373: for (i=0; i<n; i++)
                    374:        heads[i][0]= names[i][0]= '\0';
                    375: for (i=0; i<n; i++)
                    376:        {
                    377:                try:
                    378:                if (stp>=stc) break;
                    379:                strcpy(names[i], storylist[stp++]);
                    380: 
                    381:                if (!interest(names[i])) goto try;
                    382:                if (names[i][0]=='a' && isdigit(names[i][1]))
                    383:                        continue;
                    384:                if (names[i][5]=='.' && names[i][6]=='a' &&
                    385:                isdigit(names[i][7]))
                    386:                        continue;
                    387:                goto try;
                    388:        }
                    389: }
                    390: sums()
                    391: {
                    392: int i;
                    393: startsc();
                    394: getnam(10);
                    395: for (i=0; i<10; i++)
                    396:        {
                    397:        printf("%d ",i+1);
                    398:        prsum(names[i]);
                    399:        }
                    400: }
                    401: wrap()
                    402: {
                    403: FILE *f;
                    404: tmend = time(0);
                    405: f = fopen(apnlog, "a");
                    406: if (f!=NULL)
                    407:        {
                    408:        fprintf(f,
                    409:        "%s slugs %d stories %d searches %d dir %s %.16s %d secs\n",
                    410:                getlogin(), nslug, nstory, nsearch, storydir,
                    411:                ctime(&tmstart), tmend-tmstart);
                    412:        fclose(f);
                    413:        }
                    414: exit(0);
                    415: }
                    416: rate()
                    417: {
                    418: char line[100]; int r;
                    419: if (nowfile[0]==0) return;
                    420: tpos(12,0);
                    421: if (ifclear())
                    422:        clr2end();
                    423: else
                    424: if (ifclrline())
                    425:        {clrline(); tpos(13,0); clrline();}
                    426: tpos(12,0);
                    427: printf("Please rate story %s: 1 bad, 5 good: ", nowhead);
                    428: fflush(stdout);
                    429: gets(line);
                    430: r = atoi(line);
                    431: if (r<0) r=0;
                    432: if (r>5) r=5;
                    433: upd(nowfile, r);
                    434: }
                    435: 
                    436: short nread[1000];
                    437: short neval[1000];
                    438: short tscor[1000];
                    439: short sqscr[1000];
                    440: static char * scname = "Scores";
                    441: short nvl[1000];
                    442: int ngate=0;
                    443: upd(nm, r)
                    444: {
                    445: int f, n;
                    446: n = atoi(nm+1);
                    447: if (n<=0 || n>999) return;
                    448: f = open(scname, 0);
                    449: if (f>0)
                    450:        {
                    451:        ngate=1;
                    452:        read(f, nread, 2000);
                    453:        read(f, neval, 2000);
                    454:        read(f, tscor, 2000);
                    455:        read(f, sqscr, 2000);
                    456:        close(f);
                    457:        }
                    458: if (r>10) return;
                    459: nread[n]++;
                    460: if (r>0)
                    461:        {
                    462:        neval[n]++;
                    463:        tscor[n]+=r;
                    464:        sqscr[n]+= r*r;
                    465:        }
                    466: f = creat(scname, 0666);
                    467: if (f>0)
                    468:        {
                    469:        write(f, nread, 2000);
                    470:        write (f, neval, 2000);
                    471:        write (f, tscor, 2000);
                    472:        write (f, sqscr, 2000);
                    473:        close(f);
                    474:        }
                    475: }
                    476: scr(s)
                    477:        char *s;
                    478: {
                    479: int n;
                    480: double x;
                    481: if (ngate==0) upd (" 100", 100);
                    482: n = atoi(s+1);
                    483: if (neval[n]==0)
                    484:        return(' ');
                    485: x = (double) tscor[n];
                    486: x /= neval[n];
                    487: x += 0.5;
                    488: n = (int) x;
                    489: return(n+'0');
                    490: }
                    491: ctr(s)
                    492:        char *s;
                    493: {
                    494: int n;
                    495: n = atoi(s+1);
                    496: return(nread[n]);
                    497: }
                    498: int intlevel=0;
                    499: shorten(s)
                    500:        char *s;
                    501: {
                    502: if (*s!='>') intlevel=0;
                    503: else
                    504: intlevel = atoi(s+1);
                    505: if (intlevel>5) intlevel=5;
                    506: }
                    507: interest(s)
                    508:        char *s;
                    509: {
                    510: int n;
                    511: if (intlevel<=0) return(1);
                    512: n = atoi(s+1);
                    513: upd("", 100);
                    514: if (neval[n]==0) return(0);
                    515: if ( (double)tscor[n]/neval[n] > intlevel) return(1);
                    516: return(0);
                    517: }

unix.superglobalmegacorp.com

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