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