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