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