1 /* $NetBSD: tset.c,v 1.16 2008/07/21 14:19:27 lukem Exp $ */
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 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\
35 The Regents of the University of California. All rights reserved.");
40 static char sccsid
[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
42 __RCSID("$NetBSD: tset.c,v 1.16 2008/07/21 14:19:27 lukem Exp $");
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
58 int main
__P((int, char *[]));
59 void obsolete
__P((char *[]));
60 void report
__P((const char *, int, u_int
));
61 void usage
__P((void));
63 struct termios mode
, oldmode
;
65 int isreset
; /* invoked as reset */
66 int lines
, columns
; /* window size */
76 int ch
, extended
, noinit
, noset
, quiet
, Sflag
, sflag
, showterm
;
77 int erasechar
= 0, intrchar
= 0, killchar
= 0;
79 char savech
, *p
, *tcapbuf
;
80 const char *k1
, *k2
, *k3
;
83 if (tcgetattr(STDERR_FILENO
, &mode
) < 0)
84 err(1, "standard error");
87 ospeed
= cfgetospeed(&mode
);
89 if ((p
= strrchr(*argv
, '/')) != NULL
)
93 usingupper
= isupper((unsigned char)*p
);
94 if (!strcasecmp(p
, "reset")) {
100 noinit
= noset
= quiet
= Sflag
= sflag
= showterm
= extended
= 0;
101 while ((ch
= getopt(argc
, argv
, "-a:d:e:EIi: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 erasechar
= optarg
[0] == '^' && optarg
[1] != '\0' ?
114 optarg
[1] == '?' ? '\177' : CTRL(optarg
[1]) :
120 case 'I': /* no initialization strings */
123 case 'i': /* interrupt character */
124 intrchar
= optarg
[0] == '^' && optarg
[1] != '\0' ?
125 optarg
[1] == '?' ? '\177' : CTRL(optarg
[1]) :
128 case 'k': /* kill character */
129 killchar
= optarg
[0] == '^' && optarg
[1] != '\0' ?
130 optarg
[1] == '?' ? '\177' : CTRL(optarg
[1]) :
133 case 'm': /* map identifier to type */
134 add_mapping(NULL
, optarg
);
136 case 'n': /* OBSOLETE: set new tty driver */
138 case 'p': /* OBSOLETE: map identifier to type */
139 add_mapping("plugboard", optarg
);
141 case 'Q': /* don't output control key settings */
144 case 'S': /* output TERM/TERMCAP strings */
147 case 'r': /* display term on stderr */
150 case 's': /* output TERM/TERMCAP strings */
164 ttype
= get_termcap_entry(*argv
, &tcapbuf
, extended
);
167 columns
= tgetnum("co");
168 lines
= tgetnum("li");
171 /* Set window size */
172 (void)ioctl(STDERR_FILENO
, TIOCGWINSZ
, &win
);
173 if (win
.ws_row
> 0 && win
.ws_col
> 0) {
175 columns
= win
.ws_col
;
176 } else if (win
.ws_row
== 0 && win
.ws_col
== 0 &&
177 lines
> 0 && columns
> 0) {
179 win
.ws_col
= columns
;
180 (void)ioctl(STDERR_FILENO
, TIOCSWINSZ
, &win
);
183 set_control_chars(erasechar
, intrchar
, killchar
);
184 set_conversions(usingupper
);
189 /* Set the modes if they've changed. */
190 if (memcmp(&mode
, &oldmode
, sizeof(mode
)))
191 tcsetattr(STDERR_FILENO
, TCSADRAIN
, &mode
);
194 /* Get the terminal name from the entry. */
196 if (p
!= NULL
&& *p
!= ':') {
198 if ((p
= strpbrk(p
, "|:")) != NULL
) {
201 if ((ttype
= strdup(k1
)) == NULL
)
208 (void)printf("%s\n", ttype
);
211 (void)fprintf(stderr
, "Terminal type is %s.\n", ttype
);
213 * If erase, kill and interrupt characters could have been
214 * modified and not -Q, display the changes.
217 report("Erase", VERASE
, CERASE
);
218 report("Kill", VKILL
, CKILL
);
219 report("Interrupt", VINTR
, CINTR
);
224 (void)printf("%s ", ttype
);
230 * Figure out what shell we're using. A hack, we look for an
231 * environmental variable SHELL ending in "csh".
233 if ((p
= getenv("SHELL")) &&
234 !strcmp(p
+ strlen(p
) - 3, "csh")) {
235 k1
= "set noglob;\nsetenv TERM ";
236 k2
= ";\nsetenv TERMCAP '";
237 k3
= "';\nunset noglob;\n";
241 k3
= "';\nexport TERMCAP TERM;\n";
243 (void)printf("%s%s%s", k1
, ttype
, k2
);
245 (void)printf("%s", k3
);
252 * Tell the user if a control key has been changed from the default value.
255 report(name
, which
, def
)
263 new = mode
.c_cc
[which
];
264 old
= oldmode
.c_cc
[which
];
266 if (old
== new && old
== def
)
269 (void)fprintf(stderr
, "%s %s ", name
, old
== new ? "is" : "set to");
272 if (tgetstr("kb", &bp
) && new == (unsigned int)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) {
278 (void)fprintf(stderr
, "control-%c (^%c).\n", new, new);
279 } else if (new == _POSIX_VDISABLE
)
280 (void)fprintf(stderr
, "<undef>.\n");
282 (void)fprintf(stderr
, "%c.\n", new);
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.
293 static char earg
[5] = { '-', 'e', '^', 'H', '\0' };
294 static char iarg
[5] = { '-', 'i', '^', 'C', '\0' };
295 static char karg
[5] = { '-', 'k', '^', 'U', '\0' };
297 for (; *argv
; ++argv
) {
298 if (argv
[0][0] != '-' || (argv
[1] && argv
[1][0] != '-') ||
299 (argv
[0][1] != 'e' && argv
[0][1] != 'i' &&
300 argv
[0][1] != 'k') || argv
[0][2] != '\0')
319 (void)fprintf(stderr
,
320 "usage: %s [-EIQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n",