Home | History | Annotate | Line # | Download | only in stty
print.c revision 1.9
      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 /*static char sccsid[] = "from: @(#)print.c	8.6 (Berkeley) 4/16/94";*/
     36 static char *rcsid = "$Id: print.c,v 1.9 1994/09/20 04:52:09 mycroft Exp $";
     37 #endif /* not lint */
     38 
     39 #include <sys/types.h>
     40 
     41 #include <stddef.h>
     42 #include <stdio.h>
     43 #include <string.h>
     44 
     45 #include "stty.h"
     46 #include "extern.h"
     47 
     48 static void  binit __P((char *));
     49 static void  bput __P((char *));
     50 static char *ccval __P((struct cchar *, int));
     51 
     52 void
     53 print(tp, wp, ldisc, fmt)
     54 	struct termios *tp;
     55 	struct winsize *wp;
     56 	int ldisc;
     57 	enum FMT fmt;
     58 {
     59 	struct cchar *p;
     60 	long tmp;
     61 	u_char *cc;
     62 	int cnt, ispeed, ospeed;
     63 	char buf1[100], buf2[100];
     64 
     65 	cnt = 0;
     66 
     67 	/* Line discipline. */
     68 	if (ldisc != TTYDISC) {
     69 		switch(ldisc) {
     70 		case TABLDISC:
     71 			cnt += printf("tablet disc; ");
     72 			break;
     73 		case SLIPDISC:
     74 			cnt += printf("slip disc; ");
     75 			break;
     76 		case PPPDISC:
     77 			cnt += printf("ppp disc; ");
     78 			break;
     79 		default:
     80 			cnt += printf("#%d disc; ", ldisc);
     81 			break;
     82 		}
     83 	}
     84 
     85 	/* Line speed. */
     86 	ispeed = cfgetispeed(tp);
     87 	ospeed = cfgetospeed(tp);
     88 	if (ispeed != ospeed)
     89 		cnt +=
     90 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
     91 	else
     92 		cnt += printf("speed %d baud;", ispeed);
     93 	if (fmt >= BSD)
     94 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
     95 	if (cnt)
     96 		(void)printf("\n");
     97 
     98 #define	on(f)	((tmp&f) != 0)
     99 #define put(n, f, d) \
    100 	if (fmt >= BSD || on(f) != d) \
    101 		bput(n + on(f));
    102 
    103 	/* "local" flags */
    104 	tmp = tp->c_lflag;
    105 	binit("lflags");
    106 	put("-icanon", ICANON, 1);
    107 	put("-isig", ISIG, 1);
    108 	put("-iexten", IEXTEN, 1);
    109 	put("-echo", ECHO, 1);
    110 	put("-echoe", ECHOE, 0);
    111 	put("-echok", ECHOK, 0);
    112 	put("-echoke", ECHOKE, 0);
    113 	put("-echonl", ECHONL, 0);
    114 	put("-echoctl", ECHOCTL, 0);
    115 	put("-echoprt", ECHOPRT, 0);
    116 	put("-altwerase", ALTWERASE, 0);
    117 	put("-noflsh", NOFLSH, 0);
    118 	put("-tostop", TOSTOP, 0);
    119 	put("-flusho", FLUSHO, 0);
    120 	put("-pendin", PENDIN, 0);
    121 	put("-nokerninfo", NOKERNINFO, 0);
    122 	put("-extproc", EXTPROC, 0);
    123 
    124 	/* input flags */
    125 	tmp = tp->c_iflag;
    126 	binit("iflags");
    127 	put("-istrip", ISTRIP, 0);
    128 	put("-icrnl", ICRNL, 1);
    129 	put("-inlcr", INLCR, 0);
    130 	put("-igncr", IGNCR, 0);
    131 	put("-ixon", IXON, 1);
    132 	put("-ixoff", IXOFF, 0);
    133 	put("-ixany", IXANY, 1);
    134 	put("-imaxbel", IMAXBEL, 1);
    135 	put("-ignbrk", IGNBRK, 0);
    136 	put("-brkint", BRKINT, 1);
    137 	put("-inpck", INPCK, 0);
    138 	put("-ignpar", IGNPAR, 0);
    139 	put("-parmrk", PARMRK, 0);
    140 
    141 	/* output flags */
    142 	tmp = tp->c_oflag;
    143 	binit("oflags");
    144 	put("-opost", OPOST, 1);
    145 	put("-onlcr", ONLCR, 1);
    146 	put("-oxtabs", OXTABS, 1);
    147 
    148 	/* control flags (hardware state) */
    149 	tmp = tp->c_cflag;
    150 	binit("cflags");
    151 	put("-cread", CREAD, 1);
    152 	switch(tmp&CSIZE) {
    153 	case CS5:
    154 		bput("cs5");
    155 		break;
    156 	case CS6:
    157 		bput("cs6");
    158 		break;
    159 	case CS7:
    160 		bput("cs7");
    161 		break;
    162 	case CS8:
    163 		bput("cs8");
    164 		break;
    165 	}
    166 	bput("-parenb" + on(PARENB));
    167 	put("-parodd", PARODD, 0);
    168 	put("-hupcl", HUPCL, 1);
    169 	put("-clocal", CLOCAL, 0);
    170 	put("-cstopb", CSTOPB, 0);
    171 	put("-crtscts", CRTSCTS, 0);
    172 	put("-mdmbuf", MDMBUF, 0);
    173 
    174 	/* special control characters */
    175 	cc = tp->c_cc;
    176 	if (fmt == POSIX) {
    177 		binit("cchars");
    178 		for (p = cchars1; p->name; ++p) {
    179 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
    180 			    p->name, ccval(p, cc[p->sub]));
    181 			bput(buf1);
    182 		}
    183 		binit(NULL);
    184 	} else {
    185 		binit(NULL);
    186 		for (p = cchars1, cnt = 0; p->name; ++p) {
    187 			if (fmt != BSD && cc[p->sub] == p->def)
    188 				continue;
    189 #define	WD	"%-8s"
    190 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
    191 			(void)sprintf(buf2 + cnt * 8, WD, ccval(p, cc[p->sub]));
    192 			if (++cnt == LINELENGTH / 8) {
    193 				cnt = 0;
    194 				(void)printf("%s\n", buf1);
    195 				(void)printf("%s\n", buf2);
    196 			}
    197 		}
    198 		if (cnt) {
    199 			(void)printf("%s\n", buf1);
    200 			(void)printf("%s\n", buf2);
    201 		}
    202 	}
    203 }
    204 
    205 static int col;
    206 static char *label;
    207 
    208 static void
    209 binit(lb)
    210 	char *lb;
    211 {
    212 
    213 	if (col) {
    214 		(void)printf("\n");
    215 		col = 0;
    216 	}
    217 	label = lb;
    218 }
    219 
    220 static void
    221 bput(s)
    222 	char *s;
    223 {
    224 
    225 	if (col == 0) {
    226 		col = printf("%s: %s", label, s);
    227 		return;
    228 	}
    229 	if ((col + strlen(s)) > LINELENGTH) {
    230 		(void)printf("\n\t");
    231 		col = printf("%s", s) + 8;
    232 		return;
    233 	}
    234 	col += printf(" %s", s);
    235 }
    236 
    237 static char *
    238 ccval(p, c)
    239 	struct cchar *p;
    240 	int c;
    241 {
    242 	static char buf[5];
    243 	char *bp;
    244 
    245 	if (c == _POSIX_VDISABLE)
    246 		return ("<undef>");
    247 
    248 	if (p->sub == VMIN || p->sub == VTIME) {
    249 		(void)snprintf(buf, sizeof(buf), "%d", c);
    250 		return (buf);
    251 	}
    252 	bp = buf;
    253 	if (c & 0200) {
    254 		*bp++ = 'M';
    255 		*bp++ = '-';
    256 		c &= 0177;
    257 	}
    258 	if (c == 0177) {
    259 		*bp++ = '^';
    260 		*bp++ = '?';
    261 	}
    262 	else if (c < 040) {
    263 		*bp++ = '^';
    264 		*bp++ = c + '@';
    265 	}
    266 	else
    267 		*bp++ = c;
    268 	*bp = '\0';
    269 	return (buf);
    270 }
    271