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