xref: /csrg-svn/bin/stty/stty.c (revision 43100)
119907Sdist /*
240169Skarels  * Copyright (c) 1980, 1989 Regents of the University of California.
319907Sdist  * All rights reserved.  The Berkeley software License Agreement
419907Sdist  * specifies the terms and conditions for redistribution.
519907Sdist  */
619907Sdist 
713073Ssam #ifndef lint
819907Sdist char copyright[] =
940169Skarels "@(#) Copyright (c) 1980, 1989 Regents of the University of California.\n\
1019907Sdist  All rights reserved.\n";
1119907Sdist #endif not lint
1219907Sdist 
1319907Sdist #ifndef lint
14*43100Smarc static char sccsid[] = "@(#)stty.c	5.13 (Berkeley) 06/15/90";
1519907Sdist #endif not lint
1619907Sdist 
171179Sbill /*
181179Sbill  * set teletype modes
191179Sbill  */
201179Sbill 
2138674Smarc #include <sys/types.h>
2238674Smarc #include <sys/stat.h>
2337609Sbostic #include <sys/ioctl.h>
2438674Smarc #include <sys/syslog.h>
2538674Smarc #define KERNEL
2638674Smarc #include <sys/tty.h>
2738674Smarc #undef KERNEL
2838674Smarc #include <sys/termios.h>
2938674Smarc #include <sys/file.h>
3038674Smarc #include <errno.h>
3138674Smarc #include <ctype.h>
321179Sbill #include <stdio.h>
331179Sbill 
3440170Smarc #define eq(s1, s2)	(strcmp((s1), (s2)) == 0)
3538674Smarc #define WRAPCOL 65
3638674Smarc 
3740170Smarc struct modes {
3838674Smarc 	char *name;
3938674Smarc 	long set;
4038674Smarc 	long unset;
411179Sbill };
421179Sbill 
4340170Smarc struct modes imodes[] = {
4438674Smarc 	"ignbrk",	IGNBRK, 0,
4538674Smarc 	"-ignbrk",	0, IGNBRK,
4638674Smarc 	"brkint",	BRKINT, 0,
4738674Smarc 	"-brkint",	0, BRKINT,
4838674Smarc 	"ignpar",	IGNPAR, 0,
4938674Smarc 	"-ignpar",	0, IGNPAR,
5038674Smarc 	"parmrk",	PARMRK, 0,
5138674Smarc 	"-parmrk",	0, PARMRK,
5238674Smarc 	"inpck",	INPCK, 0,
5338674Smarc 	"-inpck",	0, INPCK,
5438674Smarc 	"istrip",	ISTRIP, 0,
5538674Smarc 	"-istrip",	0, ISTRIP,
5638674Smarc 	"inlcr",	INLCR, 0,
5738674Smarc 	"-inlcr",	0, INLCR,
5838674Smarc 	"igncr",	IGNCR, 0,
5938674Smarc 	"-igncr",	0, IGNCR,
6038674Smarc 	"icrnl",	ICRNL, 0,
6138674Smarc 	"-icrnl",	0, ICRNL,
6238674Smarc 	"ixon",		IXON, 0,
6338674Smarc 	"-ixon",	0, IXON,
6438674Smarc 	"flow",		IXON, 0,
6538674Smarc 	"-flow",	0, IXON,
6638674Smarc 	"ixoff",	IXOFF, 0,
6738674Smarc 	"-ixoff",	0, IXOFF,
6838674Smarc 	"tandem",	IXOFF, 0,
6938674Smarc 	"-tandem",	0, IXOFF,
7038674Smarc 	"ixany",	IXANY, 0,
7138674Smarc 	"-ixany",	0, IXANY,
7238674Smarc 	"decctlq",	0, IXANY,
7338674Smarc 	"-decctlq",	IXANY, 0,
7438674Smarc 	"imaxbel",	IMAXBEL, 0,
7538674Smarc 	"-imaxbel",	0, IMAXBEL,
7638674Smarc 	0
7738674Smarc };
781179Sbill 
7940170Smarc struct modes omodes[] = {
8038674Smarc 	"opost",	OPOST, 0,
8138674Smarc 	"-opost",	0, OPOST,
8238674Smarc 	"-litout",	OPOST, 0,
8338674Smarc 	"litout",	0, OPOST,
8438674Smarc 	"onlcr",	ONLCR, 0,
8538674Smarc 	"-onlcr",	0, ONLCR,
8638674Smarc 	"tabs",		0, OXTABS,	/* "preserve" tabs */
8738674Smarc 	"-tabs",	OXTABS, 0,
8840210Smarc 	"xtabs",	OXTABS, 0,
8940210Smarc 	"-xtabs",	0, OXTABS,
9038674Smarc 	"oxtabs",	OXTABS, 0,
9138674Smarc 	"-oxtabs",	0, OXTABS,
921179Sbill 	0
931179Sbill };
941179Sbill 
9540170Smarc struct modes cmodes[] = {
9638674Smarc 	"cs5",		CS5, CSIZE,
9738674Smarc 	"cs6",		CS6, CSIZE,
9838674Smarc 	"cs7",		CS7, CSIZE,
9938674Smarc 	"cs8",		CS8, CSIZE,
10038674Smarc 	"cstopb",	CSTOPB, 0,
10138674Smarc 	"-cstopb",	0, CSTOPB,
10238674Smarc 	"cread",	CREAD, 0,
10338674Smarc 	"-cread",	0, CREAD,
10438674Smarc 	"parenb",	PARENB, 0,
10538674Smarc 	"-parenb",	0, PARENB,
10638674Smarc 	"parodd",	PARODD, 0,
10738674Smarc 	"-parodd",	0, PARODD,
10838674Smarc 	"parity",	PARENB | CS7, PARODD | CSIZE,
10938674Smarc 	"evenp",	PARENB | CS7, PARODD | CSIZE,
11038674Smarc 	"oddp",		PARENB | CS7 | PARODD, CSIZE,
11138674Smarc 	"-parity",	CS8, PARODD | PARENB | CSIZE,
11240169Skarels 	"pass8",	CS8, PARODD | PARENB | CSIZE,
11338674Smarc 	"-evenp",	CS8, PARODD | PARENB | CSIZE,
11438674Smarc 	"-oddp",	CS8, PARODD | PARENB | CSIZE,
11538674Smarc 	"hupcl",	HUPCL, 0,
11638674Smarc 	"-hupcl",	0, HUPCL,
11738674Smarc 	"hup",		HUPCL, 0,
11838674Smarc 	"-hup",		0, HUPCL,
11938674Smarc 	"clocal",	CLOCAL, 0,
12038674Smarc 	"-clocal",	0, CLOCAL,
12138674Smarc 	"crtscts",	CRTSCTS, 0,
12238674Smarc 	"-crtscts",	0, CRTSCTS,
12338674Smarc 	0
12438674Smarc };
12538674Smarc 
12640170Smarc struct modes lmodes[] = {
12738674Smarc 	"echo",		ECHO, 0,
12838674Smarc 	"-echo",	0, ECHO,
12938674Smarc 	"echoe",	ECHOE, 0,
13038674Smarc 	"-echoe",	0, ECHOE,
13138674Smarc 	"crterase",	ECHOE, 0,
13238674Smarc 	"-crterase",	0, ECHOE,
13338674Smarc 	"crtbs",	ECHOE, 0,   /* crtbs not supported, close enough */
13438674Smarc 	"-crtbs",	0, ECHOE,
13538674Smarc 	"echok",	ECHOK, 0,
13638674Smarc 	"-echok",	0, ECHOK,
13738674Smarc 	"echoke",	ECHOKE, 0,
13838674Smarc 	"-echoke",	0, ECHOKE,
13938674Smarc 	"crtkill",	ECHOKE, 0,
14038674Smarc 	"-crtkill",	0, ECHOKE,
14138674Smarc 	"altwerase",	ALTWERASE, 0,
14238674Smarc 	"-altwerase",	0, ALTWERASE,
14338674Smarc 	"iexten",	IEXTEN, 0,
14438674Smarc 	"-iexten",	0, IEXTEN,
14538674Smarc 	"echonl",	ECHONL, 0,
14638674Smarc 	"-echonl",	0, ECHONL,
14738674Smarc 	"echoctl",	ECHOCTL, 0,
14838674Smarc 	"-echoctl",	0, ECHOCTL,
14938674Smarc 	"ctlecho",	ECHOCTL, 0,
15038674Smarc 	"-ctlecho",	0, ECHOCTL,
15138674Smarc 	"echoprt",	ECHOPRT, 0,
15238674Smarc 	"-echoprt",	0, ECHOPRT,
15338674Smarc 	"prterase",	ECHOPRT, 0,
15438674Smarc 	"-prterase",	0, ECHOPRT,
15538674Smarc 	"isig",		ISIG, 0,
15638674Smarc 	"-isig",	0, ISIG,
15738674Smarc 	"icanon",	ICANON, 0,
15838674Smarc 	"-icanon",	0, ICANON,
15938674Smarc 	"noflsh",	NOFLSH, 0,
16038674Smarc 	"-noflsh",	0, NOFLSH,
16138674Smarc 	"tostop",	TOSTOP, 0,
16238674Smarc 	"-tostop",	0, TOSTOP,
16338674Smarc 	"mdmbuf",	MDMBUF, 0,
16438674Smarc 	"-mdmbuf",	0, MDMBUF,
16538674Smarc 	"flusho",	FLUSHO, 0,
16638674Smarc 	"-flusho",	0, FLUSHO,
16738674Smarc 	"pendin",	PENDIN, 0,
16838674Smarc 	"-pendin",	0, PENDIN,
16938674Smarc 	"crt",		ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT,
17038674Smarc 	"-crt",		ECHOK, ECHOE|ECHOKE|ECHOCTL,
17138674Smarc 	"newcrt",	ECHOE|ECHOKE|ECHOCTL, ECHOK|ECHOPRT,
17238674Smarc 	"-newcrt",	ECHOK, ECHOE|ECHOKE|ECHOCTL,
17338674Smarc 	0
17438674Smarc };
17538674Smarc 
17638674Smarc /*
17738674Smarc  * Special control characters.
17838674Smarc  *
17938674Smarc  * Each entry has a list of names.  The first is the primary name
18038674Smarc  * and is used when printing the control character in the "name = val;"
18138674Smarc  * form.  The second is an abbreviation which is guaranteed to be less
18238674Smarc  * than or equal to four characters in length and is primarily used
18338674Smarc  * when printing the values in columunar form (guarantees all will
18438674Smarc  * fit within 80 cols).  The rest are optional aliases.
18538674Smarc  * All names are recognized on the command line.
18638674Smarc  */
18740170Smarc #define MAXNAMES 3
18840170Smarc struct {
18940210Smarc 	char	*names[MAXNAMES+1];
19038674Smarc 	int	sub;
19138674Smarc 	u_char	def;
19238674Smarc } cchars[] = {
19343098Sbostic 	{{ "erase", "era" },		VERASE,	CERASE, },
19443098Sbostic 	{{ "werase", "wera" },		VWERASE, CWERASE, },
19543098Sbostic 	{{ "kill", "kill" },		VKILL,	CKILL, },
19643098Sbostic 	{{ "intr", "int" },		VINTR,	CINTR, },
19743098Sbostic 	{{ "quit", "quit" },		VQUIT,	CQUIT, },
19843098Sbostic 	{{ "susp", "susp" },		VSUSP,	CSUSP, },
19943098Sbostic 	{{ "dsusp", "dsus" },		VDSUSP,	CDSUSP, },
20043098Sbostic 	{{ "eof", "eof" },		VEOF,	CEOF, },
20143098Sbostic 	{{ "eol", "eol", "brk" },	VEOL,	CEOL, },
20243098Sbostic 	{{ "eol2", "eol2" },		VEOL2,	CEOL, },
20343098Sbostic 	{{ "stop", "stop", "xoff" },	VSTOP,	CSTOP, },
20443098Sbostic 	{{ "start", "star", "xon" },	VSTART,	CSTART, },
20543098Sbostic 	{{ "lnext", "lnxt" },		VLNEXT,	CLNEXT, },
206*43100Smarc 	{{ "discard", "disc", },	VDISCARD, CDISCARD, },
20743098Sbostic 	{{ "reprint", "rpnt", "rprnt" },	VREPRINT, CREPRINT, },
20843098Sbostic 	{{ "info", "info" },		VINFO, CINFO, },
20938674Smarc 	0
21038674Smarc };
21143098Sbostic 
21240170Smarc struct winsize win;
21340170Smarc int ldisc;
21440170Smarc int dodisc;
21540170Smarc int debug = 0;
21640170Smarc int trace, dotrace;
21738674Smarc 
21838674Smarc #define OUT	stdout		/* informational output stream */
21938674Smarc #define ERR	stderr		/* error message stream */
22038674Smarc #define CTL	0		/* default control descriptor */
22140170Smarc int ctl = CTL;
22238674Smarc 
22338674Smarc extern errno;
22438674Smarc 
22538674Smarc #define NORMAL	0	/* only print modes differing from defaults */
22638674Smarc #define ALL	1	/* print all modes - POSIX standard format */
22738674Smarc #define ALL_BSD	2	/* print all modes - using BSD shorthand for cc's */
22838674Smarc #define GFMT	3	/* print modes in a form that can be re-input to stty */
22938674Smarc 
23040170Smarc 
23138674Smarc main(argc, argv)
23238674Smarc 	char *argv[];
2331179Sbill {
23438674Smarc 	struct termios t;
23538674Smarc 	int i, fmt = NORMAL;
23638674Smarc 	extern char *optarg;
23738674Smarc 	extern int optind;
23838674Smarc 	int ch;
2391179Sbill 
24038674Smarc 	argc--, argv++;
24138674Smarc 	if (argc > 0 && eq(argv[0], "-a")) {
24238674Smarc 		fmt = ALL;
24338674Smarc 		argc--, argv++;
2441179Sbill 	}
24538674Smarc 	if (argc > 0 && eq(argv[0], "-f")) {
24638674Smarc 		argc--, argv++;
24738674Smarc 		if ((ctl = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
24838674Smarc 			syserrexit(*argv);
24938674Smarc 		argc--, argv++;
2501179Sbill 	}
25138674Smarc 	if (ioctl(ctl, TIOCGETD, &ldisc) < 0)
25238674Smarc 		syserrexit("TIOCGETD");
25338674Smarc 	if (tcgetattr(ctl, &t) < 0)
25438674Smarc 		syserrexit("tcgetattr");
25538674Smarc 	if (ioctl(ctl, TIOCGWINSZ, &win) < 0)
25640170Smarc 		warning("TIOCGWINSZ: %s", strerror(errno));
25738674Smarc 	checkredirect();	/* conversion aid */
25838674Smarc 
25938674Smarc 	if (argc == 0 || fmt) {
26038674Smarc 		prmode(&t, ldisc, fmt);
2611179Sbill 		exit(0);
26238674Smarc 	} else if (argc == 1 && strlen(argv[0]) > 2 && *(argv[0]+2) == ':') {
26338674Smarc 		gfmtset(argv[0]);
26438674Smarc 		goto setit;
2651179Sbill 	}
26638674Smarc 
26738674Smarc 	while (*argv) {
26838674Smarc 		if (eq("everything", *argv)) {
26938674Smarc 			prmode(&t, ldisc, ALL_BSD);
27038674Smarc 			exit(0);
2711179Sbill 		}
27238674Smarc 		if (eq("all", *argv)) {
27338674Smarc 			prmode(&t, ldisc, ALL);
27438674Smarc 			exit(0);
2751179Sbill 		}
27638674Smarc 		if (eq("old", *argv)) {
27738674Smarc 			goto next;
2781179Sbill 		}
27938674Smarc 		if (eq("new", *argv)) {
28038674Smarc 			goto next;
2811179Sbill 		}
28238674Smarc 		if (eq("nl", *argv)) {
28338674Smarc 			t.c_iflag &= ~ICRNL;
28438674Smarc 			t.c_oflag &= ~ONLCR;
28538674Smarc 			goto next;
2861179Sbill 		}
28738674Smarc 		if (eq("-nl", *argv)) {
28838674Smarc 			t.c_iflag |= ICRNL;
28938674Smarc 			t.c_oflag |= ONLCR;
29038674Smarc 			goto next;
2913797Sroot 		}
29238674Smarc 		if (eq("dec", *argv)){
29338674Smarc 			t.c_cc[VERASE] = (u_char)0177;
29438674Smarc 			t.c_cc[VKILL] = CTRL('u');
29538674Smarc 			t.c_cc[VINTR] = CTRL('c');
29638674Smarc 			t.c_lflag &= ~ECHOPRT;
29738674Smarc 			t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
29838674Smarc 			t.c_iflag &= ~IXANY;
29938674Smarc 			goto next;
3001179Sbill 		}
30140169Skarels 		if (eq("raw", *argv)) {
30240169Skarels 			cfmakeraw(&t);
30340169Skarels 			t.c_cflag &= ~(CSIZE|PARENB);
30440169Skarels 			t.c_cflag |= CS8;
30540169Skarels 			goto next;
30640169Skarels 		}
30740169Skarels 		if (eq("cooked", *argv) || eq("-raw", *argv) ||
30840169Skarels 		    eq("sane", *argv)) {
30940169Skarels 			t.c_cflag = TTYDEF_CFLAG | (t.c_cflag & CLOCAL);
31040169Skarels 			t.c_iflag = TTYDEF_IFLAG;
31140169Skarels 			t.c_iflag |= ICRNL;
31240169Skarels 			/* preserve user-preference flags in lflag */
31340169Skarels #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
31440169Skarels 			t.c_lflag = TTYDEF_LFLAG | (t.c_lflag & LKEEP);
31540169Skarels 			t.c_oflag = TTYDEF_OFLAG;
31640169Skarels 			goto next;
31740169Skarels 		}
31838674Smarc 		if (eq("rows", *argv)) {
31938674Smarc 			if (*(argv+1) == 0)
32038674Smarc 				goto setit;
32118023Sbloom 			win.ws_row = atoi(*++argv);
32238674Smarc 			goto next;
32318023Sbloom 		}
32438674Smarc 		if (eq("ispeed", *argv)) {
32538674Smarc 			int code;
32638674Smarc 			if (*(argv+1) == 0)
32738674Smarc 				errexit("missing ispeed");
32838674Smarc 			cfsetispeed(&t, atoi(*++argv));
32938674Smarc 			goto next;
33038674Smarc 		}
33138674Smarc 		if (eq("ospeed", *argv)) {
33238674Smarc 			if (*(argv+1) == 0)
33338674Smarc 				errexit("missing ospeed");
33438674Smarc 			cfsetospeed(&t, atoi(*++argv));
33538674Smarc 			goto next;
33638674Smarc 		}
33738674Smarc 		if (eq("cols", *argv) || eq("columns", *argv)) {
33838674Smarc 			if (*(argv+1) == 0)
33938674Smarc 				goto setit;
34018023Sbloom 			win.ws_col = atoi(*++argv);
34138674Smarc 			goto next;
34218023Sbloom 		}
34338674Smarc 		if (eq("size", *argv)) {
34438674Smarc 			put("%d %d\n", win.ws_row, win.ws_col);
34525791Skarels 			exit(0);
34625791Skarels 		}
34738674Smarc 		if (eq("speed", *argv)) {
34838674Smarc 			put("%d\n", cfgetospeed(&t));
34938674Smarc 			exit(0);
35038674Smarc 		}
35138674Smarc 		for (i=0; imodes[i].name; i++)
35238674Smarc 			if (eq(imodes[i].name, *argv)) {
35338674Smarc 				t.c_iflag &= ~imodes[i].unset;
35438674Smarc 				t.c_iflag |= imodes[i].set;
35538674Smarc 				goto next;
3561179Sbill 			}
35738674Smarc 		for (i=0; omodes[i].name; i++)
35838674Smarc 			if (eq(omodes[i].name, *argv)) {
35938674Smarc 				t.c_oflag &= ~omodes[i].unset;
36038674Smarc 				t.c_oflag |= omodes[i].set;
36138674Smarc 				goto next;
36238674Smarc 			}
36338674Smarc 		for (i=0; cmodes[i].name; i++)
36438674Smarc 			if (eq(cmodes[i].name, *argv)) {
36538674Smarc 				t.c_cflag &= ~cmodes[i].unset;
36638674Smarc 				t.c_cflag |= cmodes[i].set;
36738674Smarc 				goto next;
36838674Smarc 			}
36938674Smarc 		for (i=0; lmodes[i].name; i++)
37038674Smarc 			if (eq(lmodes[i].name, *argv)) {
37138674Smarc 				t.c_lflag &= ~lmodes[i].unset;
37238674Smarc 				t.c_lflag |= lmodes[i].set;
37338674Smarc 				goto next;
37438674Smarc 			}
37538674Smarc 		for (i=0; *cchars[i].names; i++) {
37638674Smarc 			char **cp = cchars[i].names;
37738674Smarc 			while (*cp) {
37838674Smarc 				if (eq(*cp, *argv)) {
37938674Smarc 					if (*++argv == 0)
38038674Smarc 						goto setit;
38138674Smarc 					if (eq(*argv, "undef") ||
38238674Smarc 					    eq(*argv, "disable"))
38338674Smarc 						t.c_cc[cchars[i].sub] =
38438674Smarc 						   _POSIX_VDISABLE;
38538674Smarc 					else if (**argv == '^')
38638674Smarc 						t.c_cc[cchars[i].sub] =
38738674Smarc 						    ((*argv)[1] == '?') ? 0177 :
38838674Smarc 						    ((*argv)[1] == '-') ?
38938674Smarc 						     _POSIX_VDISABLE :
39038674Smarc 						     (*argv)[1] & 037;
39138674Smarc 					else
39238674Smarc 						t.c_cc[cchars[i].sub] = **argv;
39338674Smarc 					goto next;
3941179Sbill 				}
39538674Smarc 				cp++;
39638674Smarc 			}
3971179Sbill 		}
39838674Smarc 		if (isdigit(**argv)) {
39938674Smarc 			cfsetospeed(&t, atoi(*argv));
40038674Smarc 			cfsetispeed(&t, atoi(*argv));
40138674Smarc 			goto next;
40238674Smarc 		}
40338674Smarc 		/* didn't match anything */
40438674Smarc 		errexit("unknown option: %s", *argv);
40538674Smarc 		exit(1);
40638674Smarc next:
40738674Smarc 		argv++;
4081179Sbill 	}
40938674Smarc setit:
41038674Smarc 	if (tcsetattr(ctl, 0, &t) < 0)
41138674Smarc 		syserrexit("tcsetattr");
41238674Smarc 	if (ioctl(ctl, TIOCSWINSZ, &win) < 0)
41338674Smarc 		warning("can't set window size");
41438674Smarc 
41538674Smarc 	exit(0);
4161179Sbill }
4171179Sbill 
41838674Smarc gfmtset() {
4191179Sbill }
4201179Sbill 
42138674Smarc prmode(tp, ldisc, fmt)
42238674Smarc 	struct termios *tp;
4231179Sbill {
42438674Smarc 	long	i = tp->c_iflag,
42538674Smarc 		o = tp->c_oflag,
42638674Smarc 		c = tp->c_cflag,
42738674Smarc 		l = tp->c_lflag;
42838674Smarc 	u_char	*cc = tp->c_cc;
42938674Smarc 	int	ispeed = cfgetispeed(tp),
43038674Smarc 		ospeed = cfgetospeed(tp);
43138674Smarc 	char	unknown[32],
43238674Smarc 		*ld;
43338674Smarc 	char *ccval();
43438674Smarc 
4351179Sbill 
43638674Smarc 	/*
43738674Smarc 	 * line discipline
43838674Smarc 	 */
43938674Smarc 	if (ldisc != TTYDISC) {
44038674Smarc 		switch(ldisc) {
44138674Smarc 		case TABLDISC:
44238674Smarc 			ld = "tablet";
4431179Sbill 			break;
44438674Smarc 		case SLIPDISC:
44540170Smarc 			ld = "slip";
4461179Sbill 			break;
44738674Smarc 		default:
44838674Smarc 			sprintf(unknown, "#%d", ldisc);
44938674Smarc 			ld = unknown;
4501179Sbill 		}
45138674Smarc 		put("%s disc; ", ld);
45238674Smarc 	}
45338674Smarc 	/*
45438674Smarc 	 * line speed
45538674Smarc 	 */
45638674Smarc 	if (ispeed != ospeed)
45738674Smarc 		put("ispeed %d baud; ospeed %d baud;",
45838674Smarc 		     ispeed, ospeed);
45938674Smarc 	else
46038674Smarc 		put("speed %d baud;", ispeed);
46138674Smarc 	if (fmt)
46238674Smarc 		put(" %d rows; %d columns;", win.ws_row, win.ws_col);
46338674Smarc 	put("\n");
46413817Ssam 
46538674Smarc #define lput(n, f, d) if (fmt || on(f) != d) mdput(n+on(f))
46638674Smarc 	/*
46738674Smarc 	 * "local" flags
46838674Smarc 	 */
46938674Smarc #define on(f)	((l&f) != 0)
47038674Smarc 	if (debug) mdput("LFLAG: ");
47138674Smarc 	lput("-icanon ",ICANON, 1);
47238674Smarc 	lput("-isig ", ISIG, 1);
47338674Smarc 	lput("-iexten ", IEXTEN, 1);
47438674Smarc 	lput("-echo ",ECHO, 1);
47538674Smarc 	lput("-echoe ",ECHOE, 0);
47638674Smarc 	lput("-echok ",ECHOK, 0);
47738674Smarc 	lput("-echoke ",ECHOKE, 0);
47838674Smarc 	lput("-echonl ",ECHONL, 0);
47938674Smarc 	lput("-echoctl ",ECHOCTL, 0);
48038674Smarc 	lput("-echoprt ",ECHOPRT, 0);
48138674Smarc 	lput("-altwerase ",ALTWERASE, 0);
48238674Smarc 	lput("-noflsh ",NOFLSH, 0);
48338674Smarc 	lput("-tostop ",TOSTOP, 0);
48438674Smarc 	lput("-mdmbuf ",MDMBUF, 0);
48538674Smarc 	lput("-flusho ",FLUSHO, 0);
48638674Smarc 	lput("-pendin ",PENDIN, 0);
48738674Smarc 	/*
48838674Smarc 	 * input flags
48938674Smarc 	 */
49038674Smarc #undef on
49138674Smarc #define on(f)	((i&f) != 0)
49238674Smarc 	mdput(0);
49338674Smarc 	if (debug) mdput("IFLAG: ");
49438674Smarc 	lput("-istrip ", ISTRIP, 0);
49538674Smarc 	lput("-icrnl ", ICRNL, 1);
49638674Smarc 	lput("-inlcr ", INLCR, 0);
49738674Smarc 	lput("-igncr ", IGNCR, 0);
49838674Smarc 	lput("-ixon ", IXON, 1);
49938674Smarc 	lput("-ixoff ", IXOFF, 0);
50038674Smarc 	lput("-ixany ", IXANY, 1);
50138674Smarc 	lput("-imaxbel ", IMAXBEL, 1);
50238674Smarc 	lput("-ignbrk ", IGNBRK, 0);
50338674Smarc 	lput("-brkint ", BRKINT, 1);
50438674Smarc 	lput("-inpck ", INPCK, 0);
50538674Smarc 	lput("-ignpar ", IGNPAR, 0);
50638674Smarc 	lput("-parmrk ", PARMRK, 0);
50738674Smarc #undef on
50838674Smarc 	/*
50938674Smarc 	 * output flags
51038674Smarc 	 */
51138674Smarc #define on(f)	((o&f) != 0)
51238674Smarc 	mdput(0);
51338674Smarc 	if (debug) mdput("OFLAG: ");
51438674Smarc 	lput("-opost ", OPOST, 1);
51538674Smarc 	lput("-onlcr ", ONLCR, 1);
51638674Smarc 	lput("-oxtabs ", OXTABS, 1);
51738674Smarc #undef on
51838674Smarc 	/*
51938674Smarc 	 * control flags (hardware state)
52038674Smarc 	 */
52138674Smarc #define on(f)	((c&f) != 0)
52238674Smarc 	mdput(0);
52338674Smarc 	if (debug) mdput("CFLAG: ");
52438674Smarc 	lput("-cread ", CREAD, 1);
52538674Smarc 	switch(c&CSIZE) {
52638674Smarc 	case CS5: mdput("cs5 "); break;
52738674Smarc 	case CS6: mdput("cs6 "); break;
52838674Smarc 	case CS7: mdput("cs7 "); break;
52938674Smarc 	case CS8: mdput("cs8 "); break;
53038674Smarc 	}
53138674Smarc 	mdput("-parenb "+on(PARENB));
53238674Smarc 	lput("-parodd ", PARODD, 0);
53338674Smarc 	lput("-hupcl ", HUPCL, 1);
53438674Smarc 	lput("-clocal ", CLOCAL, 0);
53538674Smarc 	lput("-cstopb ", CSTOPB, 0);
53638674Smarc 	lput("-crtscts ", CRTSCTS, 0);
53738674Smarc 	mdput(0);
53838674Smarc #undef on
53938674Smarc 	/*
54038674Smarc 	 * special control characters
54138674Smarc 	 */
54238674Smarc 	if (debug) mdput("CCHARS: ");
54338674Smarc 	if (fmt != 2) {
54438674Smarc 		for (i=0; *cchars[i].names; i++) {
54538674Smarc 			char temp[64];
54638674Smarc 
54738674Smarc 			if (fmt || cc[cchars[i].sub] != cchars[i].def) {
54838674Smarc 				sprintf(temp, "%s = %s; ", *cchars[i].names,
54938674Smarc 					ccval(cc[cchars[i].sub]), fmt);
55038674Smarc 				mdput(temp);
55138674Smarc 			}
5521179Sbill 		}
55338674Smarc 		mdput(0);
55438674Smarc 	} else {
55538674Smarc 		for (i=0; *cchars[i].names; i++)
55638674Smarc 			put("%*s", strlen(*(cchars[i].names+1)) + (i>0?1:0),
55738674Smarc 				*(cchars[i].names+1));
55838674Smarc 		printf("\n");
55938674Smarc 		for (i=0; *cchars[i].names; i++)
56038674Smarc 			put("%*s", strlen(*(cchars[i].names+1)) + (i>0?1:0),
56138674Smarc 				ccval(cc[cchars[i].sub], fmt));
56238674Smarc 		printf("\n");
5631179Sbill 	}
5641179Sbill }
5651179Sbill 
56638674Smarc /*
56738674Smarc  * gross, but since we're changing the control descriptor
56838674Smarc  * from 1 to 0, most users will be probably be doing
56938674Smarc  * "stty > /dev/sometty" by accident. If 1 and 2 are both ttys,
57038674Smarc  * but not the same, assume that 1 was incorrectly redirected.
57138674Smarc  */
57238674Smarc checkredirect() {
57338674Smarc 	struct stat st1, st2;
57438674Smarc 
57538674Smarc 	if (isatty(1) && isatty(2) && fstat(1, &st1) != -1 &&
57638674Smarc 	    fstat(2, &st2) != -1 && (st1.st_rdev != st2.st_rdev))
57738674Smarc warning("stdout appears redirected, but stdin is the control descriptor");
57838674Smarc }
57938674Smarc 
58040170Smarc char *
58138674Smarc ccval(c, fmt)
58238674Smarc 	unsigned char c;
5831179Sbill {
58438674Smarc 	static char buf[128];
58538674Smarc 	char *bp;
5861179Sbill 
58738674Smarc 	*buf = 0, bp = buf;
58838674Smarc 	if (c == _POSIX_VDISABLE)
58938674Smarc 		if (fmt == 2)
59038674Smarc 			return("<u>");
59138674Smarc 		else
59238674Smarc 			return("<undef>");
59338674Smarc 	if (c & 0200) {
59438674Smarc 		strcat(buf, "M-");
59538674Smarc 		*bp++ = 'M';
59638674Smarc 		*bp++ = '-';
59738674Smarc 		c &= 0177;
5981179Sbill 	}
59938674Smarc 	if (c == 0177) {
60038674Smarc 		*bp++ = '^';
60138674Smarc 		*bp++ = '?';
6021179Sbill 	}
60338674Smarc 	else if (c < 040) {
60438674Smarc 		*bp++ = '^';
60538674Smarc 		*bp++ = c + '@';
60638674Smarc 	}
60738674Smarc 	else
60838674Smarc 		*bp++ = c;
60938674Smarc 	*bp = 0;
61038674Smarc 	return(buf);
6111179Sbill }
6121179Sbill 
61340170Smarc 
61438674Smarc mdput(s)
61538674Smarc 	char *s;
6161179Sbill {
61738674Smarc 	static int col = 0;
6181179Sbill 
61938674Smarc 	if (s == (char *)0) {
62038674Smarc 		if (col) {
62138674Smarc 			put("\n");
62238674Smarc 			col = 0;
62338674Smarc 		}
6241179Sbill 		return;
6251179Sbill 	}
62638674Smarc 	if ((col += strlen(s)) > WRAPCOL) {
62738674Smarc 		put("\n");
62838674Smarc 		col = strlen(s);
6291179Sbill 	}
63038674Smarc 	put(s);
6311179Sbill }
6321179Sbill 
63340170Smarc #include <varargs.h>
63440170Smarc 
63540170Smarc put(va_alist)
63640170Smarc 	va_dcl
6371179Sbill {
63840170Smarc 	char *fmt;
63940170Smarc 	va_list ap;
64040170Smarc 
64140170Smarc 	va_start(ap);
64240170Smarc 	fmt = va_arg(ap, char *);
64340170Smarc 	(void) vfprintf(OUT, fmt, ap);
64440170Smarc 	va_end(ap);
64538674Smarc }
6461179Sbill 
64740170Smarc 
64840170Smarc warning(va_alist)
64940170Smarc 	va_dcl
65038674Smarc {
65140170Smarc 	char *fmt;
65240170Smarc 	va_list ap;
65340170Smarc 
65438674Smarc 	fprintf(ERR, "stty: warning: ");
65540170Smarc 	va_start(ap);
65640170Smarc 	fmt = va_arg(ap, char *);
65740170Smarc 	(void) vfprintf(ERR, fmt, ap);
65840170Smarc 	va_end(ap);
65938674Smarc 	fprintf(ERR, "\n");
6601179Sbill }
6611179Sbill 
66240170Smarc 
66340170Smarc errexit(va_alist)
66440170Smarc 	va_dcl
66538674Smarc {
66640170Smarc 	char *fmt;
66740170Smarc 	va_list ap;
66840170Smarc 
66938674Smarc 	fprintf(ERR, "stty: ");
67040170Smarc 	va_start(ap);
67140170Smarc 	fmt = va_arg(ap, char *);
67240170Smarc 	(void) vfprintf(ERR, fmt, ap);
67340170Smarc 	va_end(ap);
67438674Smarc 	fprintf(ERR, "\n");
67538674Smarc 	exit(1);
67638674Smarc }
6771179Sbill 
67840170Smarc 
67940170Smarc syserrexit(va_alist)
68040170Smarc 	va_dcl
6811179Sbill {
68240170Smarc 	char *fmt;
68340170Smarc 	va_list ap;
68440170Smarc 
68538674Smarc 	fprintf(ERR, "stty: ");
68640170Smarc 	va_start(ap);
68740170Smarc 	fmt = va_arg(ap, char *);
68840170Smarc 	(void) vfprintf(ERR, fmt, ap);
68940170Smarc 	va_end(ap);
69040170Smarc 	fprintf(ERR, ": %s\n", strerror(errno));
69138674Smarc 	exit(1);
6921179Sbill }
693