Home | History | Annotate | Line # | Download | only in tset
tset.c revision 1.7
      1  1.7  mycroft /*	$NetBSD: tset.c,v 1.7 1998/07/26 23:09:50 mycroft Exp $	*/
      2  1.4      jtc 
      3  1.1      cgd /*-
      4  1.4      jtc  * Copyright (c) 1980, 1991, 1993
      5  1.4      jtc  *	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.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  */
     35  1.1      cgd 
     36  1.5    lukem #include <sys/cdefs.h>
     37  1.1      cgd #ifndef lint
     38  1.5    lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\n\
     39  1.5    lukem 	The Regents of the University of California.  All rights reserved.\n");
     40  1.1      cgd #endif /* not lint */
     41  1.1      cgd 
     42  1.1      cgd #ifndef lint
     43  1.4      jtc #if 0
     44  1.4      jtc static char sccsid[] = "@(#)tset.c	8.1 (Berkeley) 6/9/93";
     45  1.4      jtc #endif
     46  1.7  mycroft __RCSID("$NetBSD: tset.c,v 1.7 1998/07/26 23:09:50 mycroft Exp $");
     47  1.1      cgd #endif /* not lint */
     48  1.1      cgd 
     49  1.1      cgd #include <sys/types.h>
     50  1.2      cgd #include <sys/ioctl.h>
     51  1.5    lukem #include <ctype.h>
     52  1.6    lukem #include <err.h>
     53  1.1      cgd #include <errno.h>
     54  1.5    lukem #include <stdio.h>
     55  1.1      cgd #include <stdlib.h>
     56  1.1      cgd #include <string.h>
     57  1.5    lukem #include <termcap.h>
     58  1.5    lukem #include <termios.h>
     59  1.5    lukem #include <unistd.h>
     60  1.1      cgd #include "extern.h"
     61  1.1      cgd 
     62  1.5    lukem int	main __P((int, char *[]));
     63  1.1      cgd void	obsolete __P((char *[]));
     64  1.1      cgd void	report __P((char *, int, u_int));
     65  1.1      cgd void	usage __P((void));
     66  1.1      cgd 
     67  1.1      cgd struct termios mode, oldmode;
     68  1.1      cgd 
     69  1.1      cgd int	erasechar;		/* new erase character */
     70  1.1      cgd int	intrchar;		/* new interrupt character */
     71  1.1      cgd int	isreset;		/* invoked as reset */
     72  1.1      cgd int	killchar;		/* new kill character */
     73  1.1      cgd int	lines, columns;		/* window size */
     74  1.1      cgd 
     75  1.1      cgd int
     76  1.1      cgd main(argc, argv)
     77  1.1      cgd 	int argc;
     78  1.1      cgd 	char *argv[];
     79  1.1      cgd {
     80  1.1      cgd #ifdef TIOCGWINSZ
     81  1.1      cgd 	struct winsize win;
     82  1.1      cgd #endif
     83  1.2      cgd 	int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
     84  1.7  mycroft 	char savech, *p, *t, *tcapbuf;
     85  1.7  mycroft 	const char *ttype;
     86  1.1      cgd 
     87  1.1      cgd 	if (tcgetattr(STDERR_FILENO, &mode) < 0)
     88  1.6    lukem 		err(1, "standard error");
     89  1.1      cgd 
     90  1.1      cgd 	oldmode = mode;
     91  1.1      cgd 	ospeed = cfgetospeed(&mode);
     92  1.1      cgd 
     93  1.5    lukem 	if ((p = strrchr(*argv, '/')) != NULL)
     94  1.1      cgd 		++p;
     95  1.1      cgd 	else
     96  1.1      cgd 		p = *argv;
     97  1.1      cgd 	usingupper = isupper(*p);
     98  1.1      cgd 	if (!strcasecmp(p, "reset")) {
     99  1.1      cgd 		isreset = 1;
    100  1.1      cgd 		reset_mode();
    101  1.1      cgd 	}
    102  1.1      cgd 
    103  1.1      cgd 	obsolete(argv);
    104  1.2      cgd 	noinit = noset = quiet = Sflag = sflag = showterm = 0;
    105  1.6    lukem 	while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
    106  1.1      cgd 		switch (ch) {
    107  1.2      cgd 		case '-':		/* display term only */
    108  1.1      cgd 			noset = 1;
    109  1.1      cgd 			break;
    110  1.1      cgd 		case 'a':		/* OBSOLETE: map identifier to type */
    111  1.1      cgd 			add_mapping("arpanet", optarg);
    112  1.1      cgd 			break;
    113  1.1      cgd 		case 'd':		/* OBSOLETE: map identifier to type */
    114  1.1      cgd 			add_mapping("dialup", optarg);
    115  1.1      cgd 			break;
    116  1.1      cgd 		case 'e':		/* erase character */
    117  1.1      cgd 			erasechar = optarg[0] == '^' && optarg[1] != '\0' ?
    118  1.1      cgd 			    optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
    119  1.1      cgd 			    optarg[0];
    120  1.1      cgd 			break;
    121  1.2      cgd 		case 'I':		/* no initialization strings */
    122  1.1      cgd 			noinit = 1;
    123  1.1      cgd 			break;
    124  1.1      cgd 		case 'i':		/* interrupt character */
    125  1.1      cgd 			intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
    126  1.1      cgd 			    optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
    127  1.1      cgd 			    optarg[0];
    128  1.1      cgd 			break;
    129  1.1      cgd 		case 'k':		/* kill character */
    130  1.1      cgd 			killchar = optarg[0] == '^' && optarg[1] != '\0' ?
    131  1.1      cgd 			    optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
    132  1.1      cgd 			    optarg[0];
    133  1.1      cgd 			break;
    134  1.1      cgd 		case 'm':		/* map identifier to type */
    135  1.1      cgd 			add_mapping(NULL, optarg);
    136  1.1      cgd 			break;
    137  1.1      cgd 		case 'n':		/* OBSOLETE: set new tty driver */
    138  1.1      cgd 			break;
    139  1.1      cgd 		case 'p':		/* OBSOLETE: map identifier to type */
    140  1.1      cgd 			add_mapping("plugboard", optarg);
    141  1.1      cgd 			break;
    142  1.2      cgd 		case 'Q':		/* don't output control key settings */
    143  1.1      cgd 			quiet = 1;
    144  1.1      cgd 			break;
    145  1.2      cgd 		case 'S':		/* output TERM/TERMCAP strings */
    146  1.2      cgd 			Sflag = 1;
    147  1.2      cgd 			break;
    148  1.1      cgd 		case 'r':		/* display term on stderr */
    149  1.1      cgd 			showterm = 1;
    150  1.1      cgd 			break;
    151  1.2      cgd 		case 's':		/* output TERM/TERMCAP strings */
    152  1.2      cgd 			sflag = 1;
    153  1.1      cgd 			break;
    154  1.1      cgd 		case '?':
    155  1.1      cgd 		default:
    156  1.1      cgd 			usage();
    157  1.1      cgd 		}
    158  1.1      cgd 	}
    159  1.1      cgd 	argc -= optind;
    160  1.1      cgd 	argv += optind;
    161  1.1      cgd 
    162  1.1      cgd 	if (argc > 1)
    163  1.1      cgd 		usage();
    164  1.1      cgd 
    165  1.1      cgd 	ttype = get_termcap_entry(*argv, &tcapbuf);
    166  1.1      cgd 
    167  1.1      cgd 	if (!noset) {
    168  1.1      cgd 		columns = tgetnum("co");
    169  1.1      cgd 		lines = tgetnum("li");
    170  1.1      cgd 
    171  1.1      cgd #ifdef TIOCGWINSZ
    172  1.1      cgd 		/* Set window size */
    173  1.1      cgd 		(void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
    174  1.1      cgd 		if (win.ws_row == 0 && win.ws_col == 0 &&
    175  1.1      cgd 		    lines > 0 && columns > 0) {
    176  1.1      cgd 			win.ws_row = lines;
    177  1.1      cgd 			win.ws_col = columns;
    178  1.1      cgd 			(void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
    179  1.1      cgd 		}
    180  1.1      cgd #endif
    181  1.1      cgd 		set_control_chars();
    182  1.1      cgd 		set_conversions(usingupper);
    183  1.1      cgd 
    184  1.1      cgd 		if (!noinit)
    185  1.1      cgd 			set_init();
    186  1.1      cgd 
    187  1.1      cgd 		/* Set the modes if they've changed. */
    188  1.1      cgd 		if (memcmp(&mode, &oldmode, sizeof(mode)))
    189  1.1      cgd 			tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
    190  1.1      cgd 	}
    191  1.1      cgd 
    192  1.2      cgd 	/* Get the terminal name from the entry. */
    193  1.2      cgd 	p = tcapbuf;
    194  1.2      cgd 	if (p != NULL && *p != ':') {
    195  1.2      cgd 		t = p;
    196  1.5    lukem 		if ((p = strpbrk(p, "|:")) != NULL) {
    197  1.1      cgd 			savech = *p;
    198  1.1      cgd 			*p = '\0';
    199  1.1      cgd 			if ((ttype = strdup(t)) == NULL)
    200  1.6    lukem 				err(1, "strdup");
    201  1.1      cgd 			*p = savech;
    202  1.1      cgd 		}
    203  1.1      cgd 	}
    204  1.1      cgd 
    205  1.1      cgd 	if (noset)
    206  1.1      cgd 		(void)printf("%s\n", ttype);
    207  1.1      cgd 	else {
    208  1.1      cgd 		if (showterm)
    209  1.1      cgd 			(void)fprintf(stderr, "Terminal type is %s.\n", ttype);
    210  1.1      cgd 		/*
    211  1.1      cgd 		 * If erase, kill and interrupt characters could have been
    212  1.1      cgd 		 * modified and not -Q, display the changes.
    213  1.1      cgd 		 */
    214  1.1      cgd 		if (!quiet) {
    215  1.1      cgd 			report("Erase", VERASE, CERASE);
    216  1.1      cgd 			report("Kill", VKILL, CKILL);
    217  1.1      cgd 			report("Interrupt", VINTR, CINTR);
    218  1.1      cgd 		}
    219  1.1      cgd 	}
    220  1.1      cgd 
    221  1.2      cgd 	if (Sflag) {
    222  1.2      cgd 		(void)printf("%s ", ttype);
    223  1.2      cgd 		wrtermcap(tcapbuf);
    224  1.2      cgd 	}
    225  1.2      cgd 
    226  1.2      cgd 	if (sflag) {
    227  1.2      cgd 		/*
    228  1.2      cgd 		 * Figure out what shell we're using.  A hack, we look for an
    229  1.2      cgd 		 * environmental variable SHELL ending in "csh".
    230  1.2      cgd 		 */
    231  1.2      cgd 		if ((p = getenv("SHELL")) &&
    232  1.2      cgd 		    !strcmp(p + strlen(p) - 3, "csh")) {
    233  1.2      cgd 			p = "set noglob;\nsetenv TERM %s;\nsetenv TERMCAP '";
    234  1.2      cgd 			t = "';\nunset noglob;\n";
    235  1.2      cgd 		} else {
    236  1.2      cgd 			p = "TERM=%s;\nTERMCAP='";
    237  1.2      cgd 			t = "';\nexport TERMCAP TERM;\n";
    238  1.2      cgd 		}
    239  1.2      cgd 		(void)printf(p, ttype);
    240  1.2      cgd 		wrtermcap(tcapbuf);
    241  1.2      cgd 		(void)printf(t);
    242  1.2      cgd 	}
    243  1.1      cgd 
    244  1.1      cgd 	exit(0);
    245  1.1      cgd }
    246  1.1      cgd 
    247  1.1      cgd /*
    248  1.1      cgd  * Tell the user if a control key has been changed from the default value.
    249  1.1      cgd  */
    250  1.1      cgd void
    251  1.1      cgd report(name, which, def)
    252  1.1      cgd 	char *name;
    253  1.1      cgd 	int which;
    254  1.1      cgd 	u_int def;
    255  1.1      cgd {
    256  1.1      cgd 	u_int old, new;
    257  1.1      cgd 	char *bp, buf[1024];
    258  1.1      cgd 
    259  1.1      cgd 	new = mode.c_cc[which];
    260  1.1      cgd 	old = oldmode.c_cc[which];
    261  1.1      cgd 
    262  1.1      cgd 	if (old == new && old == def)
    263  1.1      cgd 		return;
    264  1.1      cgd 
    265  1.1      cgd 	(void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
    266  1.1      cgd 
    267  1.1      cgd 	bp = buf;
    268  1.1      cgd 	if (tgetstr("kb", &bp) && new == buf[0] && buf[1] == '\0')
    269  1.1      cgd 		(void)fprintf(stderr, "backspace.\n");
    270  1.1      cgd 	else if (new == 0177)
    271  1.1      cgd 		(void)fprintf(stderr, "delete.\n");
    272  1.1      cgd 	else if (new < 040) {
    273  1.1      cgd 		new ^= 0100;
    274  1.1      cgd 		(void)fprintf(stderr, "control-%c (^%c).\n", new, new);
    275  1.1      cgd 	} else
    276  1.1      cgd 		(void)fprintf(stderr, "%c.\n", new);
    277  1.1      cgd }
    278  1.1      cgd 
    279  1.1      cgd /*
    280  1.1      cgd  * Convert the obsolete argument form into something that getopt can handle.
    281  1.1      cgd  * This means that -e, -i and -k get default arguments supplied for them.
    282  1.1      cgd  */
    283  1.1      cgd void
    284  1.1      cgd obsolete(argv)
    285  1.1      cgd 	char *argv[];
    286  1.1      cgd {
    287  1.1      cgd 	for (; *argv; ++argv) {
    288  1.5    lukem 		if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
    289  1.5    lukem 		    (argv[0][1] != 'e' && argv[0][1] != 'i' &&
    290  1.5    lukem 		     argv[0][1] != 'k') || argv[0][2] != '\0')
    291  1.1      cgd 			continue;
    292  1.1      cgd 		switch(argv[0][1]) {
    293  1.1      cgd 		case 'e':
    294  1.1      cgd 			argv[0] = "-e^H";
    295  1.1      cgd 			break;
    296  1.1      cgd 		case 'i':
    297  1.1      cgd 			argv[0] = "-i^C";
    298  1.1      cgd 			break;
    299  1.1      cgd 		case 'k':
    300  1.1      cgd 			argv[0] = "-k^U";
    301  1.1      cgd 			break;
    302  1.1      cgd 		}
    303  1.1      cgd 	}
    304  1.1      cgd }
    305  1.1      cgd 
    306  1.1      cgd void
    307  1.1      cgd usage()
    308  1.1      cgd {
    309  1.1      cgd 	(void)fprintf(stderr,
    310  1.2      cgd "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n");
    311  1.1      cgd 	exit(1);
    312  1.1      cgd }
    313