tset.c revision 1.16 1 /* $NetBSD: tset.c,v 1.16 2008/07/21 14:19:27 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. 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.
18 *
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
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
37
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
41 #endif
42 __RCSID("$NetBSD: tset.c,v 1.16 2008/07/21 14:19:27 lukem Exp $");
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/ioctl.h>
47 #include <ctype.h>
48 #include <err.h>
49 #include <errno.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <termcap.h>
54 #include <termios.h>
55 #include <unistd.h>
56 #include "extern.h"
57
58 int main __P((int, char *[]));
59 void obsolete __P((char *[]));
60 void report __P((char *, int, u_int));
61 void usage __P((void));
62
63 struct termios mode, oldmode;
64
65 int isreset; /* invoked as reset */
66 int lines, columns; /* window size */
67
68 int
69 main(argc, argv)
70 int argc;
71 char *argv[];
72 {
73 #ifdef TIOCGWINSZ
74 struct winsize win;
75 #endif
76 int ch, extended, noinit, noset, quiet, Sflag, sflag, showterm;
77 int erasechar = 0, intrchar = 0, killchar = 0;
78 int usingupper;
79 char savech, *p, *q, *t, *tcapbuf;
80 const char *ttype;
81
82 if (tcgetattr(STDERR_FILENO, &mode) < 0)
83 err(1, "standard error");
84
85 oldmode = mode;
86 ospeed = cfgetospeed(&mode);
87
88 if ((p = strrchr(*argv, '/')) != NULL)
89 ++p;
90 else
91 p = *argv;
92 usingupper = isupper((unsigned char)*p);
93 if (!strcasecmp(p, "reset")) {
94 isreset = 1;
95 reset_mode();
96 }
97
98 obsolete(argv);
99 noinit = noset = quiet = Sflag = sflag = showterm = extended = 0;
100 while ((ch = getopt(argc, argv, "-a:d:e:EIi:k:m:np:QSrs")) != -1) {
101 switch (ch) {
102 case '-': /* display term only */
103 noset = 1;
104 break;
105 case 'a': /* OBSOLETE: map identifier to type */
106 add_mapping("arpanet", optarg);
107 break;
108 case 'd': /* OBSOLETE: map identifier to type */
109 add_mapping("dialup", optarg);
110 break;
111 case 'e': /* erase character */
112 erasechar = optarg[0] == '^' && optarg[1] != '\0' ?
113 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
114 optarg[0];
115 break;
116 case 'E':
117 extended = 1;
118 break;
119 case 'I': /* no initialization strings */
120 noinit = 1;
121 break;
122 case 'i': /* interrupt character */
123 intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
124 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
125 optarg[0];
126 break;
127 case 'k': /* kill character */
128 killchar = optarg[0] == '^' && optarg[1] != '\0' ?
129 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
130 optarg[0];
131 break;
132 case 'm': /* map identifier to type */
133 add_mapping(NULL, optarg);
134 break;
135 case 'n': /* OBSOLETE: set new tty driver */
136 break;
137 case 'p': /* OBSOLETE: map identifier to type */
138 add_mapping("plugboard", optarg);
139 break;
140 case 'Q': /* don't output control key settings */
141 quiet = 1;
142 break;
143 case 'S': /* output TERM/TERMCAP strings */
144 Sflag = 1;
145 break;
146 case 'r': /* display term on stderr */
147 showterm = 1;
148 break;
149 case 's': /* output TERM/TERMCAP strings */
150 sflag = 1;
151 break;
152 case '?':
153 default:
154 usage();
155 }
156 }
157 argc -= optind;
158 argv += optind;
159
160 if (argc > 1)
161 usage();
162
163 ttype = get_termcap_entry(*argv, &tcapbuf, extended);
164
165 if (!noset) {
166 columns = tgetnum("co");
167 lines = tgetnum("li");
168
169 #ifdef TIOCGWINSZ
170 /* Set window size */
171 (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
172 if (win.ws_row > 0 && win.ws_col > 0) {
173 lines = win.ws_row;
174 columns = win.ws_col;
175 } else if (win.ws_row == 0 && win.ws_col == 0 &&
176 lines > 0 && columns > 0) {
177 win.ws_row = lines;
178 win.ws_col = columns;
179 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
180 }
181 #endif
182 set_control_chars(erasechar, intrchar, killchar);
183 set_conversions(usingupper);
184
185 if (!noinit)
186 set_init();
187
188 /* Set the modes if they've changed. */
189 if (memcmp(&mode, &oldmode, sizeof(mode)))
190 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
191 }
192
193 /* Get the terminal name from the entry. */
194 p = tcapbuf;
195 if (p != NULL && *p != ':') {
196 t = p;
197 if ((p = strpbrk(p, "|:")) != NULL) {
198 savech = *p;
199 *p = '\0';
200 if ((ttype = strdup(t)) == NULL)
201 err(1, "strdup");
202 *p = savech;
203 }
204 }
205
206 if (noset)
207 (void)printf("%s\n", ttype);
208 else {
209 if (showterm)
210 (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
211 /*
212 * If erase, kill and interrupt characters could have been
213 * modified and not -Q, display the changes.
214 */
215 if (!quiet) {
216 report("Erase", VERASE, CERASE);
217 report("Kill", VKILL, CKILL);
218 report("Interrupt", VINTR, CINTR);
219 }
220 }
221
222 if (Sflag) {
223 (void)printf("%s ", ttype);
224 wrtermcap(tcapbuf);
225 }
226
227 if (sflag) {
228 /*
229 * Figure out what shell we're using. A hack, we look for an
230 * environmental variable SHELL ending in "csh".
231 */
232 if ((p = getenv("SHELL")) &&
233 !strcmp(p + strlen(p) - 3, "csh")) {
234 p = "set noglob;\nsetenv TERM ";
235 q = ";\nsetenv TERMCAP '";
236 t = "';\nunset noglob;\n";
237 } else {
238 p = "TERM=";
239 q = ";\nTERMCAP='";
240 t = "';\nexport TERMCAP TERM;\n";
241 }
242 (void)printf("%s%s%s", p, ttype, q);
243 wrtermcap(tcapbuf);
244 (void)printf("%s", t);
245 }
246
247 exit(0);
248 }
249
250 /*
251 * Tell the user if a control key has been changed from the default value.
252 */
253 void
254 report(name, which, def)
255 char *name;
256 int which;
257 u_int def;
258 {
259 u_int old, new;
260 char *bp, buf[1024];
261
262 new = mode.c_cc[which];
263 old = oldmode.c_cc[which];
264
265 if (old == new && old == def)
266 return;
267
268 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
269
270 bp = buf;
271 if (tgetstr("kb", &bp) && new == buf[0] && buf[1] == '\0')
272 (void)fprintf(stderr, "backspace.\n");
273 else if (new == 0177)
274 (void)fprintf(stderr, "delete.\n");
275 else if (new < 040) {
276 new ^= 0100;
277 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
278 } else if (new == _POSIX_VDISABLE)
279 (void)fprintf(stderr, "<undef>.\n");
280 else
281 (void)fprintf(stderr, "%c.\n", new);
282 }
283
284 /*
285 * Convert the obsolete argument form into something that getopt can handle.
286 * This means that -e, -i and -k get default arguments supplied for them.
287 */
288 void
289 obsolete(argv)
290 char *argv[];
291 {
292 for (; *argv; ++argv) {
293 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
294 (argv[0][1] != 'e' && argv[0][1] != 'i' &&
295 argv[0][1] != 'k') || argv[0][2] != '\0')
296 continue;
297 switch(argv[0][1]) {
298 case 'e':
299 argv[0] = "-e^H";
300 break;
301 case 'i':
302 argv[0] = "-i^C";
303 break;
304 case 'k':
305 argv[0] = "-k^U";
306 break;
307 }
308 }
309 }
310
311 void
312 usage()
313 {
314 (void)fprintf(stderr,
315 "usage: %s [-EIQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n",
316 getprogname());
317 exit(1);
318 }
319