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