2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char copyright[] =
38 "@(#) Copyright (c) 1980, 1991, 1993\n\
39 The Regents of the University of California. All rights reserved.\n";
43 static const char sccsid[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
46 #include <sys/types.h>
47 #include <sys/ioctl.h>
60 void obsolete(char *[]);
61 void report(const char *, int, u_int);
64 struct termios mode, oldmode;
66 int erasech; /* new erase character */
67 int intrchar; /* new interrupt character */
68 int isreset; /* invoked as reset */
69 int killch; /* new kill character */
70 int Lines, Columns; /* window size */
74 main(int argc, char *argv[])
79 int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
83 if (tcgetattr(STDERR_FILENO, &mode) < 0)
84 err(1, "standard error");
87 Ospeed = cfgetospeed(&mode);
89 if ((p = strrchr(*argv, '/')))
93 usingupper = isupper(*p);
94 if (!strcasecmp(p, "reset")) {
100 noinit = noset = quiet = Sflag = sflag = showterm = 0;
101 while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
103 case '-': /* display term only */
106 case 'a': /* OBSOLETE: map identifier to type */
107 add_mapping("arpanet", optarg);
109 case 'd': /* OBSOLETE: map identifier to type */
110 add_mapping("dialup", optarg);
112 case 'e': /* erase character */
113 erasech = optarg[0] == '^' && optarg[1] != '\0' ?
114 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
117 case 'I': /* no initialization strings */
120 case 'i': /* interrupt character */
121 intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
122 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
125 case 'k': /* kill character */
126 killch = optarg[0] == '^' && optarg[1] != '\0' ?
127 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
130 case 'm': /* map identifier to type */
131 add_mapping(NULL, optarg);
133 case 'n': /* OBSOLETE: set new tty driver */
135 case 'p': /* OBSOLETE: map identifier to type */
136 add_mapping("plugboard", optarg);
138 case 'Q': /* don't output control key settings */
141 case 'S': /* output TERM/TERMCAP strings */
144 case 'r': /* display term on stderr */
147 case 's': /* output TERM/TERMCAP strings */
161 ttype = get_termcap_entry(*argv, &tcapbuf);
164 Columns = tgetnum("co");
165 Lines = tgetnum("li");
168 /* Set window size */
169 (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
170 if (win.ws_row == 0 && win.ws_col == 0 &&
171 Lines > 0 && Columns > 0) {
173 win.ws_col = Columns;
174 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
178 set_conversions(usingupper);
183 /* Set the modes if they've changed. */
184 if (memcmp(&mode, &oldmode, sizeof(mode)))
185 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
189 (void)printf("%s\n", ttype);
192 (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
194 * If erase, kill and interrupt characters could have been
195 * modified and not -Q, display the changes.
198 report("Erase", VERASE, CERASE);
199 report("Kill", VKILL, CKILL);
200 report("Interrupt", VINTR, CINTR);
205 (void)printf("%s ", ttype);
206 if (strlen(tcapbuf) > 0)
212 * Figure out what shell we're using. A hack, we look for an
213 * environmental variable SHELL ending in "csh".
215 if ((p = getenv("SHELL")) &&
216 !strcmp(p + strlen(p) - 3, "csh")) {
217 printf("set noglob;\nsetenv TERM %s;\n", ttype);
218 if (strlen(tcapbuf) > 0) {
219 printf("setenv TERMCAP '");
223 printf("unset noglob;\n");
225 printf("TERM=%s;\n", ttype);
226 if (strlen(tcapbuf) > 0) {
229 printf("';\nexport TERMCAP;\n");
231 printf("export TERM;\n");
239 * Tell the user if a control key has been changed from the default value.
242 report(const char *name, int which, u_int def)
246 new = mode.c_cc[which];
247 old = oldmode.c_cc[which];
249 if (old == new && old == def)
252 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
255 (void)fprintf(stderr, "backspace.\n");
256 else if (new == 0177)
257 (void)fprintf(stderr, "delete.\n");
258 else if (new < 040) {
260 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
262 (void)fprintf(stderr, "%c.\n", new);
266 * Convert the obsolete argument form into something that getopt can handle.
267 * This means that -e, -i and -k get default arguments supplied for them.
270 obsolete(char *argv[])
272 for (; *argv; ++argv) {
273 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
274 (argv[0][1] != 'e' && argv[0][1] != 'i' && argv[0][1] != 'k') ||
279 argv[0] = strdup("-e^H");
282 argv[0] = strdup("-i^C");
285 argv[0] = strdup("-k^U");
294 (void)fprintf(stderr, "%s\n%s\n",
295 "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]",
296 " reset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]");