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