xref: /csrg-svn/bin/stty/stty.c (revision 45235)
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*45235Sborman static char sccsid[] = "@(#)stty.c	5.18 (Berkeley) 09/14/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,
173*45235Sborman 	"nokerninfo",	NOKERNINFO, 0,
174*45235Sborman 	"-nokerninfo",	0, NOKERNINFO,
175*45235Sborman 	"kerninfo",	0, NOKERNINFO,
176*45235Sborman 	"-kerninfo",	NOKERNINFO, 0,
17738674Smarc 	0
17838674Smarc };
17938674Smarc 
18038674Smarc /*
18138674Smarc  * Special control characters.
18238674Smarc  *
18338674Smarc  * Each entry has a list of names.  The first is the primary name
18438674Smarc  * and is used when printing the control character in the "name = val;"
18538674Smarc  * form.  The second is an abbreviation which is guaranteed to be less
18638674Smarc  * than or equal to four characters in length and is primarily used
18738674Smarc  * when printing the values in columunar form (guarantees all will
18838674Smarc  * fit within 80 cols).  The rest are optional aliases.
18938674Smarc  * All names are recognized on the command line.
19038674Smarc  */
19140170Smarc #define MAXNAMES 3
19240170Smarc struct {
19340210Smarc 	char	*names[MAXNAMES+1];
19438674Smarc 	int	sub;
19538674Smarc 	u_char	def;
19638674Smarc } cchars[] = {
19743098Sbostic 	{{ "erase", "era" },		VERASE,	CERASE, },
19843098Sbostic 	{{ "werase", "wera" },		VWERASE, CWERASE, },
19943098Sbostic 	{{ "kill", "kill" },		VKILL,	CKILL, },
20043098Sbostic 	{{ "intr", "int" },		VINTR,	CINTR, },
20143098Sbostic 	{{ "quit", "quit" },		VQUIT,	CQUIT, },
20243098Sbostic 	{{ "susp", "susp" },		VSUSP,	CSUSP, },
20343098Sbostic 	{{ "dsusp", "dsus" },		VDSUSP,	CDSUSP, },
20443098Sbostic 	{{ "eof", "eof" },		VEOF,	CEOF, },
20543098Sbostic 	{{ "eol", "eol", "brk" },	VEOL,	CEOL, },
20643098Sbostic 	{{ "eol2", "eol2" },		VEOL2,	CEOL, },
20743098Sbostic 	{{ "stop", "stop", "xoff" },	VSTOP,	CSTOP, },
20843098Sbostic 	{{ "start", "star", "xon" },	VSTART,	CSTART, },
20943098Sbostic 	{{ "lnext", "lnxt" },		VLNEXT,	CLNEXT, },
21044286Smarc 	{{ "discard", "disc", "flush" },	VDISCARD, CDISCARD, },
21143098Sbostic 	{{ "reprint", "rpnt", "rprnt" },	VREPRINT, CREPRINT, },
21244286Smarc 	{{ "status", "stat" },		VSTATUS, CSTATUS, },
21338674Smarc 	0
21438674Smarc };
21543098Sbostic 
21640170Smarc struct winsize win;
21740170Smarc int ldisc;
21840170Smarc int dodisc;
21940170Smarc int debug = 0;
22040170Smarc int trace, dotrace;
221*45235Sborman int extproc;
22238674Smarc 
22338674Smarc #define OUT	stdout		/* informational output stream */
22438674Smarc #define ERR	stderr		/* error message stream */
22538674Smarc #define CTL	0		/* default control descriptor */
22640170Smarc int ctl = CTL;
22738674Smarc 
22838674Smarc extern errno;
22938674Smarc 
23038674Smarc #define NORMAL	0	/* only print modes differing from defaults */
23138674Smarc #define ALL	1	/* print all modes - POSIX standard format */
23238674Smarc #define ALL_BSD	2	/* print all modes - using BSD shorthand for cc's */
23343323Smarc #define	GFMT	3	/* print modes in form suitable to be re-input */
23438674Smarc 
23538674Smarc main(argc, argv)
23638674Smarc 	char *argv[];
2371179Sbill {
23838674Smarc 	struct termios t;
23938674Smarc 	int i, fmt = NORMAL;
24038674Smarc 	extern char *optarg;
24138674Smarc 	extern int optind;
24238674Smarc 	int ch;
2431179Sbill 
24438674Smarc 	argc--, argv++;
24538674Smarc 	if (argc > 0 && eq(argv[0], "-a")) {
24638674Smarc 		fmt = ALL;
24738674Smarc 		argc--, argv++;
2481179Sbill 	}
24943323Smarc 	if (argc > 0 && eq(argv[0], "-g")) {
25043323Smarc 		fmt = GFMT;
25143323Smarc 		argc--, argv++;
25243323Smarc 	}
25338674Smarc 	if (argc > 0 && eq(argv[0], "-f")) {
25438674Smarc 		argc--, argv++;
25538674Smarc 		if ((ctl = open(argv[0], O_RDONLY | O_NONBLOCK)) < 0)
25638674Smarc 			syserrexit(*argv);
25738674Smarc 		argc--, argv++;
2581179Sbill 	}
25938674Smarc 	if (ioctl(ctl, TIOCGETD, &ldisc) < 0)
26038674Smarc 		syserrexit("TIOCGETD");
26138674Smarc 	if (tcgetattr(ctl, &t) < 0)
26238674Smarc 		syserrexit("tcgetattr");
26338674Smarc 	if (ioctl(ctl, TIOCGWINSZ, &win) < 0)
26440170Smarc 		warning("TIOCGWINSZ: %s", strerror(errno));
26538674Smarc 	checkredirect();	/* conversion aid */
26638674Smarc 
26738674Smarc 	if (argc == 0 || fmt) {
26838674Smarc 		prmode(&t, ldisc, fmt);
2691179Sbill 		exit(0);
2701179Sbill 	}
27138674Smarc 
27238674Smarc 	while (*argv) {
27338674Smarc 		if (eq("everything", *argv)) {
27438674Smarc 			prmode(&t, ldisc, ALL_BSD);
27538674Smarc 			exit(0);
2761179Sbill 		}
27738674Smarc 		if (eq("all", *argv)) {
27838674Smarc 			prmode(&t, ldisc, ALL);
27938674Smarc 			exit(0);
2801179Sbill 		}
28138674Smarc 		if (eq("old", *argv)) {
28238674Smarc 			goto next;
2831179Sbill 		}
28438674Smarc 		if (eq("new", *argv)) {
28538674Smarc 			goto next;
2861179Sbill 		}
28738674Smarc 		if (eq("nl", *argv)) {
28838674Smarc 			t.c_iflag &= ~ICRNL;
28938674Smarc 			t.c_oflag &= ~ONLCR;
29038674Smarc 			goto next;
2911179Sbill 		}
29238674Smarc 		if (eq("-nl", *argv)) {
29338674Smarc 			t.c_iflag |= ICRNL;
29438674Smarc 			t.c_oflag |= ONLCR;
29538674Smarc 			goto next;
2963797Sroot 		}
29738674Smarc 		if (eq("dec", *argv)){
29838674Smarc 			t.c_cc[VERASE] = (u_char)0177;
29938674Smarc 			t.c_cc[VKILL] = CTRL('u');
30038674Smarc 			t.c_cc[VINTR] = CTRL('c');
30138674Smarc 			t.c_lflag &= ~ECHOPRT;
30238674Smarc 			t.c_lflag |= ECHOE|ECHOKE|ECHOCTL;
30338674Smarc 			t.c_iflag &= ~IXANY;
30438674Smarc 			goto next;
3051179Sbill 		}
30640169Skarels 		if (eq("raw", *argv)) {
30740169Skarels 			cfmakeraw(&t);
30840169Skarels 			t.c_cflag &= ~(CSIZE|PARENB);
30940169Skarels 			t.c_cflag |= CS8;
31040169Skarels 			goto next;
31140169Skarels 		}
31245029Smarc 		if (eq("cbreak", *argv)) {
31345029Smarc 			t.c_iflag |  BRKINT|IXON|IMAXBEL;
31445029Smarc 			t.c_oflag |= OPOST;
31545029Smarc 			t.c_lflag |= ISIG|IEXTEN;
31645029Smarc 			t.c_lflag &= ~ICANON;
31745029Smarc 		}
31840169Skarels 		if (eq("cooked", *argv) || eq("-raw", *argv) ||
31945029Smarc 		    eq("sane", *argv) || eq("-cbreak", *argv)) {
32040169Skarels 			t.c_cflag = TTYDEF_CFLAG | (t.c_cflag & CLOCAL);
32140169Skarels 			t.c_iflag = TTYDEF_IFLAG;
32240169Skarels 			t.c_iflag |= ICRNL;
32340169Skarels 			/* preserve user-preference flags in lflag */
32440169Skarels #define	LKEEP	(ECHOKE|ECHOE|ECHOK|ECHOPRT|ECHOCTL|ALTWERASE|TOSTOP|NOFLSH)
32540169Skarels 			t.c_lflag = TTYDEF_LFLAG | (t.c_lflag & LKEEP);
32640169Skarels 			t.c_oflag = TTYDEF_OFLAG;
32740169Skarels 			goto next;
32840169Skarels 		}
32938674Smarc 		if (eq("rows", *argv)) {
33038674Smarc 			if (*(argv+1) == 0)
33138674Smarc 				goto setit;
33218023Sbloom 			win.ws_row = atoi(*++argv);
33338674Smarc 			goto next;
33418023Sbloom 		}
33538674Smarc 		if (eq("ispeed", *argv)) {
33638674Smarc 			int code;
33738674Smarc 			if (*(argv+1) == 0)
33838674Smarc 				errexit("missing ispeed");
33938674Smarc 			cfsetispeed(&t, atoi(*++argv));
34038674Smarc 			goto next;
34138674Smarc 		}
34238674Smarc 		if (eq("ospeed", *argv)) {
34338674Smarc 			if (*(argv+1) == 0)
34438674Smarc 				errexit("missing ospeed");
34538674Smarc 			cfsetospeed(&t, atoi(*++argv));
34638674Smarc 			goto next;
34738674Smarc 		}
34838674Smarc 		if (eq("cols", *argv) || eq("columns", *argv)) {
34938674Smarc 			if (*(argv+1) == 0)
35038674Smarc 				goto setit;
35118023Sbloom 			win.ws_col = atoi(*++argv);
35238674Smarc 			goto next;
35318023Sbloom 		}
35438674Smarc 		if (eq("size", *argv)) {
35538674Smarc 			put("%d %d\n", win.ws_row, win.ws_col);
35625791Skarels 			exit(0);
35725791Skarels 		}
358*45235Sborman 		if (eq("extrpc", *argv) || eq("-extproc", *argv)) {
359*45235Sborman 			if (**argv == '-')
360*45235Sborman 				extproc = 0;
361*45235Sborman 			else
362*45235Sborman 				extproc = 1;
363*45235Sborman 			ioctl(ctl, TIOCEXT, &extproc);
364*45235Sborman 		}
36538674Smarc 		if (eq("speed", *argv)) {
36638674Smarc 			put("%d\n", cfgetospeed(&t));
36738674Smarc 			exit(0);
36838674Smarc 		}
36938674Smarc 		for (i=0; imodes[i].name; i++)
37038674Smarc 			if (eq(imodes[i].name, *argv)) {
37138674Smarc 				t.c_iflag &= ~imodes[i].unset;
37238674Smarc 				t.c_iflag |= imodes[i].set;
37338674Smarc 				goto next;
3741179Sbill 			}
37538674Smarc 		for (i=0; omodes[i].name; i++)
37638674Smarc 			if (eq(omodes[i].name, *argv)) {
37738674Smarc 				t.c_oflag &= ~omodes[i].unset;
37838674Smarc 				t.c_oflag |= omodes[i].set;
37938674Smarc 				goto next;
38038674Smarc 			}
38138674Smarc 		for (i=0; cmodes[i].name; i++)
38238674Smarc 			if (eq(cmodes[i].name, *argv)) {
38338674Smarc 				t.c_cflag &= ~cmodes[i].unset;
38438674Smarc 				t.c_cflag |= cmodes[i].set;
38538674Smarc 				goto next;
38638674Smarc 			}
38738674Smarc 		for (i=0; lmodes[i].name; i++)
38838674Smarc 			if (eq(lmodes[i].name, *argv)) {
38938674Smarc 				t.c_lflag &= ~lmodes[i].unset;
39038674Smarc 				t.c_lflag |= lmodes[i].set;
39138674Smarc 				goto next;
39238674Smarc 			}
39338674Smarc 		for (i=0; *cchars[i].names; i++) {
39438674Smarc 			char **cp = cchars[i].names;
39538674Smarc 			while (*cp) {
39638674Smarc 				if (eq(*cp, *argv)) {
39738674Smarc 					if (*++argv == 0)
39838674Smarc 						goto setit;
39938674Smarc 					if (eq(*argv, "undef") ||
40038674Smarc 					    eq(*argv, "disable"))
40138674Smarc 						t.c_cc[cchars[i].sub] =
40238674Smarc 						   _POSIX_VDISABLE;
40338674Smarc 					else if (**argv == '^')
40438674Smarc 						t.c_cc[cchars[i].sub] =
40538674Smarc 						    ((*argv)[1] == '?') ? 0177 :
40638674Smarc 						    ((*argv)[1] == '-') ?
40738674Smarc 						     _POSIX_VDISABLE :
40838674Smarc 						     (*argv)[1] & 037;
40938674Smarc 					else
41038674Smarc 						t.c_cc[cchars[i].sub] = **argv;
41138674Smarc 					goto next;
4121179Sbill 				}
41338674Smarc 				cp++;
41438674Smarc 			}
4151179Sbill 		}
41638674Smarc 		if (isdigit(**argv)) {
41738674Smarc 			cfsetospeed(&t, atoi(*argv));
41838674Smarc 			cfsetispeed(&t, atoi(*argv));
41938674Smarc 			goto next;
42038674Smarc 		}
42143323Smarc 		if (strncmp(*argv, "-gfmt", sizeof ("-gfmt") - 1) == 0) {
42243323Smarc 			gfmtset(&t, *argv);
42343323Smarc 			goto next;
42443323Smarc 		}
42538674Smarc 		/* didn't match anything */
42638674Smarc 		errexit("unknown option: %s", *argv);
42738674Smarc 		exit(1);
42838674Smarc next:
42938674Smarc 		argv++;
4301179Sbill 	}
43138674Smarc setit:
43238674Smarc 	if (tcsetattr(ctl, 0, &t) < 0)
43338674Smarc 		syserrexit("tcsetattr");
43438674Smarc 	if (ioctl(ctl, TIOCSWINSZ, &win) < 0)
43538674Smarc 		warning("can't set window size");
43638674Smarc 
43738674Smarc 	exit(0);
4381179Sbill }
4391179Sbill 
44043323Smarc gfmtset(tp, s)
44143323Smarc 	register struct termios *tp;
44243323Smarc 	char *s;
44343323Smarc {
44443323Smarc 	register int cnt;
44543323Smarc 	char sep;
44643323Smarc 	char *saves = s;
44743323Smarc 	int cval;
44843323Smarc #define advance(c)	while (*(s) && *(s) != (c)) (s)++; if (*s) (s)++ ; \
44943323Smarc 				else \
45043323Smarc 					errexit("bad gfmt operand: %s", saves)
45143323Smarc #define chkeq(string)	if (strncmp(s, (string), strlen(string))) \
45243323Smarc 				errexit("bad gfmt operand: %s", saves)
45343323Smarc 
45443323Smarc 	if (s == NULL)
45543323Smarc 		errexit("missing gfmt string");
45643323Smarc 	advance(':');
45743323Smarc 	chkeq("iflag=");
45843323Smarc 	advance('=');
45943323Smarc 	sscanf(s, "%x", &tp->c_iflag);
46043323Smarc 
46143323Smarc 	advance(':');
46243323Smarc 	chkeq("oflag");
46343323Smarc 	advance('=');
46443323Smarc 	sscanf(s, "%x", &tp->c_oflag);
46543323Smarc 
46643323Smarc 	advance(':');
46743323Smarc 	chkeq("cflag");
46843323Smarc 	advance('=');
46943323Smarc 	sscanf(s, "%x", &tp->c_cflag);
47043323Smarc 
47143323Smarc 	advance(':');
47243323Smarc 	chkeq("lflag");
47343323Smarc 	advance('=');
47443323Smarc 	sscanf(s, "%x", &tp->c_lflag);
47543323Smarc 
47643323Smarc 	advance(':');
47743323Smarc 	chkeq("cc=");
47843323Smarc 
47943323Smarc 	for (cnt = 0, sep = '='; cnt < NCCS; cnt++, sep = ',') {
48043323Smarc 		advance(sep);
48143323Smarc 		sscanf(s, "%o", &cval);
48243323Smarc 		tp->c_cc[cnt] = cval;
48343323Smarc 	}
48443323Smarc 
48543323Smarc 	advance(':');
48643323Smarc 	chkeq("ispeed=");
48743323Smarc 	advance('=');
48843323Smarc 	sscanf(s, "%d", &tp->c_ispeed);
48943323Smarc 
49043323Smarc 	advance(':');
49143323Smarc 	chkeq("ospeed=");
49243323Smarc 	advance('=');
49343323Smarc 	sscanf(s, "%d", &tp->c_ospeed);
4941179Sbill }
4951179Sbill 
49638674Smarc prmode(tp, ldisc, fmt)
49738674Smarc 	struct termios *tp;
4981179Sbill {
49938674Smarc 	long	i = tp->c_iflag,
50038674Smarc 		o = tp->c_oflag,
50138674Smarc 		c = tp->c_cflag,
50238674Smarc 		l = tp->c_lflag;
50338674Smarc 	u_char	*cc = tp->c_cc;
50438674Smarc 	int	ispeed = cfgetispeed(tp),
50538674Smarc 		ospeed = cfgetospeed(tp);
50638674Smarc 	char	unknown[32],
50738674Smarc 		*ld;
50838674Smarc 	char *ccval();
50943323Smarc 
51043323Smarc 	if (fmt == GFMT) {
51143323Smarc 		int	cnt;
51243323Smarc 		char	sep;
51343323Smarc 
51443323Smarc 		printf("-gfmt:iflag=%x:oflag=%x:cflag=%x:lflag=%x:cc",
51543323Smarc 			i, o, c, l);
51643323Smarc 		for (cnt = 0, sep = '='; cnt < NCCS; cnt++, sep = ',')
51743323Smarc 			printf("%c%o", sep, cc[cnt]);
51843323Smarc 		printf(":ispeed=%d:ospeed=%d:\n", ispeed, ospeed);
51943323Smarc 		return;
52043323Smarc 	}
52138674Smarc 
52238674Smarc 	/*
52338674Smarc 	 * line discipline
52438674Smarc 	 */
52538674Smarc 	if (ldisc != TTYDISC) {
52638674Smarc 		switch(ldisc) {
52738674Smarc 		case TABLDISC:
52838674Smarc 			ld = "tablet";
5291179Sbill 			break;
53038674Smarc 		case SLIPDISC:
53140170Smarc 			ld = "slip";
5321179Sbill 			break;
53338674Smarc 		default:
53438674Smarc 			sprintf(unknown, "#%d", ldisc);
53538674Smarc 			ld = unknown;
5361179Sbill 		}
53738674Smarc 		put("%s disc; ", ld);
53838674Smarc 	}
53938674Smarc 	/*
54038674Smarc 	 * line speed
54138674Smarc 	 */
54238674Smarc 	if (ispeed != ospeed)
54338674Smarc 		put("ispeed %d baud; ospeed %d baud;",
54438674Smarc 		     ispeed, ospeed);
54538674Smarc 	else
54638674Smarc 		put("speed %d baud;", ispeed);
54738674Smarc 	if (fmt)
54838674Smarc 		put(" %d rows; %d columns;", win.ws_row, win.ws_col);
54938674Smarc 	put("\n");
55013817Ssam 
55138674Smarc #define lput(n, f, d) if (fmt || on(f) != d) mdput(n+on(f))
55238674Smarc 	/*
55338674Smarc 	 * "local" flags
55438674Smarc 	 */
55538674Smarc #define on(f)	((l&f) != 0)
55638674Smarc 	if (debug) mdput("LFLAG: ");
55738674Smarc 	lput("-icanon ",ICANON, 1);
55838674Smarc 	lput("-isig ", ISIG, 1);
55938674Smarc 	lput("-iexten ", IEXTEN, 1);
56038674Smarc 	lput("-echo ",ECHO, 1);
56138674Smarc 	lput("-echoe ",ECHOE, 0);
56238674Smarc 	lput("-echok ",ECHOK, 0);
56338674Smarc 	lput("-echoke ",ECHOKE, 0);
56438674Smarc 	lput("-echonl ",ECHONL, 0);
56538674Smarc 	lput("-echoctl ",ECHOCTL, 0);
56638674Smarc 	lput("-echoprt ",ECHOPRT, 0);
56738674Smarc 	lput("-altwerase ",ALTWERASE, 0);
56838674Smarc 	lput("-noflsh ",NOFLSH, 0);
56938674Smarc 	lput("-tostop ",TOSTOP, 0);
57038674Smarc 	lput("-mdmbuf ",MDMBUF, 0);
57138674Smarc 	lput("-flusho ",FLUSHO, 0);
57238674Smarc 	lput("-pendin ",PENDIN, 0);
573*45235Sborman 	lput("-nokerninfo ",NOKERNINFO, 0);
574*45235Sborman 	lput("-extproc ",EXTPROC, 0);
57538674Smarc 	/*
57638674Smarc 	 * input flags
57738674Smarc 	 */
57838674Smarc #undef on
57938674Smarc #define on(f)	((i&f) != 0)
58038674Smarc 	mdput(0);
58138674Smarc 	if (debug) mdput("IFLAG: ");
58238674Smarc 	lput("-istrip ", ISTRIP, 0);
58338674Smarc 	lput("-icrnl ", ICRNL, 1);
58438674Smarc 	lput("-inlcr ", INLCR, 0);
58538674Smarc 	lput("-igncr ", IGNCR, 0);
58638674Smarc 	lput("-ixon ", IXON, 1);
58738674Smarc 	lput("-ixoff ", IXOFF, 0);
58838674Smarc 	lput("-ixany ", IXANY, 1);
58938674Smarc 	lput("-imaxbel ", IMAXBEL, 1);
59038674Smarc 	lput("-ignbrk ", IGNBRK, 0);
59138674Smarc 	lput("-brkint ", BRKINT, 1);
59238674Smarc 	lput("-inpck ", INPCK, 0);
59338674Smarc 	lput("-ignpar ", IGNPAR, 0);
59438674Smarc 	lput("-parmrk ", PARMRK, 0);
59538674Smarc #undef on
59638674Smarc 	/*
59738674Smarc 	 * output flags
59838674Smarc 	 */
59938674Smarc #define on(f)	((o&f) != 0)
60038674Smarc 	mdput(0);
60138674Smarc 	if (debug) mdput("OFLAG: ");
60238674Smarc 	lput("-opost ", OPOST, 1);
60338674Smarc 	lput("-onlcr ", ONLCR, 1);
60438674Smarc 	lput("-oxtabs ", OXTABS, 1);
60538674Smarc #undef on
60638674Smarc 	/*
60738674Smarc 	 * control flags (hardware state)
60838674Smarc 	 */
60938674Smarc #define on(f)	((c&f) != 0)
61038674Smarc 	mdput(0);
61138674Smarc 	if (debug) mdput("CFLAG: ");
61238674Smarc 	lput("-cread ", CREAD, 1);
61338674Smarc 	switch(c&CSIZE) {
61438674Smarc 	case CS5: mdput("cs5 "); break;
61538674Smarc 	case CS6: mdput("cs6 "); break;
61638674Smarc 	case CS7: mdput("cs7 "); break;
61738674Smarc 	case CS8: mdput("cs8 "); break;
61838674Smarc 	}
61938674Smarc 	mdput("-parenb "+on(PARENB));
62038674Smarc 	lput("-parodd ", PARODD, 0);
62138674Smarc 	lput("-hupcl ", HUPCL, 1);
62238674Smarc 	lput("-clocal ", CLOCAL, 0);
62338674Smarc 	lput("-cstopb ", CSTOPB, 0);
62438674Smarc 	lput("-crtscts ", CRTSCTS, 0);
62538674Smarc 	mdput(0);
62638674Smarc #undef on
62738674Smarc 	/*
62838674Smarc 	 * special control characters
62938674Smarc 	 */
63038674Smarc 	if (debug) mdput("CCHARS: ");
63138674Smarc 	if (fmt != 2) {
63238674Smarc 		for (i=0; *cchars[i].names; i++) {
63338674Smarc 			char temp[64];
63438674Smarc 
63538674Smarc 			if (fmt || cc[cchars[i].sub] != cchars[i].def) {
63638674Smarc 				sprintf(temp, "%s = %s; ", *cchars[i].names,
63738674Smarc 					ccval(cc[cchars[i].sub]), fmt);
63838674Smarc 				mdput(temp);
63938674Smarc 			}
6401179Sbill 		}
64138674Smarc 		mdput(0);
64238674Smarc 	} else {
64338674Smarc 		for (i=0; *cchars[i].names; i++)
64438674Smarc 			put("%*s", strlen(*(cchars[i].names+1)) + (i>0?1:0),
64538674Smarc 				*(cchars[i].names+1));
64638674Smarc 		printf("\n");
64738674Smarc 		for (i=0; *cchars[i].names; i++)
64838674Smarc 			put("%*s", strlen(*(cchars[i].names+1)) + (i>0?1:0),
64938674Smarc 				ccval(cc[cchars[i].sub], fmt));
65038674Smarc 		printf("\n");
6511179Sbill 	}
6521179Sbill }
6531179Sbill 
65438674Smarc /*
65538674Smarc  * gross, but since we're changing the control descriptor
65638674Smarc  * from 1 to 0, most users will be probably be doing
65738674Smarc  * "stty > /dev/sometty" by accident. If 1 and 2 are both ttys,
65838674Smarc  * but not the same, assume that 1 was incorrectly redirected.
65938674Smarc  */
66038674Smarc checkredirect() {
66138674Smarc 	struct stat st1, st2;
66238674Smarc 
66338674Smarc 	if (isatty(1) && isatty(2) && fstat(1, &st1) != -1 &&
66438674Smarc 	    fstat(2, &st2) != -1 && (st1.st_rdev != st2.st_rdev))
66538674Smarc warning("stdout appears redirected, but stdin is the control descriptor");
66638674Smarc }
66738674Smarc 
66840170Smarc char *
66938674Smarc ccval(c, fmt)
67038674Smarc 	unsigned char c;
6711179Sbill {
67238674Smarc 	static char buf[128];
67338674Smarc 	char *bp;
6741179Sbill 
67538674Smarc 	*buf = 0, bp = buf;
67638674Smarc 	if (c == _POSIX_VDISABLE)
67738674Smarc 		if (fmt == 2)
67838674Smarc 			return("<u>");
67938674Smarc 		else
68038674Smarc 			return("<undef>");
68138674Smarc 	if (c & 0200) {
68238674Smarc 		strcat(buf, "M-");
68338674Smarc 		*bp++ = 'M';
68438674Smarc 		*bp++ = '-';
68538674Smarc 		c &= 0177;
6861179Sbill 	}
68738674Smarc 	if (c == 0177) {
68838674Smarc 		*bp++ = '^';
68938674Smarc 		*bp++ = '?';
6901179Sbill 	}
69138674Smarc 	else if (c < 040) {
69238674Smarc 		*bp++ = '^';
69338674Smarc 		*bp++ = c + '@';
69438674Smarc 	}
69538674Smarc 	else
69638674Smarc 		*bp++ = c;
69738674Smarc 	*bp = 0;
69838674Smarc 	return(buf);
6991179Sbill }
7001179Sbill 
70140170Smarc 
70238674Smarc mdput(s)
70338674Smarc 	char *s;
7041179Sbill {
70538674Smarc 	static int col = 0;
7061179Sbill 
70738674Smarc 	if (s == (char *)0) {
70838674Smarc 		if (col) {
70938674Smarc 			put("\n");
71038674Smarc 			col = 0;
71138674Smarc 		}
7121179Sbill 		return;
7131179Sbill 	}
71438674Smarc 	if ((col += strlen(s)) > WRAPCOL) {
71538674Smarc 		put("\n");
71638674Smarc 		col = strlen(s);
7171179Sbill 	}
71838674Smarc 	put(s);
7191179Sbill }
7201179Sbill 
72140170Smarc #include <varargs.h>
72240170Smarc 
72340170Smarc put(va_alist)
72440170Smarc 	va_dcl
7251179Sbill {
72640170Smarc 	char *fmt;
72740170Smarc 	va_list ap;
72840170Smarc 
72940170Smarc 	va_start(ap);
73040170Smarc 	fmt = va_arg(ap, char *);
73140170Smarc 	(void) vfprintf(OUT, fmt, ap);
73240170Smarc 	va_end(ap);
73338674Smarc }
7341179Sbill 
73540170Smarc 
73640170Smarc warning(va_alist)
73740170Smarc 	va_dcl
73838674Smarc {
73940170Smarc 	char *fmt;
74040170Smarc 	va_list ap;
74140170Smarc 
74238674Smarc 	fprintf(ERR, "stty: warning: ");
74340170Smarc 	va_start(ap);
74440170Smarc 	fmt = va_arg(ap, char *);
74540170Smarc 	(void) vfprintf(ERR, fmt, ap);
74640170Smarc 	va_end(ap);
74738674Smarc 	fprintf(ERR, "\n");
7481179Sbill }
7491179Sbill 
75040170Smarc 
75140170Smarc errexit(va_alist)
75240170Smarc 	va_dcl
75338674Smarc {
75440170Smarc 	char *fmt;
75540170Smarc 	va_list ap;
75640170Smarc 
75738674Smarc 	fprintf(ERR, "stty: ");
75840170Smarc 	va_start(ap);
75940170Smarc 	fmt = va_arg(ap, char *);
76040170Smarc 	(void) vfprintf(ERR, fmt, ap);
76140170Smarc 	va_end(ap);
76238674Smarc 	fprintf(ERR, "\n");
76338674Smarc 	exit(1);
76438674Smarc }
7651179Sbill 
76640170Smarc 
76740170Smarc syserrexit(va_alist)
76840170Smarc 	va_dcl
7691179Sbill {
77040170Smarc 	char *fmt;
77140170Smarc 	va_list ap;
77240170Smarc 
77338674Smarc 	fprintf(ERR, "stty: ");
77440170Smarc 	va_start(ap);
77540170Smarc 	fmt = va_arg(ap, char *);
77640170Smarc 	(void) vfprintf(ERR, fmt, ap);
77740170Smarc 	va_end(ap);
77840170Smarc 	fprintf(ERR, ": %s\n", strerror(errno));
77938674Smarc 	exit(1);
7801179Sbill }
781