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