xref: /freebsd/bin/stty/print.c (revision e043f37205ffbde5627ff299ad25cd532f2956f0)
14b88c807SRodney W. Grimes /*-
24b88c807SRodney W. Grimes  * Copyright (c) 1991, 1993, 1994
34b88c807SRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
44b88c807SRodney W. Grimes  *
54b88c807SRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
64b88c807SRodney W. Grimes  * modification, are permitted provided that the following conditions
74b88c807SRodney W. Grimes  * are met:
84b88c807SRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
94b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
104b88c807SRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
114b88c807SRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
124b88c807SRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
13fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
144b88c807SRodney W. Grimes  *    may be used to endorse or promote products derived from this software
154b88c807SRodney W. Grimes  *    without specific prior written permission.
164b88c807SRodney W. Grimes  *
174b88c807SRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
184b88c807SRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
194b88c807SRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
204b88c807SRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
214b88c807SRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
224b88c807SRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
234b88c807SRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
244b88c807SRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
254b88c807SRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
264b88c807SRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
274b88c807SRodney W. Grimes  * SUCH DAMAGE.
284b88c807SRodney W. Grimes  */
294b88c807SRodney W. Grimes 
304b88c807SRodney W. Grimes #include <sys/types.h>
314b88c807SRodney W. Grimes 
324b88c807SRodney W. Grimes #include <stddef.h>
334b88c807SRodney W. Grimes #include <stdio.h>
344b88c807SRodney W. Grimes #include <string.h>
354b88c807SRodney W. Grimes 
364b88c807SRodney W. Grimes #include "stty.h"
374b88c807SRodney W. Grimes #include "extern.h"
384b88c807SRodney W. Grimes 
395134c3f7SWarner Losh static void  binit(const char *);
405134c3f7SWarner Losh static void  bput(const char *);
415134c3f7SWarner Losh static const char *ccval(struct cchar *, int);
424b88c807SRodney W. Grimes 
434b88c807SRodney W. Grimes void
print(struct termios * tp,struct winsize * wp,int ldisc,enum FMT fmt)445134c3f7SWarner Losh print(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt)
454b88c807SRodney W. Grimes {
464b88c807SRodney W. Grimes 	struct cchar *p;
474b88c807SRodney W. Grimes 	long tmp;
484b88c807SRodney W. Grimes 	u_char *cc;
494b88c807SRodney W. Grimes 	int cnt, ispeed, ospeed;
504b88c807SRodney W. Grimes 	char buf1[100], buf2[100];
514b88c807SRodney W. Grimes 
524b88c807SRodney W. Grimes 	cnt = 0;
534b88c807SRodney W. Grimes 
544b88c807SRodney W. Grimes 	/* Line discipline. */
554b88c807SRodney W. Grimes 	if (ldisc != TTYDISC) {
564b88c807SRodney W. Grimes 		switch(ldisc) {
574b88c807SRodney W. Grimes 		case SLIPDISC:
584b88c807SRodney W. Grimes 			cnt += printf("slip disc; ");
594b88c807SRodney W. Grimes 			break;
600b51c95dSAndrey A. Chernov 		case PPPDISC:
610b51c95dSAndrey A. Chernov 			cnt += printf("ppp disc; ");
620b51c95dSAndrey A. Chernov 			break;
634b88c807SRodney W. Grimes 		default:
644b88c807SRodney W. Grimes 			cnt += printf("#%d disc; ", ldisc);
654b88c807SRodney W. Grimes 			break;
664b88c807SRodney W. Grimes 		}
674b88c807SRodney W. Grimes 	}
684b88c807SRodney W. Grimes 
694b88c807SRodney W. Grimes 	/* Line speed. */
704b88c807SRodney W. Grimes 	ispeed = cfgetispeed(tp);
714b88c807SRodney W. Grimes 	ospeed = cfgetospeed(tp);
724b88c807SRodney W. Grimes 	if (ispeed != ospeed)
734b88c807SRodney W. Grimes 		cnt +=
744b88c807SRodney W. Grimes 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
754b88c807SRodney W. Grimes 	else
764b88c807SRodney W. Grimes 		cnt += printf("speed %d baud;", ispeed);
774b88c807SRodney W. Grimes 	if (fmt >= BSD)
78df41cfeeSAndrey A. Chernov 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
794b88c807SRodney W. Grimes 	if (cnt)
804b88c807SRodney W. Grimes 		(void)printf("\n");
814b88c807SRodney W. Grimes 
822bf5814aSBruce Evans #define	on(f)	((tmp & (f)) != 0)
834b88c807SRodney W. Grimes #define put(n, f, d) \
842bf5814aSBruce Evans 	if (fmt >= BSD || on(f) != (d)) \
852bf5814aSBruce Evans 		bput((n) + on(f));
864b88c807SRodney W. Grimes 
874b88c807SRodney W. Grimes 	/* "local" flags */
884b88c807SRodney W. Grimes 	tmp = tp->c_lflag;
894b88c807SRodney W. Grimes 	binit("lflags");
904b88c807SRodney W. Grimes 	put("-icanon", ICANON, 1);
914b88c807SRodney W. Grimes 	put("-isig", ISIG, 1);
924b88c807SRodney W. Grimes 	put("-iexten", IEXTEN, 1);
934b88c807SRodney W. Grimes 	put("-echo", ECHO, 1);
944b88c807SRodney W. Grimes 	put("-echoe", ECHOE, 0);
954b88c807SRodney W. Grimes 	put("-echok", ECHOK, 0);
964b88c807SRodney W. Grimes 	put("-echoke", ECHOKE, 0);
974b88c807SRodney W. Grimes 	put("-echonl", ECHONL, 0);
984b88c807SRodney W. Grimes 	put("-echoctl", ECHOCTL, 0);
994b88c807SRodney W. Grimes 	put("-echoprt", ECHOPRT, 0);
1004b88c807SRodney W. Grimes 	put("-altwerase", ALTWERASE, 0);
1014b88c807SRodney W. Grimes 	put("-noflsh", NOFLSH, 0);
1024b88c807SRodney W. Grimes 	put("-tostop", TOSTOP, 0);
1034b88c807SRodney W. Grimes 	put("-flusho", FLUSHO, 0);
1044b88c807SRodney W. Grimes 	put("-pendin", PENDIN, 0);
1054b88c807SRodney W. Grimes 	put("-nokerninfo", NOKERNINFO, 0);
1064b88c807SRodney W. Grimes 	put("-extproc", EXTPROC, 0);
1074b88c807SRodney W. Grimes 
1084b88c807SRodney W. Grimes 	/* input flags */
1094b88c807SRodney W. Grimes 	tmp = tp->c_iflag;
1104b88c807SRodney W. Grimes 	binit("iflags");
1114b88c807SRodney W. Grimes 	put("-istrip", ISTRIP, 0);
1124b88c807SRodney W. Grimes 	put("-icrnl", ICRNL, 1);
1134b88c807SRodney W. Grimes 	put("-inlcr", INLCR, 0);
1144b88c807SRodney W. Grimes 	put("-igncr", IGNCR, 0);
1154b88c807SRodney W. Grimes 	put("-ixon", IXON, 1);
1164b88c807SRodney W. Grimes 	put("-ixoff", IXOFF, 0);
1174b88c807SRodney W. Grimes 	put("-ixany", IXANY, 1);
1184b88c807SRodney W. Grimes 	put("-imaxbel", IMAXBEL, 1);
1194b88c807SRodney W. Grimes 	put("-ignbrk", IGNBRK, 0);
1204b88c807SRodney W. Grimes 	put("-brkint", BRKINT, 1);
1214b88c807SRodney W. Grimes 	put("-inpck", INPCK, 0);
1224b88c807SRodney W. Grimes 	put("-ignpar", IGNPAR, 0);
1234b88c807SRodney W. Grimes 	put("-parmrk", PARMRK, 0);
124*128f63ceSBojan Novković 	put("-iutf8", IUTF8, 1);
1254b88c807SRodney W. Grimes 
1264b88c807SRodney W. Grimes 	/* output flags */
1274b88c807SRodney W. Grimes 	tmp = tp->c_oflag;
1284b88c807SRodney W. Grimes 	binit("oflags");
1294b88c807SRodney W. Grimes 	put("-opost", OPOST, 1);
1304b88c807SRodney W. Grimes 	put("-onlcr", ONLCR, 1);
1313617ddfcSAssar Westerlund 	put("-ocrnl", OCRNL, 0);
132f8f8c9f0SEd Schouten 	switch(tmp&TABDLY) {
133f8f8c9f0SEd Schouten 	case TAB0:
134f8f8c9f0SEd Schouten 		bput("tab0");
135f8f8c9f0SEd Schouten 		break;
136f8f8c9f0SEd Schouten 	case TAB3:
137f8f8c9f0SEd Schouten 		bput("tab3");
138f8f8c9f0SEd Schouten 		break;
139f8f8c9f0SEd Schouten 	}
140aaf9c3b7SAndrey A. Chernov 	put("-onocr", ONOCR, 0);
141aaf9c3b7SAndrey A. Chernov 	put("-onlret", ONLRET, 0);
1424b88c807SRodney W. Grimes 
1434b88c807SRodney W. Grimes 	/* control flags (hardware state) */
1444b88c807SRodney W. Grimes 	tmp = tp->c_cflag;
1454b88c807SRodney W. Grimes 	binit("cflags");
1464b88c807SRodney W. Grimes 	put("-cread", CREAD, 1);
1474b88c807SRodney W. Grimes 	switch(tmp&CSIZE) {
1484b88c807SRodney W. Grimes 	case CS5:
1494b88c807SRodney W. Grimes 		bput("cs5");
1504b88c807SRodney W. Grimes 		break;
1514b88c807SRodney W. Grimes 	case CS6:
1524b88c807SRodney W. Grimes 		bput("cs6");
1534b88c807SRodney W. Grimes 		break;
1544b88c807SRodney W. Grimes 	case CS7:
1554b88c807SRodney W. Grimes 		bput("cs7");
1564b88c807SRodney W. Grimes 		break;
1574b88c807SRodney W. Grimes 	case CS8:
1584b88c807SRodney W. Grimes 		bput("cs8");
1594b88c807SRodney W. Grimes 		break;
1604b88c807SRodney W. Grimes 	}
1614b88c807SRodney W. Grimes 	bput("-parenb" + on(PARENB));
1624b88c807SRodney W. Grimes 	put("-parodd", PARODD, 0);
1634b88c807SRodney W. Grimes 	put("-hupcl", HUPCL, 1);
1644b88c807SRodney W. Grimes 	put("-clocal", CLOCAL, 0);
1654b88c807SRodney W. Grimes 	put("-cstopb", CSTOPB, 0);
1662bf5814aSBruce Evans 	switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) {
1672bf5814aSBruce Evans 	case CCTS_OFLOW:
1682bf5814aSBruce Evans 		bput("ctsflow");
1692bf5814aSBruce Evans 		break;
1702bf5814aSBruce Evans 	case CRTS_IFLOW:
1712bf5814aSBruce Evans 		bput("rtsflow");
1722bf5814aSBruce Evans 		break;
1732bf5814aSBruce Evans 	default:
1742bf5814aSBruce Evans 		put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0);
1752bf5814aSBruce Evans 		break;
1762bf5814aSBruce Evans 	}
1772bf5814aSBruce Evans 	put("-dsrflow", CDSR_OFLOW, 0);
1782bf5814aSBruce Evans 	put("-dtrflow", CDTR_IFLOW, 0);
1792bf5814aSBruce Evans 	put("-mdmbuf", MDMBUF, 0);	/* XXX mdmbuf ==  dtrflow */
180705aad98SStephen Hurd 	if (on(CNO_RTSDTR))
181705aad98SStephen Hurd 		bput("-rtsdtr");
182705aad98SStephen Hurd 	else {
183705aad98SStephen Hurd 		if (fmt >= BSD)
184705aad98SStephen Hurd 			bput("rtsdtr");
185705aad98SStephen Hurd 	}
1864b88c807SRodney W. Grimes 
1874b88c807SRodney W. Grimes 	/* special control characters */
1884b88c807SRodney W. Grimes 	cc = tp->c_cc;
1894b88c807SRodney W. Grimes 	if (fmt == POSIX) {
1904b88c807SRodney W. Grimes 		binit("cchars");
1914b88c807SRodney W. Grimes 		for (p = cchars1; p->name; ++p) {
1924b88c807SRodney W. Grimes 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
1934b88c807SRodney W. Grimes 			    p->name, ccval(p, cc[p->sub]));
1944b88c807SRodney W. Grimes 			bput(buf1);
1954b88c807SRodney W. Grimes 		}
1964b88c807SRodney W. Grimes 		binit(NULL);
1974b88c807SRodney W. Grimes 	} else {
1984b88c807SRodney W. Grimes 		binit(NULL);
1994b88c807SRodney W. Grimes 		for (p = cchars1, cnt = 0; p->name; ++p) {
2004b88c807SRodney W. Grimes 			if (fmt != BSD && cc[p->sub] == p->def)
2014b88c807SRodney W. Grimes 				continue;
2024b88c807SRodney W. Grimes #define	WD	"%-8s"
203ca8f21d6SEivind Eklund 			(void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8,
204ca8f21d6SEivind Eklund 			    WD, p->name);
205ca8f21d6SEivind Eklund 			(void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8,
206ca8f21d6SEivind Eklund 			    WD, ccval(p, cc[p->sub]));
2074b88c807SRodney W. Grimes 			if (++cnt == LINELENGTH / 8) {
2084b88c807SRodney W. Grimes 				cnt = 0;
2094b88c807SRodney W. Grimes 				(void)printf("%s\n", buf1);
2104b88c807SRodney W. Grimes 				(void)printf("%s\n", buf2);
2114b88c807SRodney W. Grimes 			}
2124b88c807SRodney W. Grimes 		}
2134b88c807SRodney W. Grimes 		if (cnt) {
2144b88c807SRodney W. Grimes 			(void)printf("%s\n", buf1);
2154b88c807SRodney W. Grimes 			(void)printf("%s\n", buf2);
2164b88c807SRodney W. Grimes 		}
2174b88c807SRodney W. Grimes 	}
2184b88c807SRodney W. Grimes }
2194b88c807SRodney W. Grimes 
2204b88c807SRodney W. Grimes static int col;
2215e5a5667SKris Kennaway static const char *label;
2224b88c807SRodney W. Grimes 
2234b88c807SRodney W. Grimes static void
binit(const char * lb)2245134c3f7SWarner Losh binit(const char *lb)
2254b88c807SRodney W. Grimes {
2264b88c807SRodney W. Grimes 
2274b88c807SRodney W. Grimes 	if (col) {
2284b88c807SRodney W. Grimes 		(void)printf("\n");
2294b88c807SRodney W. Grimes 		col = 0;
2304b88c807SRodney W. Grimes 	}
2314b88c807SRodney W. Grimes 	label = lb;
2324b88c807SRodney W. Grimes }
2334b88c807SRodney W. Grimes 
2344b88c807SRodney W. Grimes static void
bput(const char * s)2355134c3f7SWarner Losh bput(const char *s)
2364b88c807SRodney W. Grimes {
2374b88c807SRodney W. Grimes 
2384b88c807SRodney W. Grimes 	if (col == 0) {
2394b88c807SRodney W. Grimes 		col = printf("%s: %s", label, s);
2404b88c807SRodney W. Grimes 		return;
2414b88c807SRodney W. Grimes 	}
2424b88c807SRodney W. Grimes 	if ((col + strlen(s)) > LINELENGTH) {
2434b88c807SRodney W. Grimes 		(void)printf("\n\t");
2444b88c807SRodney W. Grimes 		col = printf("%s", s) + 8;
2454b88c807SRodney W. Grimes 		return;
2464b88c807SRodney W. Grimes 	}
2474b88c807SRodney W. Grimes 	col += printf(" %s", s);
2484b88c807SRodney W. Grimes }
2494b88c807SRodney W. Grimes 
2505e5a5667SKris Kennaway static const char *
ccval(struct cchar * p,int c)2515134c3f7SWarner Losh ccval(struct cchar *p, int c)
2524b88c807SRodney W. Grimes {
2534b88c807SRodney W. Grimes 	static char buf[5];
2544b88c807SRodney W. Grimes 	char *bp;
2554b88c807SRodney W. Grimes 
2564b88c807SRodney W. Grimes 	if (p->sub == VMIN || p->sub == VTIME) {
2574b88c807SRodney W. Grimes 		(void)snprintf(buf, sizeof(buf), "%d", c);
2584b88c807SRodney W. Grimes 		return (buf);
2594b88c807SRodney W. Grimes 	}
2600b51c95dSAndrey A. Chernov 	if (c == _POSIX_VDISABLE)
2610b51c95dSAndrey A. Chernov 		return ("<undef>");
2624b88c807SRodney W. Grimes 	bp = buf;
2634b88c807SRodney W. Grimes 	if (c & 0200) {
2644b88c807SRodney W. Grimes 		*bp++ = 'M';
2654b88c807SRodney W. Grimes 		*bp++ = '-';
2664b88c807SRodney W. Grimes 		c &= 0177;
2674b88c807SRodney W. Grimes 	}
2684b88c807SRodney W. Grimes 	if (c == 0177) {
2694b88c807SRodney W. Grimes 		*bp++ = '^';
2704b88c807SRodney W. Grimes 		*bp++ = '?';
2714b88c807SRodney W. Grimes 	}
2724b88c807SRodney W. Grimes 	else if (c < 040) {
2734b88c807SRodney W. Grimes 		*bp++ = '^';
2744b88c807SRodney W. Grimes 		*bp++ = c + '@';
2754b88c807SRodney W. Grimes 	}
2764b88c807SRodney W. Grimes 	else
2774b88c807SRodney W. Grimes 		*bp++ = c;
2784b88c807SRodney W. Grimes 	*bp = '\0';
2794b88c807SRodney W. Grimes 	return (buf);
2804b88c807SRodney W. Grimes }
281