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