add "stty size" ala "stty speed"
[unix-history] / usr / src / bin / stty / stty.c
index 34cb5d4..8a53e0f 100644 (file)
@@ -1,10 +1,25 @@
-static char *sccsid ="@(#)stty.c       4.2 (Berkeley) %G%";
+/*
+ * Copyright (c) 1980 Regents of the University of California.
+ * All rights reserved.  The Berkeley software License Agreement
+ * specifies the terms and conditions for redistribution.
+ */
+
+#ifndef lint
+char copyright[] =
+"@(#) Copyright (c) 1980 Regents of the University of California.\n\
+ All rights reserved.\n";
+#endif not lint
+
+#ifndef lint
+static char sccsid[] = "@(#)stty.c     5.3 (Berkeley) %G%";
+#endif not lint
+
 /*
  * set teletype modes
  */
 
 #include <stdio.h>
 /*
  * set teletype modes
  */
 
 #include <stdio.h>
-#include <sgtty.h>
+#include <sys/ioctl.h>
 
 struct
 {
 
 struct
 {
@@ -27,7 +42,9 @@ struct
        "4800", B4800,
        "9600", B9600,
        "exta", EXTA,
        "4800", B4800,
        "9600", B9600,
        "exta", EXTA,
+       "19200", EXTA,
        "extb", EXTB,
        "extb", EXTB,
+       "38400", EXTB,
        0,
 };
 struct
        0,
 };
 struct
@@ -99,50 +116,55 @@ struct
        "-mdmbuf",      0, 0, 0, LMDMBUF,
        "litout",       0, 0, LLITOUT, 0,
        "-litout",      0, 0, 0, LLITOUT,
        "-mdmbuf",      0, 0, 0, LMDMBUF,
        "litout",       0, 0, LLITOUT, 0,
        "-litout",      0, 0, 0, LLITOUT,
+       "pass8",        0, 0, LPASS8, 0,
+       "-pass8",       0, 0, 0, LPASS8,
        "tostop",       0, 0, LTOSTOP, 0,
        "-tostop",      0, 0, 0, LTOSTOP,
        "flusho",       0, 0, LFLUSHO, 0,
        "-flusho",      0, 0, 0, LFLUSHO,
        "nohang",       0, 0, LNOHANG, 0,
        "-nohang",      0, 0, 0, LNOHANG,
        "tostop",       0, 0, LTOSTOP, 0,
        "-tostop",      0, 0, 0, LTOSTOP,
        "flusho",       0, 0, LFLUSHO, 0,
        "-flusho",      0, 0, 0, LFLUSHO,
        "nohang",       0, 0, LNOHANG, 0,
        "-nohang",      0, 0, 0, LNOHANG,
+#ifdef notdef
        "etxack",       0, 0, LETXACK, 0,
        "-etxack",      0, 0, 0, LETXACK,
        "etxack",       0, 0, LETXACK, 0,
        "-etxack",      0, 0, 0, LETXACK,
-       "intrup",       0, 0, LINTRUP, 0,
-       "-intrup",      0, 0, 0, LINTRUP,
+#endif
        "ctlecho",      0, 0, LCTLECH, 0,
        "-ctlecho",     0, 0, 0, LCTLECH,
        "pendin",       0, 0, LPENDIN, 0,
        "-pendin",      0, 0, 0, LPENDIN,
        "ctlecho",      0, 0, LCTLECH, 0,
        "-ctlecho",     0, 0, 0, LCTLECH,
        "pendin",       0, 0, LPENDIN, 0,
        "-pendin",      0, 0, 0, LPENDIN,
+       "decctlq",      0, 0, LDECCTQ, 0,
+       "-decctlq",     0, 0, 0, LDECCTQ,
+       "noflsh",       0, 0, LNOFLSH, 0,
+       "-noflsh",      0, 0, 0, LNOFLSH,
        0,
 };
 
 struct tchars tc;
 struct ltchars ltc;
 struct sgttyb mode;
        0,
 };
 
 struct tchars tc;
 struct ltchars ltc;
 struct sgttyb mode;
+struct winsize win;
 int    lmode;
 int    oldisc, ldisc;
 
 int    lmode;
 int    oldisc, ldisc;
 
-#define        CTRL(x)         ('x'&037)
-
 struct special {
        char    *name;
        char    *cp;
        char    def;
 } special[] = {
 struct special {
        char    *name;
        char    *cp;
        char    def;
 } special[] = {
-       "erase",        &mode.sg_erase,         CTRL(h),
-       "kill",         &mode.sg_kill,          '@',
-       "intr",         &tc.t_intrc,            0177,
-       "quit",         &tc.t_quitc,            CTRL(\\\\),
-       "start",        &tc.t_startc,           CTRL(q),
-       "stop",         &tc.t_stopc,            CTRL(s),
-       "eof",          &tc.t_eofc,             CTRL(d),
-       "brk",          &tc.t_brkc,             0377,
-       "susp",         &ltc.t_suspc,           CTRL(z),
-       "dsusp",        &ltc.t_dsuspc,          CTRL(y),
-       "rprnt",        &ltc.t_rprntc,          CTRL(r),
-       "flush",        &ltc.t_flushc,          CTRL(o),
-       "werase",       &ltc.t_werasc,          CTRL(w),
-       "lnext",        &ltc.t_lnextc,          CTRL(v),
+       "erase",        &mode.sg_erase,         CERASE,
+       "kill",         &mode.sg_kill,          CKILL,
+       "intr",         &tc.t_intrc,            CINTR,
+       "quit",         &tc.t_quitc,            CQUIT,
+       "start",        &tc.t_startc,           CSTART,
+       "stop",         &tc.t_stopc,            CSTOP,
+       "eof",          &tc.t_eofc,             CEOF,
+       "brk",          &tc.t_brkc,             CBRK,
+       "susp",         &ltc.t_suspc,           CSUSP,
+       "dsusp",        &ltc.t_dsuspc,          CDSUSP,
+       "rprnt",        &ltc.t_rprntc,          CRPRNT,
+       "flush",        &ltc.t_flushc,          CFLUSH,
+       "werase",       &ltc.t_werasc,          CWERASE,
+       "lnext",        &ltc.t_lnextc,          CLNEXT,
        0
 };
 char   *arg;
        0
 };
 char   *arg;
@@ -159,12 +181,13 @@ char      **iargv;
        setbuf(stderr, obuf);
        argc = iargc;
        argv = iargv;
        setbuf(stderr, obuf);
        argc = iargc;
        argv = iargv;
-       gtty(1, &mode);
+       ioctl(1, TIOCGETP, &mode);
        ioctl(1, TIOCGETD, &ldisc);
        oldisc = ldisc;
        ioctl(1, TIOCGETC, &tc);
        ioctl(1, TIOCLGET, &lmode);
        ioctl(1, TIOCGLTC, &ltc);
        ioctl(1, TIOCGETD, &ldisc);
        oldisc = ldisc;
        ioctl(1, TIOCGETC, &tc);
        ioctl(1, TIOCLGET, &lmode);
        ioctl(1, TIOCGLTC, &ltc);
+       ioctl(1, TIOCGWINSZ, &win);
        if(argc == 1) {
                prmodes(0);
                exit(0);
        if(argc == 1) {
                prmodes(0);
                exit(0);
@@ -219,6 +242,19 @@ char       **iargv;
                                perror("ioctl");
                        continue;
                }
                                perror("ioctl");
                        continue;
                }
+               if (eq("dec")){
+                       mode.sg_erase = 0177;
+                       mode.sg_kill = CTRL(u);
+                       tc.t_intrc = CTRL(c);
+                       ldisc = NTTYDISC;
+                       lmode &= ~LPRTERA;
+                       lmode |= LCRTBS|LCTLECH|LDECCTQ;
+                       if (mode.sg_ospeed >= B1200)
+                               lmode |= LCRTERA|LCRTKIL;
+                       if (ioctl(1, TIOCSETD, &ldisc)<0)
+                               perror("ioctl");
+                       continue;
+               }
                for (sp = special; sp->name; sp++)
                        if (eq(sp->name)) {
                                if (--argc == 0)
                for (sp = special; sp->name; sp++)
                        if (eq(sp->name)) {
                                if (--argc == 0)
@@ -226,7 +262,7 @@ char        **iargv;
                                if (**++argv == 'u')
                                        *sp->cp = 0377;
                                else if (**argv == '^')
                                if (**++argv == 'u')
                                        *sp->cp = 0377;
                                else if (**argv == '^')
-                                       *sp->cp = (*(argv[1]) == '?') ?
+                                       *sp->cp = ((*argv)[1] == '?') ?
                                            0177 : (*argv)[1] & 037;
                                else
                                        *sp->cp = **argv;
                                            0177 : (*argv)[1] & 037;
                                else
                                        *sp->cp = **argv;
@@ -241,13 +277,23 @@ char      **iargv;
                        ioctl(1, TIOCHPCL, NULL);
                        continue;
                }
                        ioctl(1, TIOCHPCL, NULL);
                        continue;
                }
+               if (eq("rows")) {
+                       if (--argc == 0)
+                               goto done;
+                       win.ws_row = atoi(*++argv);
+               }
+               if (eq("columns")) {
+                       if (--argc == 0)
+                               goto done;
+                       win.ws_col = atoi(*++argv);
+               }
                for(i=0; speeds[i].string; i++)
                        if(eq(speeds[i].string)) {
                                mode.sg_ispeed = mode.sg_ospeed = speeds[i].speed;
                                goto cont;
                        }
                if (eq("speed")) {
                for(i=0; speeds[i].string; i++)
                        if(eq(speeds[i].string)) {
                                mode.sg_ispeed = mode.sg_ospeed = speeds[i].speed;
                                goto cont;
                        }
                if (eq("speed")) {
-                       gtty(open("/dev/tty", 0), &mode);
+                       ioctl(open("/dev/tty", 0), TIOCGETP, &mode);
                        for(i=0; speeds[i].string; i++)
                                if (mode.sg_ospeed == speeds[i].speed) {
                                        printf("%s\n", speeds[i].string);
                        for(i=0; speeds[i].string; i++)
                                if (mode.sg_ospeed == speeds[i].speed) {
                                        printf("%s\n", speeds[i].string);
@@ -273,6 +319,7 @@ done:
        ioctl(1, TIOCSETC, &tc);
        ioctl(1, TIOCSLTC, &ltc);
        ioctl(1, TIOCLSET, &lmode);
        ioctl(1, TIOCSETC, &tc);
        ioctl(1, TIOCSLTC, &ltc);
        ioctl(1, TIOCLSET, &lmode);
+       ioctl(1, TIOCSWINSZ, &win);
 }
 
 eq(string)
 }
 
 eq(string)
@@ -308,6 +355,8 @@ prmodes(all)
                prspeed("output speed ", mode.sg_ospeed);
        } else
                prspeed("speed ", mode.sg_ispeed);
                prspeed("output speed ", mode.sg_ospeed);
        } else
                prspeed("speed ", mode.sg_ispeed);
+       if (all)
+               fprintf(stderr, ", %d rows, %d columns", win.ws_row, win.ws_col);
        fprintf(stderr, all==2 ? "\n" : "; ");
        m = mode.sg_flags;
        if(all==2 || (m&(EVENP|ODDP))!=(EVENP|ODDP)) {
        fprintf(stderr, all==2 ? "\n" : "; ");
        m = mode.sg_flags;
        if(all==2 || (m&(EVENP|ODDP))!=(EVENP|ODDP)) {
@@ -347,6 +396,7 @@ prmodes(all)
                int newcrt = (lmode&(LCTLECH|LCRTBS)) == (LCTLECH|LCRTBS) &&
                    (lmode&(LCRTERA|LCRTKIL)) ==
                      ((mode.sg_ospeed > B300) ? LCRTERA|LCRTKIL : 0);
                int newcrt = (lmode&(LCTLECH|LCRTBS)) == (LCTLECH|LCRTBS) &&
                    (lmode&(LCRTERA|LCRTKIL)) ==
                      ((mode.sg_ospeed > B300) ? LCRTERA|LCRTKIL : 0);
+               int nothing = 1;
                if (newcrt) {
                        if (all==2)
                                fprintf(stderr, "crt: (crtbs crterase crtkill ctlecho) ");
                if (newcrt) {
                        if (all==2)
                                fprintf(stderr, "crt: (crtbs crterase crtkill ctlecho) ");
@@ -361,19 +411,29 @@ prmodes(all)
                        lpit(LPRTERA, "-prterase ");
                }
                lpit(LTOSTOP, "-tostop ");
                        lpit(LPRTERA, "-prterase ");
                }
                lpit(LTOSTOP, "-tostop ");
-               lpit(LINTRUP, "-intrup ");
                if (all==2) {
                        fprintf(stderr, "\n");
                        any = 0;
                if (all==2) {
                        fprintf(stderr, "\n");
                        any = 0;
+                       nothing = 0;
                }
                lpit(LTILDE, "-tilde ");
                lpit(LFLUSHO, "-flusho ");
                lpit(LMDMBUF, "-mdmbuf ");
                lpit(LLITOUT, "-litout ");
                }
                lpit(LTILDE, "-tilde ");
                lpit(LFLUSHO, "-flusho ");
                lpit(LMDMBUF, "-mdmbuf ");
                lpit(LLITOUT, "-litout ");
+               lpit(LPASS8, "-pass8 ");
                lpit(LNOHANG, "-nohang ");
                lpit(LNOHANG, "-nohang ");
+               if (any) {
+                       fprintf(stderr,"\n");
+                       any = 0;
+                       nothing = 0;
+               }
+#ifdef notdef
                lpit(LETXACK, "-etxack ");
                lpit(LETXACK, "-etxack ");
+#endif
                lpit(LPENDIN, "-pendin ");
                lpit(LPENDIN, "-pendin ");
-               if (any)
+               lpit(LDECCTQ, "-decctlq ");
+               lpit(LNOFLSH, "-noflsh ");
+               if (any || nothing)
                        fprintf(stderr,"\n");
        } else if (!all)
                fprintf(stderr,"\n");
                        fprintf(stderr,"\n");
        } else if (!all)
                fprintf(stderr,"\n");
@@ -414,6 +474,7 @@ erase  kill   werase rprnt  flush  lnext  susp   intr   quit   stop   eof\
        } else if (ldisc != NETLDISC) {
                register struct special *sp;
                int first = 1;
        } else if (ldisc != NETLDISC) {
                register struct special *sp;
                int first = 1;
+
                for (sp = special; sp->name; sp++) {
                        if ((*sp->cp&0377) != (sp->def&0377)) {
                                pit(*sp->cp, sp->name, first ? "" : ", ");
                for (sp = special; sp->name; sp++) {
                        if ((*sp->cp&0377) != (sp->def&0377)) {
                                pit(*sp->cp, sp->name, first ? "" : ", ");
@@ -422,7 +483,7 @@ erase  kill   werase rprnt  flush  lnext  susp   intr   quit   stop   eof\
                        if (sp->cp == &tc.t_brkc && ldisc == 0)
                                break;
                }
                        if (sp->cp == &tc.t_brkc && ldisc == 0)
                                break;
                }
-               if (first == 0)
+               if (!first)
                        fprintf(stderr, "\n");
        }
 }
                        fprintf(stderr, "\n");
        }
 }
@@ -501,7 +562,7 @@ char *s;
 }
 
 int    speed[] = {
 }
 
 int    speed[] = {
-       0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,0,0
+       0,50,75,110,134,150,200,300,600,1200,1800,2400,4800,9600,19200,38400
 };
 
 prspeed(c, s)
 };
 
 prspeed(c, s)