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