Home | History | Annotate | Line # | Download | only in tset
tset.c revision 1.10
      1 /*	$NetBSD: tset.c,v 1.10 2000/05/31 05:50:06 blymn 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.10 2000/05/31 05:50:06 blymn Exp $");
     47 #endif /* not lint */
     48 
     49 #include <sys/types.h>
     50 #include <sys/ioctl.h>
     51 #include <ctype.h>
     52 #include <err.h>
     53 #include <errno.h>
     54 #include <stdio.h>
     55 #include <stdlib.h>
     56 #include <string.h>
     57 #include <termcap.h>
     58 #include <termios.h>
     59 #include <unistd.h>
     60 #include "extern.h"
     61 
     62 int	main __P((int, char *[]));
     63 void	obsolete __P((char *[]));
     64 void	report __P((char *, int, u_int));
     65 void	usage __P((void));
     66 
     67 struct termios mode, oldmode;
     68 
     69 int	erasechar;		/* new erase character */
     70 int	intrchar;		/* new interrupt character */
     71 int	isreset;		/* invoked as reset */
     72 int	killchar;		/* new kill character */
     73 int	lines, columns;		/* window size */
     74 
     75 int
     76 main(argc, argv)
     77 	int argc;
     78 	char *argv[];
     79 {
     80 #ifdef TIOCGWINSZ
     81 	struct winsize win;
     82 #endif
     83 	int ch, extended, noinit, noset, quiet, Sflag, sflag, showterm;
     84 	int usingupper;
     85 	char savech, *p, *t, *tcapbuf;
     86 	const char *ttype;
     87 
     88 	if (tcgetattr(STDERR_FILENO, &mode) < 0)
     89 		err(1, "standard error");
     90 
     91 	oldmode = mode;
     92 	ospeed = cfgetospeed(&mode);
     93 
     94 	if ((p = strrchr(*argv, '/')) != NULL)
     95 		++p;
     96 	else
     97 		p = *argv;
     98 	usingupper = isupper((unsigned char)*p);
     99 	if (!strcasecmp(p, "reset")) {
    100 		isreset = 1;
    101 		reset_mode();
    102 	}
    103 
    104 	obsolete(argv);
    105 	noinit = noset = quiet = Sflag = sflag = showterm = extended = 0;
    106 	while ((ch = getopt(argc, argv, "-a:d:e:EIi:k:m:np:QSrs")) != -1) {
    107 		switch (ch) {
    108 		case '-':		/* display term only */
    109 			noset = 1;
    110 			break;
    111 		case 'a':		/* OBSOLETE: map identifier to type */
    112 			add_mapping("arpanet", optarg);
    113 			break;
    114 		case 'd':		/* OBSOLETE: map identifier to type */
    115 			add_mapping("dialup", optarg);
    116 			break;
    117 		case 'e':		/* erase character */
    118 			erasechar = optarg[0] == '^' && optarg[1] != '\0' ?
    119 			    optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
    120 			    optarg[0];
    121 			break;
    122 		case 'E':
    123 			extended = 1;
    124 			break;
    125 		case 'I':		/* no initialization strings */
    126 			noinit = 1;
    127 			break;
    128 		case 'i':		/* interrupt character */
    129 			intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
    130 			    optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
    131 			    optarg[0];
    132 			break;
    133 		case 'k':		/* kill character */
    134 			killchar = optarg[0] == '^' && optarg[1] != '\0' ?
    135 			    optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
    136 			    optarg[0];
    137 			break;
    138 		case 'm':		/* map identifier to type */
    139 			add_mapping(NULL, optarg);
    140 			break;
    141 		case 'n':		/* OBSOLETE: set new tty driver */
    142 			break;
    143 		case 'p':		/* OBSOLETE: map identifier to type */
    144 			add_mapping("plugboard", optarg);
    145 			break;
    146 		case 'Q':		/* don't output control key settings */
    147 			quiet = 1;
    148 			break;
    149 		case 'S':		/* output TERM/TERMCAP strings */
    150 			Sflag = 1;
    151 			break;
    152 		case 'r':		/* display term on stderr */
    153 			showterm = 1;
    154 			break;
    155 		case 's':		/* output TERM/TERMCAP strings */
    156 			sflag = 1;
    157 			break;
    158 		case '?':
    159 		default:
    160 			usage();
    161 		}
    162 	}
    163 	argc -= optind;
    164 	argv += optind;
    165 
    166 	if (argc > 1)
    167 		usage();
    168 
    169 	ttype = get_termcap_entry(*argv, &tcapbuf, extended);
    170 
    171 	if (!noset) {
    172 		columns = tgetnum("co");
    173 		lines = tgetnum("li");
    174 
    175 #ifdef TIOCGWINSZ
    176 		/* Set window size */
    177 		(void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
    178 		if (win.ws_row == 0 && win.ws_col == 0 &&
    179 		    lines > 0 && columns > 0) {
    180 			win.ws_row = lines;
    181 			win.ws_col = columns;
    182 			(void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
    183 		}
    184 #endif
    185 		set_control_chars();
    186 		set_conversions(usingupper);
    187 
    188 		if (!noinit)
    189 			set_init();
    190 
    191 		/* Set the modes if they've changed. */
    192 		if (memcmp(&mode, &oldmode, sizeof(mode)))
    193 			tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
    194 	}
    195 
    196 	/* Get the terminal name from the entry. */
    197 	p = tcapbuf;
    198 	if (p != NULL && *p != ':') {
    199 		t = p;
    200 		if ((p = strpbrk(p, "|:")) != NULL) {
    201 			savech = *p;
    202 			*p = '\0';
    203 			if ((ttype = strdup(t)) == NULL)
    204 				err(1, "strdup");
    205 			*p = savech;
    206 		}
    207 	}
    208 
    209 	if (noset)
    210 		(void)printf("%s\n", ttype);
    211 	else {
    212 		if (showterm)
    213 			(void)fprintf(stderr, "Terminal type is %s.\n", ttype);
    214 		/*
    215 		 * If erase, kill and interrupt characters could have been
    216 		 * modified and not -Q, display the changes.
    217 		 */
    218 		if (!quiet) {
    219 			report("Erase", VERASE, CERASE);
    220 			report("Kill", VKILL, CKILL);
    221 			report("Interrupt", VINTR, CINTR);
    222 		}
    223 	}
    224 
    225 	if (Sflag) {
    226 		(void)printf("%s ", ttype);
    227 		wrtermcap(tcapbuf);
    228 	}
    229 
    230 	if (sflag) {
    231 		/*
    232 		 * Figure out what shell we're using.  A hack, we look for an
    233 		 * environmental variable SHELL ending in "csh".
    234 		 */
    235 		if ((p = getenv("SHELL")) &&
    236 		    !strcmp(p + strlen(p) - 3, "csh")) {
    237 			p = "set noglob;\nsetenv TERM %s;\nsetenv TERMCAP '";
    238 			t = "';\nunset noglob;\n";
    239 		} else {
    240 			p = "TERM=%s;\nTERMCAP='";
    241 			t = "';\nexport TERMCAP TERM;\n";
    242 		}
    243 		(void)printf(p, ttype);
    244 		wrtermcap(tcapbuf);
    245 		(void)printf(t);
    246 	}
    247 
    248 	exit(0);
    249 }
    250 
    251 /*
    252  * Tell the user if a control key has been changed from the default value.
    253  */
    254 void
    255 report(name, which, def)
    256 	char *name;
    257 	int which;
    258 	u_int def;
    259 {
    260 	u_int old, new;
    261 	char *bp, buf[1024];
    262 
    263 	new = mode.c_cc[which];
    264 	old = oldmode.c_cc[which];
    265 
    266 	if (old == new && old == def)
    267 		return;
    268 
    269 	(void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
    270 
    271 	bp = buf;
    272 	if (tgetstr("kb", &bp) && new == buf[0] && buf[1] == '\0')
    273 		(void)fprintf(stderr, "backspace.\n");
    274 	else if (new == 0177)
    275 		(void)fprintf(stderr, "delete.\n");
    276 	else if (new < 040) {
    277 		new ^= 0100;
    278 		(void)fprintf(stderr, "control-%c (^%c).\n", new, new);
    279 	} else if (new == _POSIX_VDISABLE)
    280 		(void)fprintf(stderr, "<undef>.\n");
    281 	else
    282 		(void)fprintf(stderr, "%c.\n", new);
    283 }
    284 
    285 /*
    286  * Convert the obsolete argument form into something that getopt can handle.
    287  * This means that -e, -i and -k get default arguments supplied for them.
    288  */
    289 void
    290 obsolete(argv)
    291 	char *argv[];
    292 {
    293 	for (; *argv; ++argv) {
    294 		if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
    295 		    (argv[0][1] != 'e' && argv[0][1] != 'i' &&
    296 		     argv[0][1] != 'k') || argv[0][2] != '\0')
    297 			continue;
    298 		switch(argv[0][1]) {
    299 		case 'e':
    300 			argv[0] = "-e^H";
    301 			break;
    302 		case 'i':
    303 			argv[0] = "-i^C";
    304 			break;
    305 		case 'k':
    306 			argv[0] = "-k^U";
    307 			break;
    308 		}
    309 	}
    310 }
    311 
    312 void
    313 usage()
    314 {
    315 	(void)fprintf(stderr,
    316 "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n");
    317 	exit(1);
    318 }
    319