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