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