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