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 * 3. 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 switch(tmp&TABDLY) { 140 case TAB0: 141 bput("tab0"); 142 break; 143 case TAB3: 144 bput("tab3"); 145 break; 146 } 147 put("-onocr", ONOCR, 0); 148 put("-onlret", ONLRET, 0); 149 150 /* control flags (hardware state) */ 151 tmp = tp->c_cflag; 152 binit("cflags"); 153 put("-cread", CREAD, 1); 154 switch(tmp&CSIZE) { 155 case CS5: 156 bput("cs5"); 157 break; 158 case CS6: 159 bput("cs6"); 160 break; 161 case CS7: 162 bput("cs7"); 163 break; 164 case CS8: 165 bput("cs8"); 166 break; 167 } 168 bput("-parenb" + on(PARENB)); 169 put("-parodd", PARODD, 0); 170 put("-hupcl", HUPCL, 1); 171 put("-clocal", CLOCAL, 0); 172 put("-cstopb", CSTOPB, 0); 173 switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) { 174 case CCTS_OFLOW: 175 bput("ctsflow"); 176 break; 177 case CRTS_IFLOW: 178 bput("rtsflow"); 179 break; 180 default: 181 put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0); 182 break; 183 } 184 put("-dsrflow", CDSR_OFLOW, 0); 185 put("-dtrflow", CDTR_IFLOW, 0); 186 put("-mdmbuf", MDMBUF, 0); /* XXX mdmbuf == dtrflow */ 187 188 /* special control characters */ 189 cc = tp->c_cc; 190 if (fmt == POSIX) { 191 binit("cchars"); 192 for (p = cchars1; p->name; ++p) { 193 (void)snprintf(buf1, sizeof(buf1), "%s = %s;", 194 p->name, ccval(p, cc[p->sub])); 195 bput(buf1); 196 } 197 binit(NULL); 198 } else { 199 binit(NULL); 200 for (p = cchars1, cnt = 0; p->name; ++p) { 201 if (fmt != BSD && cc[p->sub] == p->def) 202 continue; 203 #define WD "%-8s" 204 (void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8, 205 WD, p->name); 206 (void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8, 207 WD, ccval(p, cc[p->sub])); 208 if (++cnt == LINELENGTH / 8) { 209 cnt = 0; 210 (void)printf("%s\n", buf1); 211 (void)printf("%s\n", buf2); 212 } 213 } 214 if (cnt) { 215 (void)printf("%s\n", buf1); 216 (void)printf("%s\n", buf2); 217 } 218 } 219 } 220 221 static int col; 222 static const char *label; 223 224 static void 225 binit(const char *lb) 226 { 227 228 if (col) { 229 (void)printf("\n"); 230 col = 0; 231 } 232 label = lb; 233 } 234 235 static void 236 bput(const char *s) 237 { 238 239 if (col == 0) { 240 col = printf("%s: %s", label, s); 241 return; 242 } 243 if ((col + strlen(s)) > LINELENGTH) { 244 (void)printf("\n\t"); 245 col = printf("%s", s) + 8; 246 return; 247 } 248 col += printf(" %s", s); 249 } 250 251 static const char * 252 ccval(struct cchar *p, int c) 253 { 254 static char buf[5]; 255 char *bp; 256 257 if (p->sub == VMIN || p->sub == VTIME) { 258 (void)snprintf(buf, sizeof(buf), "%d", c); 259 return (buf); 260 } 261 if (c == _POSIX_VDISABLE) 262 return ("<undef>"); 263 bp = buf; 264 if (c & 0200) { 265 *bp++ = 'M'; 266 *bp++ = '-'; 267 c &= 0177; 268 } 269 if (c == 0177) { 270 *bp++ = '^'; 271 *bp++ = '?'; 272 } 273 else if (c < 040) { 274 *bp++ = '^'; 275 *bp++ = c + '@'; 276 } 277 else 278 *bp++ = c; 279 *bp = '\0'; 280 return (buf); 281 } 282