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