xref: /netbsd-src/bin/stty/print.c (revision 037708cbd4616ccd0d7d0381ebd3964d6696c188)
1 /*	$NetBSD: print.c,v 1.12 1997/04/02 03:13:16 kleink Exp $	*/
2 
3 /*-
4  * Copyright (c) 1991, 1993, 1994
5  *	The Regents of the University of California.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
39 #else
40 static char rcsid[] = "$NetBSD: print.c,v 1.12 1997/04/02 03:13:16 kleink Exp $";
41 #endif
42 #endif /* not lint */
43 
44 #include <sys/types.h>
45 
46 #include <stddef.h>
47 #include <stdio.h>
48 #include <string.h>
49 
50 #include "stty.h"
51 #include "extern.h"
52 
53 static void  binit __P((char *));
54 static void  bput __P((char *));
55 static char *ccval __P((const 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 	const 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 TABLDISC:
76 			cnt += printf("tablet 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("-ocrnl", OCRNL, 0);
152 	put("-oxtabs", OXTABS, 1);
153 
154 	/* control flags (hardware state) */
155 	tmp = tp->c_cflag;
156 	binit("cflags");
157 	put("-cread", CREAD, 1);
158 	switch(tmp&CSIZE) {
159 	case CS5:
160 		bput("cs5");
161 		break;
162 	case CS6:
163 		bput("cs6");
164 		break;
165 	case CS7:
166 		bput("cs7");
167 		break;
168 	case CS8:
169 		bput("cs8");
170 		break;
171 	}
172 	bput("-parenb" + on(PARENB));
173 	put("-parodd", PARODD, 0);
174 	put("-hupcl", HUPCL, 1);
175 	put("-clocal", CLOCAL, 0);
176 	put("-cstopb", CSTOPB, 0);
177 	put("-crtscts", CRTSCTS, 0);
178 	put("-mdmbuf", MDMBUF, 0);
179 
180 	/* special control characters */
181 	cc = tp->c_cc;
182 	if (fmt == POSIX) {
183 		binit("cchars");
184 		for (p = cchars1; p->name; ++p) {
185 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
186 			    p->name, ccval(p, cc[p->sub]));
187 			bput(buf1);
188 		}
189 		binit(NULL);
190 	} else {
191 		binit(NULL);
192 		for (p = cchars1, cnt = 0; p->name; ++p) {
193 			if (fmt != BSD && cc[p->sub] == p->def)
194 				continue;
195 #define	WD	"%-8s"
196 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
197 			(void)sprintf(buf2 + cnt * 8, WD, ccval(p, cc[p->sub]));
198 			if (++cnt == LINELENGTH / 8) {
199 				cnt = 0;
200 				(void)printf("%s\n", buf1);
201 				(void)printf("%s\n", buf2);
202 			}
203 		}
204 		if (cnt) {
205 			(void)printf("%s\n", buf1);
206 			(void)printf("%s\n", buf2);
207 		}
208 	}
209 }
210 
211 static int col;
212 static char *label;
213 
214 static void
215 binit(lb)
216 	char *lb;
217 {
218 
219 	if (col) {
220 		(void)printf("\n");
221 		col = 0;
222 	}
223 	label = lb;
224 }
225 
226 static void
227 bput(s)
228 	char *s;
229 {
230 
231 	if (col == 0) {
232 		col = printf("%s: %s", label, s);
233 		return;
234 	}
235 	if ((col + strlen(s)) > LINELENGTH) {
236 		(void)printf("\n\t");
237 		col = printf("%s", s) + 8;
238 		return;
239 	}
240 	col += printf(" %s", s);
241 }
242 
243 static char *
244 ccval(p, c)
245 	const struct cchar *p;
246 	int c;
247 {
248 	static char buf[5];
249 	char *bp;
250 
251 	if (c == _POSIX_VDISABLE)
252 		return ("<undef>");
253 
254 	if (p->sub == VMIN || p->sub == VTIME) {
255 		(void)snprintf(buf, sizeof(buf), "%d", c);
256 		return (buf);
257 	}
258 	bp = buf;
259 	if (c & 0200) {
260 		*bp++ = 'M';
261 		*bp++ = '-';
262 		c &= 0177;
263 	}
264 	if (c == 0177) {
265 		*bp++ = '^';
266 		*bp++ = '?';
267 	}
268 	else if (c < 040) {
269 		*bp++ = '^';
270 		*bp++ = c + '@';
271 	}
272 	else
273 		*bp++ = c;
274 	*bp = '\0';
275 	return (buf);
276 }
277