1 /* $OpenBSD: print.c,v 1.8 1999/10/26 22:53:52 deraadt Exp $ */ 2 /* $NetBSD: print.c,v 1.11 1996/05/07 18:20:10 jtc Exp $ */ 3 4 /*- 5 * Copyright (c) 1991, 1993, 1994 6 * The Regents of the University of California. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. All advertising materials mentioning features or use of this software 17 * must display the following acknowledgement: 18 * This product includes software developed by the University of 19 * California, Berkeley and its contributors. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #ifndef lint 38 #if 0 39 static char sccsid[] = "@(#)print.c 8.6 (Berkeley) 4/16/94"; 40 #else 41 static char rcsid[] = "$OpenBSD: print.c,v 1.8 1999/10/26 22:53:52 deraadt Exp $"; 42 #endif 43 #endif /* not lint */ 44 45 #include <sys/types.h> 46 47 #include <stddef.h> 48 #include <stdio.h> 49 #include <string.h> 50 51 #include "stty.h" 52 #include "extern.h" 53 54 static void binit __P((char *)); 55 static void bput __P((char *)); 56 static char *ccval __P((const struct cchar *, int)); 57 58 void 59 print(tp, wp, ldisc, fmt) 60 struct termios *tp; 61 struct winsize *wp; 62 int ldisc; 63 enum FMT fmt; 64 { 65 const struct cchar *p; 66 long tmp; 67 u_char *cc; 68 int cnt, ispeed, ospeed; 69 char buf1[100], buf2[100]; 70 71 cnt = 0; 72 73 /* Line discipline. */ 74 if (ldisc != TTYDISC) { 75 switch(ldisc) { 76 case TABLDISC: 77 cnt += printf("tablet disc; "); 78 break; 79 case SLIPDISC: 80 cnt += printf("slip disc; "); 81 break; 82 case PPPDISC: 83 cnt += printf("ppp disc; "); 84 break; 85 case STRIPDISC: 86 cnt += printf("strip disc; "); 87 break; 88 default: 89 cnt += printf("#%d disc; ", ldisc); 90 break; 91 } 92 } 93 94 /* Line speed. */ 95 ispeed = cfgetispeed(tp); 96 ospeed = cfgetospeed(tp); 97 if (ispeed != ospeed) 98 cnt += 99 printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed); 100 else 101 cnt += printf("speed %d baud;", ispeed); 102 if (fmt >= BSD) 103 cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col); 104 if (cnt) 105 (void)printf("\n"); 106 107 #define on(f) ((tmp&f) != 0) 108 #define put(n, f, d) \ 109 if (fmt >= BSD || on(f) != d) \ 110 bput(n + on(f)); 111 112 /* "local" flags */ 113 tmp = tp->c_lflag; 114 binit("lflags"); 115 put("-icanon", ICANON, 1); 116 put("-isig", ISIG, 1); 117 put("-iexten", IEXTEN, 1); 118 put("-echo", ECHO, 1); 119 put("-echoe", ECHOE, 0); 120 put("-echok", ECHOK, 0); 121 put("-echoke", ECHOKE, 0); 122 put("-echonl", ECHONL, 0); 123 put("-echoctl", ECHOCTL, 0); 124 put("-echoprt", ECHOPRT, 0); 125 put("-altwerase", ALTWERASE, 0); 126 put("-noflsh", NOFLSH, 0); 127 put("-tostop", TOSTOP, 0); 128 put("-flusho", FLUSHO, 0); 129 put("-pendin", PENDIN, 0); 130 put("-nokerninfo", NOKERNINFO, 0); 131 put("-extproc", EXTPROC, 0); 132 put("-xcase", XCASE, 0); 133 134 /* input flags */ 135 tmp = tp->c_iflag; 136 binit("iflags"); 137 put("-istrip", ISTRIP, 0); 138 put("-icrnl", ICRNL, 1); 139 put("-inlcr", INLCR, 0); 140 put("-igncr", IGNCR, 0); 141 put("-iuclc", IUCLC, 0); 142 put("-ixon", IXON, 1); 143 put("-ixoff", IXOFF, 0); 144 put("-ixany", IXANY, 1); 145 put("-imaxbel", IMAXBEL, 1); 146 put("-ignbrk", IGNBRK, 0); 147 put("-brkint", BRKINT, 1); 148 put("-inpck", INPCK, 0); 149 put("-ignpar", IGNPAR, 0); 150 put("-parmrk", PARMRK, 0); 151 152 /* output flags */ 153 tmp = tp->c_oflag; 154 binit("oflags"); 155 put("-opost", OPOST, 1); 156 put("-onlcr", ONLCR, 1); 157 put("-ocrnl", OCRNL, 0); 158 put("-onocr", ONOCR, 0); 159 put("-onlret", ONLRET, 0); 160 put("-olcuc", OLCUC, 0); 161 put("-oxtabs", OXTABS, 1); 162 put("-onoeot", ONOEOT, 0); 163 164 /* control flags (hardware state) */ 165 tmp = tp->c_cflag; 166 binit("cflags"); 167 put("-cread", CREAD, 1); 168 switch(tmp&CSIZE) { 169 case CS5: 170 bput("cs5"); 171 break; 172 case CS6: 173 bput("cs6"); 174 break; 175 case CS7: 176 bput("cs7"); 177 break; 178 case CS8: 179 bput("cs8"); 180 break; 181 } 182 bput("-parenb" + on(PARENB)); 183 put("-parodd", PARODD, 0); 184 put("-hupcl", HUPCL, 1); 185 put("-clocal", CLOCAL, 0); 186 put("-cstopb", CSTOPB, 0); 187 put("-crtscts", CRTSCTS, 0); 188 put("-mdmbuf", MDMBUF, 0); 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 char *label; 225 226 static void 227 binit(lb) 228 char *lb; 229 { 230 231 if (col) { 232 (void)printf("\n"); 233 col = 0; 234 } 235 label = lb; 236 } 237 238 static void 239 bput(s) 240 char *s; 241 { 242 243 if (col == 0) { 244 col = printf("%s: %s", label, s); 245 return; 246 } 247 if ((col + strlen(s)) > LINELENGTH) { 248 (void)printf("\n\t"); 249 col = printf("%s", s) + 8; 250 return; 251 } 252 col += printf(" %s", s); 253 } 254 255 static char * 256 ccval(p, c) 257 const struct cchar *p; 258 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