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