xref: /csrg-svn/bin/stty/key.c (revision 66564)
150008Sbostic /*-
260721Sbostic  * Copyright (c) 1991, 1993
360721Sbostic  *	The Regents of the University of California.  All rights reserved.
450008Sbostic  *
550008Sbostic  * %sccs.include.redist.c%
650008Sbostic  */
750008Sbostic 
850008Sbostic #ifndef lint
9*66564Spendry static char sccsid[] = "@(#)key.c	8.2 (Berkeley) 04/01/94";
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 
69*66564Spendry static int
70*66564Spendry c_key(a, b)
71*66564Spendry         const void *a, *b;
72*66564Spendry {
73*66564Spendry 
74*66564Spendry         return (strcmp(((struct key *)a)->name, ((struct key *)b)->name));
75*66564Spendry }
76*66564Spendry 
7759520Sbostic int
7850010Sbostic ksearch(argvp, ip)
7950010Sbostic 	char ***argvp;
8050010Sbostic 	struct info *ip;
8150008Sbostic {
82*66564Spendry 	char *name;
83*66564Spendry 	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
10950008Sbostic f_all(ip)
11050008Sbostic 	struct info *ip;
11150008Sbostic {
11250008Sbostic 	print(&ip->t, &ip->win, ip->ldisc, BSD);
11350008Sbostic }
11450008Sbostic 
11550008Sbostic void
11650008Sbostic f_cbreak(ip)
11750008Sbostic 	struct info *ip;
11850008Sbostic {
119*66564Spendry 
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
13250008Sbostic f_columns(ip)
13350008Sbostic 	struct info *ip;
13450008Sbostic {
135*66564Spendry 
13650008Sbostic 	ip->win.ws_col = atoi(ip->arg);
13750008Sbostic 	ip->wset = 1;
13850008Sbostic }
13950008Sbostic 
14050008Sbostic void
14150008Sbostic f_dec(ip)
14250008Sbostic 	struct info *ip;
14350008Sbostic {
144*66564Spendry 
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
15550008Sbostic f_everything(ip)
15650008Sbostic 	struct info *ip;
15750008Sbostic {
158*66564Spendry 
15950008Sbostic 	print(&ip->t, &ip->win, ip->ldisc, BSD);
16050008Sbostic }
16150008Sbostic 
16250008Sbostic void
16350008Sbostic f_extproc(ip)
16450008Sbostic 	struct info *ip;
16550008Sbostic {
16650008Sbostic 
16750008Sbostic 	if (ip->set) {
168*66564Spendry 		int tmp = 1;
16950008Sbostic 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
17050008Sbostic 	} else {
171*66564Spendry 		int tmp = 0;
17250008Sbostic 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
17350008Sbostic 	}
17450008Sbostic }
17550008Sbostic 
17650008Sbostic void
17750008Sbostic f_ispeed(ip)
17850008Sbostic 	struct info *ip;
17950008Sbostic {
180*66564Spendry 
18150008Sbostic 	cfsetispeed(&ip->t, atoi(ip->arg));
18250008Sbostic 	ip->set = 1;
18350008Sbostic }
18450008Sbostic 
18550008Sbostic void
18650008Sbostic f_nl(ip)
18750008Sbostic 	struct info *ip;
18850008Sbostic {
189*66564Spendry 
19050008Sbostic 	if (ip->off) {
19150008Sbostic 		ip->t.c_iflag |= ICRNL;
19250008Sbostic 		ip->t.c_oflag |= ONLCR;
19350008Sbostic 	} else {
19450008Sbostic 		ip->t.c_iflag &= ~ICRNL;
19550008Sbostic 		ip->t.c_oflag &= ~ONLCR;
19650008Sbostic 	}
19750008Sbostic 	ip->set = 1;
19850008Sbostic }
19950008Sbostic 
20050008Sbostic void
20150008Sbostic f_ospeed(ip)
20250008Sbostic 	struct info *ip;
20350008Sbostic {
204*66564Spendry 
20550008Sbostic 	cfsetospeed(&ip->t, atoi(ip->arg));
20650008Sbostic 	ip->set = 1;
20750008Sbostic }
20850008Sbostic 
20950008Sbostic void
21050008Sbostic f_raw(ip)
21150008Sbostic 	struct info *ip;
21250008Sbostic {
213*66564Spendry 
21450008Sbostic 	if (ip->off)
21550008Sbostic 		f_sane(ip);
21650008Sbostic 	else {
21750008Sbostic 		cfmakeraw(&ip->t);
21850008Sbostic 		ip->t.c_cflag &= ~(CSIZE|PARENB);
21950008Sbostic 		ip->t.c_cflag |= CS8;
22050008Sbostic 		ip->set = 1;
22150008Sbostic 	}
22250008Sbostic }
22350008Sbostic 
22450008Sbostic void
22550008Sbostic f_rows(ip)
22650008Sbostic 	struct info *ip;
22750008Sbostic {
228*66564Spendry 
22950008Sbostic 	ip->win.ws_row = atoi(ip->arg);
23050008Sbostic 	ip->wset = 1;
23150008Sbostic }
23250008Sbostic 
23350008Sbostic void
23450008Sbostic f_sane(ip)
23550008Sbostic 	struct info *ip;
23650008Sbostic {
237*66564Spendry 
23850008Sbostic 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL);
23950008Sbostic 	ip->t.c_iflag = TTYDEF_IFLAG;
24050008Sbostic 	ip->t.c_iflag |= ICRNL;
24150008Sbostic 	/* preserve user-preference flags in lflag */
24250008Sbostic #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
24350008Sbostic 	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
24450008Sbostic 	ip->t.c_oflag = TTYDEF_OFLAG;
24550008Sbostic 	ip->set = 1;
24650008Sbostic }
24750008Sbostic 
24850008Sbostic void
24950008Sbostic f_size(ip)
25050008Sbostic 	struct info *ip;
25150008Sbostic {
252*66564Spendry 
25350008Sbostic 	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
25450008Sbostic }
25550008Sbostic 
25650008Sbostic void
25750008Sbostic f_speed(ip)
25850008Sbostic 	struct info *ip;
25950008Sbostic {
260*66564Spendry 
26150008Sbostic 	(void)printf("%d\n", cfgetospeed(&ip->t));
26250008Sbostic }
26350008Sbostic 
26450008Sbostic void
26550008Sbostic f_tty(ip)
26650008Sbostic 	struct info *ip;
26750008Sbostic {
26850008Sbostic 	int tmp;
26950008Sbostic 
27050008Sbostic 	tmp = TTYDISC;
27150008Sbostic 	if (ioctl(0, TIOCSETD, &tmp) < 0)
27259520Sbostic 		err(1, "TIOCSETD");
27350008Sbostic }
274