xref: /csrg-svn/bin/stty/key.c (revision 60721)
150008Sbostic /*-
2*60721Sbostic  * Copyright (c) 1991, 1993
3*60721Sbostic  *	The Regents of the University of California.  All rights reserved.
450008Sbostic  *
550008Sbostic  * %sccs.include.redist.c%
650008Sbostic  */
750008Sbostic 
850008Sbostic #ifndef lint
9*60721Sbostic static char sccsid[] = "@(#)key.c	8.1 (Berkeley) 05/31/93";
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 
6959520Sbostic int
7050010Sbostic ksearch(argvp, ip)
7150010Sbostic 	char ***argvp;
7250010Sbostic 	struct info *ip;
7350008Sbostic {
7450010Sbostic 	register struct key *kp;
7550010Sbostic 	register char *name;
7650008Sbostic 	struct key tmp;
7750010Sbostic 	static int c_key __P((const void *, const void *));
7850008Sbostic 
7950010Sbostic 	name = **argvp;
8050010Sbostic 	if (*name == '-') {
8150010Sbostic 		ip->off = 1;
8250010Sbostic 		++name;
8350010Sbostic 	} else
8450010Sbostic 		ip->off = 0;
8550010Sbostic 
8650008Sbostic 	tmp.name = name;
8750010Sbostic 	if (!(kp = (struct key *)bsearch(&tmp, keys,
8850010Sbostic 	    sizeof(keys)/sizeof(struct key), sizeof(struct key), c_key)))
8959520Sbostic 		return (0);
9059520Sbostic 	if (!(kp->flags & F_OFFOK) && ip->off) {
9159520Sbostic 		errx(1, "illegal option -- %s", name);
9259520Sbostic 		usage();
9359520Sbostic 	}
9459520Sbostic 	if (kp->flags & F_NEEDARG && !(ip->arg = *++*argvp)) {
9559520Sbostic 		errx(1, "option requires an argument -- %s", name);
9659520Sbostic 		usage();
9759520Sbostic 	}
9850010Sbostic 	kp->f(ip);
9959520Sbostic 	return (1);
10050008Sbostic }
10150008Sbostic 
10259520Sbostic static int
10350008Sbostic c_key(a, b)
10450008Sbostic         const void *a, *b;
10550008Sbostic {
10659520Sbostic         return (strcmp(((struct key *)a)->name, ((struct key *)b)->name));
10750008Sbostic }
10850008Sbostic 
10950008Sbostic void
11050008Sbostic f_all(ip)
11150008Sbostic 	struct info *ip;
11250008Sbostic {
11350008Sbostic 	print(&ip->t, &ip->win, ip->ldisc, BSD);
11450008Sbostic }
11550008Sbostic 
11650008Sbostic void
11750008Sbostic f_cbreak(ip)
11850008Sbostic 	struct info *ip;
11950008Sbostic {
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 {
13550008Sbostic 	ip->win.ws_col = atoi(ip->arg);
13650008Sbostic 	ip->wset = 1;
13750008Sbostic }
13850008Sbostic 
13950008Sbostic void
14050008Sbostic f_dec(ip)
14150008Sbostic 	struct info *ip;
14250008Sbostic {
14350008Sbostic 	ip->t.c_cc[VERASE] = (u_char)0177;
14450008Sbostic 	ip->t.c_cc[VKILL] = CTRL('u');
14550008Sbostic 	ip->t.c_cc[VINTR] = CTRL('c');
14650008Sbostic 	ip->t.c_lflag &= ~ECHOPRT;
14750008Sbostic 	ip->t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
14850008Sbostic 	ip->t.c_iflag &= ~IXANY;
14950008Sbostic 	ip->set = 1;
15050008Sbostic }
15150008Sbostic 
15250008Sbostic void
15350008Sbostic f_everything(ip)
15450008Sbostic 	struct info *ip;
15550008Sbostic {
15650008Sbostic 	print(&ip->t, &ip->win, ip->ldisc, BSD);
15750008Sbostic }
15850008Sbostic 
15950008Sbostic void
16050008Sbostic f_extproc(ip)
16150008Sbostic 	struct info *ip;
16250008Sbostic {
16350008Sbostic 	int tmp;
16450008Sbostic 
16550008Sbostic 	if (ip->set) {
16650008Sbostic 		tmp = 1;
16750008Sbostic 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
16850008Sbostic 	} else {
16950008Sbostic 		tmp = 0;
17050008Sbostic 		(void)ioctl(ip->fd, TIOCEXT, &tmp);
17150008Sbostic 	}
17250008Sbostic }
17350008Sbostic 
17450008Sbostic void
17550008Sbostic f_ispeed(ip)
17650008Sbostic 	struct info *ip;
17750008Sbostic {
17850008Sbostic 	cfsetispeed(&ip->t, atoi(ip->arg));
17950008Sbostic 	ip->set = 1;
18050008Sbostic }
18150008Sbostic 
18250008Sbostic void
18350008Sbostic f_nl(ip)
18450008Sbostic 	struct info *ip;
18550008Sbostic {
18650008Sbostic 	if (ip->off) {
18750008Sbostic 		ip->t.c_iflag |= ICRNL;
18850008Sbostic 		ip->t.c_oflag |= ONLCR;
18950008Sbostic 	} else {
19050008Sbostic 		ip->t.c_iflag &= ~ICRNL;
19150008Sbostic 		ip->t.c_oflag &= ~ONLCR;
19250008Sbostic 	}
19350008Sbostic 	ip->set = 1;
19450008Sbostic }
19550008Sbostic 
19650008Sbostic void
19750008Sbostic f_ospeed(ip)
19850008Sbostic 	struct info *ip;
19950008Sbostic {
20050008Sbostic 	cfsetospeed(&ip->t, atoi(ip->arg));
20150008Sbostic 	ip->set = 1;
20250008Sbostic }
20350008Sbostic 
20450008Sbostic void
20550008Sbostic f_raw(ip)
20650008Sbostic 	struct info *ip;
20750008Sbostic {
20850008Sbostic 	if (ip->off)
20950008Sbostic 		f_sane(ip);
21050008Sbostic 	else {
21150008Sbostic 		cfmakeraw(&ip->t);
21250008Sbostic 		ip->t.c_cflag &= ~(CSIZE|PARENB);
21350008Sbostic 		ip->t.c_cflag |= CS8;
21450008Sbostic 		ip->set = 1;
21550008Sbostic 	}
21650008Sbostic }
21750008Sbostic 
21850008Sbostic void
21950008Sbostic f_rows(ip)
22050008Sbostic 	struct info *ip;
22150008Sbostic {
22250008Sbostic 	ip->win.ws_row = atoi(ip->arg);
22350008Sbostic 	ip->wset = 1;
22450008Sbostic }
22550008Sbostic 
22650008Sbostic void
22750008Sbostic f_sane(ip)
22850008Sbostic 	struct info *ip;
22950008Sbostic {
23050008Sbostic 	ip->t.c_cflag = TTYDEF_CFLAG | (ip->t.c_cflag & CLOCAL);
23150008Sbostic 	ip->t.c_iflag = TTYDEF_IFLAG;
23250008Sbostic 	ip->t.c_iflag |= ICRNL;
23350008Sbostic 	/* preserve user-preference flags in lflag */
23450008Sbostic #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
23550008Sbostic 	ip->t.c_lflag = TTYDEF_LFLAG | (ip->t.c_lflag & LKEEP);
23650008Sbostic 	ip->t.c_oflag = TTYDEF_OFLAG;
23750008Sbostic 	ip->set = 1;
23850008Sbostic }
23950008Sbostic 
24050008Sbostic void
24150008Sbostic f_size(ip)
24250008Sbostic 	struct info *ip;
24350008Sbostic {
24450008Sbostic 	(void)printf("%d %d\n", ip->win.ws_row, ip->win.ws_col);
24550008Sbostic }
24650008Sbostic 
24750008Sbostic void
24850008Sbostic f_speed(ip)
24950008Sbostic 	struct info *ip;
25050008Sbostic {
25150008Sbostic 	(void)printf("%d\n", cfgetospeed(&ip->t));
25250008Sbostic }
25350008Sbostic 
25450008Sbostic void
25550008Sbostic f_tty(ip)
25650008Sbostic 	struct info *ip;
25750008Sbostic {
25850008Sbostic 	int tmp;
25950008Sbostic 
26050008Sbostic 	tmp = TTYDISC;
26150008Sbostic 	if (ioctl(0, TIOCSETD, &tmp) < 0)
26259520Sbostic 		err(1, "TIOCSETD");
26350008Sbostic }
264