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