xref: /csrg-svn/bin/stty/print.c (revision 66452)
148942Sbostic /*-
260721Sbostic  * Copyright (c) 1991, 1993
360721Sbostic  *	The Regents of the University of California.  All rights reserved.
448942Sbostic  *
548942Sbostic  * %sccs.include.redist.c%
648942Sbostic  */
748942Sbostic 
848942Sbostic #ifndef lint
9*66452Sbostic static char sccsid[] = "@(#)print.c	8.3 (Berkeley) 03/26/94";
1048942Sbostic #endif /* not lint */
1148942Sbostic 
1248942Sbostic #include <sys/types.h>
13*66452Sbostic 
1448942Sbostic #include <stddef.h>
1548942Sbostic #include <stdio.h>
1648942Sbostic #include <string.h>
17*66452Sbostic 
1848942Sbostic #include "stty.h"
1948942Sbostic #include "extern.h"
2048942Sbostic 
2148942Sbostic static void  binit __P((char *));
2248942Sbostic static void  bput __P((char *));
2366372Sbostic static char *ccval __P((struct cchar *, int));
2448942Sbostic 
2548942Sbostic void
2648942Sbostic print(tp, wp, ldisc, fmt)
2748942Sbostic 	struct termios *tp;
2848942Sbostic 	struct winsize *wp;
2948942Sbostic 	int ldisc;
3048942Sbostic 	enum FMT fmt;
3148942Sbostic {
3248942Sbostic 	register struct cchar *p;
3348942Sbostic 	register long tmp;
3448942Sbostic 	register int cnt;
3548942Sbostic 	register u_char *cc;
3648942Sbostic 	int ispeed, ospeed;
3748942Sbostic 	char buf1[100], buf2[100];
3848942Sbostic 
3948942Sbostic 	cnt = 0;
4048942Sbostic 
4148942Sbostic 	/* Line discipline. */
4248942Sbostic 	if (ldisc != TTYDISC) {
4348942Sbostic 		switch(ldisc) {
4448942Sbostic 		case TABLDISC:
4548942Sbostic 			cnt += printf("tablet disc; ");
4648942Sbostic 			break;
4748942Sbostic 		case SLIPDISC:
4848942Sbostic 			cnt += printf("slip disc; ");
4948942Sbostic 			break;
5048942Sbostic 		default:
5148942Sbostic 			cnt += printf("#%d disc; ", ldisc);
5248942Sbostic 			break;
5348942Sbostic 		}
5448942Sbostic 	}
5548942Sbostic 
5648942Sbostic 	/* Line speed. */
5748942Sbostic 	ispeed = cfgetispeed(tp);
5848942Sbostic 	ospeed = cfgetospeed(tp);
5948942Sbostic 	if (ispeed != ospeed)
6048942Sbostic 		cnt +=
6148942Sbostic 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
6248942Sbostic 	else
6348942Sbostic 		cnt += printf("speed %d baud;", ispeed);
6448942Sbostic 	if (fmt >= BSD)
6548942Sbostic 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
6648942Sbostic 	if (cnt)
6748942Sbostic 		(void)printf("\n");
6848942Sbostic 
6948942Sbostic #define	on(f)	((tmp&f) != 0)
7048942Sbostic #define put(n, f, d) \
7148942Sbostic 	if (fmt >= BSD || on(f) != d) \
7248942Sbostic 		bput(n + on(f));
7348942Sbostic 
7448942Sbostic 	/* "local" flags */
7548942Sbostic 	tmp = tp->c_lflag;
7648942Sbostic 	binit("lflags");
7748942Sbostic 	put("-icanon", ICANON, 1);
7848942Sbostic 	put("-isig", ISIG, 1);
7948942Sbostic 	put("-iexten", IEXTEN, 1);
8048942Sbostic 	put("-echo", ECHO, 1);
8148942Sbostic 	put("-echoe", ECHOE, 0);
8248942Sbostic 	put("-echok", ECHOK, 0);
8348942Sbostic 	put("-echoke", ECHOKE, 0);
8448942Sbostic 	put("-echonl", ECHONL, 0);
8548942Sbostic 	put("-echoctl", ECHOCTL, 0);
8648942Sbostic 	put("-echoprt", ECHOPRT, 0);
8748942Sbostic 	put("-altwerase", ALTWERASE, 0);
8848942Sbostic 	put("-noflsh", NOFLSH, 0);
8948942Sbostic 	put("-tostop", TOSTOP, 0);
9048942Sbostic 	put("-mdmbuf", MDMBUF, 0);
9148942Sbostic 	put("-flusho", FLUSHO, 0);
9248942Sbostic 	put("-pendin", PENDIN, 0);
9348942Sbostic 	put("-nokerninfo", NOKERNINFO, 0);
9448942Sbostic 	put("-extproc", EXTPROC, 0);
9548942Sbostic 
9648942Sbostic 	/* input flags */
9748942Sbostic 	tmp = tp->c_iflag;
9848942Sbostic 	binit("iflags");
9948942Sbostic 	put("-istrip", ISTRIP, 0);
10048942Sbostic 	put("-icrnl", ICRNL, 1);
10148942Sbostic 	put("-inlcr", INLCR, 0);
10248942Sbostic 	put("-igncr", IGNCR, 0);
10348942Sbostic 	put("-ixon", IXON, 1);
10448942Sbostic 	put("-ixoff", IXOFF, 0);
10548942Sbostic 	put("-ixany", IXANY, 1);
10648942Sbostic 	put("-imaxbel", IMAXBEL, 1);
10748942Sbostic 	put("-ignbrk", IGNBRK, 0);
10848942Sbostic 	put("-brkint", BRKINT, 1);
10948942Sbostic 	put("-inpck", INPCK, 0);
11048942Sbostic 	put("-ignpar", IGNPAR, 0);
11148942Sbostic 	put("-parmrk", PARMRK, 0);
11248942Sbostic 
11348942Sbostic 	/* output flags */
11448942Sbostic 	tmp = tp->c_oflag;
11548942Sbostic 	binit("oflags");
11648942Sbostic 	put("-opost", OPOST, 1);
11748942Sbostic 	put("-onlcr", ONLCR, 1);
11848942Sbostic 	put("-oxtabs", OXTABS, 1);
11948942Sbostic 
12048942Sbostic 	/* control flags (hardware state) */
12148942Sbostic 	tmp = tp->c_cflag;
12248942Sbostic 	binit("cflags");
12348942Sbostic 	put("-cread", CREAD, 1);
12448942Sbostic 	switch(tmp&CSIZE) {
12548942Sbostic 	case CS5:
12648942Sbostic 		bput("cs5");
12748942Sbostic 		break;
12848942Sbostic 	case CS6:
12948942Sbostic 		bput("cs6");
13048942Sbostic 		break;
13148942Sbostic 	case CS7:
13248942Sbostic 		bput("cs7");
13348942Sbostic 		break;
13448942Sbostic 	case CS8:
13548942Sbostic 		bput("cs8");
13648942Sbostic 		break;
13748942Sbostic 	}
13848942Sbostic 	bput("-parenb" + on(PARENB));
13948942Sbostic 	put("-parodd", PARODD, 0);
14048942Sbostic 	put("-hupcl", HUPCL, 1);
14148942Sbostic 	put("-clocal", CLOCAL, 0);
14248942Sbostic 	put("-cstopb", CSTOPB, 0);
14348942Sbostic 	put("-crtscts", CRTSCTS, 0);
14448942Sbostic 
14548942Sbostic 	/* special control characters */
14648942Sbostic 	cc = tp->c_cc;
14748942Sbostic 	if (fmt == POSIX) {
14848942Sbostic 		binit("cchars");
14950041Sbostic 		for (p = cchars1; p->name; ++p) {
15048942Sbostic 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
15166372Sbostic 			    p->name, ccval(p, cc[p->sub]));
15248942Sbostic 			bput(buf1);
15348942Sbostic 		}
15448942Sbostic 		binit(NULL);
15548942Sbostic 	} else {
15648942Sbostic 		binit(NULL);
15750041Sbostic 		for (p = cchars1, cnt = 0; p->name; ++p) {
15848942Sbostic 			if (fmt != BSD && cc[p->sub] == p->def)
15948942Sbostic 				continue;
16048942Sbostic #define	WD	"%-8s"
16148942Sbostic 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
16266372Sbostic 			(void)sprintf(buf2 + cnt * 8, WD, ccval(p, cc[p->sub]));
16348942Sbostic 			if (++cnt == LINELENGTH / 8) {
16448942Sbostic 				cnt = 0;
16548942Sbostic 				(void)printf("%s\n", buf1);
16648942Sbostic 				(void)printf("%s\n", buf2);
16748942Sbostic 			}
16848942Sbostic 		}
16948942Sbostic 		if (cnt) {
17048942Sbostic 			(void)printf("%s\n", buf1);
17148942Sbostic 			(void)printf("%s\n", buf2);
17248942Sbostic 		}
17348942Sbostic 	}
17448942Sbostic }
17548942Sbostic 
17648942Sbostic static int col;
17748942Sbostic static char *label;
17848942Sbostic 
17948942Sbostic static void
18048942Sbostic binit(lb)
18148942Sbostic 	char *lb;
18248942Sbostic {
18348942Sbostic 	if (col) {
18448942Sbostic 		(void)printf("\n");
18548942Sbostic 		col = 0;
18648942Sbostic 	}
18748942Sbostic 	label = lb;
18848942Sbostic }
18948942Sbostic 
19048942Sbostic static void
19148942Sbostic bput(s)
19248942Sbostic 	char *s;
19348942Sbostic {
19448942Sbostic 	if (col == 0) {
19548942Sbostic 		col = printf("%s: %s", label, s);
19648942Sbostic 		return;
19748942Sbostic 	}
19848942Sbostic 	if ((col + strlen(s)) > LINELENGTH) {
19948942Sbostic 		(void)printf("\n\t");
20048942Sbostic 		col = printf("%s", s) + 8;
20148942Sbostic 		return;
20248942Sbostic 	}
20348942Sbostic 	col += printf(" %s", s);
20448942Sbostic }
20548942Sbostic 
20648942Sbostic static char *
20766372Sbostic ccval(p, c)
20866372Sbostic 	struct cchar *p;
20948942Sbostic 	int c;
21048942Sbostic {
21148942Sbostic 	static char buf[5];
21248942Sbostic 	char *bp;
21348942Sbostic 
21448942Sbostic 	if (c == _POSIX_VDISABLE)
21559521Sbostic 		return ("<undef>");
21648942Sbostic 
21766372Sbostic 	if (p->sub == VMIN || p->sub == VTIME) {
21866372Sbostic 		(void)snprintf(buf, sizeof(buf), "%d", c);
21966372Sbostic 		return (buf);
22066372Sbostic 	}
22148942Sbostic 	bp = buf;
22248942Sbostic 	if (c & 0200) {
22348942Sbostic 		*bp++ = 'M';
22448942Sbostic 		*bp++ = '-';
22548942Sbostic 		c &= 0177;
22648942Sbostic 	}
22748942Sbostic 	if (c == 0177) {
22848942Sbostic 		*bp++ = '^';
22948942Sbostic 		*bp++ = '?';
23048942Sbostic 	}
23148942Sbostic 	else if (c < 040) {
23248942Sbostic 		*bp++ = '^';
23348942Sbostic 		*bp++ = c + '@';
23448942Sbostic 	}
23548942Sbostic 	else
23648942Sbostic 		*bp++ = c;
23748942Sbostic 	*bp = '\0';
23859521Sbostic 	return (buf);
23948942Sbostic }
240