1 /*- 2 * Copyright (c) 1991, 1993, 1994 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 4. Neither the name of the University nor the names of its contributors 14 * may be used to endorse or promote products derived from this software 15 * without specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #ifndef lint 31 #if 0 32 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94"; 33 #endif 34 #endif /* not lint */ 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <sys/types.h> 39 40 #include <stddef.h> 41 #include <stdio.h> 42 #include <string.h> 43 44 #include "stty.h" 45 #include "extern.h" 46 47 static void binit(const char *); 48 static void bput(const char *); 49 static const char *ccval(struct cchar *, int); 50 51 void 52 print(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt) 53 { 54 struct cchar *p; 55 long tmp; 56 u_char *cc; 57 int cnt, ispeed, ospeed; 58 char buf1[100], buf2[100]; 59 60 cnt = 0; 61 62 /* Line discipline. */ 63 if (ldisc != TTYDISC) { 64 switch(ldisc) { 65 case SLIPDISC: 66 cnt += printf("slip disc; "); 67 break; 68 case PPPDISC: 69 cnt += printf("ppp disc; "); 70 break; 71 default: 72 cnt += printf("#%d disc; ", ldisc); 73 break; 74 } 75 } 76 77 /* Line speed. */ 78 ispeed = cfgetispeed(tp); 79 ospeed = cfgetospeed(tp); 80 if (ispeed != ospeed) 81 cnt += 82 printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed); 83 else 84 cnt += printf("speed %d baud;", ispeed); 85 if (fmt >= BSD) 86 cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col); 87 if (cnt) 88 (void)printf("\n"); 89 90 #define on(f) ((tmp & (f)) != 0) 91 #define put(n, f, d) \ 92 if (fmt >= BSD || on(f) != (d)) \ 93 bput((n) + on(f)); 94 95 /* "local" flags */ 96 tmp = tp->c_lflag; 97 binit("lflags"); 98 put("-icanon", ICANON, 1); 99 put("-isig", ISIG, 1); 100 put("-iexten", IEXTEN, 1); 101 put("-echo", ECHO, 1); 102 put("-echoe", ECHOE, 0); 103 put("-echok", ECHOK, 0); 104 put("-echoke", ECHOKE, 0); 105 put("-echonl", ECHONL, 0); 106 put("-echoctl", ECHOCTL, 0); 107 put("-echoprt", ECHOPRT, 0); 108 put("-altwerase", ALTWERASE, 0); 109 put("-noflsh", NOFLSH, 0); 110 put("-tostop", TOSTOP, 0); 111 put("-flusho", FLUSHO, 0); 112 put("-pendin", PENDIN, 0); 113 put("-nokerninfo", NOKERNINFO, 0); 114 put("-extproc", EXTPROC, 0); 115 116 /* input flags */ 117 tmp = tp->c_iflag; 118 binit("iflags"); 119 put("-istrip", ISTRIP, 0); 120 put("-icrnl", ICRNL, 1); 121 put("-inlcr", INLCR, 0); 122 put("-igncr", IGNCR, 0); 123 put("-ixon", IXON, 1); 124 put("-ixoff", IXOFF, 0); 125 put("-ixany", IXANY, 1); 126 put("-imaxbel", IMAXBEL, 1); 127 put("-ignbrk", IGNBRK, 0); 128 put("-brkint", BRKINT, 1); 129 put("-inpck", INPCK, 0); 130 put("-ignpar", IGNPAR, 0); 131 put("-parmrk", PARMRK, 0); 132 133 /* output flags */ 134 tmp = tp->c_oflag; 135 binit("oflags"); 136 put("-opost", OPOST, 1); 137 put("-onlcr", ONLCR, 1); 138 put("-ocrnl", OCRNL, 0); 139 put("-oxtabs", OXTABS, 1); 140 put("-onocr", ONOCR, 0); 141 put("-onlret", ONLRET, 0); 142 143 /* control flags (hardware state) */ 144 tmp = tp->c_cflag; 145 binit("cflags"); 146 put("-cread", CREAD, 1); 147 switch(tmp&CSIZE) { 148 case CS5: 149 bput("cs5"); 150 break; 151 case CS6: 152 bput("cs6"); 153 break; 154 case CS7: 155 bput("cs7"); 156 break; 157 case CS8: 158 bput("cs8"); 159 break; 160 } 161 bput("-parenb" + on(PARENB)); 162 put("-parodd", PARODD, 0); 163 put("-hupcl", HUPCL, 1); 164 put("-clocal", CLOCAL, 0); 165 put("-cstopb", CSTOPB, 0); 166 switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) { 167 case CCTS_OFLOW: 168 bput("ctsflow"); 169 break; 170 case CRTS_IFLOW: 171 bput("rtsflow"); 172 break; 173 default: 174 put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0); 175 break; 176 } 177 put("-dsrflow", CDSR_OFLOW, 0); 178 put("-dtrflow", CDTR_IFLOW, 0); 179 put("-mdmbuf", MDMBUF, 0); /* XXX mdmbuf == dtrflow */ 180 181 /* special control characters */ 182 cc = tp->c_cc; 183 if (fmt == POSIX) { 184 binit("cchars"); 185 for (p = cchars1; p->name; ++p) { 186 (void)snprintf(buf1, sizeof(buf1), "%s = %s;", 187 p->name, ccval(p, cc[p->sub])); 188 bput(buf1); 189 } 190 binit(NULL); 191 } else { 192 binit(NULL); 193 for (p = cchars1, cnt = 0; p->name; ++p) { 194 if (fmt != BSD && cc[p->sub] == p->def) 195 continue; 196 #define WD "%-8s" 197 (void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8, 198 WD, p->name); 199 (void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8, 200 WD, ccval(p, cc[p->sub])); 201 if (++cnt == LINELENGTH / 8) { 202 cnt = 0; 203 (void)printf("%s\n", buf1); 204 (void)printf("%s\n", buf2); 205 } 206 } 207 if (cnt) { 208 (void)printf("%s\n", buf1); 209 (void)printf("%s\n", buf2); 210 } 211 } 212 } 213 214 static int col; 215 static const char *label; 216 217 static void 218 binit(const char *lb) 219 { 220 221 if (col) { 222 (void)printf("\n"); 223 col = 0; 224 } 225 label = lb; 226 } 227 228 static void 229 bput(const char *s) 230 { 231 232 if (col == 0) { 233 col = printf("%s: %s", label, s); 234 return; 235 } 236 if ((col + strlen(s)) > LINELENGTH) { 237 (void)printf("\n\t"); 238 col = printf("%s", s) + 8; 239 return; 240 } 241 col += printf(" %s", s); 242 } 243 244 static const char * 245 ccval(struct cchar *p, int c) 246 { 247 static char buf[5]; 248 char *bp; 249 250 if (p->sub == VMIN || p->sub == VTIME) { 251 (void)snprintf(buf, sizeof(buf), "%d", c); 252 return (buf); 253 } 254 if (c == _POSIX_VDISABLE) 255 return ("<undef>"); 256 bp = buf; 257 if (c & 0200) { 258 *bp++ = 'M'; 259 *bp++ = '-'; 260 c &= 0177; 261 } 262 if (c == 0177) { 263 *bp++ = '^'; 264 *bp++ = '?'; 265 } 266 else if (c < 040) { 267 *bp++ = '^'; 268 *bp++ = c + '@'; 269 } 270 else 271 *bp++ = c; 272 *bp = '\0'; 273 return (buf); 274 } 275