print.c revision 59788
11541Srgrimes/*-
21541Srgrimes * Copyright (c) 1991, 1993, 1994
31541Srgrimes *	The Regents of the University of California.  All rights reserved.
41541Srgrimes *
51541Srgrimes * Redistribution and use in source and binary forms, with or without
61541Srgrimes * modification, are permitted provided that the following conditions
71541Srgrimes * are met:
81541Srgrimes * 1. Redistributions of source code must retain the above copyright
91541Srgrimes *    notice, this list of conditions and the following disclaimer.
101541Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111541Srgrimes *    notice, this list of conditions and the following disclaimer in the
121541Srgrimes *    documentation and/or other materials provided with the distribution.
131541Srgrimes * 3. All advertising materials mentioning features or use of this software
141541Srgrimes *    must display the following acknowledgement:
151541Srgrimes *	This product includes software developed by the University of
161541Srgrimes *	California, Berkeley and its contributors.
171541Srgrimes * 4. Neither the name of the University nor the names of its contributors
181541Srgrimes *    may be used to endorse or promote products derived from this software
191541Srgrimes *    without specific prior written permission.
201541Srgrimes *
211541Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221541Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231541Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241541Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251541Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261541Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271541Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281541Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291541Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301541Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311541Srgrimes * SUCH DAMAGE.
321541Srgrimes */
331541Srgrimes
341541Srgrimes#ifndef lint
351541Srgrimes#if 0
361541Srgrimesstatic char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
371541Srgrimes#endif
3814526Shsustatic const char rcsid[] =
3945515Sdes  "$FreeBSD: head/bin/stty/print.c 59788 2000-04-30 17:04:26Z ache $";
401541Srgrimes#endif /* not lint */
411541Srgrimes
4241774Sdillon#include <sys/types.h>
4341774Sdillon
4413226Swollman#include <stddef.h>
4514328Speter#include <stdio.h>
4613226Swollman#include <string.h>
471541Srgrimes
481541Srgrimes#include "stty.h"
491541Srgrimes#include "extern.h"
501541Srgrimes
511541Srgrimes#include <sys/ioctl_compat.h>	/* XXX NTTYDISC is too well hidden */
521541Srgrimes
532729Sdfrstatic void  binit __P((char *));
5413226Swollmanstatic void  bput __P((char *));
552729Sdfrstatic char *ccval __P((struct cchar *, int));
562729Sdfr
5713226Swollmanvoid
582729Sdfrprint(tp, wp, ldisc, fmt)
591541Srgrimes	struct termios *tp;
601541Srgrimes	struct winsize *wp;
611541Srgrimes	int ldisc;
621541Srgrimes	enum FMT fmt;
631541Srgrimes{
641541Srgrimes	struct cchar *p;
651541Srgrimes	long tmp;
661541Srgrimes	u_char *cc;
679369Sdg	int cnt, ispeed, ospeed;
681541Srgrimes	char buf1[100], buf2[100];
691541Srgrimes
701541Srgrimes	cnt = 0;
711541Srgrimes
721541Srgrimes	/* Line discipline. */
731541Srgrimes	if (ldisc != TTYDISC) {
741541Srgrimes		switch(ldisc) {
7537087Sbde		case NTTYDISC:
761541Srgrimes			cnt += printf("new tty disc; ");
7745515Sdes			break;
7833882Sguido		case SLIPDISC:
7945515Sdes			cnt += printf("slip disc; ");
805455Sdg			break;
816577Sguido		case PPPDISC:
8233882Sguido			cnt += printf("ppp disc; ");
8333882Sguido			break;
8433882Sguido		default:
858747Sdg			cnt += printf("#%d disc; ", ldisc);
868747Sdg			break;
878747Sdg		}
8815722Swollman	}
8915722Swollman
901541Srgrimes	/* Line speed. */
918747Sdg	ispeed = cfgetispeed(tp);
9236079Swollman	ospeed = cfgetospeed(tp);
9336079Swollman	if (ispeed != ospeed)
9436079Swollman		cnt +=
9536079Swollman		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
9636079Swollman	else
9736079Swollman		cnt += printf("speed %d baud;", ispeed);
9836079Swollman	if (fmt >= BSD)
9921737Sdg		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
10021737Sdg	if (cnt)
10115722Swollman		(void)printf("\n");
10240931Sdg
10340931Sdg#define	on(f)	((tmp & (f)) != 0)
10440931Sdg#define put(n, f, d) \
10540931Sdg	if (fmt >= BSD || on(f) != (d)) \
10640931Sdg		bput((n) + on(f));
10740931Sdg
1081541Srgrimes	/* "local" flags */
1091541Srgrimes	tmp = tp->c_lflag;
1101541Srgrimes	binit("lflags");
1111541Srgrimes	put("-icanon", ICANON, 1);
1126492Sjoerg	put("-isig", ISIG, 1);
11315538Sphk	put("-iexten", IEXTEN, 1);
1146492Sjoerg	put("-echo", ECHO, 1);
1156492Sjoerg	put("-echoe", ECHOE, 0);
1161541Srgrimes	put("-echok", ECHOK, 0);
1176492Sjoerg	put("-echoke", ECHOKE, 0);
1186492Sjoerg	put("-echonl", ECHONL, 0);
1191541Srgrimes	put("-echoctl", ECHOCTL, 0);
1206492Sjoerg	put("-echoprt", ECHOPRT, 0);
1216492Sjoerg	put("-altwerase", ALTWERASE, 0);
1221541Srgrimes	put("-noflsh", NOFLSH, 0);
1236492Sjoerg	put("-tostop", TOSTOP, 0);
1246492Sjoerg	put("-flusho", FLUSHO, 0);
12515543Sphk	put("-pendin", PENDIN, 0);
1266492Sjoerg	put("-nokerninfo", NOKERNINFO, 0);
1271541Srgrimes	put("-extproc", EXTPROC, 0);
1281541Srgrimes
1291541Srgrimes	/* input flags */
1301541Srgrimes	tmp = tp->c_iflag;
1311541Srgrimes	binit("iflags");
1321541Srgrimes	put("-istrip", ISTRIP, 0);
1331541Srgrimes	put("-icrnl", ICRNL, 1);
1341541Srgrimes	put("-inlcr", INLCR, 0);
1351541Srgrimes	put("-igncr", IGNCR, 0);
1361541Srgrimes	put("-ixon", IXON, 1);
1371541Srgrimes	put("-ixoff", IXOFF, 0);
1382729Sdfr	put("-ixany", IXANY, 1);
1392729Sdfr	put("-imaxbel", IMAXBEL, 1);
1402729Sdfr	put("-ignbrk", IGNBRK, 0);
1412729Sdfr	put("-brkint", BRKINT, 1);
1422729Sdfr	put("-inpck", INPCK, 0);
1432729Sdfr	put("-ignpar", IGNPAR, 0);
1442729Sdfr	put("-parmrk", PARMRK, 0);
1452729Sdfr
1462729Sdfr	/* output flags */
1472729Sdfr	tmp = tp->c_oflag;
1482729Sdfr	binit("oflags");
1492729Sdfr	put("-opost", OPOST, 1);
1502729Sdfr	put("-onlcr", ONLCR, 1);
1512729Sdfr	put("-oxtabs", OXTABS, 1);
1522729Sdfr
1532729Sdfr	/* control flags (hardware state) */
1542729Sdfr	tmp = tp->c_cflag;
1552729Sdfr	binit("cflags");
1562729Sdfr	put("-cread", CREAD, 1);
1572729Sdfr	switch(tmp&CSIZE) {
1582729Sdfr	case CS5:
1592729Sdfr		bput("cs5");
1602729Sdfr		break;
1612729Sdfr	case CS6:
1622729Sdfr		bput("cs6");
1632729Sdfr		break;
1642729Sdfr	case CS7:
1652729Sdfr		bput("cs7");
1662729Sdfr		break;
1672729Sdfr	case CS8:
1682729Sdfr		bput("cs8");
1692729Sdfr		break;
1702729Sdfr	}
1712729Sdfr	bput("-parenb" + on(PARENB));
1722729Sdfr	put("-parodd", PARODD, 0);
1732729Sdfr	put("-hupcl", HUPCL, 1);
1742729Sdfr	put("-clocal", CLOCAL, 0);
1759759Sbde	put("-cstopb", CSTOPB, 0);
1769759Sbde	switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) {
1779759Sbde	case CCTS_OFLOW:
1781541Srgrimes		bput("ctsflow");
1799759Sbde		break;
1809759Sbde	case CRTS_IFLOW:
1819759Sbde		bput("rtsflow");
1829759Sbde		break;
1839759Sbde	default:
1849759Sbde		put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0);
1851541Srgrimes		break;
1861541Srgrimes	}
1871541Srgrimes	put("-dsrflow", CDSR_OFLOW, 0);
1881541Srgrimes	put("-dtrflow", CDTR_IFLOW, 0);
1891541Srgrimes	put("-mdmbuf", MDMBUF, 0);	/* XXX mdmbuf ==  dtrflow */
1901541Srgrimes
1919759Sbde	/* special control characters */
192	cc = tp->c_cc;
193	if (fmt == POSIX) {
194		binit("cchars");
195		for (p = cchars1; p->name; ++p) {
196			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
197			    p->name, ccval(p, cc[p->sub]));
198			bput(buf1);
199		}
200		binit(NULL);
201	} else {
202		binit(NULL);
203		for (p = cchars1, cnt = 0; p->name; ++p) {
204			if (fmt != BSD && cc[p->sub] == p->def)
205				continue;
206#define	WD	"%-8s"
207			(void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8,
208			    WD, p->name);
209			(void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8,
210			    WD, ccval(p, cc[p->sub]));
211			if (++cnt == LINELENGTH / 8) {
212				cnt = 0;
213				(void)printf("%s\n", buf1);
214				(void)printf("%s\n", buf2);
215			}
216		}
217		if (cnt) {
218			(void)printf("%s\n", buf1);
219			(void)printf("%s\n", buf2);
220		}
221	}
222}
223
224static int col;
225static char *label;
226
227static void
228binit(lb)
229	char *lb;
230{
231
232	if (col) {
233		(void)printf("\n");
234		col = 0;
235	}
236	label = lb;
237}
238
239static void
240bput(s)
241	char *s;
242{
243
244	if (col == 0) {
245		col = printf("%s: %s", label, s);
246		return;
247	}
248	if ((col + strlen(s)) > LINELENGTH) {
249		(void)printf("\n\t");
250		col = printf("%s", s) + 8;
251		return;
252	}
253	col += printf(" %s", s);
254}
255
256static char *
257ccval(p, c)
258	struct cchar *p;
259	int c;
260{
261	static char buf[5];
262	char *bp;
263
264	if (p->sub == VMIN || p->sub == VTIME) {
265		(void)snprintf(buf, sizeof(buf), "%d", c);
266		return (buf);
267	}
268	if (c == _POSIX_VDISABLE)
269		return ("<undef>");
270	bp = buf;
271	if (c & 0200) {
272		*bp++ = 'M';
273		*bp++ = '-';
274		c &= 0177;
275	}
276	if (c == 0177) {
277		*bp++ = '^';
278		*bp++ = '?';
279	}
280	else if (c < 040) {
281		*bp++ = '^';
282		*bp++ = c + '@';
283	}
284	else
285		*bp++ = c;
286	*bp = '\0';
287	return (buf);
288}
289