Annotation of researchv8dc/cmd/stty.c, revision 1.1

1.1     ! root        1: #include <stdio.h>
        !             2: #include <sgtty.h>
        !             3: 
        !             4: #define        CTRL(x)         ('x'&037)
        !             5: 
        !             6: 
        !             7: extern int     errno, ntty_ld, tty_ld;
        !             8: int ttyfd;
        !             9: 
        !            10: 
        !            11: struct {
        !            12:        char    *s_name;
        !            13:        int     s_define;
        !            14:        int     s_speed;
        !            15: }
        !            16:        speeds[] = {
        !            17:                "0",    B0,     0,
        !            18:                "50",   B50,    50,
        !            19:                "75",   B75,    75,
        !            20:                "110",  B110,   110,
        !            21:                "134",  B134,   134,
        !            22:                "150",  B150,   150,
        !            23:                "200",  B200,   200,
        !            24:                "300",  B300,   300,
        !            25:                "600",  B600,   600,
        !            26:                "1200", B1200,  1200,
        !            27:                "1800", B1800,  1800,
        !            28:                "2400", B2400,  2400,
        !            29:                "4800", B4800,  4800,
        !            30:                "9600", B9600,  9600,
        !            31:                "exta", EXTA,   19200,
        !            32:                "extb", EXTB,   0,
        !            33:                "19200",        EXTA,   19200,          /* must follow extb */
        !            34:                0,
        !            35:        };
        !            36: 
        !            37: 
        !            38: struct {
        !            39:        char    *string;
        !            40:        int     set;
        !            41:        int     reset;
        !            42:        int     ntty;
        !            43: }
        !            44:        modes[] = {
        !            45:                { "even",       EVENP,  0 },
        !            46:                { "-even",      0,      EVENP },        
        !            47:                { "odd",        ODDP,   0 },
        !            48:                { "-odd",       0,      ODDP }, 
        !            49:                { "raw",        RAW,    0 },
        !            50:                { "-raw",       0,      RAW },  
        !            51:                { "cooked",     0,      RAW },  
        !            52:                { "-nl",        CRMOD,  0 },
        !            53:                { "nl",         0,      CRMOD },        
        !            54:                { "echo",       ECHO,   0 },
        !            55:                { "-echo",      0,      ECHO }, 
        !            56:                { "LCASE",      LCASE,  0 },
        !            57:                { "lcase",      LCASE,  0 },
        !            58:                { "-LCASE",     0,      LCASE },        
        !            59:                { "-lcase",     0,      LCASE },        
        !            60:                { "-tabs",      XTABS,  0 },
        !            61:                { "tabs",       0,      XTABS },        
        !            62:                { "tandem",     TANDEM, 0 },
        !            63:                { "-tandem",    0,      TANDEM },       
        !            64:                { "cbreak",     CBREAK, 0 },
        !            65:                { "-cbreak",    0,      CBREAK },       
        !            66:                { "cr0",        CR0,    CR3 },  
        !            67:                { "cr1",        CR1,    CR3 },  
        !            68:                { "cr2",        CR2,    CR3 },  
        !            69:                { "cr3",        CR3,    CR3 },  
        !            70:                { "tab0",       TAB0,   XTABS },        
        !            71:                { "tab1",       TAB1,   XTABS },        
        !            72:                { "tab2",       TAB2,   XTABS },        
        !            73:                { "nl0",        NL0,    NL3 },  
        !            74:                { "nl1",        NL1,    NL3 },  
        !            75:                { "nl2",        NL2,    NL3 },  
        !            76:                { "nl3",        NL3,    NL3 },  
        !            77:                { "ff0",        FF0,    FF1 },  
        !            78:                { "ff1",        FF1,    FF1 },  
        !            79:                { "bs0",        BS0,    BS1 },  
        !            80:                { "bs1",        BS1,    BS1 },  
        !            81:                { "33",         CR1,    ALLDELAY },     
        !            82:                { "tty33",      CR1,    ALLDELAY },     
        !            83:                { "37",         FF1+CR2+TAB1+NL1,       ALLDELAY },     
        !            84:                { "tty37",      FF1+CR2+TAB1+NL1,       ALLDELAY },     
        !            85:                { "05",         NL2,    ALLDELAY },     
        !            86:                { "vt05",       NL2,    ALLDELAY },     
        !            87:                { "tn",         CR1,    ALLDELAY },     
        !            88:                { "tn300",      CR1,    ALLDELAY },     
        !            89:                { "ti",         CR2,    ALLDELAY },     
        !            90:                { "ti700",      CR2,    ALLDELAY },     
        !            91:                { "tek",        FF1,    ALLDELAY },     
        !            92: 
        !            93:                { "crtbs",      LCRTBS,         LPRTERA,        1 },
        !            94:                { "-crtbs",     0,              LCRTBS,         1 },
        !            95:                { "prterase",   LPRTERA,        LCRTBS+LCRTKIL+LCRTERA, 1 },
        !            96:                { "-prterase",  0,              LPRTERA,        1 },
        !            97:                { "crterase",   LCRTERA,        LPRTERA,        1 },
        !            98:                { "-crterase",  0,              LCRTERA,        1 },
        !            99:                { "crtkill",    LCRTKIL,        LPRTERA,        1 },
        !           100:                { "-crtkill",   0,              LCRTKIL,        1 },
        !           101:                { "ctlecho",    LCTLECH,        0,              1 },
        !           102:                { "-ctlecho",   0,              LCTLECH,        1 },
        !           103:                { "tilde",      LTILDE,         0,              1 },
        !           104:                { "-tilde",     0,              LTILDE,         1 },
        !           105:                { "litout",     LLITOUT,        0,              1 },
        !           106:                { "-litout",    0,              LLITOUT,        1 },
        !           107:                { "flusho",     LFLUSHO,        0,              1 },
        !           108:                { "-flusho",    0,              LFLUSHO,        1 },
        !           109:                { "nohang",     LNOHANG,        0,              1 },
        !           110:                { "-nohang",    0,              LNOHANG,        1 },
        !           111:                { "decctq",     LDECCTQ,        0,              1 },
        !           112:                { "-decctq",    0,              LDECCTQ,        1 },
        !           113: /*
        !           114:                { "pendin",     LPENDIN,        0,              1 },
        !           115:                { "-pendin",    0,              LPENDIN,        1 },
        !           116:                { "mdmbuf",     LMDMBUF,        0,              1 },
        !           117:                { "-mdmbuf",    0,              LMDMBUF,        1 },
        !           118:                { "tostop",     LTOSTOP,        0,              1 },
        !           119:                { "-tostop",    0,              LTOSTOP,        1 },
        !           120:                { "etxack",     LETXACK,        0,              1 },
        !           121:                { "-etxack",    0,              LETXACK,        1 },
        !           122:                { "intrup",     LINTRUP,        0,              1 },
        !           123:                { "-intrup",    0,              LINTRUP,        1 },
        !           124: */
        !           125:                0,
        !           126:        };
        !           127: 
        !           128: 
        !           129: struct sgttyb  mode;
        !           130: struct tchars  tc;
        !           131: struct ltchars ltc;
        !           132: struct luchars luc;
        !           133: 
        !           134: struct special {
        !           135:        char    *name;
        !           136:        char    *cp;
        !           137:        char    def;
        !           138: }
        !           139:        special[] = {
        !           140:                "erase",        &mode.sg_erase,         CTRL(h),
        !           141:                "kill",         &mode.sg_kill,          '@',
        !           142: 
        !           143:                "intr",         &tc.t_intrc,            0177,
        !           144:                "quit",         &tc.t_quitc,            CTRL(\\),
        !           145:                "start",        &tc.t_startc,           CTRL(q),
        !           146:                "stop",         &tc.t_stopc,            CTRL(s),
        !           147:                "eof",          &tc.t_eofc,             CTRL(d),
        !           148:                "brk",          &tc.t_brkc,             0377,
        !           149: 
        !           150:                "susp",         &ltc.t_suspc,           CTRL(z),
        !           151:                "dsusp",        &ltc.t_dsuspc,          CTRL(y),
        !           152:                "rprnt",        &ltc.t_rprntc,          CTRL(r),
        !           153:                "flush",        &ltc.t_flushc,          CTRL(o),
        !           154:                "werase",       &ltc.t_werasc,          CTRL(w),
        !           155:                "lnext",        &ltc.t_lnextc,          CTRL(v),
        !           156: 
        !           157:                "undo",         &luc.t_undoc,           0377,
        !           158:                "urot",         &luc.t_urotc,           0377,
        !           159:                0
        !           160:        };
        !           161: 
        !           162: 
        !           163: char   *ego;
        !           164: int    ldisc;          /* current line disc */
        !           165: int    lmode;          /* new tty local mode bits */
        !           166: 
        !           167: 
        !           168: main (argc, argv)
        !           169:        int     argc;
        !           170:        char    *argv[];
        !           171: {
        !           172:        char    *arg, obuf[BUFSIZ];
        !           173:        int     i;
        !           174:        struct special *sp;
        !           175: 
        !           176:        ego = argv[0];
        !           177:        setbuf (stderr, obuf);
        !           178:        if ((ttyfd = open("/dev/tty", 1)) < 0) {
        !           179:                fprintf(stderr, "%s: can't open /dev/tty\n", ego);
        !           180:                exit(1);
        !           181:        }
        !           182:        (void) getmodes();
        !           183: /*
        !           184:        if (argc>=2 && ldisc!=tty_ld && ldisc!=ntty_ld)
        !           185:                fprintf(stderr, "%s: warning: no tty line discipline.\n", ego);
        !           186: */
        !           187:        if (argc < 2) {
        !           188:                prmodes();
        !           189:                exit (0);
        !           190:        }
        !           191: 
        !           192:        for (arg = *++argv; --argc > 0; arg = *++argv) {
        !           193: 
        !           194: #define eq(x)  (!strcmp (arg, x))
        !           195: 
        !           196:                if (eq ("all")) {
        !           197:                        prmodes();
        !           198:                        continue;
        !           199:                }
        !           200: 
        !           201:                if (eq ("old") || eq ("-new"))
        !           202:                        if (swdisc (tty_ld))
        !           203:                                goto done1;
        !           204:                        else    continue;
        !           205: 
        !           206:                if (eq ("new"))
        !           207:                        if (swdisc (ntty_ld))
        !           208:                                goto done1;
        !           209:                        else    continue;
        !           210: 
        !           211:                if (eq ("crt")) {
        !           212:                        lmode |= LCRTBS | LCTLECH;
        !           213:                        if (speeds[mode.sg_ispeed].s_speed >= 1200)
        !           214:                                lmode |= LCRTERA | LCRTKIL;
        !           215:                        continue;
        !           216:                }
        !           217: 
        !           218:                if (eq ("ek")) {
        !           219:                        mode.sg_erase = '#';
        !           220:                        mode.sg_kill = '@';
        !           221:                        continue;
        !           222:                }
        !           223: 
        !           224:                if (eq ("hup")) {
        !           225:                        (void) ioctl (ttyfd, TIOCHPCL, NULL);
        !           226:                        continue;
        !           227:                }
        !           228: 
        !           229:                for (sp = special; sp->name; sp++)
        !           230:                        if (eq (sp->name)) {
        !           231:                                if (--argc > 0) {
        !           232:                                        arg = *++argv;
        !           233:                                        if (*arg == 'u')
        !           234:                                                *sp->cp = 0377;
        !           235:                                        else if (*arg == '^')
        !           236:                                                *sp->cp = (arg[1] == '?') ?
        !           237:                                                    0177 : arg[1] & 037;
        !           238:                                        else    *sp->cp = *arg;
        !           239:                                        goto cont;
        !           240:                                }
        !           241:                                fprintf(stderr, "%s: missing %s character\n",
        !           242:                                    ego, arg);
        !           243:                                goto done1;
        !           244:                        }
        !           245: 
        !           246:                for (i = 0; speeds[i].s_name; i++)
        !           247:                        if (eq (speeds[i].s_name)) {
        !           248:                                mode.sg_ispeed = mode.sg_ospeed = 
        !           249:                                    speeds[i].s_define;
        !           250:                                goto cont;
        !           251:                        }
        !           252: 
        !           253:                if (eq ("gspeed")) {
        !           254:                        mode.sg_ispeed = B300;
        !           255:                        mode.sg_ospeed = B9600;
        !           256:                        continue;
        !           257:                }
        !           258: 
        !           259:                for (i = 0; modes[i].string; i++)
        !           260:                        if (eq (modes[i].string)) {
        !           261:                                if (modes[i].ntty) {
        !           262:                                        lmode &= ~modes[i].reset;
        !           263:                                        lmode |= modes[i].set;
        !           264:                                }
        !           265:                                else {
        !           266:                                        mode.sg_flags &= ~modes[i].reset;
        !           267:                                        mode.sg_flags |= modes[i].set;
        !           268:                                }
        !           269:                                goto cont;
        !           270:                        }
        !           271: 
        !           272:                fprintf(stderr, "%s: %s: unknown mode\n", ego, arg);
        !           273: done1:
        !           274:                setmodes();
        !           275:                exit (1);
        !           276: cont:
        !           277:                ;
        !           278:        }
        !           279: 
        !           280:        setmodes();
        !           281:        exit (0);
        !           282: }
        !           283: 
        !           284: 
        !           285: getmodes()
        !           286: {
        !           287:        int ret;
        !           288:        ldisc = ioctl (ttyfd, FIOLOOKLD, 0);
        !           289:        ret = ioctl (ttyfd, TIOCGETP, &mode) == -1;
        !           290:        (void) ioctl (ttyfd, TIOCGETC, &tc);
        !           291:        if (ldisc == ntty_ld) {
        !           292:                (void) ioctl (ttyfd, TIOCLGET, &lmode);
        !           293:                (void) ioctl (ttyfd, TIOCGLTC, &ltc);
        !           294:                (void) ioctl (ttyfd, TIOCGLUC, &luc);
        !           295:        }
        !           296:        return ret;
        !           297: }
        !           298: 
        !           299: 
        !           300: pit (what, itsname, sep)
        !           301:        unsigned char   what;
        !           302:        char    *itsname, *sep;
        !           303: {
        !           304:        printf("%s", itsname);
        !           305:        if (what == 0377) {
        !           306:                printf(" <undef>%s", sep);
        !           307:                return;
        !           308:        }
        !           309:        printf(" = ");
        !           310:        if (what & 0200) {
        !           311:                printf("M-");
        !           312:                what &= ~ 0200;
        !           313:        }
        !           314:        if (what == 0177) {
        !           315:                printf("^");
        !           316:                what = '?';
        !           317:        }
        !           318:        else if (what < ' ') {
        !           319:                printf("^");
        !           320:                what += '@';
        !           321:        }
        !           322:        printf("%c%s", what, sep);
        !           323:        return;
        !           324: }
        !           325: 
        !           326: 
        !           327: prmodes()
        !           328: {
        !           329:        register int    m;
        !           330: 
        !           331:        if (mode.sg_ispeed != mode.sg_ospeed)
        !           332:                printf("input speed: %d baud, output speed: %d baud\n",
        !           333:                    speeds[mode.sg_ispeed].s_speed,
        !           334:                    speeds[mode.sg_ospeed].s_speed);
        !           335:        else
        !           336:                printf("speed: %d baud\n",
        !           337:                       speeds[mode.sg_ispeed].s_speed);
        !           338:        pit (mode.sg_erase, "erase", "; ");
        !           339:        pit (mode.sg_kill, "kill", "; ");
        !           340:        pit (tc.t_intrc, "intr", "; ");
        !           341:        pit (tc.t_quitc, "quit", "\n");
        !           342:        pit (tc.t_startc, "start", "; ");
        !           343:        pit (tc.t_stopc, "stop", "; ");
        !           344:        pit (tc.t_eofc, "eof", "; ");
        !           345:        pit (tc.t_brkc, "brk", "\n");
        !           346: 
        !           347:        if (ldisc == ntty_ld) {
        !           348:                pit (ltc.t_werasc, "werase", "; ");
        !           349:                pit (ltc.t_rprntc, "rprnt", "; ");
        !           350:                pit (ltc.t_flushc, "flush", "; ");
        !           351:                pit (ltc.t_lnextc, "lnext", "\n");
        !           352:                pit (ltc.t_suspc, "susp", "; ");
        !           353:                pit (ltc.t_dsuspc, "dsusp", "; ");
        !           354:                pit (luc.t_undoc, "undo", "; ");
        !           355:                pit (luc.t_urotc, "urot", "\n");
        !           356:        }
        !           357:        if (ldisc == tty_ld)
        !           358:                printf("old ");
        !           359:        else if (ldisc == ntty_ld)
        !           360:                printf("new ");
        !           361:        m = mode.sg_flags;
        !           362: 
        !           363:        if (m & EVENP)
        !           364:                printf("even ");
        !           365:        if (m & ODDP)
        !           366:                printf("odd ");
        !           367: 
        !           368: #define        mpit(what,str)  printf(str + ((m & what) != 0))
        !           369: 
        !           370:        mpit (RAW, "-raw ");
        !           371:        printf("-nl " + ((m & CRMOD) == 0));
        !           372:        mpit (ECHO, "-echo ");
        !           373:        mpit (LCASE, "-lcase ");
        !           374:        printf("-tabs " + ((m & XTABS) != XTABS));
        !           375:        mpit (CBREAK, "-cbreak ");
        !           376:        mpit (TANDEM, "-tandem ");
        !           377: 
        !           378: #define delay(x,y)     printf("%s%d ", y, x)
        !           379: 
        !           380:        delay ((m & NLDELAY) / NL1, "nl");
        !           381:        if ((m & TBDELAY) != XTABS)
        !           382:                delay ((m & TBDELAY) / TAB1, "tab");
        !           383:        delay ((m & CRDELAY) / CR1, "cr");
        !           384:        delay ((m & VTDELAY) / FF1, "ff");
        !           385:        delay ((m & BSDELAY) / BS1, "bs");
        !           386:        printf("\n");
        !           387: 
        !           388:        if (ldisc == ntty_ld) {
        !           389:                m = lmode;
        !           390:                mpit (LCRTBS, "-crtbs ");
        !           391:                mpit (LCRTERA, "-crterase ");
        !           392:                mpit (LCRTKIL, "-crtkill ");
        !           393:                mpit (LCTLECH, "-ctlecho ");
        !           394:                mpit (LPRTERA, "-prterase ");
        !           395:                printf("\n");
        !           396: 
        !           397:                mpit (LTILDE, "-tilde ");
        !           398:                mpit (LFLUSHO, "-flusho ");
        !           399:                mpit (LLITOUT, "-litout ");
        !           400:                mpit (LNOHANG, "-nohang ");
        !           401:                mpit (LDECCTQ, "-decctq ");
        !           402:                printf("\n");
        !           403: /*
        !           404:                mpit (LMDMBUF, "-mdmbuf ");
        !           405:                mpit (LTOSTOP, "-tostop ");
        !           406:                mpit (LINTRUP, "-intrup ");
        !           407:                mpit (LETXACK, "-etxack ");
        !           408:                mpit (LPENDIN, "-pendin ");
        !           409:                printf("\n");
        !           410: */
        !           411:        }
        !           412:        return;
        !           413: }
        !           414: 
        !           415: 
        !           416: setmodes()
        !           417: {
        !           418:        (void) ioctl (ttyfd, TIOCSETN, &mode);
        !           419:        (void) ioctl (ttyfd, TIOCSETC, &tc);
        !           420:        if (ldisc == ntty_ld) {
        !           421:                (void) ioctl (ttyfd, TIOCSLTC, &ltc);
        !           422:                (void) ioctl (ttyfd, TIOCSLUC, &luc);
        !           423:                (void) ioctl (ttyfd, TIOCLSET, &lmode);
        !           424:        }
        !           425:        return;
        !           426: }
        !           427: 
        !           428: 
        !           429: int
        !           430: swdisc (newld)
        !           431:        int     newld;
        !           432: {
        !           433:        int curld = ioctl(ttyfd, FIOLOOKLD, 0);
        !           434: 
        !           435:        if ((curld==tty_ld || curld==ntty_ld) && 0 <= ioctl (ttyfd, FIOPOPLD, 0)) {
        !           436:                if (0 <= ioctl (ttyfd, FIOPUSHLD, &newld)) {
        !           437:                        (void) ioctl (ttyfd, TIOCSETN, &mode);
        !           438:                        (void) ioctl (ttyfd, TIOCSETC, &tc);
        !           439:                        getmodes();
        !           440:                        return (0);
        !           441:                }
        !           442:                else {
        !           443:                        (void) ioctl (ttyfd, FIOPUSHLD, &ldisc);
        !           444:                        setmodes();
        !           445:                }
        !           446:        }
        !           447:        fprintf(stderr, "%s: can't switch line disciplines\n", ego);
        !           448:        return (-1);
        !           449: }

unix.superglobalmegacorp.com

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