2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 static char copyright[] =
36 "@(#) Copyright (c) 1980, 1991, 1993\n\
37 The Regents of the University of California. All rights reserved.\n";
41 static char sccsid[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
44 #include <sys/types.h>
45 #include <sys/ioctl.h>
55 void obsolete __P((char *[]));
56 void report __P((char *, int, u_int));
57 void usage __P((void));
59 struct termios mode, oldmode;
61 int erasechar; /* new erase character */
62 int intrchar; /* new interrupt character */
63 int isreset; /* invoked as reset */
64 int killchar; /* new kill character */
65 int lines, columns; /* window size */
75 int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
76 char savech, *p, *t, *tcapbuf, *ttype;
78 if (tcgetattr(STDERR_FILENO, &mode) < 0)
79 err("standard error: %s", strerror(errno));
82 ospeed = cfgetospeed(&mode);
84 if (p = strrchr(*argv, '/'))
88 usingupper = isupper(*p);
89 if (!strcasecmp(p, "reset")) {
95 noinit = noset = quiet = Sflag = sflag = showterm = 0;
96 while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != EOF) {
98 case '-': /* display term only */
101 case 'a': /* OBSOLETE: map identifier to type */
102 add_mapping("arpanet", optarg);
104 case 'd': /* OBSOLETE: map identifier to type */
105 add_mapping("dialup", optarg);
107 case 'e': /* erase character */
108 erasechar = optarg[0] == '^' && optarg[1] != '\0' ?
109 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
112 case 'I': /* no initialization strings */
115 case 'i': /* interrupt character */
116 intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
117 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
120 case 'k': /* kill character */
121 killchar = optarg[0] == '^' && optarg[1] != '\0' ?
122 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
125 case 'm': /* map identifier to type */
126 add_mapping(NULL, optarg);
128 case 'n': /* OBSOLETE: set new tty driver */
130 case 'p': /* OBSOLETE: map identifier to type */
131 add_mapping("plugboard", optarg);
133 case 'Q': /* don't output control key settings */
136 case 'S': /* output TERM/TERMCAP strings */
139 case 'r': /* display term on stderr */
142 case 's': /* output TERM/TERMCAP strings */
156 ttype = get_termcap_entry(*argv, &tcapbuf);
159 columns = tgetnum("co");
160 lines = tgetnum("li");
163 /* Set window size */
164 (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
165 if (win.ws_row == 0 && win.ws_col == 0 &&
166 lines > 0 && columns > 0) {
168 win.ws_col = columns;
169 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
173 set_conversions(usingupper);
178 /* Set the modes if they've changed. */
179 if (memcmp(&mode, &oldmode, sizeof(mode)))
180 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
183 /* Get the terminal name from the entry. */
185 if (p != NULL && *p != ':') {
187 if (p = strpbrk(p, "|:")) {
190 if ((ttype = strdup(t)) == NULL)
191 err("%s", strerror(errno));
197 (void)printf("%s\n", ttype);
200 (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
202 * If erase, kill and interrupt characters could have been
203 * modified and not -Q, display the changes.
206 report("Erase", VERASE, CERASE);
207 report("Kill", VKILL, CKILL);
208 report("Interrupt", VINTR, CINTR);
213 (void)printf("%s ", ttype);
219 * Figure out what shell we're using. A hack, we look for an
220 * environmental variable SHELL ending in "csh".
222 if ((p = getenv("SHELL")) &&
223 !strcmp(p + strlen(p) - 3, "csh")) {
224 p = "set noglob;\nsetenv TERM %s;\nsetenv TERMCAP '";
225 t = "';\nunset noglob;\n";
227 p = "TERM=%s;\nTERMCAP='";
228 t = "';\nexport TERMCAP TERM;\n";
230 (void)printf(p, ttype);
239 * Tell the user if a control key has been changed from the default value.
242 report(name, which, def)
250 new = mode.c_cc[which];
251 old = oldmode.c_cc[which];
253 if (old == new && old == def)
256 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
259 if (tgetstr("kb", &bp) && new == buf[0] && buf[1] == '\0')
260 (void)fprintf(stderr, "backspace.\n");
261 else if (new == 0177)
262 (void)fprintf(stderr, "delete.\n");
263 else if (new < 040) {
265 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
267 (void)fprintf(stderr, "%c.\n", new);
271 * Convert the obsolete argument form into something that getopt can handle.
272 * This means that -e, -i and -k get default arguments supplied for them.
278 for (; *argv; ++argv) {
279 if (argv[0][0] != '-' || argv[1] && argv[1][0] != '-' ||
280 argv[0][1] != 'e' && argv[0][1] != 'i' &&
281 argv[0][1] != 'k' || argv[0][2] != '\0')
300 (void)fprintf(stderr,
301 "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n");