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