150008Sbostic /*-
266642Spendry * Copyright (c) 1991, 1993, 1994
360721Sbostic * The Regents of the University of California. All rights reserved.
450008Sbostic *
550008Sbostic * %sccs.include.redist.c%
650008Sbostic */
750008Sbostic
850008Sbostic #ifndef lint
9*68388Sdab static char sccsid[] = "@(#)key.c 8.4 (Berkeley) 02/20/95";
1050008Sbostic #endif /* not lint */
1150008Sbostic
1250008Sbostic #include <sys/types.h>
1359520Sbostic
1459520Sbostic #include <err.h>
1550008Sbostic #include <errno.h>
1650008Sbostic #include <stdlib.h>
1750008Sbostic #include <stdio.h>
1850008Sbostic #include <string.h>
1959520Sbostic
2050008Sbostic #include "stty.h"
2150008Sbostic #include "extern.h"
2250008Sbostic
2350008Sbostic __BEGIN_DECLS
2450008Sbostic void f_all __P((struct info *));
2550008Sbostic void f_cbreak __P((struct info *));
2650008Sbostic void f_columns __P((struct info *));
2750008Sbostic void f_dec __P((struct info *));
2850008Sbostic void f_everything __P((struct info *));
2950008Sbostic void f_extproc __P((struct info *));
3050008Sbostic void f_ispeed __P((struct info *));
3150008Sbostic void f_nl __P((struct info *));
3250008Sbostic void f_ospeed __P((struct info *));
3350008Sbostic void f_raw __P((struct info *));
3450008Sbostic void f_rows __P((struct info *));
3550008Sbostic void f_sane __P((struct info *));
3650008Sbostic void f_size __P((struct info *));
3750008Sbostic void f_speed __P((struct info *));
3850008Sbostic void f_tty __P((struct info *));
3950008Sbostic __END_DECLS
4050008Sbostic
4150041Sbostic static struct key {
4250041Sbostic char *name; /* name */
4350041Sbostic void (*f) __P((struct info *)); /* function */
4450041Sbostic #define F_NEEDARG 0x01 /* needs an argument */
4550041Sbostic #define F_OFFOK 0x02 /* can turn off */
4650041Sbostic int flags;
4750041Sbostic } keys[] = {
4859520Sbostic { "all", f_all, 0 },
4959520Sbostic { "cbreak", f_cbreak, F_OFFOK },
5059520Sbostic { "cols", f_columns, F_NEEDARG },
5159520Sbostic { "columns", f_columns, F_NEEDARG },
5259520Sbostic { "cooked", f_sane, 0 },
5359520Sbostic { "dec", f_dec, 0 },
5459520Sbostic { "everything", f_everything, 0 },
5559520Sbostic { "extproc", f_extproc, F_OFFOK },
5659520Sbostic { "ispeed", f_ispeed, F_NEEDARG },
5759520Sbostic { "new", f_tty, 0 },
5859520Sbostic { "nl", f_nl, F_OFFOK },
5959520Sbostic { "old", f_tty, 0 },
6059520Sbostic { "ospeed", f_ospeed, F_NEEDARG },
6159520Sbostic { "raw", f_raw, F_OFFOK },
6259520Sbostic { "rows", f_rows, F_NEEDARG },
6359520Sbostic { "sane", f_sane, 0 },
6459520Sbostic { "size", f_size, 0 },
6559520Sbostic { "speed", f_speed, 0 },
6659520Sbostic { "tty", f_tty, 0 },
6750008Sbostic };
6850008Sbostic
6966564Spendry static int
c_key(a,b)7066564Spendry c_key(a, b)
7166564Spendry const void *a, *b;
7266564Spendry {
7366564Spendry
7466564Spendry return (strcmp(((struct key *)a)->name, ((struct key *)b)->name));
7566564Spendry }
7666564Spendry
7759520Sbostic int
ksearch(argvp,ip)7850010Sbostic ksearch(argvp, ip)
7950010Sbostic char ***argvp;
8050010Sbostic struct info *ip;
8150008Sbostic {
8266564Spendry char *name;
8366564Spendry struct key *kp, tmp;
8450008Sbostic
8550010Sbostic name = **argvp;
8650010Sbostic if (*name == '-') {
8750010Sbostic ip->off = 1;
8850010Sbostic ++name;
8950010Sbostic } else
9050010Sbostic ip->off = 0;
9150010Sbostic
9250008Sbostic tmp.name = name;
9350010Sbostic if (!(kp = (struct key *)bsearch(&tmp, keys,
9450010Sbostic sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
9559520Sbostic return (0);
9659520Sbostic if (!(kp->flags & F_OFFOK) && ip->off) {
9759520Sbostic errx(1, "illegal option -- %s", name);
9859520Sbostic usage();
9959520Sbostic }
10059520Sbostic if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
10159520Sbostic errx(1, "option requires an argument -- %s", name);
10259520Sbostic usage();
10359520Sbostic }
10450010Sbostic kp->f(ip);
10559520Sbostic return (1);
10650008Sbostic }
10750008Sbostic
10850008Sbostic void
f_all(ip)10950008Sbostic f_all(ip)
11050008Sbostic struct info *ip;
11150008Sbostic {
11250008Sbostic print(&ip->t, &ip->win, ip->ldisc, BSD);
11350008Sbostic }
11450008Sbostic
11550008Sbostic void
f_cbreak(ip)11650008Sbostic f_cbreak(ip)
11750008Sbostic struct info *ip;
11850008Sbostic {
11966564Spendry
12050008Sbostic if (ip->off)
12150008Sbostic f_sane(ip);
12250008Sbostic else {
12350008Sbostic ip->t.c_iflag |= BRKINT|IXON|IMAXBEL;
12450008Sbostic ip->t.c_oflag |= OPOST;
12550008Sbostic ip->t.c_lflag |= ISIG|IEXTEN;
12650008Sbostic ip->t.c_lflag &= ~ICANON;
12750008Sbostic ip->set = 1;
12850008Sbostic }
12950008Sbostic }
13050008Sbostic
13150008Sbostic void
f_columns(ip)13250008Sbostic f_columns(ip)
13350008Sbostic struct info *ip;
13450008Sbostic {
13566564Spendry
13650008Sbostic ip->win.ws_col = atoi(ip->arg);
13750008Sbostic ip->wset = 1;
13850008Sbostic }
13950008Sbostic
14050008Sbostic void
f_dec(ip)14150008Sbostic f_dec(ip)
14250008Sbostic struct info *ip;
14350008Sbostic {
14466564Spendry
14550008Sbostic ip->t.c_cc[VERASE] = (u_char)0177;
14650008Sbostic ip->t.c_cc[VKILL] = CTRL('u');
14750008Sbostic ip->t.c_cc[VINTR] = CTRL('c');
14850008Sbostic ip->t.c_lflag &= ~ECHOPRT;
14950008Sbostic ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
15050008Sbostic ip->t.c_iflag &= ~IXANY;
15150008Sbostic ip->set = 1;
15250008Sbostic }
15350008Sbostic
15450008Sbostic void
f_everything(ip)15550008Sbostic f_everything(ip)
15650008Sbostic struct info *ip;
15750008Sbostic {
15866564Spendry
15950008Sbostic print(&ip->t, &ip->win, ip->ldisc, BSD);
16050008Sbostic }
16150008Sbostic
16250008Sbostic void
f_extproc(ip)16350008Sbostic f_extproc(ip)
16450008Sbostic struct info *ip;
16550008Sbostic {
16650008Sbostic
167*68388Sdab if (ip->off) {
168*68388Sdab int tmp = 0;
16950008Sbostic (void)ioctl(ip->fd, TIOCEXT, &tmp);
17050008Sbostic } else {
171*68388Sdab int tmp = 1;
17250008Sbostic (void)ioctl(ip->fd, TIOCEXT, &tmp);
17350008Sbostic }
174*68388Sdab ip->set = 1;
17550008Sbostic }
17650008Sbostic
17750008Sbostic void
f_ispeed(ip)17850008Sbostic f_ispeed(ip)
17950008Sbostic struct info *ip;
18050008Sbostic {
18166564Spendry
18250008Sbostic cfsetispeed(&ip->t, atoi(ip->arg));
18350008Sbostic ip->set = 1;
18450008Sbostic }
18550008Sbostic
18650008Sbostic void
f_nl(ip)18750008Sbostic f_nl(ip)
18850008Sbostic struct info *ip;
18950008Sbostic {
19066564Spendry
19150008Sbostic if (ip->off) {
19250008Sbostic ip->t.c_iflag |= ICRNL;
19350008Sbostic ip->t.c_oflag |= ONLCR;
19450008Sbostic } else {
19550008Sbostic ip->t.c_iflag &= ~ICRNL;
19650008Sbostic ip->t.c_oflag &= ~ONLCR;
19750008Sbostic }
19850008Sbostic ip->set = 1;
19950008Sbostic }
20050008Sbostic
20150008Sbostic void
f_ospeed(ip)20250008Sbostic f_ospeed(ip)
20350008Sbostic struct info *ip;
20450008Sbostic {
20566564Spendry
20650008Sbostic cfsetospeed(&ip->t, atoi(ip->arg));
20750008Sbostic ip->set = 1;
20850008Sbostic }
20950008Sbostic
21050008Sbostic void
f_raw(ip)21150008Sbostic f_raw(ip)
21250008Sbostic struct info *ip;
21350008Sbostic {
21466564Spendry
21550008Sbostic if (ip->off)
21650008Sbostic f_sane(ip);
21750008Sbostic else {
21850008Sbostic cfmakeraw(&ip->t);
21950008Sbostic ip->t.c_cflag &= ~(CSIZE|PARENB);
22050008Sbostic ip->t.c_cflag |= CS8;
22150008Sbostic ip->set = 1;
22250008Sbostic }
22350008Sbostic }
22450008Sbostic
22550008Sbostic void
f_rows(ip)22650008Sbostic f_rows(ip)
22750008Sbostic struct info *ip;
22850008Sbostic {
22966564Spendry
23050008Sbostic ip->win.ws_row = atoi(ip->arg);
23150008Sbostic ip->wset = 1;
23250008Sbostic }
23350008Sbostic
23450008Sbostic void
f_sane(ip)23550008Sbostic f_sane(ip)
23650008Sbostic struct info *ip;
23750008Sbostic {
23866564Spendry
23950008Sbostic ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL);
24050008Sbostic ip->t.c_iflag = TTYDEF_IFLAG;
24150008Sbostic ip->t.c_iflag |= ICRNL;
24250008Sbostic /* preserve user-preference flags in lflag */
24350008Sbostic #define LKEEP (ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
24450008Sbostic ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
24550008Sbostic ip->t.c_oflag = TTYDEF_OFLAG;
24650008Sbostic ip->set = 1;
24750008Sbostic }
24850008Sbostic
24950008Sbostic void
f_size(ip)25050008Sbostic f_size(ip)
25150008Sbostic struct info *ip;
25250008Sbostic {
25366564Spendry
25450008Sbostic (void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
25550008Sbostic }
25650008Sbostic
25750008Sbostic void
f_speed(ip)25850008Sbostic f_speed(ip)
25950008Sbostic struct info *ip;
26050008Sbostic {
26166564Spendry
26250008Sbostic (void)printf("%d\n", cfgetospeed(&ip->t));
26350008Sbostic }
26450008Sbostic
26550008Sbostic void
f_tty(ip)26650008Sbostic f_tty(ip)
26750008Sbostic struct info *ip;
26850008Sbostic {
26950008Sbostic int tmp;
27050008Sbostic
27150008Sbostic tmp = TTYDISC;
27250008Sbostic if (ioctl(0, TIOCSETD, &tmp) < 0)
27359520Sbostic err(1, "TIOCSETD");
27450008Sbostic }
275