Home | History | Annotate | Line # | Download | only in stty
print.c revision 1.1.1.2
      1      1.1      cgd /*-
      2  1.1.1.2  mycroft  * Copyright (c) 1991, 1993, 1994
      3  1.1.1.2  mycroft  *	The Regents of the University of California.  All rights reserved.
      4      1.1      cgd  *
      5      1.1      cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1      cgd  * modification, are permitted provided that the following conditions
      7      1.1      cgd  * are met:
      8      1.1      cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1      cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1      cgd  *    must display the following acknowledgement:
     15      1.1      cgd  *	This product includes software developed by the University of
     16      1.1      cgd  *	California, Berkeley and its contributors.
     17      1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1      cgd  *    may be used to endorse or promote products derived from this software
     19      1.1      cgd  *    without specific prior written permission.
     20      1.1      cgd  *
     21      1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1      cgd  * SUCH DAMAGE.
     32      1.1      cgd  */
     33      1.1      cgd 
     34      1.1      cgd #ifndef lint
     35  1.1.1.2  mycroft static char sccsid[] = "@(#)print.c	8.6 (Berkeley) 4/16/94";
     36      1.1      cgd #endif /* not lint */
     37      1.1      cgd 
     38      1.1      cgd #include <sys/types.h>
     39  1.1.1.2  mycroft 
     40      1.1      cgd #include <stddef.h>
     41      1.1      cgd #include <stdio.h>
     42      1.1      cgd #include <string.h>
     43  1.1.1.2  mycroft 
     44      1.1      cgd #include "stty.h"
     45      1.1      cgd #include "extern.h"
     46      1.1      cgd 
     47      1.1      cgd static void  binit __P((char *));
     48      1.1      cgd static void  bput __P((char *));
     49  1.1.1.2  mycroft static char *ccval __P((struct cchar *, int));
     50      1.1      cgd 
     51      1.1      cgd void
     52      1.1      cgd print(tp, wp, ldisc, fmt)
     53      1.1      cgd 	struct termios *tp;
     54      1.1      cgd 	struct winsize *wp;
     55      1.1      cgd 	int ldisc;
     56      1.1      cgd 	enum FMT fmt;
     57      1.1      cgd {
     58  1.1.1.2  mycroft 	struct cchar *p;
     59  1.1.1.2  mycroft 	long tmp;
     60  1.1.1.2  mycroft 	u_char *cc;
     61  1.1.1.2  mycroft 	int cnt, ispeed, ospeed;
     62      1.1      cgd 	char buf1[100], buf2[100];
     63      1.1      cgd 
     64      1.1      cgd 	cnt = 0;
     65      1.1      cgd 
     66      1.1      cgd 	/* Line discipline. */
     67      1.1      cgd 	if (ldisc != TTYDISC) {
     68      1.1      cgd 		switch(ldisc) {
     69      1.1      cgd 		case TABLDISC:
     70      1.1      cgd 			cnt += printf("tablet disc; ");
     71      1.1      cgd 			break;
     72      1.1      cgd 		case SLIPDISC:
     73      1.1      cgd 			cnt += printf("slip disc; ");
     74      1.1      cgd 			break;
     75      1.1      cgd 		default:
     76      1.1      cgd 			cnt += printf("#%d disc; ", ldisc);
     77      1.1      cgd 			break;
     78      1.1      cgd 		}
     79      1.1      cgd 	}
     80      1.1      cgd 
     81      1.1      cgd 	/* Line speed. */
     82      1.1      cgd 	ispeed = cfgetispeed(tp);
     83      1.1      cgd 	ospeed = cfgetospeed(tp);
     84      1.1      cgd 	if (ispeed != ospeed)
     85      1.1      cgd 		cnt +=
     86      1.1      cgd 		    printf("ispeed %d baud; ospeed %d baud;", ispeed, ospeed);
     87      1.1      cgd 	else
     88      1.1      cgd 		cnt += printf("speed %d baud;", ispeed);
     89      1.1      cgd 	if (fmt >= BSD)
     90      1.1      cgd 		cnt += printf(" %d rows; %d columns;", wp->ws_row, wp->ws_col);
     91      1.1      cgd 	if (cnt)
     92      1.1      cgd 		(void)printf("\n");
     93      1.1      cgd 
     94      1.1      cgd #define	on(f)	((tmp&f) != 0)
     95      1.1      cgd #define put(n, f, d) \
     96      1.1      cgd 	if (fmt >= BSD || on(f) != d) \
     97      1.1      cgd 		bput(n + on(f));
     98      1.1      cgd 
     99      1.1      cgd 	/* "local" flags */
    100      1.1      cgd 	tmp = tp->c_lflag;
    101      1.1      cgd 	binit("lflags");
    102      1.1      cgd 	put("-icanon", ICANON, 1);
    103      1.1      cgd 	put("-isig", ISIG, 1);
    104      1.1      cgd 	put("-iexten", IEXTEN, 1);
    105      1.1      cgd 	put("-echo", ECHO, 1);
    106      1.1      cgd 	put("-echoe", ECHOE, 0);
    107      1.1      cgd 	put("-echok", ECHOK, 0);
    108      1.1      cgd 	put("-echoke", ECHOKE, 0);
    109      1.1      cgd 	put("-echonl", ECHONL, 0);
    110      1.1      cgd 	put("-echoctl", ECHOCTL, 0);
    111      1.1      cgd 	put("-echoprt", ECHOPRT, 0);
    112      1.1      cgd 	put("-altwerase", ALTWERASE, 0);
    113      1.1      cgd 	put("-noflsh", NOFLSH, 0);
    114      1.1      cgd 	put("-tostop", TOSTOP, 0);
    115      1.1      cgd 	put("-flusho", FLUSHO, 0);
    116      1.1      cgd 	put("-pendin", PENDIN, 0);
    117      1.1      cgd 	put("-nokerninfo", NOKERNINFO, 0);
    118      1.1      cgd 	put("-extproc", EXTPROC, 0);
    119      1.1      cgd 
    120      1.1      cgd 	/* input flags */
    121      1.1      cgd 	tmp = tp->c_iflag;
    122      1.1      cgd 	binit("iflags");
    123      1.1      cgd 	put("-istrip", ISTRIP, 0);
    124      1.1      cgd 	put("-icrnl", ICRNL, 1);
    125      1.1      cgd 	put("-inlcr", INLCR, 0);
    126      1.1      cgd 	put("-igncr", IGNCR, 0);
    127      1.1      cgd 	put("-ixon", IXON, 1);
    128      1.1      cgd 	put("-ixoff", IXOFF, 0);
    129      1.1      cgd 	put("-ixany", IXANY, 1);
    130      1.1      cgd 	put("-imaxbel", IMAXBEL, 1);
    131      1.1      cgd 	put("-ignbrk", IGNBRK, 0);
    132      1.1      cgd 	put("-brkint", BRKINT, 1);
    133      1.1      cgd 	put("-inpck", INPCK, 0);
    134      1.1      cgd 	put("-ignpar", IGNPAR, 0);
    135      1.1      cgd 	put("-parmrk", PARMRK, 0);
    136      1.1      cgd 
    137      1.1      cgd 	/* output flags */
    138      1.1      cgd 	tmp = tp->c_oflag;
    139      1.1      cgd 	binit("oflags");
    140      1.1      cgd 	put("-opost", OPOST, 1);
    141      1.1      cgd 	put("-onlcr", ONLCR, 1);
    142      1.1      cgd 	put("-oxtabs", OXTABS, 1);
    143      1.1      cgd 
    144      1.1      cgd 	/* control flags (hardware state) */
    145      1.1      cgd 	tmp = tp->c_cflag;
    146      1.1      cgd 	binit("cflags");
    147      1.1      cgd 	put("-cread", CREAD, 1);
    148      1.1      cgd 	switch(tmp&CSIZE) {
    149      1.1      cgd 	case CS5:
    150      1.1      cgd 		bput("cs5");
    151      1.1      cgd 		break;
    152      1.1      cgd 	case CS6:
    153      1.1      cgd 		bput("cs6");
    154      1.1      cgd 		break;
    155      1.1      cgd 	case CS7:
    156      1.1      cgd 		bput("cs7");
    157      1.1      cgd 		break;
    158      1.1      cgd 	case CS8:
    159      1.1      cgd 		bput("cs8");
    160      1.1      cgd 		break;
    161      1.1      cgd 	}
    162      1.1      cgd 	bput("-parenb" + on(PARENB));
    163      1.1      cgd 	put("-parodd", PARODD, 0);
    164      1.1      cgd 	put("-hupcl", HUPCL, 1);
    165      1.1      cgd 	put("-clocal", CLOCAL, 0);
    166      1.1      cgd 	put("-cstopb", CSTOPB, 0);
    167      1.1      cgd 	put("-crtscts", CRTSCTS, 0);
    168  1.1.1.2  mycroft 	put("-mdmbuf", MDMBUF, 0);
    169      1.1      cgd 
    170      1.1      cgd 	/* special control characters */
    171      1.1      cgd 	cc = tp->c_cc;
    172      1.1      cgd 	if (fmt == POSIX) {
    173      1.1      cgd 		binit("cchars");
    174      1.1      cgd 		for (p = cchars1; p->name; ++p) {
    175      1.1      cgd 			(void)snprintf(buf1, sizeof(buf1), "%s = %s;",
    176  1.1.1.2  mycroft 			    p->name, ccval(p, cc[p->sub]));
    177      1.1      cgd 			bput(buf1);
    178      1.1      cgd 		}
    179      1.1      cgd 		binit(NULL);
    180      1.1      cgd 	} else {
    181      1.1      cgd 		binit(NULL);
    182      1.1      cgd 		for (p = cchars1, cnt = 0; p->name; ++p) {
    183      1.1      cgd 			if (fmt != BSD && cc[p->sub] == p->def)
    184      1.1      cgd 				continue;
    185      1.1      cgd #define	WD	"%-8s"
    186      1.1      cgd 			(void)sprintf(buf1 + cnt * 8, WD, p->name);
    187  1.1.1.2  mycroft 			(void)sprintf(buf2 + cnt * 8, WD, ccval(p, cc[p->sub]));
    188      1.1      cgd 			if (++cnt == LINELENGTH / 8) {
    189      1.1      cgd 				cnt = 0;
    190      1.1      cgd 				(void)printf("%s\n", buf1);
    191      1.1      cgd 				(void)printf("%s\n", buf2);
    192      1.1      cgd 			}
    193      1.1      cgd 		}
    194      1.1      cgd 		if (cnt) {
    195      1.1      cgd 			(void)printf("%s\n", buf1);
    196      1.1      cgd 			(void)printf("%s\n", buf2);
    197      1.1      cgd 		}
    198      1.1      cgd 	}
    199      1.1      cgd }
    200      1.1      cgd 
    201      1.1      cgd static int col;
    202      1.1      cgd static char *label;
    203      1.1      cgd 
    204      1.1      cgd static void
    205      1.1      cgd binit(lb)
    206      1.1      cgd 	char *lb;
    207      1.1      cgd {
    208  1.1.1.2  mycroft 
    209      1.1      cgd 	if (col) {
    210      1.1      cgd 		(void)printf("\n");
    211      1.1      cgd 		col = 0;
    212      1.1      cgd 	}
    213      1.1      cgd 	label = lb;
    214      1.1      cgd }
    215      1.1      cgd 
    216      1.1      cgd static void
    217      1.1      cgd bput(s)
    218      1.1      cgd 	char *s;
    219      1.1      cgd {
    220  1.1.1.2  mycroft 
    221      1.1      cgd 	if (col == 0) {
    222      1.1      cgd 		col = printf("%s: %s", label, s);
    223      1.1      cgd 		return;
    224      1.1      cgd 	}
    225      1.1      cgd 	if ((col + strlen(s)) > LINELENGTH) {
    226      1.1      cgd 		(void)printf("\n\t");
    227      1.1      cgd 		col = printf("%s", s) + 8;
    228      1.1      cgd 		return;
    229      1.1      cgd 	}
    230      1.1      cgd 	col += printf(" %s", s);
    231      1.1      cgd }
    232      1.1      cgd 
    233      1.1      cgd static char *
    234  1.1.1.2  mycroft ccval(p, c)
    235  1.1.1.2  mycroft 	struct cchar *p;
    236      1.1      cgd 	int c;
    237      1.1      cgd {
    238      1.1      cgd 	static char buf[5];
    239      1.1      cgd 	char *bp;
    240      1.1      cgd 
    241      1.1      cgd 	if (c == _POSIX_VDISABLE)
    242  1.1.1.2  mycroft 		return ("<undef>");
    243      1.1      cgd 
    244  1.1.1.2  mycroft 	if (p->sub == VMIN || p->sub == VTIME) {
    245  1.1.1.2  mycroft 		(void)snprintf(buf, sizeof(buf), "%d", c);
    246  1.1.1.2  mycroft 		return (buf);
    247  1.1.1.2  mycroft 	}
    248      1.1      cgd 	bp = buf;
    249      1.1      cgd 	if (c & 0200) {
    250      1.1      cgd 		*bp++ = 'M';
    251      1.1      cgd 		*bp++ = '-';
    252      1.1      cgd 		c &= 0177;
    253      1.1      cgd 	}
    254      1.1      cgd 	if (c == 0177) {
    255      1.1      cgd 		*bp++ = '^';
    256      1.1      cgd 		*bp++ = '?';
    257      1.1      cgd 	}
    258      1.1      cgd 	else if (c < 040) {
    259      1.1      cgd 		*bp++ = '^';
    260      1.1      cgd 		*bp++ = c + '@';
    261      1.1      cgd 	}
    262      1.1      cgd 	else
    263      1.1      cgd 		*bp++ = c;
    264      1.1      cgd 	*bp = '\0';
    265  1.1.1.2  mycroft 	return (buf);
    266      1.1      cgd }
    267