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