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