Annotation of researchv10no/cmd/300.c, revision 1.1

1.1     ! root        1: /*
        !             2:  *     300 [+12] [-n] [-dt,l,c]
        !             3:  *     for DTC/DASI 300
        !             4:  *     +12     12-pitch, 6 lines/inch (needs to be faked on this terminal
        !             5:  *     -n      # increments for half-line spacing
        !             6:  *     -dt,l,c delays for tab,  line char
        !             7:  */
        !             8: 
        !             9: 
        !            10: char x300vers[] = "@(#)300.c   1.7";
        !            11: 
        !            12: #include <stdio.h>
        !            13: #include <signal.h>
        !            14: #include <sys/types.h>
        !            15: #include <sys/stat.h>
        !            16: #include <sgtty.h>
        !            17: /* input sequences (TTY37 style) */
        !            18: #define        ESC     033     /* escape */
        !            19: #define        HFWD    '9'
        !            20: #define        HREV    '8'
        !            21: #define        FREV    '7'
        !            22: #define        SO      016     /* shift out - enter greek */
        !            23: #define        SI      017     /* shift in */
        !            24: 
        !            25: /* output specials (300 style) */
        !            26: #define        PLOT    006     /* ack, on/off plot mode */
        !            27: #define BEL    007     /* exit plot mode */
        !            28: #define        U       013
        !            29: #define        D       012
        !            30: #define        R       ' '
        !            31: #define        L       '\b'
        !            32: #define LF     '\n'
        !            33: #define CR     '\r'
        !            34: #define TABVAL 4       /* approx equivalent of blanks per tab */
        !            35: #define FLIPMODE       intrmod ^= 01
        !            36: 
        !            37: int    nlcnt,          /* accumulated newline count */
        !            38:        frevcnt,        /* accumulated reverse line-feeds */
        !            39:        pitch12,        /* 1==> 12-pitch&6lines/inch, 0==>10-pitch */
        !            40:        fullsiz,        /* # increments for full line */
        !            41:        halfsiz = 4;    /* # increments for halfline */
        !            42: int    tabcnt,         /* accumulated tabs in 1 line */
        !            43:        charcnt,        /* # chars in line */
        !            44:        nblcnt;         /* nonblanks contiguous */
        !            45: int    delay[3] = {3,90,20};
        !            46:                        /* tab limit, characters/line, contiguous lim*/
        !            47: char   *ttydev;
        !            48: int    svstmode;       /* for mesg restore */
        !            49: int    restore();
        !            50: int    svsgflgs;
        !            51: struct sgttyb  sgb;
        !            52: char   *scanptr;       /* side-effect of getnum() */
        !            53: 
        !            54: int    intrmod =1,     /* internal mode, 1==> text, 0 ==> plot */
        !            55:        extrmod = 1;    /* external, same */
        !            56: 
        !            57: 
        !            58: main(argc, argv)
        !            59: char **argv;
        !            60: {
        !            61:        register c;
        !            62: 
        !            63:        scanarg(argc, argv);
        !            64:        if (((int)signal(SIGINT, SIG_IGN) & 01) == 0)
        !            65:                signal(SIGINT, restore);
        !            66:        if (gtty(1, &sgb) == 0)
        !            67:                fixtty();
        !            68:        setbuf(stdin, calloc(BUFSIZ,1));
        !            69:        while ((c = getchar()) != EOF) {
        !            70:                if (intrmod) {
        !            71:                        if (c == '\n') {
        !            72:                                if (frevcnt)
        !            73:                                        flushrv();
        !            74:                                nlcnt++;
        !            75:                                continue;
        !            76:                        } else if (nlcnt)
        !            77:                                flushnl();
        !            78:                        else if (frevcnt && c != ESC)
        !            79:                                flushrv();
        !            80:                }
        !            81:                if (c == PLOT) {
        !            82:                        FLIPMODE;
        !            83:                        continue;
        !            84:                }
        !            85:                if (c == BEL) {
        !            86:                        extrmod = intrmod = 1;
        !            87:                        putchar(BEL);
        !            88:                        continue;
        !            89:                }
        !            90:                if (c == SO) {
        !            91:                        special();
        !            92:                        continue;
        !            93:                }
        !            94:                if (c != ESC) {
        !            95:                        charcnt++;
        !            96:                        if (c == ' ')
        !            97:                                nblcnt = 0;
        !            98:                        else if (c == '\t') {
        !            99:                                charcnt += TABVAL;
        !           100:                                nblcnt = 0;
        !           101:                                if (++tabcnt >= delay[0]) {
        !           102:                                        putx('\0');
        !           103:                                        tabcnt = 0;
        !           104:                                        if (delay[0] == 0)      /* 2 nulls per tab */
        !           105:                                                putx('\0');
        !           106:                                }
        !           107:                        } else if (c == '\b')
        !           108:                                charcnt--;
        !           109:                        else if (++nblcnt >= delay[2]) {
        !           110:                                nblcnt = 0;
        !           111:                                putx('\0');
        !           112:                                if (delay[2] == 0)
        !           113:                                        putx('\0');
        !           114:                        }
        !           115:                        if (intrmod != extrmod) {
        !           116:                                putchar(PLOT);  /* flip real state */
        !           117:                                extrmod = intrmod;
        !           118:                        }
        !           119:                        putchar(c);
        !           120:                        continue;
        !           121:                }
        !           122:                FLIPMODE;
        !           123:                c = getchar();
        !           124:                if (frevcnt && c != FREV) {
        !           125:                        FLIPMODE;
        !           126:                        flushrv();
        !           127:                        FLIPMODE;
        !           128:                }
        !           129:                if (c == HREV)
        !           130:                        nplot(halfsiz,U);
        !           131:                else if (c == HFWD)
        !           132:                        nplot(halfsiz,D);
        !           133:                else if (c == FREV)
        !           134:                        frevcnt++;
        !           135:                FLIPMODE;
        !           136:        }
        !           137:        flusher();
        !           138:        restore();
        !           139: }
        !           140: 
        !           141: 
        !           142: /*     scanarg: scan arguments and set flags; ignore unknown args */
        !           143: scanarg(argc, argv)
        !           144: char **argv;
        !           145: {
        !           146:        register char *p;
        !           147: 
        !           148:        while (--argc > 0) {
        !           149:                p = *++argv;
        !           150:                if (strcmp(p, "+12") == 0)
        !           151:                        pitch12 = 1;
        !           152:                else if (*p++ == '-') {
        !           153:                        if (*p == 'd')
        !           154:                                getdelay(++p);
        !           155:                        else if (*p > '0' && *p <= '9')
        !           156:                                halfsiz = *p - '0';
        !           157:                }
        !           158:        }
        !           159:        fullsiz = pitch12 ? 6 : 8;
        !           160: }
        !           161: 
        !           162: /*     getdelay: scan fields of delay arg */
        !           163: getdelay(p)
        !           164: register char *p;
        !           165: {
        !           166:        register i;
        !           167: 
        !           168:        for (i = 0; i <= 2; i++) {
        !           169:                if (*p == ',') {
        !           170:                        p++;
        !           171:                        continue;
        !           172:                }
        !           173:                if (*p == '\0')
        !           174:                        break;
        !           175:                delay[i] = getnum(p);
        !           176:                p = scanptr+1;
        !           177:                if (*scanptr != ',')
        !           178:                        break;
        !           179:        }
        !           180: }
        !           181: 
        !           182: /*     fixtty: get tty status and save; remove CR-LF mapping */
        !           183: fixtty()
        !           184: {
        !           185:        struct stat sb;
        !           186:        extern char *ttyname();
        !           187: 
        !           188:        svsgflgs = sgb.sg_flags;
        !           189:        sgb.sg_flags &= ~CRMOD;
        !           190:        stty(1, &sgb);          /* stty nl  */
        !           191:        fstat(1, &sb);
        !           192:        svstmode = sb.st_mode;
        !           193:        ttydev = ttyname(1);
        !           194:        chmod(ttydev, 0600);            /* mesg n */
        !           195: }
        !           196: 
        !           197: getnum(p)
        !           198: register char *p;
        !           199: {
        !           200:        register i = 0 ;
        !           201: 
        !           202:        while (*p >= '0' && *p <= '9')
        !           203:                i = 10*i + *p++ - '0';
        !           204:        scanptr = p;
        !           205:        return(i);
        !           206: }
        !           207: 
        !           208: /*     flusher: flush accumulated newlines, reverse line feeds, buffer */
        !           209: flusher()
        !           210: {
        !           211:        if (nlcnt)
        !           212:                flushnl();
        !           213:        if (frevcnt)
        !           214:                flushrv();
        !           215:        fflush(stdout);
        !           216: }
        !           217: 
        !           218: /*     flushrv: flush accumulated reverse line feeds */
        !           219: /*     note: expects to be out of plot mode on entry */
        !           220: char frv1[] = {U,PLOT,U,U,PLOT,0};     /* 1 FREV leftover */
        !           221: char frv2[] = {U,U,PLOT,U,U,U,U,PLOT,0};       /* 2 of them */
        !           222: char frvadj[] = {U,U,U,LF,LF,LF,0};    /* forms tractor fixup */
        !           223: flushrv()
        !           224: {
        !           225:        register numleft;
        !           226: 
        !           227:        if (pitch12) {
        !           228:                numleft = frevcnt % 3;
        !           229:                frevcnt = 4 * (frevcnt / 3);
        !           230:        } else
        !           231:                numleft = 0;
        !           232:        while (frevcnt--) {
        !           233:                putx(U);
        !           234:                nplot(5,'\0');          /* slow down somewhat */
        !           235:        }
        !           236:        if (numleft == 1)
        !           237:                putstr(frv1);
        !           238:        else if (numleft == 2)
        !           239:                putstr(frv2);
        !           240:        putstr(frvadj);
        !           241:        frevcnt = 0;
        !           242: }
        !           243: 
        !           244: /*     flushnl: flush accumulated newlines (count in nlcnt) */
        !           245: char nl1[] = {LF, PLOT, LF, LF, PLOT, 0}; /* 12pitch: 1 nl */
        !           246: char nl2[] = {LF, LF, PLOT, LF, LF, LF, LF, PLOT, 0}; /* 2 nls */
        !           247: flushnl()
        !           248: {
        !           249:        register numleft;
        !           250: 
        !           251:        if (pitch12) {
        !           252:                numleft = nlcnt % 3;
        !           253:                nlcnt = 4 * (nlcnt/3);
        !           254:        } else
        !           255:                numleft = 0;
        !           256:        putx(CR);       /* must have 1 CR; only 1 needed */
        !           257:        while (nlcnt--)
        !           258:                putx(LF);       /* no plot mode needed for these */
        !           259:        if (numleft == 1)
        !           260:                putstr(nl1);
        !           261:        else if (numleft == 2)
        !           262:                putstr(nl2);
        !           263:        if (charcnt > delay[1])
        !           264:                nplot(1 + charcnt/20,'\0');
        !           265:        nlcnt = charcnt = nblcnt = tabcnt = 0;
        !           266: }
        !           267: 
        !           268: putstr(p)
        !           269: register char *p;
        !           270: {
        !           271:        while (*p)
        !           272:                putx(*p++);
        !           273: }
        !           274: 
        !           275: restore()
        !           276: {
        !           277:        putchar(BEL);
        !           278:        if (isatty(1)) {
        !           279:                sgb.sg_flags = svsgflgs;
        !           280:                stty(1, &sgb);
        !           281:                chmod(ttydev, svstmode);
        !           282:        }
        !           283:        exit(0);
        !           284: }
        !           285: 
        !           286: char alpha[]   = {L,'c',R,R,'(',L,0};
        !           287: char beta[]    = {'B',L,L,D,D,'|',R,R,U,U,0};
        !           288: char delta[]   = {'o',U,U,'<',D,D,0};
        !           289: char DELTA[]   = {L,L,'/',0203,D,'-',0204,R,'-',0203,U,'\\',L,L,0};
        !           290: char epsilon[] = {'<','-',0};
        !           291: char eta[]     = {'n',R,R,D,D,'|',L,L,U,U,0};
        !           292: char gamma[]   = {')',R,'/',L,0};
        !           293: char GAMMA[]   = {L,L,'|',R,R,0203,U,'-',0203,D,R,R,'`',L,L,0};
        !           294: char infinity[]        = {L,L,'c',0204,R,'o',L,L,0};
        !           295: char integral[]        = {'|','\'',R,R,'`',0203,L,0206,D,'\'',L,'`',R,R,0206,U,0};
        !           296: char lambda[]  = {'\\',0204,D,L,'\'',D,L,'\'',0205,U,R,R,0};
        !           297: char LAMBDA[]  = {L,L,'/',0204,R,'\\',L,L,0};
        !           298: char mu[]      = {'u',L,L,',',R,R,0};
        !           299: char nabla[]   = {L,L,'\\',0203,U,'-',0204,R,'-',0203,D,'/',L,L,0};
        !           300: char not[]     = {'-',0202,R,U,',',D,0202,L,0};
        !           301: char nu[]      = {L,'(',0203,R,'/',L,L,0};
        !           302: char omega[]   = {L,'u',0203,R,'u',L,L,0};
        !           303: char OMEGA[]   = {'O',D,D,L,'-',R,R,'-',L,U,U,0};
        !           304: char partial[] = {'o',R,D,'`',L,U,'`',L,U,'`',R,D,0};
        !           305: char phi[]     = {'o','/',0};
        !           306: char PHI[]     = {'o','[',']',0};
        !           307: char psi[]     = {'/','-',D,D,R,R,'\'',0204,L,'\'',R,R,U,U,0};
        !           308: char PSI[]     = {'[',']','-',D,D,R,R,'\'',0204,L,'`',R,R,U,U,0};
        !           309: char pi[]      = {U,'-',0203,D,'"',D,'"',0203,U,0};
        !           310: char PI[]      = {L,L,'[',']',0204,R,'[',']',L,L,0203,U,'-',0203,D,0};
        !           311: char rho[]     = {'o',L,L,D,D,'|',U,U,R,R,0};
        !           312: char sigma[]   = {'o',D,R,R,'~',U,L,L,0};
        !           313: char SIGMA[]   = {'>',0202,D,'-',0205,U,'-',D,D,0};
        !           314: char tau[]     = {'t',D,R,R,'~',L,L,L,'~',R,U,0};
        !           315: char theta[]   = {'O','-',0};
        !           316: char THETA[]   = {'O','=',0};
        !           317: char xi[]      = {'c',R,D,',',L,0203,U,'c',L,D,'`',R,D,0};
        !           318: char zeta[]    = {'c',R,D,',',L,0203,U,'<',D,D,0};
        !           319: 
        !           320: char   tab[] ={
        !           321:        'A',    /* alpha */
        !           322:        'B',    /* beta */
        !           323:        'D',    /* delta */
        !           324:        'W',    /* DELTA */
        !           325:        'S',    /* epsilon */
        !           326:        'N',    /* eta */
        !           327:        '\\',   /* gamma */
        !           328:        'G',    /* GAMMA */
        !           329:        'o',    /* infinity - not in M37 */
        !           330:        '^',    /* integral */
        !           331:        'L',    /* lambda */
        !           332:        'E',    /* LAMBDA */
        !           333:        'M',    /* mu */
        !           334:        '[',    /* nabla (del) */
        !           335:        '_',    /* not */
        !           336:        '@',    /* nu */
        !           337:        'C',    /* omega */
        !           338:        'Z',    /* OMEGA */
        !           339:        ']',    /* partial */
        !           340:        'U',    /* phi */
        !           341:        'F',    /* PHI */
        !           342:        'V',    /* psi */
        !           343:        'H',    /* PSI */
        !           344:        'J',    /* pi */
        !           345:        'P',    /* PI */
        !           346:        'K',    /* rho */
        !           347:        'Y',    /* sigma */
        !           348:        'R',    /* SIGMA */
        !           349:        'I',    /* tau */
        !           350:        'T',    /* theta */
        !           351:        'O',    /* THETA */
        !           352:        'X',    /* xi */
        !           353:        'Q',    /* zeta */
        !           354:        0
        !           355: };
        !           356: char   *trans[] = {
        !           357:        alpha,
        !           358:        beta,
        !           359:        delta,
        !           360:        DELTA,
        !           361:        epsilon,
        !           362:        eta,
        !           363:        gamma,
        !           364:        GAMMA,
        !           365:        infinity,
        !           366:        integral,
        !           367:        lambda,
        !           368:        LAMBDA,
        !           369:        mu,
        !           370:        nabla,
        !           371:        not,
        !           372:        nu,
        !           373:        omega,
        !           374:        OMEGA,
        !           375:        partial,
        !           376:        phi,
        !           377:        PHI,
        !           378:        psi,
        !           379:        PSI,
        !           380:        pi,
        !           381:        PI,
        !           382:        rho,
        !           383:        sigma,
        !           384:        SIGMA,
        !           385:        tau,
        !           386:        theta,
        !           387:        THETA,
        !           388:        xi,
        !           389:        zeta,
        !           390:        0
        !           391: };
        !           392: 
        !           393: special()
        !           394: {
        !           395:        register c, i;
        !           396: 
        !           397:    loop:
        !           398:        if ((c = getchar()) == SI || c < 0)
        !           399:                return;
        !           400:        for (i = 0; tab[i] != 0; i++)
        !           401:                if (c == tab[i]) {
        !           402:                        plot(trans[i]);
        !           403:                        goto loop;
        !           404:                }
        !           405:        putx(c);
        !           406:        goto loop;
        !           407: }
        !           408: 
        !           409: plot(s)
        !           410: register char *s;
        !           411: {
        !           412:        register i, c;
        !           413: 
        !           414:        FLIPMODE;
        !           415:        for (i = 0; (c = s[i]) != 0; i++) {
        !           416:                if (c & 0200)
        !           417:                        nplot(c&0177, s[++i]);
        !           418:                else
        !           419:                        putx(c);
        !           420:        }
        !           421:        FLIPMODE;
        !           422:        putx(' ');
        !           423: }
        !           424: 
        !           425: nplot(n, c)
        !           426: register n, c;
        !           427: {
        !           428:        while(n--)
        !           429:                putx(c);
        !           430: }
        !           431: 
        !           432: /*     putx: add ordinary (not PLOT or BEL) character to output, adding
        !           433:        extra PLOT when needed to flip state. */
        !           434: putx(c)
        !           435: register c;
        !           436: {
        !           437:        if (intrmod != extrmod) {
        !           438:                putchar(PLOT);          /* modes mismatch; flip for real */
        !           439:                extrmod = intrmod;
        !           440:        }
        !           441:        putchar(c);
        !           442: }

unix.superglobalmegacorp.com

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