Annotation of src/usr.bin/tset/tset.c, Revision 1.3
1.3 ! millert 1: /* $OpenBSD: tset.c,v 1.2 1996/06/26 05:41:58 deraadt Exp $ */
1.1 deraadt 2: /* $NetBSD: tset.c,v 1.4 1994/12/07 05:08:15 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1980, 1991, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: static char copyright[] =
39: "@(#) Copyright (c) 1980, 1991, 1993\n\
40: The Regents of the University of California. All rights reserved.\n";
41: #endif /* not lint */
42:
43: #ifndef lint
44: #if 0
45: static char sccsid[] = "@(#)tset.c 8.1 (Berkeley) 6/9/93";
46: #endif
1.3 ! millert 47: static char rcsid[] = "$OpenBSD: tset.c,v 1.2 1996/06/26 05:41:58 deraadt Exp $";
1.1 deraadt 48: #endif /* not lint */
49:
50: #include <sys/types.h>
51: #include <sys/ioctl.h>
52: #include <termios.h>
53: #include <errno.h>
54: #include <unistd.h>
55: #include <stdlib.h>
56: #include <stdio.h>
57: #include <ctype.h>
58: #include <string.h>
59: #include "extern.h"
60:
61: void obsolete __P((char *[]));
62: void report __P((char *, int, u_int));
63: void usage __P((void));
64:
65: struct termios mode, oldmode;
66:
67: int erasechar; /* new erase character */
68: int intrchar; /* new interrupt character */
69: int isreset; /* invoked as reset */
70: int killchar; /* new kill character */
71: int lines, columns; /* window size */
72:
73: int
74: main(argc, argv)
75: int argc;
76: char *argv[];
77: {
78: #ifdef TIOCGWINSZ
79: struct winsize win;
80: #endif
81: int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
82: char savech, *p, *t, *tcapbuf, *ttype;
83:
84: if (tcgetattr(STDERR_FILENO, &mode) < 0)
85: err("standard error: %s", strerror(errno));
86:
87: oldmode = mode;
88: ospeed = cfgetospeed(&mode);
89:
90: if (p = strrchr(*argv, '/'))
91: ++p;
92: else
93: p = *argv;
94: usingupper = isupper(*p);
95: if (!strcasecmp(p, "reset")) {
96: isreset = 1;
97: reset_mode();
98: }
99:
100: obsolete(argv);
101: noinit = noset = quiet = Sflag = sflag = showterm = 0;
1.3 ! millert 102: while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
1.1 deraadt 103: switch (ch) {
104: case '-': /* display term only */
105: noset = 1;
106: break;
107: case 'a': /* OBSOLETE: map identifier to type */
108: add_mapping("arpanet", optarg);
109: break;
110: case 'd': /* OBSOLETE: map identifier to type */
111: add_mapping("dialup", optarg);
112: break;
113: case 'e': /* erase character */
114: erasechar = optarg[0] == '^' && optarg[1] != '\0' ?
115: optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
116: optarg[0];
117: break;
118: case 'I': /* no initialization strings */
119: noinit = 1;
120: break;
121: case 'i': /* interrupt character */
122: intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
123: optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
124: optarg[0];
125: break;
126: case 'k': /* kill character */
127: killchar = optarg[0] == '^' && optarg[1] != '\0' ?
128: optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
129: optarg[0];
130: break;
131: case 'm': /* map identifier to type */
132: add_mapping(NULL, optarg);
133: break;
134: case 'n': /* OBSOLETE: set new tty driver */
135: break;
136: case 'p': /* OBSOLETE: map identifier to type */
137: add_mapping("plugboard", optarg);
138: break;
139: case 'Q': /* don't output control key settings */
140: quiet = 1;
141: break;
142: case 'S': /* output TERM/TERMCAP strings */
143: Sflag = 1;
144: break;
145: case 'r': /* display term on stderr */
146: showterm = 1;
147: break;
148: case 's': /* output TERM/TERMCAP strings */
149: sflag = 1;
150: break;
151: case '?':
152: default:
153: usage();
154: }
155: }
156: argc -= optind;
157: argv += optind;
158:
159: if (argc > 1)
160: usage();
161:
162: ttype = get_termcap_entry(*argv, &tcapbuf);
163:
164: if (!noset) {
165: columns = tgetnum("co");
166: lines = tgetnum("li");
167:
168: #ifdef TIOCGWINSZ
169: /* Set window size */
170: (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
171: if (win.ws_row == 0 && win.ws_col == 0 &&
172: lines > 0 && columns > 0) {
173: win.ws_row = lines;
174: win.ws_col = columns;
175: (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
176: }
177: #endif
178: set_control_chars();
179: set_conversions(usingupper);
180:
181: if (!noinit)
182: set_init();
183:
184: /* Set the modes if they've changed. */
185: if (memcmp(&mode, &oldmode, sizeof(mode)))
186: tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
187: }
188:
189: /* Get the terminal name from the entry. */
190: p = tcapbuf;
191: if (p != NULL && *p != ':') {
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 (Sflag) {
219: (void)printf("%s ", ttype);
220: wrtermcap(tcapbuf);
221: }
222:
223: if (sflag) {
224: /*
225: * Figure out what shell we're using. A hack, we look for an
226: * environmental variable SHELL ending in "csh".
227: */
228: if ((p = getenv("SHELL")) &&
229: !strcmp(p + strlen(p) - 3, "csh")) {
230: p = "set noglob;\nsetenv TERM %s;\nsetenv TERMCAP '";
231: t = "';\nunset noglob;\n";
232: } else {
233: p = "TERM=%s;\nTERMCAP='";
234: t = "';\nexport TERMCAP TERM;\n";
235: }
236: (void)printf(p, ttype);
237: wrtermcap(tcapbuf);
238: (void)printf(t);
239: }
240:
241: exit(0);
242: }
243:
244: /*
245: * Tell the user if a control key has been changed from the default value.
246: */
247: void
248: report(name, which, def)
249: char *name;
250: int which;
251: u_int def;
252: {
253: u_int old, new;
254: char *bp, buf[1024];
255:
256: new = mode.c_cc[which];
257: old = oldmode.c_cc[which];
258:
259: if (old == new && old == def)
260: return;
261:
262: (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
263:
264: bp = buf;
265: if (tgetstr("kb", &bp) && new == buf[0] && buf[1] == '\0')
266: (void)fprintf(stderr, "backspace.\n");
267: else if (new == 0177)
268: (void)fprintf(stderr, "delete.\n");
269: else if (new < 040) {
270: new ^= 0100;
271: (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
272: } else
273: (void)fprintf(stderr, "%c.\n", new);
274: }
275:
276: /*
277: * Convert the obsolete argument form into something that getopt can handle.
278: * This means that -e, -i and -k get default arguments supplied for them.
279: */
280: void
281: obsolete(argv)
282: char *argv[];
283: {
284: for (; *argv; ++argv) {
285: if (argv[0][0] != '-' || argv[1] && argv[1][0] != '-' ||
286: argv[0][1] != 'e' && argv[0][1] != 'i' &&
287: argv[0][1] != 'k' || argv[0][2] != '\0')
288: continue;
289: switch(argv[0][1]) {
290: case 'e':
291: argv[0] = "-e^H";
292: break;
293: case 'i':
294: argv[0] = "-i^C";
295: break;
296: case 'k':
297: argv[0] = "-k^U";
298: break;
299: }
300: }
301: }
302:
303: void
304: usage()
305: {
306: (void)fprintf(stderr,
307: "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n");
308: exit(1);
309: }