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