xref: /freebsd/bin/stty/print.c (revision 22cf89c938886d14f5796fc49f9f020c23ea8eaf)
1 /*-
2  * Copyright (c) 1991, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
33 #endif
34 #endif /* not lint */
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 
38 #include <stddef.h>
39 #include <stdio.h>
40 #include <string.h>
41 
42 #include "stty.h"
43 #include "extern.h"
44 
45 static void  binit(const char *);
46 static void  bput(const char *);
47 static const char *ccval(struct cchar *, int);
48 
49 void
50 print(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt)
51 {
52 	struct cchar *p;
53 	long tmp;
54 	u_char *cc;
55 	int cnt, ispeed, ospeed;
56 	char buf1[100], buf2[100];
57 
58 	cnt = 0;
59 
60 	/* Line discipline. */
61 	if (ldisc != TTYDISC) {
62 		switch(ldisc) {
63 		case SLIPDISC:
64 			cnt += printf("slip disc; ");
65 			break;
66 		case PPPDISC:
67 			cnt += printf("ppp disc; ");
68 			break;
69 		default:
70 			cnt += printf("#%d disc; ", ldisc);
71 			break;
72 		}
73 	}
74 
75 	/* Line speed. */
76 	ispeed = cfgetispeed(tp);
77 	ospeed = cfgetospeed(tp);
78 	if (ispeed != ospeed)
79 		cnt +=
80 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
81 	else
82 		cnt += printf("speed %d baud;", ispeed);
83 	if (fmt >= BSD)
84 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
85 	if (cnt)
86 		(void)printf("\n");
87 
88 #define	on(f)	((tmp & (f)) != 0)
89 #define put(n, f, d) \
90 	if (fmt >= BSD || on(f) != (d)) \
91 		bput((n) + on(f));
92 
93 	/* "local" flags */
94 	tmp = tp->c_lflag;
95 	binit("lflags");
96 	put("-icanon", ICANON, 1);
97 	put("-isig", ISIG, 1);
98 	put("-iexten", IEXTEN, 1);
99 	put("-echo", ECHO, 1);
100 	put("-echoe", ECHOE, 0);
101 	put("-echok", ECHOK, 0);
102 	put("-echoke", ECHOKE, 0);
103 	put("-echonl", ECHONL, 0);
104 	put("-echoctl", ECHOCTL, 0);
105 	put("-echoprt", ECHOPRT, 0);
106 	put("-altwerase", ALTWERASE, 0);
107 	put("-noflsh", NOFLSH, 0);
108 	put("-tostop", TOSTOP, 0);
109 	put("-flusho", FLUSHO, 0);
110 	put("-pendin", PENDIN, 0);
111 	put("-nokerninfo", NOKERNINFO, 0);
112 	put("-extproc", EXTPROC, 0);
113 
114 	/* input flags */
115 	tmp = tp->c_iflag;
116 	binit("iflags");
117 	put("-istrip", ISTRIP, 0);
118 	put("-icrnl", ICRNL, 1);
119 	put("-inlcr", INLCR, 0);
120 	put("-igncr", IGNCR, 0);
121 	put("-ixon", IXON, 1);
122 	put("-ixoff", IXOFF, 0);
123 	put("-ixany", IXANY, 1);
124 	put("-imaxbel", IMAXBEL, 1);
125 	put("-ignbrk", IGNBRK, 0);
126 	put("-brkint", BRKINT, 1);
127 	put("-inpck", INPCK, 0);
128 	put("-ignpar", IGNPAR, 0);
129 	put("-parmrk", PARMRK, 0);
130 
131 	/* output flags */
132 	tmp = tp->c_oflag;
133 	binit("oflags");
134 	put("-opost", OPOST, 1);
135 	put("-onlcr", ONLCR, 1);
136 	put("-ocrnl", OCRNL, 0);
137 	switch(tmp&TABDLY) {
138 	case TAB0:
139 		bput("tab0");
140 		break;
141 	case TAB3:
142 		bput("tab3");
143 		break;
144 	}
145 	put("-onocr", ONOCR, 0);
146 	put("-onlret", ONLRET, 0);
147 
148 	/* control flags (hardware state) */
149 	tmp = tp->c_cflag;
150 	binit("cflags");
151 	put("-cread", CREAD, 1);
152 	switch(tmp&CSIZE) {
153 	case CS5:
154 		bput("cs5");
155 		break;
156 	case CS6:
157 		bput("cs6");
158 		break;
159 	case CS7:
160 		bput("cs7");
161 		break;
162 	case CS8:
163 		bput("cs8");
164 		break;
165 	}
166 	bput("-parenb" + on(PARENB));
167 	put("-parodd", PARODD, 0);
168 	put("-hupcl", HUPCL, 1);
169 	put("-clocal", CLOCAL, 0);
170 	put("-cstopb", CSTOPB, 0);
171 	switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) {
172 	case CCTS_OFLOW:
173 		bput("ctsflow");
174 		break;
175 	case CRTS_IFLOW:
176 		bput("rtsflow");
177 		break;
178 	default:
179 		put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0);
180 		break;
181 	}
182 	put("-dsrflow", CDSR_OFLOW, 0);
183 	put("-dtrflow", CDTR_IFLOW, 0);
184 	put("-mdmbuf", MDMBUF, 0);	/* XXX mdmbuf ==  dtrflow */
185 	if (on(CNO_RTSDTR))
186 		bput("-rtsdtr");
187 	else {
188 		if (fmt >= BSD)
189 			bput("rtsdtr");
190 	}
191 
192 	/* special control characters */
193 	cc = tp->c_cc;
194 	if (fmt == POSIX) {
195 		binit("cchars");
196 		for (p = cchars1; p->name; ++p) {
197 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
198 			    p->name, ccval(p, cc[p->sub]));
199 			bput(buf1);
200 		}
201 		binit(NULL);
202 	} else {
203 		binit(NULL);
204 		for (p = cchars1, cnt = 0; p->name; ++p) {
205 			if (fmt != BSD && cc[p->sub] == p->def)
206 				continue;
207 #define	WD	"%-8s"
208 			(void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8,
209 			    WD, p->name);
210 			(void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8,
211 			    WD, ccval(p, cc[p->sub]));
212 			if (++cnt == LINELENGTH / 8) {
213 				cnt = 0;
214 				(void)printf("%s\n", buf1);
215 				(void)printf("%s\n", buf2);
216 			}
217 		}
218 		if (cnt) {
219 			(void)printf("%s\n", buf1);
220 			(void)printf("%s\n", buf2);
221 		}
222 	}
223 }
224 
225 static int col;
226 static const char *label;
227 
228 static void
229 binit(const char *lb)
230 {
231 
232 	if (col) {
233 		(void)printf("\n");
234 		col = 0;
235 	}
236 	label = lb;
237 }
238 
239 static void
240 bput(const char *s)
241 {
242 
243 	if (col == 0) {
244 		col = printf("%s: %s", label, s);
245 		return;
246 	}
247 	if ((col + strlen(s)) > LINELENGTH) {
248 		(void)printf("\n\t");
249 		col = printf("%s", s) + 8;
250 		return;
251 	}
252 	col += printf(" %s", s);
253 }
254 
255 static const char *
256 ccval(struct cchar *p, int c)
257 {
258 	static char buf[5];
259 	char *bp;
260 
261 	if (p->sub == VMIN || p->sub == VTIME) {
262 		(void)snprintf(buf, sizeof(buf), "%d", c);
263 		return (buf);
264 	}
265 	if (c == _POSIX_VDISABLE)
266 		return ("<undef>");
267 	bp = buf;
268 	if (c & 0200) {
269 		*bp++ = 'M';
270 		*bp++ = '-';
271 		c &= 0177;
272 	}
273 	if (c == 0177) {
274 		*bp++ = '^';
275 		*bp++ = '?';
276 	}
277 	else if (c < 040) {
278 		*bp++ = '^';
279 		*bp++ = c + '@';
280 	}
281 	else
282 		*bp++ = c;
283 	*bp = '\0';
284 	return (buf);
285 }
286