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 #include <sys/types.h> 37 38 #include <stddef.h> 39 #include <stdio.h> 40 #include <string.h> 41 42 #include "stty.h" 43 #include "extern.h" 44 45 static void binit(const char *); 46 static void bput(const char *); 47 static const char *ccval(struct cchar *, int); 48 49 void 50 print(struct termios *tp, struct winsize *wp, int ldisc, enum FMT fmt) 51 { 52 struct cchar *p; 53 long tmp; 54 u_char *cc; 55 int cnt, ispeed, ospeed; 56 char buf1[100], buf2[100]; 57 58 cnt = 0; 59 60 /* Line discipline. */ 61 if (ldisc != TTYDISC) { 62 switch(ldisc) { 63 case SLIPDISC: 64 cnt += printf("slip disc; "); 65 break; 66 case PPPDISC: 67 cnt += printf("ppp disc; "); 68 break; 69 default: 70 cnt += printf("#%d disc; ", ldisc); 71 break; 72 } 73 } 74 75 /* Line speed. */ 76 ispeed = cfgetispeed(tp); 77 ospeed = cfgetospeed(tp); 78 if (ispeed != ospeed) 79 cnt += 80 printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed); 81 else 82 cnt += printf("speed %d baud;", ispeed); 83 if (fmt >= BSD) 84 cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col); 85 if (cnt) 86 (void)printf("\n"); 87 88 #define on(f) ((tmp & (f)) != 0) 89 #define put(n, f, d) \ 90 if (fmt >= BSD || on(f) != (d)) \ 91 bput((n) + on(f)); 92 93 /* "local" flags */ 94 tmp = tp->c_lflag; 95 binit("lflags"); 96 put("-icanon", ICANON, 1); 97 put("-isig", ISIG, 1); 98 put("-iexten", IEXTEN, 1); 99 put("-echo", ECHO, 1); 100 put("-echoe", ECHOE, 0); 101 put("-echok", ECHOK, 0); 102 put("-echoke", ECHOKE, 0); 103 put("-echonl", ECHONL, 0); 104 put("-echoctl", ECHOCTL, 0); 105 put("-echoprt", ECHOPRT, 0); 106 put("-altwerase", ALTWERASE, 0); 107 put("-noflsh", NOFLSH, 0); 108 put("-tostop", TOSTOP, 0); 109 put("-flusho", FLUSHO, 0); 110 put("-pendin", PENDIN, 0); 111 put("-nokerninfo", NOKERNINFO, 0); 112 put("-extproc", EXTPROC, 0); 113 114 /* input flags */ 115 tmp = tp->c_iflag; 116 binit("iflags"); 117 put("-istrip", ISTRIP, 0); 118 put("-icrnl", ICRNL, 1); 119 put("-inlcr", INLCR, 0); 120 put("-igncr", IGNCR, 0); 121 put("-ixon", IXON, 1); 122 put("-ixoff", IXOFF, 0); 123 put("-ixany", IXANY, 1); 124 put("-imaxbel", IMAXBEL, 1); 125 put("-ignbrk", IGNBRK, 0); 126 put("-brkint", BRKINT, 1); 127 put("-inpck", INPCK, 0); 128 put("-ignpar", IGNPAR, 0); 129 put("-parmrk", PARMRK, 0); 130 put("-iutf8", IUTF8, 1); 131 132 /* output flags */ 133 tmp = tp->c_oflag; 134 binit("oflags"); 135 put("-opost", OPOST, 1); 136 put("-onlcr", ONLCR, 1); 137 put("-ocrnl", OCRNL, 0); 138 switch(tmp&TABDLY) { 139 case TAB0: 140 bput("tab0"); 141 break; 142 case TAB3: 143 bput("tab3"); 144 break; 145 } 146 put("-onocr", ONOCR, 0); 147 put("-onlret", ONLRET, 0); 148 149 /* control flags (hardware state) */ 150 tmp = tp->c_cflag; 151 binit("cflags"); 152 put("-cread", CREAD, 1); 153 switch(tmp&CSIZE) { 154 case CS5: 155 bput("cs5"); 156 break; 157 case CS6: 158 bput("cs6"); 159 break; 160 case CS7: 161 bput("cs7"); 162 break; 163 case CS8: 164 bput("cs8"); 165 break; 166 } 167 bput("-parenb" + on(PARENB)); 168 put("-parodd", PARODD, 0); 169 put("-hupcl", HUPCL, 1); 170 put("-clocal", CLOCAL, 0); 171 put("-cstopb", CSTOPB, 0); 172 switch(tmp & (CCTS_OFLOW | CRTS_IFLOW)) { 173 case CCTS_OFLOW: 174 bput("ctsflow"); 175 break; 176 case CRTS_IFLOW: 177 bput("rtsflow"); 178 break; 179 default: 180 put("-crtscts", CCTS_OFLOW | CRTS_IFLOW, 0); 181 break; 182 } 183 put("-dsrflow", CDSR_OFLOW, 0); 184 put("-dtrflow", CDTR_IFLOW, 0); 185 put("-mdmbuf", MDMBUF, 0); /* XXX mdmbuf == dtrflow */ 186 if (on(CNO_RTSDTR)) 187 bput("-rtsdtr"); 188 else { 189 if (fmt >= BSD) 190 bput("rtsdtr"); 191 } 192 193 /* special control characters */ 194 cc = tp->c_cc; 195 if (fmt == POSIX) { 196 binit("cchars"); 197 for (p = cchars1; p->name; ++p) { 198 (void)snprintf(buf1, sizeof(buf1), "%s = %s;", 199 p->name, ccval(p, cc[p->sub])); 200 bput(buf1); 201 } 202 binit(NULL); 203 } else { 204 binit(NULL); 205 for (p = cchars1, cnt = 0; p->name; ++p) { 206 if (fmt != BSD && cc[p->sub] == p->def) 207 continue; 208 #define WD "%-8s" 209 (void)snprintf(buf1 + cnt * 8, sizeof(buf1) - cnt * 8, 210 WD, p->name); 211 (void)snprintf(buf2 + cnt * 8, sizeof(buf2) - cnt * 8, 212 WD, ccval(p, cc[p->sub])); 213 if (++cnt == LINELENGTH / 8) { 214 cnt = 0; 215 (void)printf("%s\n", buf1); 216 (void)printf("%s\n", buf2); 217 } 218 } 219 if (cnt) { 220 (void)printf("%s\n", buf1); 221 (void)printf("%s\n", buf2); 222 } 223 } 224 } 225 226 static int col; 227 static const char *label; 228 229 static void 230 binit(const char *lb) 231 { 232 233 if (col) { 234 (void)printf("\n"); 235 col = 0; 236 } 237 label = lb; 238 } 239 240 static void 241 bput(const char *s) 242 { 243 244 if (col == 0) { 245 col = printf("%s: %s", label, s); 246 return; 247 } 248 if ((col + strlen(s)) > LINELENGTH) { 249 (void)printf("\n\t"); 250 col = printf("%s", s) + 8; 251 return; 252 } 253 col += printf(" %s", s); 254 } 255 256 static const char * 257 ccval(struct cchar *p, int c) 258 { 259 static char buf[5]; 260 char *bp; 261 262 if (p->sub == VMIN || p->sub == VTIME) { 263 (void)snprintf(buf, sizeof(buf), "%d", c); 264 return (buf); 265 } 266 if (c == _POSIX_VDISABLE) 267 return ("<undef>"); 268 bp = buf; 269 if (c & 0200) { 270 *bp++ = 'M'; 271 *bp++ = '-'; 272 c &= 0177; 273 } 274 if (c == 0177) { 275 *bp++ = '^'; 276 *bp++ = '?'; 277 } 278 else if (c < 040) { 279 *bp++ = '^'; 280 *bp++ = c + '@'; 281 } 282 else 283 *bp++ = c; 284 *bp = '\0'; 285 return (buf); 286 } 287