xref: /titanic_51/usr/src/cmd/ttymon/stty.c (revision aab83bb83be7342f6cfccaed8d5fe0b2f404855d)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 1999-2002 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  * Copyright (c) 2014, Joyent, Inc.  All rights reserved.
26  */
27 
28 /*
29  * Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
30  * All Rights Reserved
31  *
32  */
33 
34 #include <stdio.h>
35 #include <ctype.h>
36 #include <locale.h>
37 #include <sys/types.h>
38 #include <termio.h>
39 #include <sys/stermio.h>
40 #include <sys/termiox.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <limits.h>
44 #ifdef EUC
45 #include <sys/stat.h>
46 #include <fcntl.h>
47 #include <unistd.h>
48 #include <sys/param.h>
49 #include <sys/stropts.h>
50 #include <sys/eucioctl.h>
51 #include <sys/csiioctl.h>
52 #include <sys/stream.h>
53 #include <sys/termios.h>
54 #include <sys/ldterm.h>
55 #include <getwidth.h>
56 #endif /* EUC */
57 #include "stty.h"
58 
59 extern const char *not_supported[];
60 
61 extern char *getenv();
62 extern void exit();
63 extern void perror();
64 extern int get_ttymode();
65 extern int set_ttymode();
66 
67 static char *STTY = "stty: ";
68 static int pitt = 0;
69 static struct termios cb;
70 static struct termio ocb; /* for non-streams devices */
71 static struct stio stio;
72 static struct termiox termiox;
73 static struct winsize winsize, owinsize;
74 static int term;
75 #ifdef EUC
76 static struct eucioc kwp;
77 static eucwidth_t wp;
78 static ldterm_cs_data_user_t cswp;	/* User side codeset width data */
79 static ldterm_cs_data_user_t kcswp;	/* Kernel side codeset width data */
80 static int invalid_ldterm_dat_file;
81 #endif /* EUC */
82 
83 static void prmodes();
84 static void pramodes();
85 static void pit(unsigned char what, char *itsname, char *sep);
86 static void delay(int m, char *s);
87 static void prspeed(char *c, int s);
88 static void prencode();
89 
90 int
91 main(int argc, char *argv[])
92 {
93 	int i;
94 	int fd;
95 	char *s_arg, *sttyparse();	/* s_arg: ptr to mode to be set */
96 #ifdef	EUC
97 	char *lc;
98 	char tmps[PATH_MAX];
99 #endif	/* EUC */
100 
101 	(void) setlocale(LC_ALL, "");
102 #if !defined(TEXT_DOMAIN)
103 #define	TEXT_DOMAIN "SYS_TEST"
104 #endif
105 	(void) textdomain(TEXT_DOMAIN);
106 
107 #ifdef EUC
108 	lc = setlocale(LC_CTYPE, (const char *)NULL);
109 	if (lc) {
110 		sprintf(tmps, _LDTERM_DAT_PATH, lc);
111 
112 		fd = open(tmps, O_RDONLY, 0);
113 		if (fd != -1) {
114 			if (read(fd, (void *)&cswp, sizeof (cswp)) <
115 			    sizeof (cswp)) {
116 				(void) fprintf(stderr, gettext(
117 				    "cannot read entire %s file\n"), tmps);
118 				exit(2);
119 			}
120 
121 			(void) close(fd);
122 
123 			/*
124 			 * If the ldterm.dat contains invalid data or
125 			 * the current locale name is too long, we clear
126 			 * the 'cswp' and flag the invalid ldterm.dat since
127 			 * we are not going to use the data.
128 			 */
129 			if (cswp.version > LDTERM_DATA_VERSION ||
130 			    cswp.codeset_type < LDTERM_CS_TYPE_MIN ||
131 			    cswp.codeset_type > LDTERM_CS_TYPE_MAX ||
132 			    strlen(lc) >= MAXNAMELEN ||
133 			    (cswp.codeset_type == LDTERM_CS_TYPE_EUC &&
134 			    cswp.csinfo_num > LDTERM_CS_TYPE_EUC_MAX_SUBCS) ||
135 			    (cswp.codeset_type == LDTERM_CS_TYPE_PCCS &&
136 			    (cswp.csinfo_num < LDTERM_CS_TYPE_PCCS_MIN_SUBCS ||
137 			    cswp.csinfo_num > LDTERM_CS_TYPE_PCCS_MAX_SUBCS))) {
138 				(void) memset((void *)&cswp, 0, sizeof (cswp));
139 				invalid_ldterm_dat_file = 1;
140 			} else {
141 				(void) strcpy(cswp.locale_name, lc);
142 			}
143 		}
144 	}
145 
146 	getwidth(&wp);
147 #endif /* EUC */
148 
149 	if ((term = get_ttymode(0, &ocb, &cb, &stio, &termiox, &winsize
150 #ifdef EUC
151 	    /* */, &kwp, &kcswp
152 #endif /* EUC */
153 	    /* */)) < 0) {
154 		perror(STTY);
155 		exit(2);
156 	}
157 	owinsize = winsize;
158 	if (argc == 1) {
159 		prmodes();
160 		exit(0);
161 	}
162 	if ((argc == 2) && (argv[1][0] == '-') && (argv[1][2] == '\0'))
163 	switch (argv[1][1]) {
164 		case 'a':
165 			pramodes();
166 			return (0);
167 		case 'g':
168 			prencode();
169 			return (0);
170 		case '-':
171 			prmodes(); /* stty -- */
172 			return (0);
173 		default:
174 			(void) fprintf(stderr, gettext(
175 			    "usage: stty [-a| -g]\n"));
176 			(void) fprintf(stderr, gettext(
177 			    "       stty [modes]\n"));
178 			return (2);
179 	}
180 
181 	if ((argc == 3) && (argv[1][0] == '-') && (argv[1][2] == '\0') &&
182 	    (argv[2][0] == '-') && (argv[2][1] == '-') && (argv[2][2] == '\0'))
183 	switch (argv[1][1]) {
184 	case 'a':
185 		pramodes();
186 		return (0);
187 	case 'g':
188 		prencode();
189 		return (0);
190 	default:
191 		(void) fprintf(stderr, gettext(
192 		    "usage: stty [-a| -g]\n"));
193 		(void) fprintf(stderr, gettext(
194 		    "       stty [modes]\n"));
195 		return (2);
196 	}
197 	if ((argc >= 3) && (argv[1][0] == '-') && (argv[1][1] == '-') &&
198 	    (argv[1][2] == '\0')) {
199 		/* ignore -- */
200 		--argc;
201 		++argv;
202 	}
203 	if (s_arg = sttyparse(argc, argv, term, &ocb, &cb, &termiox, &winsize
204 #ifdef EUC
205 	    /* */, &wp, &kwp, &cswp, &kcswp
206 #endif /* EUC */
207 	    /* */)) {
208 		char *s = s_arg;
209 		if (*s == '-') s++;
210 		for (i = 0; not_supported[i]; i++) {
211 			if (strcmp(not_supported[i], s) == 0) {
212 				(void) fprintf(stderr,
213 				    gettext(
214 				    "mode not supported on this device: %s\n"),
215 				    s_arg);
216 				exit(2);
217 			}
218 		}
219 		(void) fprintf(stderr, gettext("unknown mode: %s\n"), s_arg);
220 		return (2);
221 	}
222 
223 	if (set_ttymode(0, term, &ocb, &cb, &stio, &termiox, &winsize, &owinsize
224 #ifdef EUC
225 	    /* */, &kwp, &kcswp, invalid_ldterm_dat_file
226 #endif /* EUC */
227 	    /* */) == -1) {
228 		perror(STTY);
229 		return (2);
230 	}
231 	return (0);
232 }
233 
234 void
235 prmodes(void)				/* print modes, no options, argc is 1 */
236 {
237 	int m;
238 
239 	if (!(term & ASYNC)) {
240 		m = stio.imode;
241 		if (m & IUCLC)
242 			(void) printf("iuclc ");
243 		else
244 			(void) printf("-iuclc ");
245 		m = stio.omode;
246 		if (m & OLCUC)
247 			(void) printf("olcuc ");
248 		else
249 			(void) printf("-olcuc ");
250 		if (m & TAB3)
251 			(void) printf("tab3 ");
252 		m = stio.lmode;
253 		if (m & XCASE)
254 			(void) printf("xcase ");
255 		else
256 			(void) printf("-xcase ");
257 		if (m & STFLUSH)
258 			(void) printf("stflush ");
259 		else
260 			(void) printf("-stflush ");
261 		if (m & STWRAP)
262 			(void) printf("stwrap ");
263 		else
264 			(void) printf("-stwrap ");
265 		if (m & STAPPL)
266 			(void) printf("stappl ");
267 		else
268 			(void) printf("-stappl ");
269 		(void) printf("\n");
270 	}
271 	if (term & ASYNC) {
272 		m = cb.c_cflag;
273 		if ((term & TERMIOS) && cfgetispeed(&cb) != 0 &&
274 		    cfgetispeed(&cb) != cfgetospeed(&cb)) {
275 			prspeed("ispeed ", cfgetispeed(&cb));
276 			prspeed("ospeed ", cfgetospeed(&cb));
277 		} else
278 			prspeed("speed ", cfgetospeed(&cb));
279 		if (m&PARENB) {
280 			if ((m&PAREXT) && (term & TERMIOS)) {
281 				if (m&PARODD)
282 					(void) printf("markp ");
283 				else
284 					(void) printf("spacep ");
285 			} else {
286 				if (m&PARODD)
287 					(void) printf("oddp ");
288 				else
289 					(void) printf("evenp ");
290 			}
291 		} else
292 			(void) printf("-parity ");
293 		if (((m&PARENB) && !(m&CS7)) || (!(m&PARENB) && !(m&CS8)))
294 			(void) printf("cs%c ", '5'+(m&CSIZE)/CS6);
295 		if (m&CSTOPB)
296 			(void) printf("cstopb ");
297 		if (m&HUPCL)
298 			(void) printf("hupcl ");
299 		if (!(m&CREAD))
300 			(void) printf("-cread ");
301 		if (m&CLOCAL)
302 			(void) printf("clocal ");
303 		if (m&LOBLK)
304 			(void) printf("loblk ");
305 		(void) printf("\n");
306 		if (ocb.c_line != 0)
307 			(void) printf(gettext("line = %d; "), ocb.c_line);
308 		if (term & WINDOW) {
309 			(void) printf(gettext("rows = %d; columns = %d;"),
310 			    winsize.ws_row, winsize.ws_col);
311 			(void) printf(gettext(
312 			    " ypixels = %d; xpixels = %d;\n"),
313 			    winsize.ws_ypixel, winsize.ws_xpixel);
314 		}
315 		if ((cb.c_lflag&ICANON) == 0)
316 			(void) printf(gettext("min = %d; time = %d;\n"),
317 			    cb.c_cc[VMIN], cb.c_cc[VTIME]);
318 		if (cb.c_cc[VINTR] != CINTR)
319 			pit(cb.c_cc[VINTR], "intr", "; ");
320 		if (cb.c_cc[VQUIT] != CQUIT)
321 			pit(cb.c_cc[VQUIT], "quit", "; ");
322 		if (cb.c_cc[VERASE] != CERASE)
323 			pit(cb.c_cc[VERASE], "erase", "; ");
324 		if (cb.c_cc[VKILL] != CKILL)
325 			pit(cb.c_cc[VKILL], "kill", "; ");
326 		if (cb.c_cc[VEOF] != CEOF)
327 			pit(cb.c_cc[VEOF], "eof", "; ");
328 		if (cb.c_cc[VEOL] != CNUL)
329 			pit(cb.c_cc[VEOL], "eol", "; ");
330 		if (cb.c_cc[VEOL2] != CNUL)
331 			pit(cb.c_cc[VEOL2], "eol2", "; ");
332 		if (cb.c_cc[VSWTCH] != CSWTCH)
333 			pit(cb.c_cc[VSWTCH], "swtch", "; ");
334 		if (term & TERMIOS) {
335 			if (cb.c_cc[VSTART] != CSTART)
336 				pit(cb.c_cc[VSTART], "start", "; ");
337 			if (cb.c_cc[VSTOP] != CSTOP)
338 				pit(cb.c_cc[VSTOP], "stop", "; ");
339 			if (cb.c_cc[VSUSP] != CSUSP)
340 				pit(cb.c_cc[VSUSP], "susp", "; ");
341 			if (cb.c_cc[VDSUSP] != CDSUSP)
342 				pit(cb.c_cc[VDSUSP], "dsusp", "; ");
343 			if (cb.c_cc[VREPRINT] != CRPRNT)
344 				pit(cb.c_cc[VREPRINT], "rprnt", "; ");
345 			if (cb.c_cc[VDISCARD] != CFLUSH)
346 				pit(cb.c_cc[VDISCARD], "flush", "; ");
347 			if (cb.c_cc[VWERASE] != CWERASE)
348 				pit(cb.c_cc[VWERASE], "werase", "; ");
349 			if (cb.c_cc[VLNEXT] != CLNEXT)
350 				pit(cb.c_cc[VLNEXT], "lnext", "; ");
351 			if (cb.c_cc[VSTATUS] != CSTATUS)
352 				pit(cb.c_cc[VSTATUS], "status", "; ");
353 		}
354 		if (pitt) (void) printf("\n");
355 		m = cb.c_iflag;
356 		if (m&IGNBRK)
357 			(void) printf("ignbrk ");
358 		else if (m&BRKINT)
359 			(void) printf("brkint ");
360 		if (!(m&INPCK))
361 			(void) printf("-inpck ");
362 		else if (m&IGNPAR)
363 			(void) printf("ignpar ");
364 		if (m&PARMRK)
365 			(void) printf("parmrk ");
366 		if (!(m&ISTRIP))
367 			(void) printf("-istrip ");
368 		if (m&INLCR)
369 			(void) printf("inlcr ");
370 		if (m&IGNCR)
371 			(void) printf("igncr ");
372 		if (m&ICRNL)
373 			(void) printf("icrnl ");
374 		if (m&IUCLC)
375 			(void) printf("iuclc ");
376 		if (!(m&IXON))
377 			(void) printf("-ixon ");
378 		else if (!(m&IXANY))
379 			(void) printf("-ixany ");
380 		if (m&IXOFF)
381 			(void) printf("ixoff ");
382 		if ((term & TERMIOS) && (m&IMAXBEL))
383 			(void) printf("imaxbel ");
384 		m = cb.c_oflag;
385 		if (!(m&OPOST))
386 			(void) printf("-opost ");
387 		else {
388 			if (m&OLCUC)
389 				(void) printf("olcuc ");
390 			if (m&ONLCR)
391 				(void) printf("onlcr ");
392 			if (m&OCRNL)
393 				(void) printf("ocrnl ");
394 			if (m&ONOCR)
395 				(void) printf("onocr ");
396 			if (m&ONLRET)
397 				(void) printf("onlret ");
398 			if (m&OFILL)
399 				if (m&OFDEL)
400 					(void) printf("del-fill ");
401 				else
402 					(void) printf("nul-fill ");
403 			delay((m&CRDLY)/CR1, "cr");
404 			delay((m&NLDLY)/NL1, "nl");
405 			delay((m&TABDLY)/TAB1, "tab");
406 			delay((m&BSDLY)/BS1, "bs");
407 			delay((m&VTDLY)/VT1, "vt");
408 			delay((m&FFDLY)/FF1, "ff");
409 		}
410 		(void) printf("\n");
411 		m = cb.c_lflag;
412 		if (!(m&ISIG))
413 			(void) printf("-isig ");
414 		if (!(m&ICANON))
415 			(void) printf("-icanon ");
416 		if (m&XCASE)
417 			(void) printf("xcase ");
418 		(void) printf("-echo "+((m&ECHO) != 0));
419 		(void) printf("-echoe "+((m&ECHOE) != 0));
420 		(void) printf("-echok "+((m&ECHOK) != 0));
421 		if (m&ECHONL)
422 			(void) printf("echonl ");
423 		if (m&NOFLSH)
424 			(void) printf("noflsh ");
425 		if (m&TOSTOP)
426 			(void) printf("tostop ");
427 		if (m&ECHOCTL)
428 			(void) printf("echoctl ");
429 		if (m&ECHOPRT)
430 			(void) printf("echoprt ");
431 		if (m&ECHOKE)
432 			(void) printf("echoke ");
433 		if (m&DEFECHO)
434 			(void) printf("defecho ");
435 		if (m&FLUSHO)
436 			(void) printf("flusho ");
437 		if (m&PENDIN)
438 			(void) printf("pendin ");
439 		if (m&IEXTEN)
440 			(void) printf("iexten ");
441 		(void) printf("\n");
442 	}
443 	if (term & FLOW) {
444 		m = termiox.x_hflag;
445 		if (m & RTSXOFF)
446 			(void) printf("rtsxoff ");
447 		if (m & CTSXON)
448 			(void) printf("ctsxon ");
449 		if (m & DTRXOFF)
450 			(void) printf("dtrxoff ");
451 		if (m & CDXON)
452 			(void) printf("cdxon ");
453 		if (m & ISXOFF)
454 			(void) printf("isxoff ");
455 		m = termiox.x_cflag;
456 		switch (m & XMTCLK) {
457 			case XCIBRG: (void)printf("xcibrg ");
458 					break;
459 			case XCTSET: (void)printf("xctset ");
460 					break;
461 			case XCRSET: (void)printf("xcrset ");
462 		}
463 
464 		switch (m & RCVCLK) {
465 			case RCIBRG: (void)printf("rcibrg ");
466 					break;
467 			case RCTSET: (void)printf("rctset ");
468 					break;
469 			case RCRSET: (void)printf("rcrset ");
470 		}
471 
472 		switch (m & TSETCLK) {
473 			case TSETCOFF: (void)printf("tsetcoff ");
474 					break;
475 			case TSETCRBRG: (void)printf("tsetcrbrg ");
476 					break;
477 			case TSETCTBRG: (void)printf("tsetctbrg ");
478 					break;
479 			case TSETCTSET: (void)printf("tsetctset ");
480 					break;
481 			case TSETCRSET: (void)printf("tsetcrset ");
482 		}
483 
484 		switch (m & RSETCLK) {
485 			case RSETCOFF: (void)printf("rsetcoff ");
486 					break;
487 			case RSETCRBRG: (void)printf("rsetcrbrg ");
488 					break;
489 			case RSETCTBRG: (void)printf("rsetctbrg ");
490 					break;
491 			case RSETCTSET: (void)printf("rsetctset ");
492 					break;
493 			case RSETCRSET: (void)printf("rsetcrset ");
494 		}
495 		(void) printf("\n");
496 	}
497 }
498 
499 void
500 pramodes(void)				/* print all modes, -a option */
501 {
502 	int m;
503 
504 	m = cb.c_cflag;
505 	if (term & ASYNC) {
506 		if ((term & TERMIOS) && cfgetispeed(&cb) != 0 &&
507 		    cfgetispeed(&cb) != cfgetospeed(&cb)) {
508 			prspeed("ispeed ", cfgetispeed(&cb));
509 			prspeed("ospeed ", cfgetospeed(&cb));
510 		} else
511 			prspeed("speed ", cfgetospeed(&cb));
512 		if (!(term & TERMIOS))
513 			(void) printf(gettext("line = %d; "), ocb.c_line);
514 		(void) printf("\n");
515 		if (term & WINDOW) {
516 			(void) printf(gettext("rows = %d; columns = %d;"),
517 			    winsize.ws_row, winsize.ws_col);
518 			(void) printf(gettext(
519 			    " ypixels = %d; xpixels = %d;\n"),
520 			    winsize.ws_ypixel, winsize.ws_xpixel);
521 		}
522 #ifdef EUC
523 		if ((term & CSIW) && kcswp.locale_name[0]) {
524 			(void) printf("csdata %s\n", kcswp.locale_name);
525 		} else {
526 			(void) printf("csdata ?\n");
527 		}
528 		/*
529 		 * If kwp.eucw[0] is zero, it means the current codeset type
530 		 * in the ldterm is not EUC.
531 		 */
532 		if ((term & EUCW) && kwp.eucw[0]) {
533 			(void) printf("eucw %d:%d:%d:%d, ", kwp.eucw[0],
534 			    kwp.eucw[1], kwp.eucw[2], kwp.eucw[3]);
535 			(void) printf("scrw %d:%d:%d:%d\n", kwp.scrw[0],
536 			    kwp.scrw[1], kwp.scrw[2], kwp.scrw[3]);
537 		} else
538 			(void) printf("eucw ?, scrw ?\n");
539 #endif /* EUC */
540 		if ((cb.c_lflag&ICANON) == 0)
541 			(void) printf(gettext("min = %d; time = %d;\n"),
542 			    cb.c_cc[VMIN], cb.c_cc[VTIME]);
543 		pit(cb.c_cc[VINTR], "intr", "; ");
544 		pit(cb.c_cc[VQUIT], "quit", "; ");
545 		pit(cb.c_cc[VERASE], "erase", "; ");
546 		pit(cb.c_cc[VKILL], "kill", ";\n");
547 		pit(cb.c_cc[VEOF], "eof", "; ");
548 		pit(cb.c_cc[VEOL], "eol", "; ");
549 		pit(cb.c_cc[VEOL2], "eol2", "; ");
550 		pit(cb.c_cc[VSWTCH], "swtch", ";\n");
551 		if (term & TERMIOS) {
552 			pit(cb.c_cc[VSTART], "start", "; ");
553 			pit(cb.c_cc[VSTOP], "stop", "; ");
554 			pit(cb.c_cc[VSUSP], "susp", "; ");
555 			pit(cb.c_cc[VDSUSP], "dsusp", ";\n");
556 			pit(cb.c_cc[VREPRINT], "rprnt", "; ");
557 			pit(cb.c_cc[VDISCARD], "flush", "; ");
558 			pit(cb.c_cc[VWERASE], "werase", "; ");
559 			pit(cb.c_cc[VLNEXT], "lnext", ";\n");
560 			pit(cb.c_cc[VSTATUS], "status", ";\n");
561 		}
562 	} else
563 		pit((unsigned)stio.tab, "ctab", "\n");
564 	m = cb.c_cflag;
565 	(void) printf("-parenb "+((m&PARENB) != 0));
566 	(void) printf("-parodd "+((m&PARODD) != 0));
567 	(void) printf("cs%c ", '5'+(m&CSIZE)/CS6);
568 	(void) printf("-cstopb "+((m&CSTOPB) != 0));
569 	(void) printf("-hupcl "+((m&HUPCL) != 0));
570 	(void) printf("-cread "+((m&CREAD) != 0));
571 	(void) printf("-clocal "+((m&CLOCAL) != 0));
572 
573 	(void) printf("-loblk "+((m&LOBLK) != 0));
574 	(void) printf("-crtscts "+((m&CRTSCTS) != 0));
575 	(void) printf("-crtsxoff "+((m&CRTSXOFF) != 0));
576 	if (term & TERMIOS)
577 		(void) printf("-parext "+((m&PAREXT) != 0));
578 
579 	(void) printf("\n");
580 	m = cb.c_iflag;
581 	(void) printf("-ignbrk "+((m&IGNBRK) != 0));
582 	(void) printf("-brkint "+((m&BRKINT) != 0));
583 	(void) printf("-ignpar "+((m&IGNPAR) != 0));
584 	(void) printf("-parmrk "+((m&PARMRK) != 0));
585 	(void) printf("-inpck "+((m&INPCK) != 0));
586 	(void) printf("-istrip "+((m&ISTRIP) != 0));
587 	(void) printf("-inlcr "+((m&INLCR) != 0));
588 	(void) printf("-igncr "+((m&IGNCR) != 0));
589 	(void) printf("-icrnl "+((m&ICRNL) != 0));
590 	(void) printf("-iuclc "+((m&IUCLC) != 0));
591 	(void) printf("\n");
592 	(void) printf("-ixon "+((m&IXON) != 0));
593 	(void) printf("-ixany "+((m&IXANY) != 0));
594 	(void) printf("-ixoff "+((m&IXOFF) != 0));
595 	if (term & TERMIOS)
596 		(void) printf("-imaxbel "+((m&IMAXBEL) != 0));
597 	(void) printf("\n");
598 	m = cb.c_lflag;
599 	(void) printf("-isig "+((m&ISIG) != 0));
600 	(void) printf("-icanon "+((m&ICANON) != 0));
601 	(void) printf("-xcase "+((m&XCASE) != 0));
602 	(void) printf("-echo "+((m&ECHO) != 0));
603 	(void) printf("-echoe "+((m&ECHOE) != 0));
604 	(void) printf("-echok "+((m&ECHOK) != 0));
605 	(void) printf("-echonl "+((m&ECHONL) != 0));
606 	(void) printf("-noflsh "+((m&NOFLSH) != 0));
607 	if (term & TERMIOS) {
608 		(void) printf("\n");
609 		(void) printf("-tostop "+((m&TOSTOP) != 0));
610 		(void) printf("-echoctl "+((m&ECHOCTL) != 0));
611 		(void) printf("-echoprt "+((m&ECHOPRT) != 0));
612 		(void) printf("-echoke "+((m&ECHOKE) != 0));
613 		(void) printf("-defecho "+((m&DEFECHO) != 0));
614 		(void) printf("-flusho "+((m&FLUSHO) != 0));
615 		(void) printf("-pendin "+((m&PENDIN) != 0));
616 		(void) printf("-iexten "+((m&IEXTEN) != 0));
617 	}
618 	if (!(term & ASYNC)) {
619 		(void) printf("-stflush "+((m&STFLUSH) != 0));
620 		(void) printf("-stwrap "+((m&STWRAP) != 0));
621 		(void) printf("-stappl "+((m&STAPPL) != 0));
622 	}
623 	(void) printf("\n");
624 	m = cb.c_oflag;
625 	(void) printf("-opost "+((m&OPOST) != 0));
626 	(void) printf("-olcuc "+((m&OLCUC) != 0));
627 	(void) printf("-onlcr "+((m&ONLCR) != 0));
628 	(void) printf("-ocrnl "+((m&OCRNL) != 0));
629 	(void) printf("-onocr "+((m&ONOCR) != 0));
630 	(void) printf("-onlret "+((m&ONLRET) != 0));
631 	(void) printf("-ofill "+((m&OFILL) != 0));
632 	(void) printf("-ofdel "+((m&OFDEL) != 0));
633 	delay((m&CRDLY)/CR1, "cr");
634 	delay((m&NLDLY)/NL1, "nl");
635 	delay((m&TABDLY)/TAB1, "tab");
636 	delay((m&BSDLY)/BS1, "bs");
637 	delay((m&VTDLY)/VT1, "vt");
638 	delay((m&FFDLY)/FF1, "ff");
639 	(void) printf("\n");
640 	if (term & FLOW) {
641 		m = termiox.x_hflag;
642 		(void) printf("-rtsxoff "+((m&RTSXOFF) != 0));
643 		(void) printf("-ctsxon "+((m&CTSXON) != 0));
644 		(void) printf("-dtrxoff "+((m&DTRXOFF) != 0));
645 		(void) printf("-cdxon "+((m&CDXON) != 0));
646 		(void) printf("-isxoff "+((m&ISXOFF) != 0));
647 		m = termiox.x_cflag;
648 		switch (m & XMTCLK) {
649 			case XCIBRG: (void)printf("xcibrg ");
650 					break;
651 			case XCTSET: (void)printf("xctset ");
652 					break;
653 			case XCRSET: (void)printf("xcrset ");
654 		}
655 
656 		switch (m & RCVCLK) {
657 			case RCIBRG: (void)printf("rcibrg ");
658 					break;
659 			case RCTSET: (void)printf("rctset ");
660 					break;
661 			case RCRSET: (void)printf("rcrset ");
662 		}
663 
664 		switch (m & TSETCLK) {
665 			case TSETCOFF: (void)printf("tsetcoff ");
666 					break;
667 			case TSETCRBRG: (void)printf("tsetcrbrg ");
668 					break;
669 			case TSETCTBRG: (void)printf("tsetctbrg ");
670 					break;
671 			case TSETCTSET: (void)printf("tsetctset ");
672 					break;
673 			case TSETCRSET: (void)printf("tsetcrset ");
674 		}
675 
676 		switch (m & RSETCLK) {
677 			case RSETCOFF: (void)printf("rsetcoff ");
678 					break;
679 			case RSETCRBRG: (void)printf("rsetcrbrg ");
680 					break;
681 			case RSETCTBRG: (void)printf("rsetctbrg ");
682 					break;
683 			case RSETCTSET: (void)printf("rsetctset ");
684 					break;
685 			case RSETCRSET: (void)printf("rsetcrset ");
686 		}
687 		(void) printf("\n");
688 	}
689 }
690 
691 /* print function for prmodes() and pramodes() */
692 void
693 pit(unsigned char what, char *itsname, char *sep)
694 {
695 
696 	pitt++;
697 	(void) printf("%s", itsname);
698 	if ((term & TERMIOS) && what == _POSIX_VDISABLE ||
699 	    !(term & TERMIOS) && what == 0200) {
700 		(void) printf(" = <undef>%s", sep);
701 		return;
702 	}
703 	(void) printf(" = ");
704 	if (what & 0200 && !isprint(what)) {
705 		(void) printf("-");
706 		what &= ~ 0200;
707 	}
708 	if (what == 0177) {
709 		(void) printf("^?%s", sep);
710 		return;
711 	} else if (what < ' ') {
712 		(void) printf("^");
713 		what += '`';
714 		if (what > 'z')
715 			what -= 'a' -'A';
716 	}
717 	(void) printf("%c%s", what, sep);
718 }
719 
720 void
721 delay(int m, char *s)
722 {
723 	if (m)
724 		(void) printf("%s%d ", s, m);
725 }
726 
727 void
728 prspeed(char *c, int scode)
729 {
730 	int sval = -1;
731 	int i;
732 
733 	for (i = 0; speeds[i].string; i++) {
734 		if (speeds[i].code == scode) {
735 			sval = speeds[i].value;
736 			break;
737 		}
738 	}
739 
740 	(void) printf("%s%d baud; ", c, sval);
741 }
742 
743 /* print current settings for use with  */
744 void
745 prencode(void)		/* another stty cmd, used for -g option */
746 {
747 	int i, last;
748 
749 	/*
750 	 * Although there are only 16 control chars defined as of April 1995,
751 	 * prencode() and encode() will not have to be changed if up to MAX_CC
752 	 * control chars are defined in the future.  A maximum of MAX_CC rather
753 	 * than NCCS control chars are printed because the last control slot
754 	 * is unused.  "stty -g" prints out a total of NUM_FIELDS fields
755 	 * (NUM_MODES modes + MAX_CC control chars).  First print the input,
756 	 * output, control, and line discipline modes.
757 	 */
758 	(void) printf("%x:%x:%x:%x", cb.c_iflag, cb.c_oflag, cb.c_cflag,
759 	    cb.c_lflag);
760 
761 	/* Print the control character fields. */
762 	if (term & TERMIOS)
763 		last = MAX_CC;
764 	else
765 		last = NCC;
766 #ifdef EUC
767 	if (term & CSIW) {
768 		for (i = 0; i < MAX_CC; i++)
769 			(void) printf(":%x", (i >= last) ? 0 : cb.c_cc[i]);
770 		/*
771 		 * Print out ldterm_cs_data_user_t data fields for
772 		 * PSARC/1999/140 TCR2. This change introduces additional
773 		 * 44 fields that come from the ldterm_cs_data_user_t data
774 		 * structure.
775 		 */
776 		(void) printf(":%x:%x:%x:", kcswp.version, kcswp.codeset_type,
777 		    kcswp.csinfo_num);
778 		if (*kcswp.locale_name == '\0') {
779 			(void) printf("00");
780 		} else {
781 			for (i = 0; kcswp.locale_name[i] && i < MAXNAMELEN; i++)
782 				(void) printf("%02x", kcswp.locale_name[i]);
783 		}
784 		for (i = 0; i < LDTERM_CS_MAX_CODESETS; i++)
785 			(void) printf(":%x:%x:%x:%x",
786 			    kcswp.eucpc_data[i].byte_length,
787 			    kcswp.eucpc_data[i].screen_width,
788 			    kcswp.eucpc_data[i].msb_start,
789 			    kcswp.eucpc_data[i].msb_end);
790 	} else {
791 #endif /* EUC */
792 		for (i = 0; i < last; i++)
793 			(void) printf(":%x", cb.c_cc[i]);
794 #ifdef EUC
795 	}
796 #endif /* EUC */
797 	(void) printf("\n");
798 }
799