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