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 * 4. Neither the name of the University nor the names of its contributors
141556Srgrimes *    may be used to endorse or promote products derived from this software
151556Srgrimes *    without specific prior written permission.
161556Srgrimes *
171556Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
181556Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
191556Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
201556Srgrimes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
211556Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
221556Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
231556Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
241556Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
251556Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
261556Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
271556Srgrimes * SUCH DAMAGE.
281556Srgrimes */
291556Srgrimes
301556Srgrimes#ifndef lint
3136152Scharnier#if 0
3236152Scharnierstatic char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
3336152Scharnier#endif
341556Srgrimes#endif /* not lint */
3599110Sobrien#include <sys/cdefs.h>
3699110Sobrien__FBSDID("$FreeBSD$");
371556Srgrimes
381556Srgrimes#include <sys/types.h>
391556Srgrimes
401556Srgrimes#include <stddef.h>
411556Srgrimes#include <stdio.h>
421556Srgrimes#include <string.h>
431556Srgrimes
441556Srgrimes#include "stty.h"
451556Srgrimes#include "extern.h"
461556Srgrimes
4790111Simpstatic void  binit(const char *);
4890111Simpstatic void  bput(const char *);
4990111Simpstatic const char *ccval(struct cchar *, int);
501556Srgrimes
511556Srgrimesvoid
5290111Simpprint(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt)
531556Srgrimes{
541556Srgrimes	struct cchar *p;
551556Srgrimes	long tmp;
561556Srgrimes	u_char *cc;
571556Srgrimes	int cnt, ispeed, ospeed;
581556Srgrimes	char buf1[100], buf2[100];
591556Srgrimes
601556Srgrimes	cnt = 0;
611556Srgrimes
621556Srgrimes	/* Line discipline. */
631556Srgrimes	if (ldisc != TTYDISC) {
641556Srgrimes		switch(ldisc) {
658855Srgrimes		case SLIPDISC:
661556Srgrimes			cnt += printf("slip disc; ");
671556Srgrimes			break;
688148Sache		case PPPDISC:
698148Sache			cnt += printf("ppp disc; ");
708148Sache			break;
718855Srgrimes		default:
721556Srgrimes			cnt += printf("#%d disc; ", ldisc);
731556Srgrimes			break;
741556Srgrimes		}
751556Srgrimes	}
761556Srgrimes
771556Srgrimes	/* Line speed. */
781556Srgrimes	ispeed = cfgetispeed(tp);
791556Srgrimes	ospeed = cfgetospeed(tp);
801556Srgrimes	if (ispeed != ospeed)
811556Srgrimes		cnt +=
821556Srgrimes		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
831556Srgrimes	else
841556Srgrimes		cnt += printf("speed %d baud;", ispeed);
851556Srgrimes	if (fmt >= BSD)
8659788Sache		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
871556Srgrimes	if (cnt)
881556Srgrimes		(void)printf("\n");
891556Srgrimes
908170Sbde#define	on(f)	((tmp & (f)) != 0)
911556Srgrimes#define put(n, f, d) \
928170Sbde	if (fmt >= BSD || on(f) != (d)) \
938170Sbde		bput((n) + on(f));
941556Srgrimes
951556Srgrimes	/* "local" flags */
961556Srgrimes	tmp = tp->c_lflag;
971556Srgrimes	binit("lflags");
981556Srgrimes	put("-icanon", ICANON, 1);
991556Srgrimes	put("-isig", ISIG, 1);
1001556Srgrimes	put("-iexten", IEXTEN, 1);
1011556Srgrimes	put("-echo", ECHO, 1);
1021556Srgrimes	put("-echoe", ECHOE, 0);
1031556Srgrimes	put("-echok", ECHOK, 0);
1041556Srgrimes	put("-echoke", ECHOKE, 0);
1051556Srgrimes	put("-echonl", ECHONL, 0);
1061556Srgrimes	put("-echoctl", ECHOCTL, 0);
1071556Srgrimes	put("-echoprt", ECHOPRT, 0);
1081556Srgrimes	put("-altwerase", ALTWERASE, 0);
1091556Srgrimes	put("-noflsh", NOFLSH, 0);
1101556Srgrimes	put("-tostop", TOSTOP, 0);
1111556Srgrimes	put("-flusho", FLUSHO, 0);
1121556Srgrimes	put("-pendin", PENDIN, 0);
1131556Srgrimes	put("-nokerninfo", NOKERNINFO, 0);
1141556Srgrimes	put("-extproc", EXTPROC, 0);
1151556Srgrimes
1161556Srgrimes	/* input flags */
1171556Srgrimes	tmp = tp->c_iflag;
1181556Srgrimes	binit("iflags");
1191556Srgrimes	put("-istrip", ISTRIP, 0);
1201556Srgrimes	put("-icrnl", ICRNL, 1);
1211556Srgrimes	put("-inlcr", INLCR, 0);
1221556Srgrimes	put("-igncr", IGNCR, 0);
1231556Srgrimes	put("-ixon", IXON, 1);
1241556Srgrimes	put("-ixoff", IXOFF, 0);
1251556Srgrimes	put("-ixany", IXANY, 1);
1261556Srgrimes	put("-imaxbel", IMAXBEL, 1);
1271556Srgrimes	put("-ignbrk", IGNBRK, 0);
1281556Srgrimes	put("-brkint", BRKINT, 1);
1291556Srgrimes	put("-inpck", INPCK, 0);
1301556Srgrimes	put("-ignpar", IGNPAR, 0);
1311556Srgrimes	put("-parmrk", PARMRK, 0);
1321556Srgrimes
1331556Srgrimes	/* output flags */
1341556Srgrimes	tmp = tp->c_oflag;
1351556Srgrimes	binit("oflags");
1361556Srgrimes	put("-opost", OPOST, 1);
1371556Srgrimes	put("-onlcr", ONLCR, 1);
13873421Sassar	put("-ocrnl", OCRNL, 0);
139182052Sed	switch(tmp&TABDLY) {
140182052Sed	case TAB0:
141182052Sed		bput("tab0");
142182052Sed		break;
143182052Sed	case TAB3:
144182052Sed		bput("tab3");
145182052Sed		break;
146182052Sed	}
147146636Sache	put("-onocr", ONOCR, 0);
148146636Sache	put("-onlret", ONLRET, 0);
1491556Srgrimes
1501556Srgrimes	/* control flags (hardware state) */
1511556Srgrimes	tmp = tp->c_cflag;
1521556Srgrimes	binit("cflags");
1531556Srgrimes	put("-cread", CREAD, 1);
1541556Srgrimes	switch(tmp&CSIZE) {
1551556Srgrimes	case CS5:
1561556Srgrimes		bput("cs5");
1571556Srgrimes		break;
1581556Srgrimes	case CS6:
1591556Srgrimes		bput("cs6");
1601556Srgrimes		break;
1611556Srgrimes	case CS7:
1621556Srgrimes		bput("cs7");
1631556Srgrimes		break;
1641556Srgrimes	case CS8:
1651556Srgrimes		bput("cs8");
1661556Srgrimes		break;
1671556Srgrimes	}
1681556Srgrimes	bput("-parenb" + on(PARENB));
1691556Srgrimes	put("-parodd", PARODD, 0);
1701556Srgrimes	put("-hupcl", HUPCL, 1);
1711556Srgrimes	put("-clocal", CLOCAL, 0);
1721556Srgrimes	put("-cstopb", CSTOPB, 0);
1738170Sbde	switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) {
1748170Sbde	case CCTS_OFLOW:
1758170Sbde		bput("ctsflow");
1768170Sbde		break;
1778170Sbde	case CRTS_IFLOW:
1788170Sbde		bput("rtsflow");
1798170Sbde		break;
1808170Sbde	default:
1818170Sbde		put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0);
1828170Sbde		break;
1838170Sbde	}
1848170Sbde	put("-dsrflow", CDSR_OFLOW, 0);
1858170Sbde	put("-dtrflow", CDTR_IFLOW, 0);
1868170Sbde	put("-mdmbuf", MDMBUF, 0);	/* XXX mdmbuf ==  dtrflow */
1871556Srgrimes
1881556Srgrimes	/* special control characters */
1891556Srgrimes	cc = tp->c_cc;
1901556Srgrimes	if (fmt == POSIX) {
1911556Srgrimes		binit("cchars");
1921556Srgrimes		for (p = cchars1; p->name; ++p) {
1931556Srgrimes			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
1941556Srgrimes			    p->name, ccval(p, cc[p->sub]));
1951556Srgrimes			bput(buf1);
1961556Srgrimes		}
1971556Srgrimes		binit(NULL);
1981556Srgrimes	} else {
1991556Srgrimes		binit(NULL);
2001556Srgrimes		for (p = cchars1, cnt = 0; p->name; ++p) {
2011556Srgrimes			if (fmt != BSD && cc[p->sub] == p->def)
2021556Srgrimes				continue;
2031556Srgrimes#define	WD	"%-8s"
20431686Seivind			(void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8,
20531686Seivind			    WD, p->name);
20631686Seivind			(void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8,
20731686Seivind			    WD, ccval(p, cc[p->sub]));
2081556Srgrimes			if (++cnt == LINELENGTH / 8) {
2091556Srgrimes				cnt = 0;
2101556Srgrimes				(void)printf("%s\n", buf1);
2111556Srgrimes				(void)printf("%s\n", buf2);
2121556Srgrimes			}
2131556Srgrimes		}
2141556Srgrimes		if (cnt) {
2151556Srgrimes			(void)printf("%s\n", buf1);
2161556Srgrimes			(void)printf("%s\n", buf2);
2171556Srgrimes		}
2181556Srgrimes	}
2191556Srgrimes}
2201556Srgrimes
2211556Srgrimesstatic int col;
22276810Skrisstatic const char *label;
2231556Srgrimes
2241556Srgrimesstatic void
22590111Simpbinit(const char *lb)
2261556Srgrimes{
2271556Srgrimes
2281556Srgrimes	if (col) {
2291556Srgrimes		(void)printf("\n");
2301556Srgrimes		col = 0;
2311556Srgrimes	}
2321556Srgrimes	label = lb;
2331556Srgrimes}
2341556Srgrimes
2351556Srgrimesstatic void
23690111Simpbput(const char *s)
2371556Srgrimes{
2381556Srgrimes
2391556Srgrimes	if (col == 0) {
2401556Srgrimes		col = printf("%s: %s", label, s);
2411556Srgrimes		return;
2421556Srgrimes	}
2431556Srgrimes	if ((col + strlen(s)) > LINELENGTH) {
2441556Srgrimes		(void)printf("\n\t");
2451556Srgrimes		col = printf("%s", s) + 8;
2461556Srgrimes		return;
2471556Srgrimes	}
2481556Srgrimes	col += printf(" %s", s);
2491556Srgrimes}
2501556Srgrimes
25176810Skrisstatic const char *
25290111Simpccval(struct cchar *p, int c)
2531556Srgrimes{
2541556Srgrimes	static char buf[5];
2551556Srgrimes	char *bp;
2561556Srgrimes
2571556Srgrimes	if (p->sub == VMIN || p->sub == VTIME) {
2581556Srgrimes		(void)snprintf(buf, sizeof(buf), "%d", c);
2591556Srgrimes		return (buf);
2601556Srgrimes	}
2618148Sache	if (c == _POSIX_VDISABLE)
2628148Sache		return ("<undef>");
2631556Srgrimes	bp = buf;
2641556Srgrimes	if (c & 0200) {
2651556Srgrimes		*bp++ = 'M';
2661556Srgrimes		*bp++ = '-';
2671556Srgrimes		c &= 0177;
2681556Srgrimes	}
2691556Srgrimes	if (c == 0177) {
2701556Srgrimes		*bp++ = '^';
2711556Srgrimes		*bp++ = '?';
2721556Srgrimes	}
2731556Srgrimes	else if (c < 040) {
2741556Srgrimes		*bp++ = '^';
2751556Srgrimes		*bp++ = c + '@';
2761556Srgrimes	}
2771556Srgrimes	else
2781556Srgrimes		*bp++ = c;
2791556Srgrimes	*bp = '\0';
2801556Srgrimes	return (buf);
2811556Srgrimes}
282