Annotation of researchv9/cmd/stty.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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