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 if (on(CNO_RTSDTR)) 188 bput("-rtsdtr"); 189 else { 190 if (fmt >= BSD) 191 bput("rtsdtr"); 192 } 193 194 /* special control characters */ 195 cc = tp->c_cc; 196 if (fmt == POSIX) { 197 binit("cchars"); 198 for (p = cchars1; p->name; ++p) { 199 (void)snprintf(buf1, sizeof(buf1), "%s = %s;", 200 p->name, ccval(p, cc[p->sub])); 201 bput(buf1); 202 } 203 binit(NULL); 204 } else { 205 binit(NULL); 206 for (p = cchars1, cnt = 0; p->name; ++p) { 207 if (fmt != BSD && cc[p->sub] == p->def) 208 continue; 209 #define WD "%-8s" 210 (void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8, 211 WD, p->name); 212 (void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8, 213 WD, ccval(p, cc[p->sub])); 214 if (++cnt == LINELENGTH / 8) { 215 cnt = 0; 216 (void)printf("%s\n", buf1); 217 (void)printf("%s\n", buf2); 218 } 219 } 220 if (cnt) { 221 (void)printf("%s\n", buf1); 222 (void)printf("%s\n", buf2); 223 } 224 } 225 } 226 227 static int col; 228 static const char *label; 229 230 static void 231 binit(const char *lb) 232 { 233 234 if (col) { 235 (void)printf("\n"); 236 col = 0; 237 } 238 label = lb; 239 } 240 241 static void 242 bput(const char *s) 243 { 244 245 if (col == 0) { 246 col = printf("%s: %s", label, s); 247 return; 248 } 249 if ((col + strlen(s)) > LINELENGTH) { 250 (void)printf("\n\t"); 251 col = printf("%s", s) + 8; 252 return; 253 } 254 col += printf(" %s", s); 255 } 256 257 static const char * 258 ccval(struct cchar *p, int c) 259 { 260 static char buf[5]; 261 char *bp; 262 263 if (p->sub == VMIN || p->sub == VTIME) { 264 (void)snprintf(buf, sizeof(buf), "%d", c); 265 return (buf); 266 } 267 if (c == _POSIX_VDISABLE) 268 return ("<undef>"); 269 bp = buf; 270 if (c & 0200) { 271 *bp++ = 'M'; 272 *bp++ = '-'; 273 c &= 0177; 274 } 275 if (c == 0177) { 276 *bp++ = '^'; 277 *bp++ = '?'; 278 } 279 else if (c < 040) { 280 *bp++ = '^'; 281 *bp++ = c + '@'; 282 } 283 else 284 *bp++ = c; 285 *bp = '\0'; 286 return (buf); 287 } 288