xref: /csrg-svn/bin/stty/print.c (revision 50041)
148942Sbostic /*-
248942Sbostic  * Copyright (c) 1991 The Regents of the University of California.
348942Sbostic  * All rights reserved.
448942Sbostic  *
548942Sbostic  * %sccs.include.redist.c%
648942Sbostic  */
748942Sbostic 
848942Sbostic #ifndef lint
9*50041Sbostic static char sccsid[] = "@(#)print.c	5.4 (Berkeley) 06/10/91";
1048942Sbostic #endif /* not lint */
1148942Sbostic 
1248942Sbostic #include <sys/types.h>
1348942Sbostic #include <stddef.h>
1448942Sbostic #include <stdio.h>
1548942Sbostic #include <string.h>
1648942Sbostic #include "stty.h"
1748942Sbostic #include "extern.h"
1848942Sbostic 
1948942Sbostic static void  binit __P((char *));
2048942Sbostic static void  bput __P((char *));
2148942Sbostic static char *ccval __P((int));
2248942Sbostic 
2348942Sbostic void
2448942Sbostic print(tp, wp, ldisc, fmt)
2548942Sbostic 	struct termios *tp;
2648942Sbostic 	struct winsize *wp;
2748942Sbostic 	int ldisc;
2848942Sbostic 	enum FMT fmt;
2948942Sbostic {
3048942Sbostic 	register struct cchar *p;
3148942Sbostic 	register long tmp;
3248942Sbostic 	register int cnt;
3348942Sbostic 	register u_char *cc;
3448942Sbostic 	int ispeed, ospeed;
3548942Sbostic 	char buf1[100], buf2[100];
3648942Sbostic 
3748942Sbostic 	cnt = 0;
3848942Sbostic 
3948942Sbostic 	/* Line discipline. */
4048942Sbostic 	if (ldisc != TTYDISC) {
4148942Sbostic 		switch(ldisc) {
4248942Sbostic 		case TABLDISC:
4348942Sbostic 			cnt += printf("tablet disc; ");
4448942Sbostic 			break;
4548942Sbostic 		case SLIPDISC:
4648942Sbostic 			cnt += printf("slip disc; ");
4748942Sbostic 			break;
4848942Sbostic 		default:
4948942Sbostic 			cnt += printf("#%d disc; ", ldisc);
5048942Sbostic 			break;
5148942Sbostic 		}
5248942Sbostic 	}
5348942Sbostic 
5448942Sbostic 	/* Line speed. */
5548942Sbostic 	ispeed = cfgetispeed(tp);
5648942Sbostic 	ospeed = cfgetospeed(tp);
5748942Sbostic 	if (ispeed != ospeed)
5848942Sbostic 		cnt +=
5948942Sbostic 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
6048942Sbostic 	else
6148942Sbostic 		cnt += printf("speed %d baud;", ispeed);
6248942Sbostic 	if (fmt >= BSD)
6348942Sbostic 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
6448942Sbostic 	if (cnt)
6548942Sbostic 		(void)printf("\n");
6648942Sbostic 
6748942Sbostic #define	on(f)	((tmp&f) != 0)
6848942Sbostic #define put(n, f, d) \
6948942Sbostic 	if (fmt >= BSD || on(f) != d) \
7048942Sbostic 		bput(n + on(f));
7148942Sbostic 
7248942Sbostic 	/* "local" flags */
7348942Sbostic 	tmp = tp->c_lflag;
7448942Sbostic 	binit("lflags");
7548942Sbostic 	put("-icanon", ICANON, 1);
7648942Sbostic 	put("-isig", ISIG, 1);
7748942Sbostic 	put("-iexten", IEXTEN, 1);
7848942Sbostic 	put("-echo", ECHO, 1);
7948942Sbostic 	put("-echoe", ECHOE, 0);
8048942Sbostic 	put("-echok", ECHOK, 0);
8148942Sbostic 	put("-echoke", ECHOKE, 0);
8248942Sbostic 	put("-echonl", ECHONL, 0);
8348942Sbostic 	put("-echoctl", ECHOCTL, 0);
8448942Sbostic 	put("-echoprt", ECHOPRT, 0);
8548942Sbostic 	put("-altwerase", ALTWERASE, 0);
8648942Sbostic 	put("-noflsh", NOFLSH, 0);
8748942Sbostic 	put("-tostop", TOSTOP, 0);
8848942Sbostic 	put("-mdmbuf", MDMBUF, 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);
14248942Sbostic 
14348942Sbostic 	/* special control characters */
14448942Sbostic 	cc = tp->c_cc;
14548942Sbostic 	if (fmt == POSIX) {
14648942Sbostic 		binit("cchars");
147*50041Sbostic 		for (p = cchars1; p->name; ++p) {
14848942Sbostic 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
14948942Sbostic 			    p->name, ccval(cc[p->sub]));
15048942Sbostic 			bput(buf1);
15148942Sbostic 		}
15248942Sbostic 		binit(NULL);
15348942Sbostic 	} else {
15448942Sbostic 		binit(NULL);
155*50041Sbostic 		for (p = cchars1, cnt = 0; p->name; ++p) {
15648942Sbostic 			if (fmt != BSD && cc[p->sub] == p->def)
15748942Sbostic 				continue;
15848942Sbostic #define	WD	"%-8s"
15948942Sbostic 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
16048942Sbostic 			(void)sprintf(buf2 + cnt * 8, WD, ccval(cc[p->sub]));
16148942Sbostic 			if (++cnt == LINELENGTH / 8) {
16248942Sbostic 				cnt = 0;
16348942Sbostic 				(void)printf("%s\n", buf1);
16448942Sbostic 				(void)printf("%s\n", buf2);
16548942Sbostic 			}
16648942Sbostic 		}
16748942Sbostic 		if (cnt) {
16848942Sbostic 			(void)printf("%s\n", buf1);
16948942Sbostic 			(void)printf("%s\n", buf2);
17048942Sbostic 		}
17148942Sbostic 	}
17248942Sbostic }
17348942Sbostic 
17448942Sbostic static int col;
17548942Sbostic static char *label;
17648942Sbostic 
17748942Sbostic static void
17848942Sbostic binit(lb)
17948942Sbostic 	char *lb;
18048942Sbostic {
18148942Sbostic 	if (col) {
18248942Sbostic 		(void)printf("\n");
18348942Sbostic 		col = 0;
18448942Sbostic 	}
18548942Sbostic 	label = lb;
18648942Sbostic }
18748942Sbostic 
18848942Sbostic static void
18948942Sbostic bput(s)
19048942Sbostic 	char *s;
19148942Sbostic {
19248942Sbostic 	if (col == 0) {
19348942Sbostic 		col = printf("%s: %s", label, s);
19448942Sbostic 		return;
19548942Sbostic 	}
19648942Sbostic 	if ((col + strlen(s)) > LINELENGTH) {
19748942Sbostic 		(void)printf("\n\t");
19848942Sbostic 		col = printf("%s", s) + 8;
19948942Sbostic 		return;
20048942Sbostic 	}
20148942Sbostic 	col += printf(" %s", s);
20248942Sbostic }
20348942Sbostic 
20448942Sbostic static char *
20548942Sbostic ccval(c)
20648942Sbostic 	int c;
20748942Sbostic {
20848942Sbostic 	static char buf[5];
20948942Sbostic 	char *bp;
21048942Sbostic 
21148942Sbostic 	if (c == _POSIX_VDISABLE)
21248942Sbostic 		return("<undef>");
21348942Sbostic 
21448942Sbostic 	bp = buf;
21548942Sbostic 	if (c & 0200) {
21648942Sbostic 		*bp++ = 'M';
21748942Sbostic 		*bp++ = '-';
21848942Sbostic 		c &= 0177;
21948942Sbostic 	}
22048942Sbostic 	if (c == 0177) {
22148942Sbostic 		*bp++ = '^';
22248942Sbostic 		*bp++ = '?';
22348942Sbostic 	}
22448942Sbostic 	else if (c < 040) {
22548942Sbostic 		*bp++ = '^';
22648942Sbostic 		*bp++ = c + '@';
22748942Sbostic 	}
22848942Sbostic 	else
22948942Sbostic 		*bp++ = c;
23048942Sbostic 	*bp = '\0';
23148942Sbostic 	return(buf);
23248942Sbostic }
233