xref: /csrg-svn/bin/stty/print.c (revision 48942)
1*48942Sbostic /*-
2*48942Sbostic  * Copyright (c) 1991 The Regents of the University of California.
3*48942Sbostic  * All rights reserved.
4*48942Sbostic  *
5*48942Sbostic  * %sccs.include.redist.c%
6*48942Sbostic  */
7*48942Sbostic 
8*48942Sbostic #ifndef lint
9*48942Sbostic static char sccsid[] = "@(#)print.c	5.1 (Berkeley) 05/02/91";
10*48942Sbostic #endif /* not lint */
11*48942Sbostic 
12*48942Sbostic #include <sys/types.h>
13*48942Sbostic #include <sys/ioctl.h>
14*48942Sbostic #include <termios.h>
15*48942Sbostic #include <stddef.h>
16*48942Sbostic #include <stdio.h>
17*48942Sbostic #include <string.h>
18*48942Sbostic #include "stty.h"
19*48942Sbostic #include "extern.h"
20*48942Sbostic 
21*48942Sbostic static void  binit __P((char *));
22*48942Sbostic static void  bput __P((char *));
23*48942Sbostic static char *ccval __P((int));
24*48942Sbostic 
25*48942Sbostic void
26*48942Sbostic print(tp, wp, ldisc, fmt)
27*48942Sbostic 	struct termios *tp;
28*48942Sbostic 	struct winsize *wp;
29*48942Sbostic 	int ldisc;
30*48942Sbostic 	enum FMT fmt;
31*48942Sbostic {
32*48942Sbostic 	extern struct cchar cchars1[];
33*48942Sbostic 	register struct cchar *p;
34*48942Sbostic 	register long tmp;
35*48942Sbostic 	register int cnt;
36*48942Sbostic 	register u_char *cc;
37*48942Sbostic 	int ispeed, ospeed;
38*48942Sbostic 	char buf1[100], buf2[100];
39*48942Sbostic 
40*48942Sbostic 	cnt = 0;
41*48942Sbostic 
42*48942Sbostic 	/* Line discipline. */
43*48942Sbostic 	if (ldisc != TTYDISC) {
44*48942Sbostic 		switch(ldisc) {
45*48942Sbostic 		case TABLDISC:
46*48942Sbostic 			cnt += printf("tablet disc; ");
47*48942Sbostic 			break;
48*48942Sbostic 		case SLIPDISC:
49*48942Sbostic 			cnt += printf("slip disc; ");
50*48942Sbostic 			break;
51*48942Sbostic 		default:
52*48942Sbostic 			cnt += printf("#%d disc; ", ldisc);
53*48942Sbostic 			break;
54*48942Sbostic 		}
55*48942Sbostic 	}
56*48942Sbostic 
57*48942Sbostic 	/* Line speed. */
58*48942Sbostic 	ispeed = cfgetispeed(tp);
59*48942Sbostic 	ospeed = cfgetospeed(tp);
60*48942Sbostic 	if (ispeed != ospeed)
61*48942Sbostic 		cnt +=
62*48942Sbostic 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
63*48942Sbostic 	else
64*48942Sbostic 		cnt += printf("speed %d baud;", ispeed);
65*48942Sbostic 	if (fmt >= BSD)
66*48942Sbostic 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
67*48942Sbostic 	if (cnt)
68*48942Sbostic 		(void)printf("\n");
69*48942Sbostic 
70*48942Sbostic #define	on(f)	((tmp&f) != 0)
71*48942Sbostic #define put(n, f, d) \
72*48942Sbostic 	if (fmt >= BSD || on(f) != d) \
73*48942Sbostic 		bput(n + on(f));
74*48942Sbostic 
75*48942Sbostic 	/* "local" flags */
76*48942Sbostic 	tmp = tp->c_lflag;
77*48942Sbostic 	binit("lflags");
78*48942Sbostic 	put("-icanon", ICANON, 1);
79*48942Sbostic 	put("-isig", ISIG, 1);
80*48942Sbostic 	put("-iexten", IEXTEN, 1);
81*48942Sbostic 	put("-echo", ECHO, 1);
82*48942Sbostic 	put("-echoe", ECHOE, 0);
83*48942Sbostic 	put("-echok", ECHOK, 0);
84*48942Sbostic 	put("-echoke", ECHOKE, 0);
85*48942Sbostic 	put("-echonl", ECHONL, 0);
86*48942Sbostic 	put("-echoctl", ECHOCTL, 0);
87*48942Sbostic 	put("-echoprt", ECHOPRT, 0);
88*48942Sbostic 	put("-altwerase", ALTWERASE, 0);
89*48942Sbostic 	put("-noflsh", NOFLSH, 0);
90*48942Sbostic 	put("-tostop", TOSTOP, 0);
91*48942Sbostic 	put("-mdmbuf", MDMBUF, 0);
92*48942Sbostic 	put("-flusho", FLUSHO, 0);
93*48942Sbostic 	put("-pendin", PENDIN, 0);
94*48942Sbostic 	put("-nokerninfo", NOKERNINFO, 0);
95*48942Sbostic 	put("-extproc", EXTPROC, 0);
96*48942Sbostic 
97*48942Sbostic 	/* input flags */
98*48942Sbostic 	tmp = tp->c_iflag;
99*48942Sbostic 	binit("iflags");
100*48942Sbostic 	put("-istrip", ISTRIP, 0);
101*48942Sbostic 	put("-icrnl", ICRNL, 1);
102*48942Sbostic 	put("-inlcr", INLCR, 0);
103*48942Sbostic 	put("-igncr", IGNCR, 0);
104*48942Sbostic 	put("-ixon", IXON, 1);
105*48942Sbostic 	put("-ixoff", IXOFF, 0);
106*48942Sbostic 	put("-ixany", IXANY, 1);
107*48942Sbostic 	put("-imaxbel", IMAXBEL, 1);
108*48942Sbostic 	put("-ignbrk", IGNBRK, 0);
109*48942Sbostic 	put("-brkint", BRKINT, 1);
110*48942Sbostic 	put("-inpck", INPCK, 0);
111*48942Sbostic 	put("-ignpar", IGNPAR, 0);
112*48942Sbostic 	put("-parmrk", PARMRK, 0);
113*48942Sbostic 
114*48942Sbostic 	/* output flags */
115*48942Sbostic 	tmp = tp->c_oflag;
116*48942Sbostic 	binit("oflags");
117*48942Sbostic 	put("-opost", OPOST, 1);
118*48942Sbostic 	put("-onlcr", ONLCR, 1);
119*48942Sbostic 	put("-oxtabs", OXTABS, 1);
120*48942Sbostic 
121*48942Sbostic 	/* control flags (hardware state) */
122*48942Sbostic 	tmp = tp->c_cflag;
123*48942Sbostic 	binit("cflags");
124*48942Sbostic 	put("-cread", CREAD, 1);
125*48942Sbostic 	switch(tmp&CSIZE) {
126*48942Sbostic 	case CS5:
127*48942Sbostic 		bput("cs5");
128*48942Sbostic 		break;
129*48942Sbostic 	case CS6:
130*48942Sbostic 		bput("cs6");
131*48942Sbostic 		break;
132*48942Sbostic 	case CS7:
133*48942Sbostic 		bput("cs7");
134*48942Sbostic 		break;
135*48942Sbostic 	case CS8:
136*48942Sbostic 		bput("cs8");
137*48942Sbostic 		break;
138*48942Sbostic 	}
139*48942Sbostic 	bput("-parenb" + on(PARENB));
140*48942Sbostic 	put("-parodd", PARODD, 0);
141*48942Sbostic 	put("-hupcl", HUPCL, 1);
142*48942Sbostic 	put("-clocal", CLOCAL, 0);
143*48942Sbostic 	put("-cstopb", CSTOPB, 0);
144*48942Sbostic 	put("-crtscts", CRTSCTS, 0);
145*48942Sbostic 
146*48942Sbostic 	/* special control characters */
147*48942Sbostic 	cc = tp->c_cc;
148*48942Sbostic 	if (fmt == POSIX) {
149*48942Sbostic 		binit("cchars");
150*48942Sbostic 		for (p = cchars1; p->name; ++p) {
151*48942Sbostic 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
152*48942Sbostic 			    p->name, ccval(cc[p->sub]));
153*48942Sbostic 			bput(buf1);
154*48942Sbostic 		}
155*48942Sbostic 		binit(NULL);
156*48942Sbostic 	} else {
157*48942Sbostic 		binit(NULL);
158*48942Sbostic 		for (p = cchars1, cnt = 0; p->name; ++p) {
159*48942Sbostic 			if (fmt != BSD && cc[p->sub] == p->def)
160*48942Sbostic 				continue;
161*48942Sbostic #define	WD	"%-8s"
162*48942Sbostic 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
163*48942Sbostic 			(void)sprintf(buf2 + cnt * 8, WD, ccval(cc[p->sub]));
164*48942Sbostic 			if (++cnt == LINELENGTH / 8) {
165*48942Sbostic 				cnt = 0;
166*48942Sbostic 				(void)printf("%s\n", buf1);
167*48942Sbostic 				(void)printf("%s\n", buf2);
168*48942Sbostic 			}
169*48942Sbostic 		}
170*48942Sbostic 		if (cnt) {
171*48942Sbostic 			(void)printf("%s\n", buf1);
172*48942Sbostic 			(void)printf("%s\n", buf2);
173*48942Sbostic 		}
174*48942Sbostic 	}
175*48942Sbostic }
176*48942Sbostic 
177*48942Sbostic static int col;
178*48942Sbostic static char *label;
179*48942Sbostic 
180*48942Sbostic static void
181*48942Sbostic binit(lb)
182*48942Sbostic 	char *lb;
183*48942Sbostic {
184*48942Sbostic 	if (col) {
185*48942Sbostic 		(void)printf("\n");
186*48942Sbostic 		col = 0;
187*48942Sbostic 	}
188*48942Sbostic 	label = lb;
189*48942Sbostic }
190*48942Sbostic 
191*48942Sbostic static void
192*48942Sbostic bput(s)
193*48942Sbostic 	char *s;
194*48942Sbostic {
195*48942Sbostic 	if (col == 0) {
196*48942Sbostic 		col = printf("%s: %s", label, s);
197*48942Sbostic 		return;
198*48942Sbostic 	}
199*48942Sbostic 	if ((col + strlen(s)) > LINELENGTH) {
200*48942Sbostic 		(void)printf("\n\t");
201*48942Sbostic 		col = printf("%s", s) + 8;
202*48942Sbostic 		return;
203*48942Sbostic 	}
204*48942Sbostic 	col += printf(" %s", s);
205*48942Sbostic }
206*48942Sbostic 
207*48942Sbostic static char *
208*48942Sbostic ccval(c)
209*48942Sbostic 	int c;
210*48942Sbostic {
211*48942Sbostic 	static char buf[5];
212*48942Sbostic 	char *bp;
213*48942Sbostic 
214*48942Sbostic 	if (c == _POSIX_VDISABLE)
215*48942Sbostic 		return("<undef>");
216*48942Sbostic 
217*48942Sbostic 	bp = buf;
218*48942Sbostic 	if (c & 0200) {
219*48942Sbostic 		*bp++ = 'M';
220*48942Sbostic 		*bp++ = '-';
221*48942Sbostic 		c &= 0177;
222*48942Sbostic 	}
223*48942Sbostic 	if (c == 0177) {
224*48942Sbostic 		*bp++ = '^';
225*48942Sbostic 		*bp++ = '?';
226*48942Sbostic 	}
227*48942Sbostic 	else if (c < 040) {
228*48942Sbostic 		*bp++ = '^';
229*48942Sbostic 		*bp++ = c + '@';
230*48942Sbostic 	}
231*48942Sbostic 	else
232*48942Sbostic 		*bp++ = c;
233*48942Sbostic 	*bp = '\0';
234*48942Sbostic 	return(buf);
235*48942Sbostic }
236