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