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