xref: /csrg-svn/bin/stty/stty.c (revision 44286)
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*44286Smarc static char sccsid[] = "@(#)stty.c	5.15 (Berkeley) 06/26/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, },
194*44286Smarc 	{{ "erase2", "era2" },		VERASE2, _POSIX_VDISABLE, },
19543098Sbostic 	{{ "werase", "wera" },		VWERASE, CWERASE, },
19643098Sbostic 	{{ "kill", "kill" },		VKILL,	CKILL, },
19743098Sbostic 	{{ "intr", "int" },		VINTR,	CINTR, },
19843098Sbostic 	{{ "quit", "quit" },		VQUIT,	CQUIT, },
19943098Sbostic 	{{ "susp", "susp" },		VSUSP,	CSUSP, },
20043098Sbostic 	{{ "dsusp", "dsus" },		VDSUSP,	CDSUSP, },
20143098Sbostic 	{{ "eof", "eof" },		VEOF,	CEOF, },
20243098Sbostic 	{{ "eol", "eol", "brk" },	VEOL,	CEOL, },
20343098Sbostic 	{{ "eol2", "eol2" },		VEOL2,	CEOL, },
20443098Sbostic 	{{ "stop", "stop", "xoff" },	VSTOP,	CSTOP, },
20543098Sbostic 	{{ "start", "star", "xon" },	VSTART,	CSTART, },
20643098Sbostic 	{{ "lnext", "lnxt" },		VLNEXT,	CLNEXT, },
207*44286Smarc 	{{ "discard", "disc", "flush" },	VDISCARD, CDISCARD, },
20843098Sbostic 	{{ "reprint", "rpnt", "rprnt" },	VREPRINT, CREPRINT, },
209*44286Smarc 	{{ "status", "stat" },		VSTATUS, CSTATUS, },
21038674Smarc 	0
21138674Smarc };
21243098Sbostic 
21340170Smarc struct winsize win;
21440170Smarc int ldisc;
21540170Smarc int dodisc;
21640170Smarc int debug = 0;
21740170Smarc int trace, dotrace;
21838674Smarc 
21938674Smarc #define OUT	stdout		/* informational output stream */
22038674Smarc #define ERR	stderr		/* error message stream */
22138674Smarc #define CTL	0		/* default control descriptor */
22240170Smarc int ctl = CTL;
22338674Smarc 
22438674Smarc extern errno;
22538674Smarc 
22638674Smarc #define NORMAL	0	/* only print modes differing from defaults */
22738674Smarc #define ALL	1	/* print all modes - POSIX standard format */
22838674Smarc #define ALL_BSD	2	/* print all modes - using BSD shorthand for cc's */
22943323Smarc #define	GFMT	3	/* print modes in form suitable to be re-input */
23038674Smarc 
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 	}
24543323Smarc 	if (argc > 0 && eq(argv[0], "-g")) {
24643323Smarc 		fmt = GFMT;
24743323Smarc 		argc--, argv++;
24843323Smarc 	}
24938674Smarc 	if (argc > 0 && eq(argv[0], "-f")) {
25038674Smarc 		argc--, argv++;
25138674Smarc 		if ((ctl = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
25238674Smarc 			syserrexit(*argv);
25338674Smarc 		argc--, argv++;
2541179Sbill 	}
25538674Smarc 	if (ioctl(ctl, TIOCGETD, &ldisc) < 0)
25638674Smarc 		syserrexit("TIOCGETD");
25738674Smarc 	if (tcgetattr(ctl, &t) < 0)
25838674Smarc 		syserrexit("tcgetattr");
25938674Smarc 	if (ioctl(ctl, TIOCGWINSZ, &win) < 0)
26040170Smarc 		warning("TIOCGWINSZ: %s", strerror(errno));
26138674Smarc 	checkredirect();	/* conversion aid */
26238674Smarc 
26338674Smarc 	if (argc == 0 || fmt) {
26438674Smarc 		prmode(&t, ldisc, fmt);
2651179Sbill 		exit(0);
2661179Sbill 	}
26738674Smarc 
26838674Smarc 	while (*argv) {
26938674Smarc 		if (eq("everything", *argv)) {
27038674Smarc 			prmode(&t, ldisc, ALL_BSD);
27138674Smarc 			exit(0);
2721179Sbill 		}
27338674Smarc 		if (eq("all", *argv)) {
27438674Smarc 			prmode(&t, ldisc, ALL);
27538674Smarc 			exit(0);
2761179Sbill 		}
27738674Smarc 		if (eq("old", *argv)) {
27838674Smarc 			goto next;
2791179Sbill 		}
28038674Smarc 		if (eq("new", *argv)) {
28138674Smarc 			goto next;
2821179Sbill 		}
28338674Smarc 		if (eq("nl", *argv)) {
28438674Smarc 			t.c_iflag &= ~ICRNL;
28538674Smarc 			t.c_oflag &= ~ONLCR;
28638674Smarc 			goto next;
2871179Sbill 		}
28838674Smarc 		if (eq("-nl", *argv)) {
28938674Smarc 			t.c_iflag |= ICRNL;
29038674Smarc 			t.c_oflag |= ONLCR;
29138674Smarc 			goto next;
2923797Sroot 		}
29338674Smarc 		if (eq("dec", *argv)){
29438674Smarc 			t.c_cc[VERASE] = (u_char)0177;
29538674Smarc 			t.c_cc[VKILL] = CTRL('u');
29638674Smarc 			t.c_cc[VINTR] = CTRL('c');
29738674Smarc 			t.c_lflag &= ~ECHOPRT;
29838674Smarc 			t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
29938674Smarc 			t.c_iflag &= ~IXANY;
30038674Smarc 			goto next;
3011179Sbill 		}
30240169Skarels 		if (eq("raw", *argv)) {
30340169Skarels 			cfmakeraw(&t);
30440169Skarels 			t.c_cflag &= ~(CSIZE|PARENB);
30540169Skarels 			t.c_cflag |= CS8;
30640169Skarels 			goto next;
30740169Skarels 		}
30840169Skarels 		if (eq("cooked", *argv) || eq("-raw", *argv) ||
30940169Skarels 		    eq("sane", *argv)) {
31040169Skarels 			t.c_cflag = TTYDEF_CFLAG | (t.c_cflag & CLOCAL);
31140169Skarels 			t.c_iflag = TTYDEF_IFLAG;
31240169Skarels 			t.c_iflag |= ICRNL;
31340169Skarels 			/* preserve user-preference flags in lflag */
31440169Skarels #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
31540169Skarels 			t.c_lflag = TTYDEF_LFLAG | (t.c_lflag & LKEEP);
31640169Skarels 			t.c_oflag = TTYDEF_OFLAG;
31740169Skarels 			goto next;
31840169Skarels 		}
31938674Smarc 		if (eq("rows", *argv)) {
32038674Smarc 			if (*(argv+1) == 0)
32138674Smarc 				goto setit;
32218023Sbloom 			win.ws_row = atoi(*++argv);
32338674Smarc 			goto next;
32418023Sbloom 		}
32538674Smarc 		if (eq("ispeed", *argv)) {
32638674Smarc 			int code;
32738674Smarc 			if (*(argv+1) == 0)
32838674Smarc 				errexit("missing ispeed");
32938674Smarc 			cfsetispeed(&t, atoi(*++argv));
33038674Smarc 			goto next;
33138674Smarc 		}
33238674Smarc 		if (eq("ospeed", *argv)) {
33338674Smarc 			if (*(argv+1) == 0)
33438674Smarc 				errexit("missing ospeed");
33538674Smarc 			cfsetospeed(&t, atoi(*++argv));
33638674Smarc 			goto next;
33738674Smarc 		}
33838674Smarc 		if (eq("cols", *argv) || eq("columns", *argv)) {
33938674Smarc 			if (*(argv+1) == 0)
34038674Smarc 				goto setit;
34118023Sbloom 			win.ws_col = atoi(*++argv);
34238674Smarc 			goto next;
34318023Sbloom 		}
34438674Smarc 		if (eq("size", *argv)) {
34538674Smarc 			put("%d %d\n", win.ws_row, win.ws_col);
34625791Skarels 			exit(0);
34725791Skarels 		}
34838674Smarc 		if (eq("speed", *argv)) {
34938674Smarc 			put("%d\n", cfgetospeed(&t));
35038674Smarc 			exit(0);
35138674Smarc 		}
35238674Smarc 		for (i=0; imodes[i].name; i++)
35338674Smarc 			if (eq(imodes[i].name, *argv)) {
35438674Smarc 				t.c_iflag &= ~imodes[i].unset;
35538674Smarc 				t.c_iflag |= imodes[i].set;
35638674Smarc 				goto next;
3571179Sbill 			}
35838674Smarc 		for (i=0; omodes[i].name; i++)
35938674Smarc 			if (eq(omodes[i].name, *argv)) {
36038674Smarc 				t.c_oflag &= ~omodes[i].unset;
36138674Smarc 				t.c_oflag |= omodes[i].set;
36238674Smarc 				goto next;
36338674Smarc 			}
36438674Smarc 		for (i=0; cmodes[i].name; i++)
36538674Smarc 			if (eq(cmodes[i].name, *argv)) {
36638674Smarc 				t.c_cflag &= ~cmodes[i].unset;
36738674Smarc 				t.c_cflag |= cmodes[i].set;
36838674Smarc 				goto next;
36938674Smarc 			}
37038674Smarc 		for (i=0; lmodes[i].name; i++)
37138674Smarc 			if (eq(lmodes[i].name, *argv)) {
37238674Smarc 				t.c_lflag &= ~lmodes[i].unset;
37338674Smarc 				t.c_lflag |= lmodes[i].set;
37438674Smarc 				goto next;
37538674Smarc 			}
37638674Smarc 		for (i=0; *cchars[i].names; i++) {
37738674Smarc 			char **cp = cchars[i].names;
37838674Smarc 			while (*cp) {
37938674Smarc 				if (eq(*cp, *argv)) {
38038674Smarc 					if (*++argv == 0)
38138674Smarc 						goto setit;
38238674Smarc 					if (eq(*argv, "undef") ||
38338674Smarc 					    eq(*argv, "disable"))
38438674Smarc 						t.c_cc[cchars[i].sub] =
38538674Smarc 						   _POSIX_VDISABLE;
38638674Smarc 					else if (**argv == '^')
38738674Smarc 						t.c_cc[cchars[i].sub] =
38838674Smarc 						    ((*argv)[1] == '?') ? 0177 :
38938674Smarc 						    ((*argv)[1] == '-') ?
39038674Smarc 						     _POSIX_VDISABLE :
39138674Smarc 						     (*argv)[1] & 037;
39238674Smarc 					else
39338674Smarc 						t.c_cc[cchars[i].sub] = **argv;
39438674Smarc 					goto next;
3951179Sbill 				}
39638674Smarc 				cp++;
39738674Smarc 			}
3981179Sbill 		}
39938674Smarc 		if (isdigit(**argv)) {
40038674Smarc 			cfsetospeed(&t, atoi(*argv));
40138674Smarc 			cfsetispeed(&t, atoi(*argv));
40238674Smarc 			goto next;
40338674Smarc 		}
40443323Smarc 		if (strncmp(*argv, "-gfmt", sizeof ("-gfmt") - 1) == 0) {
40543323Smarc 			gfmtset(&t, *argv);
40643323Smarc 			goto next;
40743323Smarc 		}
40838674Smarc 		/* didn't match anything */
40938674Smarc 		errexit("unknown option: %s", *argv);
41038674Smarc 		exit(1);
41138674Smarc next:
41238674Smarc 		argv++;
4131179Sbill 	}
41438674Smarc setit:
41538674Smarc 	if (tcsetattr(ctl, 0, &t) < 0)
41638674Smarc 		syserrexit("tcsetattr");
41738674Smarc 	if (ioctl(ctl, TIOCSWINSZ, &win) < 0)
41838674Smarc 		warning("can't set window size");
41938674Smarc 
42038674Smarc 	exit(0);
4211179Sbill }
4221179Sbill 
42343323Smarc gfmtset(tp, s)
42443323Smarc 	register struct termios *tp;
42543323Smarc 	char *s;
42643323Smarc {
42743323Smarc 	register int cnt;
42843323Smarc 	char sep;
42943323Smarc 	char *saves = s;
43043323Smarc 	int cval;
43143323Smarc #define advance(c)	while (*(s) && *(s) != (c)) (s)++; if (*s) (s)++ ; \
43243323Smarc 				else \
43343323Smarc 					errexit("bad gfmt operand: %s", saves)
43443323Smarc #define chkeq(string)	if (strncmp(s, (string), strlen(string))) \
43543323Smarc 				errexit("bad gfmt operand: %s", saves)
43643323Smarc 
43743323Smarc 	if (s == NULL)
43843323Smarc 		errexit("missing gfmt string");
43943323Smarc 	advance(':');
44043323Smarc 	chkeq("iflag=");
44143323Smarc 	advance('=');
44243323Smarc 	sscanf(s, "%x", &tp->c_iflag);
44343323Smarc 
44443323Smarc 	advance(':');
44543323Smarc 	chkeq("oflag");
44643323Smarc 	advance('=');
44743323Smarc 	sscanf(s, "%x", &tp->c_oflag);
44843323Smarc 
44943323Smarc 	advance(':');
45043323Smarc 	chkeq("cflag");
45143323Smarc 	advance('=');
45243323Smarc 	sscanf(s, "%x", &tp->c_cflag);
45343323Smarc 
45443323Smarc 	advance(':');
45543323Smarc 	chkeq("lflag");
45643323Smarc 	advance('=');
45743323Smarc 	sscanf(s, "%x", &tp->c_lflag);
45843323Smarc 
45943323Smarc 	advance(':');
46043323Smarc 	chkeq("cc=");
46143323Smarc 
46243323Smarc 	for (cnt = 0, sep = '='; cnt < NCCS; cnt++, sep = ',') {
46343323Smarc 		advance(sep);
46443323Smarc 		sscanf(s, "%o", &cval);
46543323Smarc 		tp->c_cc[cnt] = cval;
46643323Smarc 	}
46743323Smarc 
46843323Smarc 	advance(':');
46943323Smarc 	chkeq("ispeed=");
47043323Smarc 	advance('=');
47143323Smarc 	sscanf(s, "%d", &tp->c_ispeed);
47243323Smarc 
47343323Smarc 	advance(':');
47443323Smarc 	chkeq("ospeed=");
47543323Smarc 	advance('=');
47643323Smarc 	sscanf(s, "%d", &tp->c_ospeed);
4771179Sbill }
4781179Sbill 
47938674Smarc prmode(tp, ldisc, fmt)
48038674Smarc 	struct termios *tp;
4811179Sbill {
48238674Smarc 	long	i = tp->c_iflag,
48338674Smarc 		o = tp->c_oflag,
48438674Smarc 		c = tp->c_cflag,
48538674Smarc 		l = tp->c_lflag;
48638674Smarc 	u_char	*cc = tp->c_cc;
48738674Smarc 	int	ispeed = cfgetispeed(tp),
48838674Smarc 		ospeed = cfgetospeed(tp);
48938674Smarc 	char	unknown[32],
49038674Smarc 		*ld;
49138674Smarc 	char *ccval();
49243323Smarc 
49343323Smarc 	if (fmt == GFMT) {
49443323Smarc 		int	cnt;
49543323Smarc 		char	sep;
49643323Smarc 
49743323Smarc 		printf("-gfmt:iflag=%x:oflag=%x:cflag=%x:lflag=%x:cc",
49843323Smarc 			i, o, c, l);
49943323Smarc 		for (cnt = 0, sep = '='; cnt < NCCS; cnt++, sep = ',')
50043323Smarc 			printf("%c%o", sep, cc[cnt]);
50143323Smarc 		printf(":ispeed=%d:ospeed=%d:\n", ispeed, ospeed);
50243323Smarc 		return;
50343323Smarc 	}
50438674Smarc 
50538674Smarc 	/*
50638674Smarc 	 * line discipline
50738674Smarc 	 */
50838674Smarc 	if (ldisc != TTYDISC) {
50938674Smarc 		switch(ldisc) {
51038674Smarc 		case TABLDISC:
51138674Smarc 			ld = "tablet";
5121179Sbill 			break;
51338674Smarc 		case SLIPDISC:
51440170Smarc 			ld = "slip";
5151179Sbill 			break;
51638674Smarc 		default:
51738674Smarc 			sprintf(unknown, "#%d", ldisc);
51838674Smarc 			ld = unknown;
5191179Sbill 		}
52038674Smarc 		put("%s disc; ", ld);
52138674Smarc 	}
52238674Smarc 	/*
52338674Smarc 	 * line speed
52438674Smarc 	 */
52538674Smarc 	if (ispeed != ospeed)
52638674Smarc 		put("ispeed %d baud; ospeed %d baud;",
52738674Smarc 		     ispeed, ospeed);
52838674Smarc 	else
52938674Smarc 		put("speed %d baud;", ispeed);
53038674Smarc 	if (fmt)
53138674Smarc 		put(" %d rows; %d columns;", win.ws_row, win.ws_col);
53238674Smarc 	put("\n");
53313817Ssam 
53438674Smarc #define lput(n, f, d) if (fmt || on(f) != d) mdput(n+on(f))
53538674Smarc 	/*
53638674Smarc 	 * "local" flags
53738674Smarc 	 */
53838674Smarc #define on(f)	((l&f) != 0)
53938674Smarc 	if (debug) mdput("LFLAG: ");
54038674Smarc 	lput("-icanon ",ICANON, 1);
54138674Smarc 	lput("-isig ", ISIG, 1);
54238674Smarc 	lput("-iexten ", IEXTEN, 1);
54338674Smarc 	lput("-echo ",ECHO, 1);
54438674Smarc 	lput("-echoe ",ECHOE, 0);
54538674Smarc 	lput("-echok ",ECHOK, 0);
54638674Smarc 	lput("-echoke ",ECHOKE, 0);
54738674Smarc 	lput("-echonl ",ECHONL, 0);
54838674Smarc 	lput("-echoctl ",ECHOCTL, 0);
54938674Smarc 	lput("-echoprt ",ECHOPRT, 0);
55038674Smarc 	lput("-altwerase ",ALTWERASE, 0);
55138674Smarc 	lput("-noflsh ",NOFLSH, 0);
55238674Smarc 	lput("-tostop ",TOSTOP, 0);
55338674Smarc 	lput("-mdmbuf ",MDMBUF, 0);
55438674Smarc 	lput("-flusho ",FLUSHO, 0);
55538674Smarc 	lput("-pendin ",PENDIN, 0);
55638674Smarc 	/*
55738674Smarc 	 * input flags
55838674Smarc 	 */
55938674Smarc #undef on
56038674Smarc #define on(f)	((i&f) != 0)
56138674Smarc 	mdput(0);
56238674Smarc 	if (debug) mdput("IFLAG: ");
56338674Smarc 	lput("-istrip ", ISTRIP, 0);
56438674Smarc 	lput("-icrnl ", ICRNL, 1);
56538674Smarc 	lput("-inlcr ", INLCR, 0);
56638674Smarc 	lput("-igncr ", IGNCR, 0);
56738674Smarc 	lput("-ixon ", IXON, 1);
56838674Smarc 	lput("-ixoff ", IXOFF, 0);
56938674Smarc 	lput("-ixany ", IXANY, 1);
57038674Smarc 	lput("-imaxbel ", IMAXBEL, 1);
57138674Smarc 	lput("-ignbrk ", IGNBRK, 0);
57238674Smarc 	lput("-brkint ", BRKINT, 1);
57338674Smarc 	lput("-inpck ", INPCK, 0);
57438674Smarc 	lput("-ignpar ", IGNPAR, 0);
57538674Smarc 	lput("-parmrk ", PARMRK, 0);
57638674Smarc #undef on
57738674Smarc 	/*
57838674Smarc 	 * output flags
57938674Smarc 	 */
58038674Smarc #define on(f)	((o&f) != 0)
58138674Smarc 	mdput(0);
58238674Smarc 	if (debug) mdput("OFLAG: ");
58338674Smarc 	lput("-opost ", OPOST, 1);
58438674Smarc 	lput("-onlcr ", ONLCR, 1);
58538674Smarc 	lput("-oxtabs ", OXTABS, 1);
58638674Smarc #undef on
58738674Smarc 	/*
58838674Smarc 	 * control flags (hardware state)
58938674Smarc 	 */
59038674Smarc #define on(f)	((c&f) != 0)
59138674Smarc 	mdput(0);
59238674Smarc 	if (debug) mdput("CFLAG: ");
59338674Smarc 	lput("-cread ", CREAD, 1);
59438674Smarc 	switch(c&CSIZE) {
59538674Smarc 	case CS5: mdput("cs5 "); break;
59638674Smarc 	case CS6: mdput("cs6 "); break;
59738674Smarc 	case CS7: mdput("cs7 "); break;
59838674Smarc 	case CS8: mdput("cs8 "); break;
59938674Smarc 	}
60038674Smarc 	mdput("-parenb "+on(PARENB));
60138674Smarc 	lput("-parodd ", PARODD, 0);
60238674Smarc 	lput("-hupcl ", HUPCL, 1);
60338674Smarc 	lput("-clocal ", CLOCAL, 0);
60438674Smarc 	lput("-cstopb ", CSTOPB, 0);
60538674Smarc 	lput("-crtscts ", CRTSCTS, 0);
60638674Smarc 	mdput(0);
60738674Smarc #undef on
60838674Smarc 	/*
60938674Smarc 	 * special control characters
61038674Smarc 	 */
61138674Smarc 	if (debug) mdput("CCHARS: ");
61238674Smarc 	if (fmt != 2) {
61338674Smarc 		for (i=0; *cchars[i].names; i++) {
61438674Smarc 			char temp[64];
61538674Smarc 
61638674Smarc 			if (fmt || cc[cchars[i].sub] != cchars[i].def) {
61738674Smarc 				sprintf(temp, "%s = %s; ", *cchars[i].names,
61838674Smarc 					ccval(cc[cchars[i].sub]), fmt);
61938674Smarc 				mdput(temp);
62038674Smarc 			}
6211179Sbill 		}
62238674Smarc 		mdput(0);
62338674Smarc 	} else {
62438674Smarc 		for (i=0; *cchars[i].names; i++)
62538674Smarc 			put("%*s", strlen(*(cchars[i].names+1)) + (i>0?1:0),
62638674Smarc 				*(cchars[i].names+1));
62738674Smarc 		printf("\n");
62838674Smarc 		for (i=0; *cchars[i].names; i++)
62938674Smarc 			put("%*s", strlen(*(cchars[i].names+1)) + (i>0?1:0),
63038674Smarc 				ccval(cc[cchars[i].sub], fmt));
63138674Smarc 		printf("\n");
6321179Sbill 	}
6331179Sbill }
6341179Sbill 
63538674Smarc /*
63638674Smarc  * gross, but since we're changing the control descriptor
63738674Smarc  * from 1 to 0, most users will be probably be doing
63838674Smarc  * "stty > /dev/sometty" by accident. If 1 and 2 are both ttys,
63938674Smarc  * but not the same, assume that 1 was incorrectly redirected.
64038674Smarc  */
64138674Smarc checkredirect() {
64238674Smarc 	struct stat st1, st2;
64338674Smarc 
64438674Smarc 	if (isatty(1) && isatty(2) && fstat(1, &st1) != -1 &&
64538674Smarc 	    fstat(2, &st2) != -1 && (st1.st_rdev != st2.st_rdev))
64638674Smarc warning("stdout appears redirected, but stdin is the control descriptor");
64738674Smarc }
64838674Smarc 
64940170Smarc char *
65038674Smarc ccval(c, fmt)
65138674Smarc 	unsigned char c;
6521179Sbill {
65338674Smarc 	static char buf[128];
65438674Smarc 	char *bp;
6551179Sbill 
65638674Smarc 	*buf = 0, bp = buf;
65738674Smarc 	if (c == _POSIX_VDISABLE)
65838674Smarc 		if (fmt == 2)
65938674Smarc 			return("<u>");
66038674Smarc 		else
66138674Smarc 			return("<undef>");
66238674Smarc 	if (c & 0200) {
66338674Smarc 		strcat(buf, "M-");
66438674Smarc 		*bp++ = 'M';
66538674Smarc 		*bp++ = '-';
66638674Smarc 		c &= 0177;
6671179Sbill 	}
66838674Smarc 	if (c == 0177) {
66938674Smarc 		*bp++ = '^';
67038674Smarc 		*bp++ = '?';
6711179Sbill 	}
67238674Smarc 	else if (c < 040) {
67338674Smarc 		*bp++ = '^';
67438674Smarc 		*bp++ = c + '@';
67538674Smarc 	}
67638674Smarc 	else
67738674Smarc 		*bp++ = c;
67838674Smarc 	*bp = 0;
67938674Smarc 	return(buf);
6801179Sbill }
6811179Sbill 
68240170Smarc 
68338674Smarc mdput(s)
68438674Smarc 	char *s;
6851179Sbill {
68638674Smarc 	static int col = 0;
6871179Sbill 
68838674Smarc 	if (s == (char *)0) {
68938674Smarc 		if (col) {
69038674Smarc 			put("\n");
69138674Smarc 			col = 0;
69238674Smarc 		}
6931179Sbill 		return;
6941179Sbill 	}
69538674Smarc 	if ((col += strlen(s)) > WRAPCOL) {
69638674Smarc 		put("\n");
69738674Smarc 		col = strlen(s);
6981179Sbill 	}
69938674Smarc 	put(s);
7001179Sbill }
7011179Sbill 
70240170Smarc #include <varargs.h>
70340170Smarc 
70440170Smarc put(va_alist)
70540170Smarc 	va_dcl
7061179Sbill {
70740170Smarc 	char *fmt;
70840170Smarc 	va_list ap;
70940170Smarc 
71040170Smarc 	va_start(ap);
71140170Smarc 	fmt = va_arg(ap, char *);
71240170Smarc 	(void) vfprintf(OUT, fmt, ap);
71340170Smarc 	va_end(ap);
71438674Smarc }
7151179Sbill 
71640170Smarc 
71740170Smarc warning(va_alist)
71840170Smarc 	va_dcl
71938674Smarc {
72040170Smarc 	char *fmt;
72140170Smarc 	va_list ap;
72240170Smarc 
72338674Smarc 	fprintf(ERR, "stty: warning: ");
72440170Smarc 	va_start(ap);
72540170Smarc 	fmt = va_arg(ap, char *);
72640170Smarc 	(void) vfprintf(ERR, fmt, ap);
72740170Smarc 	va_end(ap);
72838674Smarc 	fprintf(ERR, "\n");
7291179Sbill }
7301179Sbill 
73140170Smarc 
73240170Smarc errexit(va_alist)
73340170Smarc 	va_dcl
73438674Smarc {
73540170Smarc 	char *fmt;
73640170Smarc 	va_list ap;
73740170Smarc 
73838674Smarc 	fprintf(ERR, "stty: ");
73940170Smarc 	va_start(ap);
74040170Smarc 	fmt = va_arg(ap, char *);
74140170Smarc 	(void) vfprintf(ERR, fmt, ap);
74240170Smarc 	va_end(ap);
74338674Smarc 	fprintf(ERR, "\n");
74438674Smarc 	exit(1);
74538674Smarc }
7461179Sbill 
74740170Smarc 
74840170Smarc syserrexit(va_alist)
74940170Smarc 	va_dcl
7501179Sbill {
75140170Smarc 	char *fmt;
75240170Smarc 	va_list ap;
75340170Smarc 
75438674Smarc 	fprintf(ERR, "stty: ");
75540170Smarc 	va_start(ap);
75640170Smarc 	fmt = va_arg(ap, char *);
75740170Smarc 	(void) vfprintf(ERR, fmt, ap);
75840170Smarc 	va_end(ap);
75940170Smarc 	fprintf(ERR, ": %s\n", strerror(errno));
76038674Smarc 	exit(1);
7611179Sbill }
762