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