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
34 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1991, 1993\n\
41 The Regents of the University of California. All rights reserved.\n";
45 static const char sccsid[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
62 void obsolete(char *[]);
63 void report(const char *, int, u_int);
66 struct termios mode, oldmode;
68 int erasech; /* new erase character */
69 int intrchar; /* new interrupt character */
70 int isreset; /* invoked as reset */
71 int killch; /* new kill character */
72 int Lines, Columns; /* window size */
76 main(int argc, char *argv[])
81 int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
85 if (tcgetattr(STDERR_FILENO, &mode) < 0)
86 err(1, "standard error");
89 Ospeed = cfgetospeed(&mode);
91 if ((p = strrchr(*argv, '/')))
95 usingupper = isupper(*p);
96 if (!strcasecmp(p, "reset")) {
102 noinit = noset = quiet = Sflag = sflag = showterm = 0;
103 while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
105 case '-': /* display term only */
108 case 'a': /* OBSOLETE: map identifier to type */
109 add_mapping("arpanet", optarg);
111 case 'd': /* OBSOLETE: map identifier to type */
112 add_mapping("dialup", optarg);
114 case 'e': /* erase character */
115 erasech = optarg[0] == '^' && optarg[1] != '\0' ?
116 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
119 case 'I': /* no initialization strings */
122 case 'i': /* interrupt character */
123 intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
124 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
127 case 'k': /* kill character */
128 killch = optarg[0] == '^' && optarg[1] != '\0' ?
129 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
132 case 'm': /* map identifier to type */
133 add_mapping(NULL, optarg);
135 case 'n': /* OBSOLETE: set new tty driver */
137 case 'p': /* OBSOLETE: map identifier to type */
138 add_mapping("plugboard", optarg);
140 case 'Q': /* don't output control key settings */
143 case 'S': /* output TERM/TERMCAP strings */
146 case 'r': /* display term on stderr */
149 case 's': /* output TERM/TERMCAP strings */
163 ttype = get_termcap_entry(*argv, &tcapbuf);
166 Columns = tgetnum("co");
167 Lines = tgetnum("li");
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) {
175 win.ws_col = Columns;
176 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
180 set_conversions(usingupper);
185 /* Set the modes if they've changed. */
186 if (memcmp(&mode, &oldmode, sizeof(mode)))
187 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
191 (void)printf("%s\n", ttype);
194 (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
196 * If erase, kill and interrupt characters could have been
197 * modified and not -Q, display the changes.
200 report("Erase", VERASE, CERASE);
201 report("Kill", VKILL, CKILL);
202 report("Interrupt", VINTR, CINTR);
207 (void)printf("%s ", ttype);
208 if (strlen(tcapbuf) > 0)
214 * Figure out what shell we're using. A hack, we look for an
215 * environmental variable SHELL ending in "csh".
217 if ((p = getenv("SHELL")) &&
218 !strcmp(p + strlen(p) - 3, "csh")) {
219 printf("set noglob;\nsetenv TERM %s;\n", ttype);
220 if (strlen(tcapbuf) > 0) {
221 printf("setenv TERMCAP '");
225 printf("unset noglob;\n");
227 printf("TERM=%s;\n", ttype);
228 if (strlen(tcapbuf) > 0) {
231 printf("';\nexport TERMCAP;\n");
233 printf("export TERM;\n");
241 * Tell the user if a control key has been changed from the default value.
244 report(const char *name, int which, u_int def)
248 new = mode.c_cc[which];
249 old = oldmode.c_cc[which];
251 if (old == new && old == def)
254 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
257 (void)fprintf(stderr, "backspace.\n");
258 else if (new == 0177)
259 (void)fprintf(stderr, "delete.\n");
260 else if (new < 040) {
262 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
264 (void)fprintf(stderr, "%c.\n", new);
268 * Convert the obsolete argument form into something that getopt can handle.
269 * This means that -e, -i and -k get default arguments supplied for them.
272 obsolete(char *argv[])
274 for (; *argv; ++argv) {
275 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
276 (argv[0][1] != 'e' && argv[0][1] != 'i' && argv[0][1] != 'k') ||
281 argv[0] = strdup("-e^H");
284 argv[0] = strdup("-i^C");
287 argv[0] = strdup("-k^U");
296 (void)fprintf(stderr, "%s\n%s\n",
297 "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]",
298 " reset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]");