BSD 4_4_Lite1 release
[unix-history] / usr / src / bin / stty / stty.c
index 573a6fe..4a840c2 100644 (file)
@@ -1,54 +1,76 @@
 /*-
 /*-
- * Copyright (c) 1989, 1991 The Regents of the University of California.
- * All rights reserved.
+ * Copyright (c) 1989, 1991, 1993, 1994
+ *     The Regents of the University of California.  All rights reserved.
  *
  *
- * %sccs.include.redist.c%
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. All advertising materials mentioning features or use of this software
+ *    must display the following acknowledgement:
+ *     This product includes software developed by the University of
+ *     California, Berkeley and its contributors.
+ * 4. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
  */
 
 #ifndef lint
  */
 
 #ifndef lint
-char copyright[] =
-"@(#) Copyright (c) 1989, 1991 The Regents of the University of California.\n\
- All rights reserved.\n";
+static char copyright[] =
+"@(#) Copyright (c) 1989, 1991, 1993, 1994\n\
      The Regents of the University of California.  All rights reserved.\n";
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)stty.c     5.23 (Berkeley) %G%";
+static char sccsid[] = "@(#)stty.c     8.3 (Berkeley) 4/2/94";
 #endif /* not lint */
 
 #include <sys/types.h>
 #endif /* not lint */
 
 #include <sys/types.h>
-#include <sys/ioctl.h>
-#include <termios.h>
-#include <fcntl.h>
+
+#include <ctype.h>
+#include <err.h>
 #include <errno.h>
 #include <errno.h>
-#include <unistd.h>
+#include <fcntl.h>
 #include <stdio.h>
 #include <stdio.h>
-#include <ctype.h>
 #include <stdlib.h>
 #include <string.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
+
 #include "stty.h"
 #include "extern.h"
 
 #include "stty.h"
 #include "extern.h"
 
-static void usage __P((void));
-
+int
 main(argc, argv) 
        int argc;
 main(argc, argv) 
        int argc;
-       char **argv;
+       char *argv[];
 {
 {
-       extern char *optarg;
-       extern int opterr, optind;
-       extern struct cchar cchars1[], cchars2[];
-       extern struct modes cmodes[], imodes[], lmodes[], omodes[];
-       register struct modes *mp;
-       register struct cchar *cp;
-       struct winsize win;
-       struct termios t;
+       struct info i;
        enum FMT fmt;
        enum FMT fmt;
-       int ch, ctl, ldisc, tmp;
+       int ch;
 
 
-       ctl = STDIN_FILENO;
        fmt = NOTSET;
        fmt = NOTSET;
+       i.fd = STDIN_FILENO;
+
        opterr = 0;
        opterr = 0;
-       while ((ch = getopt(argc, argv, "aef:g")) != EOF)
+       while (optind < argc &&
+           strspn(argv[optind], "-aefg") == strlen(argv[optind]) &&
+           (ch = getopt(argc, argv, "aef:g")) != EOF)
                switch(ch) {
                case 'a':               /* undocumented: POSIX compatibility */
                        fmt = POSIX;
                switch(ch) {
                case 'a':               /* undocumented: POSIX compatibility */
                        fmt = POSIX;
@@ -57,8 +79,8 @@ main(argc, argv)
                        fmt = BSD;
                        break;
                case 'f':
                        fmt = BSD;
                        break;
                case 'f':
-                       if ((ctl = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
-                               err(optarg);
+                       if ((i.fd = open(optarg, O_RDONLY | O_NONBLOCK)) < 0)
+                               err(1, "%s", optarg);
                        break;
                case 'g':
                        fmt = GFLAG;
                        break;
                case 'g':
                        fmt = GFLAG;
@@ -71,11 +93,11 @@ main(argc, argv)
 args:  argc -= optind;
        argv += optind;
 
 args:  argc -= optind;
        argv += optind;
 
-       if (ioctl(ctl, TIOCGETD, &ldisc) < 0)
-               err("TIOCGETD: %s", strerror(errno));
-       if (tcgetattr(ctl, &t) < 0)
-               err("tcgetattr: %s", strerror(errno));
-       if (ioctl(ctl, TIOCGWINSZ, &win) < 0)
+       if (ioctl(i.fd, TIOCGETD, &i.ldisc) < 0)
+               err(1, "TIOCGETD");
+       if (tcgetattr(i.fd, &i.t) < 0)
+               err(1, "tcgetattr");
+       if (ioctl(i.fd, TIOCGWINSZ, &i.win) < 0)
                warn("TIOCGWINSZ: %s\n", strerror(errno));
 
        checkredirect();                        /* conversion aid */
                warn("TIOCGWINSZ: %s\n", strerror(errno));
 
        checkredirect();                        /* conversion aid */
@@ -87,201 +109,53 @@ args:     argc -= optind;
                /* FALLTHROUGH */
        case BSD:
        case POSIX:
                /* FALLTHROUGH */
        case BSD:
        case POSIX:
-               print(&t, &win, ldisc, fmt);
+               print(&i.t, &i.win, i.ldisc, fmt);
                break;
        case GFLAG:
                break;
        case GFLAG:
-               gprint(&t, &win, ldisc);
+               gprint(&i.t, &i.win, i.ldisc);
                break;
        }
        
                break;
        }
        
-#define        CHK(s)  (**argv == s[0] && !strcmp(*argv, s))
-
-       for (; *argv; ++argv) {
-               if (CHK("-nl")) {
-                       t.c_iflag |= ICRNL;
-                       t.c_oflag |= ONLCR;
-                       continue;
-               }
-               if (CHK("all")) {
-                       print(&t, &win, ldisc, BSD);
-                       continue;
-               }
-               if (CHK("-cbreak"))
-                       goto reset;
-               if (CHK("cbreak")) {
-                       t.c_iflag | BRKINT|IXON|IMAXBEL;
-                       t.c_oflag |= OPOST;
-                       t.c_lflag |= ISIG|IEXTEN;
-                       t.c_lflag &= ~ICANON;
-                       continue;
-               }
-               if (CHK("cols")) {
-                       if (!*++argv)
-                               err("option requires an argument -- cols");
-                       goto columns;
-               }
-               if (CHK("columns")) {
-                       if (!*++argv)
-                               err("option requires an argument -- columns");
-columns:               win.ws_col = atoi(*argv);
-                       continue;
-               }
-               if (CHK("cooked"))
-                       goto reset;
-               if (CHK("dec")) {
-                       t.c_cc[VERASE] = (u_char)0177;
-                       t.c_cc[VKILL] = CTRL('u');
-                       t.c_cc[VINTR] = CTRL('c');
-                       t.c_lflag &= ~ECHOPRT;
-                       t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
-                       t.c_iflag &= ~IXANY;
-                       continue;
-               }
-               if (CHK("everything")) {
-                       print(&t, &win, ldisc, BSD);
-                       continue;
-               }
-               if (CHK("-extproc")) {
-                       tmp = 0;
-                       ioctl(ctl, TIOCEXT, &tmp);
-                       continue;
-               }
-               if (CHK("extrpc")) {
-                       tmp = 1;
-                       ioctl(ctl, TIOCEXT, &tmp);
+       for (i.set = i.wset = 0; *argv; ++argv) {
+               if (ksearch(&argv, &i))
                        continue;
                        continue;
-               }
-               if (CHK("ispeed")) {
-                       if (!*++argv)
-                               err("option requires an argument -- ispeed");
-                       cfsetispeed(&t, atoi(*argv));
-                       continue;
-               }
-               if (CHK("new"))
-                       goto tty;
-               if (CHK("nl")) {
-                       t.c_iflag &= ~ICRNL;
-                       t.c_oflag &= ~ONLCR;
-                       continue;
-               }
-               if (CHK("old"))
-                       goto tty;
-               if (CHK("ospeed")) {
-                       if (!*++argv)
-                               err("option requires an argument -- ospeed");
-                       cfsetospeed(&t, atoi(*argv));
-                       continue;
-               }
-               if (CHK("-raw"))
-                       goto reset;
-               if (CHK("raw")) {
-                       cfmakeraw(&t);
-                       t.c_cflag &= ~(CSIZE|PARENB);
-                       t.c_cflag |= CS8;
-                       continue;
-               }
-               if (CHK("rows")) {
-                       if (!*++argv)
-                               err("option requires an argument -- rows");
-                       win.ws_row = atoi(*argv);
-                       continue;
-               }
-               if (CHK("sane")) {
-reset:                 t.c_cflag = TTYDEF_CFLAG | (t.c_cflag & CLOCAL);
-                       t.c_iflag = TTYDEF_IFLAG;
-                       t.c_iflag |= ICRNL;
-                       /* preserve user-preference flags in lflag */
-#define        LKEEP   (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
-                       t.c_lflag = TTYDEF_LFLAG | (t.c_lflag & LKEEP);
-                       t.c_oflag = TTYDEF_OFLAG;
-                       continue;
-               }
-               if (CHK("size")) {
-                       (void)printf("%d %d\n", win.ws_row, win.ws_col);
-                       continue;
-               }
-               if (CHK("speed")) {
-                       (void)printf("%d\n", cfgetospeed(&t));
+
+               if (csearch(&argv, &i))
                        continue;
                        continue;
-               }
-               if (CHK("tty")) {
-tty:                   tmp = TTYDISC;
-                       if (ioctl(0, TIOCSETD, &tmp) < 0)
-                               err("TIOCSETD: %s", strerror(errno));
+
+               if (msearch(&argv, &i))
                        continue;
                        continue;
-               }
-               
-               for (mp = cmodes; mp->name; ++mp)
-                       if (CHK(mp->name)) {
-                               t.c_cflag &= ~mp->unset;
-                               t.c_cflag |= mp->set;
-                               goto next;
-                       }
-               for (mp = imodes; mp->name; ++mp)
-                       if (CHK(mp->name)) {
-                               t.c_iflag &= ~mp->unset;
-                               t.c_iflag |= mp->set;
-                               goto next;
-                       }
-               for (mp = lmodes; mp->name; ++mp)
-                       if (CHK(mp->name)) {
-                               t.c_lflag &= ~mp->unset;
-                               t.c_lflag |= mp->set;
-                               goto next;
-                       }
-               for (mp = omodes; mp->name; ++mp)
-                       if (CHK(mp->name)) {
-                               t.c_oflag &= ~mp->unset;
-                               t.c_oflag |= mp->set;
-                               goto next;
-                       }
-               for (cp = cchars1; cp->name; ++cp) {
-                       if (!CHK(cp->name))
-                               continue;
-                       goto ccfound;
-               }
-               for (cp = cchars2; cp->name; ++cp) {
-                       if (!CHK(cp->name))
-                               continue;
-ccfound:               if (!*++argv)
-                               err("option requires an argument -- %s",
-                                   cp->name);
-                       if (CHK("undef") || CHK("disable"))
-                               t.c_cc[cp->sub] = _POSIX_VDISABLE;
-                       else if (**argv == '^')
-                               t.c_cc[cp->sub] = 
-                                   ((*argv)[1] == '?') ? 0177 :
-                                   ((*argv)[1] == '-') ? _POSIX_VDISABLE :
-                                   (*argv)[1] & 037;
-                       else
-                               t.c_cc[cp->sub] = **argv;
-                       goto next;
-               }
 
                if (isdigit(**argv)) {
 
                if (isdigit(**argv)) {
-                       cfsetospeed(&t, atoi(*argv));
-                       cfsetispeed(&t, atoi(*argv));
-                       goto next;
+                       int speed;
+
+                       speed = atoi(*argv);
+                       cfsetospeed(&i.t, speed);
+                       cfsetispeed(&i.t, speed);
+                       i.set = 1;
+                       continue;
                }
                }
+
                if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
                if (!strncmp(*argv, "gfmt1", sizeof("gfmt1") - 1)) {
-                       gread(&t, *argv + sizeof("gfmt1") - 1);
-                       goto next;
+                       gread(&i.t, *argv + sizeof("gfmt1") - 1);
+                       continue;
                }
 
                }
 
-               err("illegal option -- %s", *argv);
-next:          continue;
+               warnx("illegal option -- %s", *argv);
+               usage();
        }
 
        }
 
-       if (tcsetattr(ctl, 0, &t) < 0)
-               err("tcsetattr: %s", strerror(errno));
-       if (ioctl(ctl, TIOCSWINSZ, &win) < 0)
-               warn("TIOCSWINSZ: %s", strerror(errno));
+       if (i.set && tcsetattr(i.fd, 0, &i.t) < 0)
+               err(1, "tcsetattr");
+       if (i.wset && ioctl(i.fd, TIOCSWINSZ, &i.win) < 0)
+               warn("TIOCSWINSZ");
        exit(0);
 }
 
        exit(0);
 }
 
-static void
+void
 usage()
 {
 usage()
 {
-       (void)fprintf(stderr, "usage: stty: [-eg] [-f file] [options]\n");
-       exit(1);
+
+       (void)fprintf(stderr, "usage: stty: [-a|-e|-g] [-f file] [options]\n");
+       exit (1);
 }
 }