Home | History | Annotate | Line # | Download | only in stty
      1 /* $NetBSD: print.c,v 1.23 2013/09/12 19:47:23 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.23 2013/09/12 19:47:23 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 queue, const char *ldisc,
     56     enum FMT fmt)
     57 {
     58 	const struct cchar *p;
     59 	long tmp;
     60 	u_char *cc;
     61 	int cnt, ispeed, ospeed;
     62 	char buf1[100], buf2[100];
     63 
     64 	cnt = 0;
     65 
     66 	/* Line speed. */
     67 	ispeed = cfgetispeed(tp);
     68 	ospeed = cfgetospeed(tp);
     69 	if (ispeed != ospeed)
     70 		cnt +=
     71 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
     72 	else
     73 		cnt += printf("speed %d baud;", ispeed);
     74 	if (fmt >= STTY_BSD) {
     75 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
     76 		if (queue)
     77 			cnt += printf(" queue = %d;", queue);
     78 		if (ldisc)
     79 			cnt += printf(" line = %s;", ldisc);
     80 	}
     81 
     82 	if (cnt)
     83 		(void)printf("\n");
     84 
     85 #define	on(f)	((tmp&f) != 0)
     86 #define put(n, f, d) \
     87 	if (fmt >= STTY_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 
    128 	/* output flags */
    129 	tmp = tp->c_oflag;
    130 	binit("oflags");
    131 	put("-opost", OPOST, 1);
    132 	put("-onlcr", ONLCR, 1);
    133 	put("-ocrnl", OCRNL, 0);
    134 	put("-oxtabs", OXTABS, 1);
    135 	put("-onocr", OXTABS, 0);
    136 	put("-onlret", OXTABS, 0);
    137 
    138 	/* control flags (hardware state) */
    139 	tmp = tp->c_cflag;
    140 	binit("cflags");
    141 	put("-cread", CREAD, 1);
    142 	switch(tmp&CSIZE) {
    143 	case CS5:
    144 		bput("cs5");
    145 		break;
    146 	case CS6:
    147 		bput("cs6");
    148 		break;
    149 	case CS7:
    150 		bput("cs7");
    151 		break;
    152 	case CS8:
    153 		bput("cs8");
    154 		break;
    155 	}
    156 	bput("-parenb" + on(PARENB));
    157 	put("-parodd", PARODD, 0);
    158 	put("-hupcl", HUPCL, 1);
    159 	put("-clocal", CLOCAL, 0);
    160 	put("-cstopb", CSTOPB, 0);
    161 	put("-crtscts", CRTSCTS, 0);
    162 	put("-mdmbuf", MDMBUF, 0);
    163 	put("-cdtrcts", CDTRCTS, 0);
    164 
    165 	/* special control characters */
    166 	cc = tp->c_cc;
    167 	if (fmt == STTY_POSIX) {
    168 		binit("cchars");
    169 		for (p = cchars1; p->name; ++p) {
    170 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
    171 			    p->name, ccval(p, cc[p->sub]));
    172 			bput(buf1);
    173 		}
    174 		binit(NULL);
    175 	} else {
    176 		binit(NULL);
    177 		for (p = cchars1, cnt = 0; p->name; ++p) {
    178 			if (fmt != STTY_BSD && cc[p->sub] == p->def)
    179 				continue;
    180 #define	WD	"%-8s"
    181 			(void)snprintf(buf1 + cnt * 8, 9, WD, p->name);
    182 			(void)snprintf(buf2 + cnt * 8, 9, WD, ccval(p, cc[p->sub]));
    183 			if (++cnt == LINELENGTH / 8) {
    184 				cnt = 0;
    185 				(void)printf("%s\n", buf1);
    186 				(void)printf("%s\n", buf2);
    187 			}
    188 		}
    189 		if (cnt) {
    190 			(void)printf("%s\n", buf1);
    191 			(void)printf("%s\n", buf2);
    192 		}
    193 	}
    194 }
    195 
    196 static int col;
    197 static const char *label;
    198 
    199 static void
    200 binit(const char *lb)
    201 {
    202 
    203 	if (col) {
    204 		(void)printf("\n");
    205 		col = 0;
    206 	}
    207 	label = lb;
    208 }
    209 
    210 static void
    211 bput(const char *s)
    212 {
    213 
    214 	if (col == 0) {
    215 		col = printf("%s: %s", label, s);
    216 		return;
    217 	}
    218 	if ((col + strlen(s)) > LINELENGTH) {
    219 		(void)printf("\n\t");
    220 		col = printf("%s", s) + 8;
    221 		return;
    222 	}
    223 	col += printf(" %s", s);
    224 }
    225 
    226 static const char *
    227 ccval(const struct cchar *p, int c)
    228 {
    229 	static char buf[5];
    230 	char *bp;
    231 
    232 	if (c == _POSIX_VDISABLE)
    233 		return ("<undef>");
    234 
    235 	if (p->sub == VMIN || p->sub == VTIME) {
    236 		(void)snprintf(buf, sizeof(buf), "%d", c);
    237 		return (buf);
    238 	}
    239 	bp = buf;
    240 	if (c & 0200) {
    241 		*bp++ = 'M';
    242 		*bp++ = '-';
    243 		c &= 0177;
    244 	}
    245 	if (c == 0177) {
    246 		*bp++ = '^';
    247 		*bp++ = '?';
    248 	}
    249 	else if (c < 040) {
    250 		*bp++ = '^';
    251 		*bp++ = c + '@';
    252 	}
    253 	else
    254 		*bp++ = c;
    255 	*bp = '\0';
    256 	return (buf);
    257 }
    258