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