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