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