print.c revision 8855
11556Srgrimes/*-
21556Srgrimes * Copyright (c) 1991, 1993, 1994
31556Srgrimes *	The Regents of the University of California.  All rights reserved.
41556Srgrimes *
51556Srgrimes * Redistribution and use in source and binary forms, with or without
61556Srgrimes * modification, are permitted provided that the following conditions
71556Srgrimes * are met:
81556Srgrimes * 1. Redistributions of source code must retain the above copyright
91556Srgrimes *    notice, this list of conditions and the following disclaimer.
101556Srgrimes * 2. Redistributions in binary form must reproduce the above copyright
111556Srgrimes *    notice, this list of conditions and the following disclaimer in the
121556Srgrimes *    documentation and/or other materials provided with the distribution.
131556Srgrimes * 3. All advertising materials mentioning features or use of this software
141556Srgrimes *    must display the following acknowledgement:
151556Srgrimes *	This product includes software developed by the University of
161556Srgrimes *	California, Berkeley and its contributors.
171556Srgrimes * 4. Neither the name of the University nor the names of its contributors
181556Srgrimes *    may be used to endorse or promote products derived from this software
191556Srgrimes *    without specific prior written permission.
201556Srgrimes *
211556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
221556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
231556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
241556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
251556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
271556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
281556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
291556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
301556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
311556Srgrimes * SUCH DAMAGE.
323044Sdg *
338855Srgrimes *	$Id: print.c,v 1.4 1995/04/29 15:17:16 bde Exp $
341556Srgrimes */
351556Srgrimes
361556Srgrimes#ifndef lint
371556Srgrimesstatic char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
381556Srgrimes#endif /* not lint */
391556Srgrimes
401556Srgrimes#include <sys/types.h>
411556Srgrimes
421556Srgrimes#include <stddef.h>
431556Srgrimes#include <stdio.h>
441556Srgrimes#include <string.h>
451556Srgrimes
461556Srgrimes#include "stty.h"
471556Srgrimes#include "extern.h"
481556Srgrimes
498170Sbde#include <sys/ioctl_compat.h>	/* XXX NTTYDISC is too well hidden */
508170Sbde
511556Srgrimesstatic void  binit __P((char *));
521556Srgrimesstatic void  bput __P((char *));
531556Srgrimesstatic char *ccval __P((struct cchar *, int));
541556Srgrimes
551556Srgrimesvoid
561556Srgrimesprint(tp, wp, ldisc, fmt)
571556Srgrimes	struct termios *tp;
581556Srgrimes	struct winsize *wp;
591556Srgrimes	int ldisc;
601556Srgrimes	enum FMT fmt;
611556Srgrimes{
621556Srgrimes	struct cchar *p;
631556Srgrimes	long tmp;
641556Srgrimes	u_char *cc;
651556Srgrimes	int cnt, ispeed, ospeed;
661556Srgrimes	char buf1[100], buf2[100];
671556Srgrimes
681556Srgrimes	cnt = 0;
691556Srgrimes
701556Srgrimes	/* Line discipline. */
711556Srgrimes	if (ldisc != TTYDISC) {
721556Srgrimes		switch(ldisc) {
738855Srgrimes		case TABLDISC:
741556Srgrimes			cnt += printf("tablet disc; ");
751556Srgrimes			break;
768170Sbde		case NTTYDISC:
778170Sbde			cnt += printf("new tty disc; ");
788170Sbde			break;
798855Srgrimes		case SLIPDISC:
801556Srgrimes			cnt += printf("slip disc; ");
811556Srgrimes			break;
828148Sache		case PPPDISC:
838148Sache			cnt += printf("ppp disc; ");
848148Sache			break;
858855Srgrimes		default:
861556Srgrimes			cnt += printf("#%d disc; ", ldisc);
871556Srgrimes			break;
881556Srgrimes		}
891556Srgrimes	}
901556Srgrimes
911556Srgrimes	/* Line speed. */
921556Srgrimes	ispeed = cfgetispeed(tp);
931556Srgrimes	ospeed = cfgetospeed(tp);
941556Srgrimes	if (ispeed != ospeed)
951556Srgrimes		cnt +=
961556Srgrimes		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
971556Srgrimes	else
981556Srgrimes		cnt += printf("speed %d baud;", ispeed);
991556Srgrimes	if (fmt >= BSD)
1001556Srgrimes		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
1011556Srgrimes	if (cnt)
1021556Srgrimes		(void)printf("\n");
1031556Srgrimes
1048170Sbde#define	on(f)	((tmp & (f)) != 0)
1051556Srgrimes#define put(n, f, d) \
1068170Sbde	if (fmt >= BSD || on(f) != (d)) \
1078170Sbde		bput((n) + on(f));
1081556Srgrimes
1091556Srgrimes	/* "local" flags */
1101556Srgrimes	tmp = tp->c_lflag;
1111556Srgrimes	binit("lflags");
1121556Srgrimes	put("-icanon", ICANON, 1);
1131556Srgrimes	put("-isig", ISIG, 1);
1141556Srgrimes	put("-iexten", IEXTEN, 1);
1151556Srgrimes	put("-echo", ECHO, 1);
1161556Srgrimes	put("-echoe", ECHOE, 0);
1171556Srgrimes	put("-echok", ECHOK, 0);
1181556Srgrimes	put("-echoke", ECHOKE, 0);
1191556Srgrimes	put("-echonl", ECHONL, 0);
1201556Srgrimes	put("-echoctl", ECHOCTL, 0);
1211556Srgrimes	put("-echoprt", ECHOPRT, 0);
1221556Srgrimes	put("-altwerase", ALTWERASE, 0);
1231556Srgrimes	put("-noflsh", NOFLSH, 0);
1241556Srgrimes	put("-tostop", TOSTOP, 0);
1251556Srgrimes	put("-flusho", FLUSHO, 0);
1261556Srgrimes	put("-pendin", PENDIN, 0);
1271556Srgrimes	put("-nokerninfo", NOKERNINFO, 0);
1281556Srgrimes	put("-extproc", EXTPROC, 0);
1291556Srgrimes
1301556Srgrimes	/* input flags */
1311556Srgrimes	tmp = tp->c_iflag;
1321556Srgrimes	binit("iflags");
1331556Srgrimes	put("-istrip", ISTRIP, 0);
1341556Srgrimes	put("-icrnl", ICRNL, 1);
1351556Srgrimes	put("-inlcr", INLCR, 0);
1361556Srgrimes	put("-igncr", IGNCR, 0);
1371556Srgrimes	put("-ixon", IXON, 1);
1381556Srgrimes	put("-ixoff", IXOFF, 0);
1391556Srgrimes	put("-ixany", IXANY, 1);
1401556Srgrimes	put("-imaxbel", IMAXBEL, 1);
1411556Srgrimes	put("-ignbrk", IGNBRK, 0);
1421556Srgrimes	put("-brkint", BRKINT, 1);
1431556Srgrimes	put("-inpck", INPCK, 0);
1441556Srgrimes	put("-ignpar", IGNPAR, 0);
1451556Srgrimes	put("-parmrk", PARMRK, 0);
1461556Srgrimes
1471556Srgrimes	/* output flags */
1481556Srgrimes	tmp = tp->c_oflag;
1491556Srgrimes	binit("oflags");
1501556Srgrimes	put("-opost", OPOST, 1);
1511556Srgrimes	put("-onlcr", ONLCR, 1);
1521556Srgrimes	put("-oxtabs", OXTABS, 1);
1531556Srgrimes
1541556Srgrimes	/* control flags (hardware state) */
1551556Srgrimes	tmp = tp->c_cflag;
1561556Srgrimes	binit("cflags");
1571556Srgrimes	put("-cread", CREAD, 1);
1581556Srgrimes	switch(tmp&CSIZE) {
1591556Srgrimes	case CS5:
1601556Srgrimes		bput("cs5");
1611556Srgrimes		break;
1621556Srgrimes	case CS6:
1631556Srgrimes		bput("cs6");
1641556Srgrimes		break;
1651556Srgrimes	case CS7:
1661556Srgrimes		bput("cs7");
1671556Srgrimes		break;
1681556Srgrimes	case CS8:
1691556Srgrimes		bput("cs8");
1701556Srgrimes		break;
1711556Srgrimes	}
1721556Srgrimes	bput("-parenb" + on(PARENB));
1731556Srgrimes	put("-parodd", PARODD, 0);
1741556Srgrimes	put("-hupcl", HUPCL, 1);
1751556Srgrimes	put("-clocal", CLOCAL, 0);
1761556Srgrimes	put("-cstopb", CSTOPB, 0);
1778170Sbde	switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) {
1788170Sbde	case CCTS_OFLOW:
1798170Sbde		bput("ctsflow");
1808170Sbde		break;
1818170Sbde	case CRTS_IFLOW:
1828170Sbde		bput("rtsflow");
1838170Sbde		break;
1848170Sbde	default:
1858170Sbde		put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0);
1868170Sbde		break;
1878170Sbde	}
1888170Sbde	put("-dsrflow", CDSR_OFLOW, 0);
1898170Sbde	put("-dtrflow", CDTR_IFLOW, 0);
1908170Sbde	put("-mdmbuf", MDMBUF, 0);	/* XXX mdmbuf ==  dtrflow */
1911556Srgrimes
1921556Srgrimes	/* special control characters */
1931556Srgrimes	cc = tp->c_cc;
1941556Srgrimes	if (fmt == POSIX) {
1951556Srgrimes		binit("cchars");
1961556Srgrimes		for (p = cchars1; p->name; ++p) {
1971556Srgrimes			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
1981556Srgrimes			    p->name, ccval(p, cc[p->sub]));
1991556Srgrimes			bput(buf1);
2001556Srgrimes		}
2011556Srgrimes		binit(NULL);
2021556Srgrimes	} else {
2031556Srgrimes		binit(NULL);
2041556Srgrimes		for (p = cchars1, cnt = 0; p->name; ++p) {
2051556Srgrimes			if (fmt != BSD && cc[p->sub] == p->def)
2061556Srgrimes				continue;
2071556Srgrimes#define	WD	"%-8s"
2081556Srgrimes			(void)sprintf(buf1 + cnt * 8, WD, p->name);
2091556Srgrimes			(void)sprintf(buf2 + cnt * 8, WD, ccval(p, cc[p->sub]));
2101556Srgrimes			if (++cnt == LINELENGTH / 8) {
2111556Srgrimes				cnt = 0;
2121556Srgrimes				(void)printf("%s\n", buf1);
2131556Srgrimes				(void)printf("%s\n", buf2);
2141556Srgrimes			}
2151556Srgrimes		}
2161556Srgrimes		if (cnt) {
2171556Srgrimes			(void)printf("%s\n", buf1);
2181556Srgrimes			(void)printf("%s\n", buf2);
2191556Srgrimes		}
2201556Srgrimes	}
2211556Srgrimes}
2221556Srgrimes
2231556Srgrimesstatic int col;
2241556Srgrimesstatic char *label;
2251556Srgrimes
2261556Srgrimesstatic void
2271556Srgrimesbinit(lb)
2281556Srgrimes	char *lb;
2291556Srgrimes{
2301556Srgrimes
2311556Srgrimes	if (col) {
2321556Srgrimes		(void)printf("\n");
2331556Srgrimes		col = 0;
2341556Srgrimes	}
2351556Srgrimes	label = lb;
2361556Srgrimes}
2371556Srgrimes
2381556Srgrimesstatic void
2391556Srgrimesbput(s)
2401556Srgrimes	char *s;
2411556Srgrimes{
2421556Srgrimes
2431556Srgrimes	if (col == 0) {
2441556Srgrimes		col = printf("%s: %s", label, s);
2451556Srgrimes		return;
2461556Srgrimes	}
2471556Srgrimes	if ((col + strlen(s)) > LINELENGTH) {
2481556Srgrimes		(void)printf("\n\t");
2491556Srgrimes		col = printf("%s", s) + 8;
2501556Srgrimes		return;
2511556Srgrimes	}
2521556Srgrimes	col += printf(" %s", s);
2531556Srgrimes}
2541556Srgrimes
2551556Srgrimesstatic char *
2561556Srgrimesccval(p, c)
2571556Srgrimes	struct cchar *p;
2581556Srgrimes	int c;
2591556Srgrimes{
2601556Srgrimes	static char buf[5];
2611556Srgrimes	char *bp;
2621556Srgrimes
2631556Srgrimes	if (p->sub == VMIN || p->sub == VTIME) {
2641556Srgrimes		(void)snprintf(buf, sizeof(buf), "%d", c);
2651556Srgrimes		return (buf);
2661556Srgrimes	}
2678148Sache	if (c == _POSIX_VDISABLE)
2688148Sache		return ("<undef>");
2691556Srgrimes	bp = buf;
2701556Srgrimes	if (c & 0200) {
2711556Srgrimes		*bp++ = 'M';
2721556Srgrimes		*bp++ = '-';
2731556Srgrimes		c &= 0177;
2741556Srgrimes	}
2751556Srgrimes	if (c == 0177) {
2761556Srgrimes		*bp++ = '^';
2771556Srgrimes		*bp++ = '?';
2781556Srgrimes	}
2791556Srgrimes	else if (c < 040) {
2801556Srgrimes		*bp++ = '^';
2811556Srgrimes		*bp++ = c + '@';
2821556Srgrimes	}
2831556Srgrimes	else
2841556Srgrimes		*bp++ = c;
2851556Srgrimes	*bp = '\0';
2861556Srgrimes	return (buf);
2871556Srgrimes}
288