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