tset.c revision 1.6 1 /* $NetBSD: tset.c,v 1.6 1997/10/20 01:07:54 lukem Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\n\
39 The Regents of the University of California. All rights reserved.\n");
40 #endif /* not lint */
41
42 #ifndef lint
43 #if 0
44 static char sccsid[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
45 #endif
46 __RCSID("$NetBSD: tset.c,v 1.6 1997/10/20 01:07:54 lukem Exp $");
47 #endif /* not lint */
48
49 #include <sys/types.h>
50 #include <sys/ioctl.h>
51 #include <ctype.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <termcap.h>
58 #include <termios.h>
59 #include <unistd.h>
60 #include "extern.h"
61
62 int main __P((int, char *[]));
63 void obsolete __P((char *[]));
64 void report __P((char *, int, u_int));
65 void usage __P((void));
66
67 struct termios mode, oldmode;
68
69 int erasechar; /* new erase character */
70 int intrchar; /* new interrupt character */
71 int isreset; /* invoked as reset */
72 int killchar; /* new kill character */
73 int lines, columns; /* window size */
74
75 int
76 main(argc, argv)
77 int argc;
78 char *argv[];
79 {
80 #ifdef TIOCGWINSZ
81 struct winsize win;
82 #endif
83 int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
84 char savech, *p, *t, *tcapbuf, *ttype;
85
86 if (tcgetattr(STDERR_FILENO, &mode) < 0)
87 err(1, "standard error");
88
89 oldmode = mode;
90 ospeed = cfgetospeed(&mode);
91
92 if ((p = strrchr(*argv, '/')) != NULL)
93 ++p;
94 else
95 p = *argv;
96 usingupper = isupper(*p);
97 if (!strcasecmp(p, "reset")) {
98 isreset = 1;
99 reset_mode();
100 }
101
102 obsolete(argv);
103 noinit = noset = quiet = Sflag = sflag = showterm = 0;
104 while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
105 switch (ch) {
106 case '-': /* display term only */
107 noset = 1;
108 break;
109 case 'a': /* OBSOLETE: map identifier to type */
110 add_mapping("arpanet", optarg);
111 break;
112 case 'd': /* OBSOLETE: map identifier to type */
113 add_mapping("dialup", optarg);
114 break;
115 case 'e': /* erase character */
116 erasechar = optarg[0] == '^' && optarg[1] != '\0' ?
117 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
118 optarg[0];
119 break;
120 case 'I': /* no initialization strings */
121 noinit = 1;
122 break;
123 case 'i': /* interrupt character */
124 intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
125 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
126 optarg[0];
127 break;
128 case 'k': /* kill character */
129 killchar = optarg[0] == '^' && optarg[1] != '\0' ?
130 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
131 optarg[0];
132 break;
133 case 'm': /* map identifier to type */
134 add_mapping(NULL, optarg);
135 break;
136 case 'n': /* OBSOLETE: set new tty driver */
137 break;
138 case 'p': /* OBSOLETE: map identifier to type */
139 add_mapping("plugboard", optarg);
140 break;
141 case 'Q': /* don't output control key settings */
142 quiet = 1;
143 break;
144 case 'S': /* output TERM/TERMCAP strings */
145 Sflag = 1;
146 break;
147 case 'r': /* display term on stderr */
148 showterm = 1;
149 break;
150 case 's': /* output TERM/TERMCAP strings */
151 sflag = 1;
152 break;
153 case '?':
154 default:
155 usage();
156 }
157 }
158 argc -= optind;
159 argv += optind;
160
161 if (argc > 1)
162 usage();
163
164 ttype = get_termcap_entry(*argv, &tcapbuf);
165
166 if (!noset) {
167 columns = tgetnum("co");
168 lines = tgetnum("li");
169
170 #ifdef TIOCGWINSZ
171 /* Set window size */
172 (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
173 if (win.ws_row == 0 && win.ws_col == 0 &&
174 lines > 0 && columns > 0) {
175 win.ws_row = lines;
176 win.ws_col = columns;
177 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
178 }
179 #endif
180 set_control_chars();
181 set_conversions(usingupper);
182
183 if (!noinit)
184 set_init();
185
186 /* Set the modes if they've changed. */
187 if (memcmp(&mode, &oldmode, sizeof(mode)))
188 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
189 }
190
191 /* Get the terminal name from the entry. */
192 p = tcapbuf;
193 if (p != NULL && *p != ':') {
194 t = p;
195 if ((p = strpbrk(p, "|:")) != NULL) {
196 savech = *p;
197 *p = '\0';
198 if ((ttype = strdup(t)) == NULL)
199 err(1, "strdup");
200 *p = savech;
201 }
202 }
203
204 if (noset)
205 (void)printf("%s\n", ttype);
206 else {
207 if (showterm)
208 (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
209 /*
210 * If erase, kill and interrupt characters could have been
211 * modified and not -Q, display the changes.
212 */
213 if (!quiet) {
214 report("Erase", VERASE, CERASE);
215 report("Kill", VKILL, CKILL);
216 report("Interrupt", VINTR, CINTR);
217 }
218 }
219
220 if (Sflag) {
221 (void)printf("%s ", ttype);
222 wrtermcap(tcapbuf);
223 }
224
225 if (sflag) {
226 /*
227 * Figure out what shell we're using. A hack, we look for an
228 * environmental variable SHELL ending in "csh".
229 */
230 if ((p = getenv("SHELL")) &&
231 !strcmp(p + strlen(p) - 3, "csh")) {
232 p = "set noglob;\nsetenv TERM %s;\nsetenv TERMCAP '";
233 t = "';\nunset noglob;\n";
234 } else {
235 p = "TERM=%s;\nTERMCAP='";
236 t = "';\nexport TERMCAP TERM;\n";
237 }
238 (void)printf(p, ttype);
239 wrtermcap(tcapbuf);
240 (void)printf(t);
241 }
242
243 exit(0);
244 }
245
246 /*
247 * Tell the user if a control key has been changed from the default value.
248 */
249 void
250 report(name, which, def)
251 char *name;
252 int which;
253 u_int def;
254 {
255 u_int old, new;
256 char *bp, buf[1024];
257
258 new = mode.c_cc[which];
259 old = oldmode.c_cc[which];
260
261 if (old == new && old == def)
262 return;
263
264 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
265
266 bp = buf;
267 if (tgetstr("kb", &bp) && new == buf[0] && buf[1] == '\0')
268 (void)fprintf(stderr, "backspace.\n");
269 else if (new == 0177)
270 (void)fprintf(stderr, "delete.\n");
271 else if (new < 040) {
272 new ^= 0100;
273 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
274 } else
275 (void)fprintf(stderr, "%c.\n", new);
276 }
277
278 /*
279 * Convert the obsolete argument form into something that getopt can handle.
280 * This means that -e, -i and -k get default arguments supplied for them.
281 */
282 void
283 obsolete(argv)
284 char *argv[];
285 {
286 for (; *argv; ++argv) {
287 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
288 (argv[0][1] != 'e' && argv[0][1] != 'i' &&
289 argv[0][1] != 'k') || argv[0][2] != '\0')
290 continue;
291 switch(argv[0][1]) {
292 case 'e':
293 argv[0] = "-e^H";
294 break;
295 case 'i':
296 argv[0] = "-i^C";
297 break;
298 case 'k':
299 argv[0] = "-k^U";
300 break;
301 }
302 }
303 }
304
305 void
306 usage()
307 {
308 (void)fprintf(stderr,
309 "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n");
310 exit(1);
311 }
312