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