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