Home | History | Annotate | Line # | Download | only in stty
print.c revision 1.14
      1 /*	$NetBSD: print.c,v 1.14 1997/10/20 08:08:07 scottr 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. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed by the University of
     18  *	California, Berkeley and its contributors.
     19  * 4. Neither the name of the University nor the names of its contributors
     20  *    may be used to endorse or promote products derived from this software
     21  *    without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  * SUCH DAMAGE.
     34  */
     35 
     36 #include <sys/cdefs.h>
     37 #ifndef lint
     38 #if 0
     39 static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
     40 #else
     41 __RCSID("$NetBSD: print.c,v 1.14 1997/10/20 08:08:07 scottr 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 		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("-ignbrk", IGNBRK, 0);
    142 	put("-brkint", BRKINT, 1);
    143 	put("-inpck", INPCK, 0);
    144 	put("-ignpar", IGNPAR, 0);
    145 	put("-parmrk", PARMRK, 0);
    146 
    147 	/* output flags */
    148 	tmp = tp->c_oflag;
    149 	binit("oflags");
    150 	put("-opost", OPOST, 1);
    151 	put("-onlcr", ONLCR, 1);
    152 	put("-ocrnl", OCRNL, 0);
    153 	put("-oxtabs", OXTABS, 1);
    154 
    155 	/* control flags (hardware state) */
    156 	tmp = tp->c_cflag;
    157 	binit("cflags");
    158 	put("-cread", CREAD, 1);
    159 	switch(tmp&CSIZE) {
    160 	case CS5:
    161 		bput("cs5");
    162 		break;
    163 	case CS6:
    164 		bput("cs6");
    165 		break;
    166 	case CS7:
    167 		bput("cs7");
    168 		break;
    169 	case CS8:
    170 		bput("cs8");
    171 		break;
    172 	}
    173 	bput("-parenb" + on(PARENB));
    174 	put("-parodd", PARODD, 0);
    175 	put("-hupcl", HUPCL, 1);
    176 	put("-clocal", CLOCAL, 0);
    177 	put("-cstopb", CSTOPB, 0);
    178 	put("-crtscts", CRTSCTS, 0);
    179 	put("-mdmbuf", MDMBUF, 0);
    180 	put("-cdtrcts", CDTRCTS, 0);
    181 
    182 	/* special control characters */
    183 	cc = tp->c_cc;
    184 	if (fmt == POSIX) {
    185 		binit("cchars");
    186 		for (p = cchars1; p->name; ++p) {
    187 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
    188 			    p->name, ccval(p, cc[p->sub]));
    189 			bput(buf1);
    190 		}
    191 		binit(NULL);
    192 	} else {
    193 		binit(NULL);
    194 		for (p = cchars1, cnt = 0; p->name; ++p) {
    195 			if (fmt != BSD && cc[p->sub] == p->def)
    196 				continue;
    197 #define	WD	"%-8s"
    198 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
    199 			(void)sprintf(buf2 + cnt * 8, WD, ccval(p, cc[p->sub]));
    200 			if (++cnt == LINELENGTH / 8) {
    201 				cnt = 0;
    202 				(void)printf("%s\n", buf1);
    203 				(void)printf("%s\n", buf2);
    204 			}
    205 		}
    206 		if (cnt) {
    207 			(void)printf("%s\n", buf1);
    208 			(void)printf("%s\n", buf2);
    209 		}
    210 	}
    211 }
    212 
    213 static int col;
    214 static char *label;
    215 
    216 static void
    217 binit(lb)
    218 	char *lb;
    219 {
    220 
    221 	if (col) {
    222 		(void)printf("\n");
    223 		col = 0;
    224 	}
    225 	label = lb;
    226 }
    227 
    228 static void
    229 bput(s)
    230 	char *s;
    231 {
    232 
    233 	if (col == 0) {
    234 		col = printf("%s: %s", label, s);
    235 		return;
    236 	}
    237 	if ((col + strlen(s)) > LINELENGTH) {
    238 		(void)printf("\n\t");
    239 		col = printf("%s", s) + 8;
    240 		return;
    241 	}
    242 	col += printf(" %s", s);
    243 }
    244 
    245 static char *
    246 ccval(p, c)
    247 	const struct cchar *p;
    248 	int c;
    249 {
    250 	static char buf[5];
    251 	char *bp;
    252 
    253 	if (c == _POSIX_VDISABLE)
    254 		return ("<undef>");
    255 
    256 	if (p->sub == VMIN || p->sub == VTIME) {
    257 		(void)snprintf(buf, sizeof(buf), "%d", c);
    258 		return (buf);
    259 	}
    260 	bp = buf;
    261 	if (c & 0200) {
    262 		*bp++ = 'M';
    263 		*bp++ = '-';
    264 		c &= 0177;
    265 	}
    266 	if (c == 0177) {
    267 		*bp++ = '^';
    268 		*bp++ = '?';
    269 	}
    270 	else if (c < 040) {
    271 		*bp++ = '^';
    272 		*bp++ = c + '@';
    273 	}
    274 	else
    275 		*bp++ = c;
    276 	*bp = '\0';
    277 	return (buf);
    278 }
    279