- argc--, argv++;
- if (argc > 0 && eq(argv[0], "-a")) {
- fmt = ALL;
- argc--, argv++;
- }
- if (argc > 0 && eq(argv[0], "-g")) {
- fmt = GFMT;
- argc--, argv++;
- }
- if (argc > 0 && eq(argv[0], "-f")) {
- argc--, argv++;
- if ((ctl = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
- syserrexit(*argv);
- argc--, argv++;
- }
- if (ioctl(ctl, TIOCGETD, &ldisc) < 0)
- syserrexit("TIOCGETD");
- if (tcgetattr(ctl, &t) < 0)
- syserrexit("tcgetattr");
- if (ioctl(ctl, TIOCGWINSZ, &win) < 0)
- warning("TIOCGWINSZ: %s", strerror(errno));
- checkredirect(); /* conversion aid */
-
- if (argc == 0 || fmt) {
- prmode(&t, ldisc, fmt);
- exit(0);
- }
-
- while (*argv) {
- if (eq("everything", *argv)) {
- prmode(&t, ldisc, ALL_BSD);
- exit(0);
- }
- if (eq("all", *argv)) {
- prmode(&t, ldisc, ALL);
- exit(0);
- }
- if (eq("tty", *argv) || eq("old", *argv) || eq("new", *argv)) {
- int nldisc = TTYDISC;
-
- if (ioctl(0, TIOCSETD, &nldisc) < 0)
- syserrexit("TIOCSETD");
- goto next;
- }
- if (eq("nl", *argv)) {
- t.c_iflag &= ~ICRNL;
- t.c_oflag &= ~ONLCR;
- goto next;
- }
- if (eq("-nl", *argv)) {
- t.c_iflag |= ICRNL;
- t.c_oflag |= ONLCR;
- goto next;
- }
- if (eq("dec", *argv)){
- 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;
- goto next;
- }
- if (eq("raw", *argv)) {
- cfmakeraw(&t);
- t.c_cflag &= ~(CSIZE|PARENB);
- t.c_cflag |= CS8;
- goto next;
- }
- if (eq("cbreak", *argv)) {
- t.c_iflag | BRKINT|IXON|IMAXBEL;
- t.c_oflag |= OPOST;
- t.c_lflag |= ISIG|IEXTEN;
- t.c_lflag &= ~ICANON;
- }
- if (eq("cooked", *argv) || eq("-raw", *argv) ||
- eq("sane", *argv) || eq("-cbreak", *argv)) {
- 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;
- goto next;
- }
- if (eq("rows", *argv)) {
- if (*(argv+1) == 0)
- goto setit;
- win.ws_row = atoi(*++argv);
- goto next;
- }
- if (eq("ispeed", *argv)) {
- int code;
- if (*(argv+1) == 0)
- errexit("missing ispeed");
- cfsetispeed(&t, atoi(*++argv));
- goto next;
- }
- if (eq("ospeed", *argv)) {
- if (*(argv+1) == 0)
- errexit("missing ospeed");
- cfsetospeed(&t, atoi(*++argv));
- goto next;
- }
- if (eq("cols", *argv) || eq("columns", *argv)) {
- if (*(argv+1) == 0)
- goto setit;
- win.ws_col = atoi(*++argv);
- goto next;
- }
- if (eq("size", *argv)) {
- put("%d %d\n", win.ws_row, win.ws_col);
- exit(0);
- }
- if (eq("extrpc", *argv) || eq("-extproc", *argv)) {
- if (**argv == '-')
- extproc = 0;
- else
- extproc = 1;
- ioctl(ctl, TIOCEXT, &extproc);
- }
- if (eq("speed", *argv)) {
- put("%d\n", cfgetospeed(&t));
- exit(0);
- }
- for (i=0; imodes[i].name; i++)
- if (eq(imodes[i].name, *argv)) {
- t.c_iflag &= ~imodes[i].unset;
- t.c_iflag |= imodes[i].set;
- goto next;
- }
- for (i=0; omodes[i].name; i++)
- if (eq(omodes[i].name, *argv)) {
- t.c_oflag &= ~omodes[i].unset;
- t.c_oflag |= omodes[i].set;
- goto next;
- }
- for (i=0; cmodes[i].name; i++)
- if (eq(cmodes[i].name, *argv)) {
- t.c_cflag &= ~cmodes[i].unset;
- t.c_cflag |= cmodes[i].set;
- goto next;
- }
- for (i=0; lmodes[i].name; i++)
- if (eq(lmodes[i].name, *argv)) {
- t.c_lflag &= ~lmodes[i].unset;
- t.c_lflag |= lmodes[i].set;
- goto next;
- }
- for (i=0; *cchars[i].names; i++) {
- char **cp = cchars[i].names;
- while (*cp) {
- if (eq(*cp, *argv)) {
- if (*++argv == 0)
- goto setit;
- if (eq(*argv, "undef") ||
- eq(*argv, "disable"))
- t.c_cc[cchars[i].sub] =
- _POSIX_VDISABLE;
- else if (**argv == '^')
- t.c_cc[cchars[i].sub] =
- ((*argv)[1] == '?') ? 0177 :
- ((*argv)[1] == '-') ?
- _POSIX_VDISABLE :
- (*argv)[1] & 037;
- else
- t.c_cc[cchars[i].sub] = **argv;
- goto next;
- }
- cp++;
- }
- }
- if (isdigit(**argv)) {
- cfsetospeed(&t, atoi(*argv));
- cfsetispeed(&t, atoi(*argv));
- goto next;
- }
- if (strncmp(*argv, "-gfmt", sizeof ("-gfmt") - 1) == 0) {
- gfmtset(&t, *argv);
- goto next;
- }
- /* didn't match anything */
- errexit("unknown option: %s", *argv);
- exit(1);
-next:
- argv++;
- }
-setit:
- if (tcsetattr(ctl, 0, &t) < 0)
- syserrexit("tcsetattr");
- if (ioctl(ctl, TIOCSWINSZ, &win) < 0)
- warning("can't set window size");
-
- exit(0);
-}
-
-gfmtset(tp, s)
- register struct termios *tp;
- char *s;
-{
- register int cnt;
- char sep;
- char *saves = s;
- int cval;
-#define advance(c) while (*(s) && *(s) != (c)) (s)++; if (*s) (s)++ ; \
- else \
- errexit("bad gfmt operand: %s", saves)
-#define chkeq(string) if (strncmp(s, (string), strlen(string))) \
- errexit("bad gfmt operand: %s", saves)
-
- if (s == NULL)
- errexit("missing gfmt string");
- advance(':');
- chkeq("iflag=");
- advance('=');
- sscanf(s, "%x", &tp->c_iflag);
-
- advance(':');
- chkeq("oflag");
- advance('=');
- sscanf(s, "%x", &tp->c_oflag);
-
- advance(':');
- chkeq("cflag");
- advance('=');
- sscanf(s, "%x", &tp->c_cflag);
-
- advance(':');
- chkeq("lflag");
- advance('=');
- sscanf(s, "%x", &tp->c_lflag);
-
- advance(':');
- chkeq("cc=");
-
- for (cnt = 0, sep = '='; cnt < NCCS; cnt++, sep = ',') {
- advance(sep);
- sscanf(s, "%o", &cval);
- tp->c_cc[cnt] = cval;
- }
-
- advance(':');
- chkeq("ispeed=");
- advance('=');
- sscanf(s, "%d", &tp->c_ispeed);
-
- advance(':');
- chkeq("ospeed=");
- advance('=');
- sscanf(s, "%d", &tp->c_ospeed);
-}
-
-prmode(tp, ldisc, fmt)
- struct termios *tp;
-{
- long i = tp->c_iflag,
- o = tp->c_oflag,
- c = tp->c_cflag,
- l = tp->c_lflag;
- u_char *cc = tp->c_cc;
- int ispeed = cfgetispeed(tp),
- ospeed = cfgetospeed(tp);
- char unknown[32],
- *ld;
- char *ccval();
-
- if (fmt == GFMT) {
- int cnt;
- char sep;