]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - usr.bin/tset/tset.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / usr.bin / tset / tset.c
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
34 #include <sys/cdefs.h>
35
36 __FBSDID("$FreeBSD$");
37
38 #ifndef lint
39 static const char copyright[] =
40 "@(#) Copyright (c) 1980, 1991, 1993\n\
41         The Regents of the University of California.  All rights reserved.\n";
42 #endif
43
44 #ifndef lint
45 static const char sccsid[] = "@(#)tset.c        8.1 (Berkeley) 6/9/93";
46 #endif
47
48 #include <sys/types.h>
49 #include <sys/ioctl.h>
50
51 #include <ctype.h>
52 #include <err.h>
53 #include <stdio.h>
54 #include <stdlib.h>
55 #include <string.h>
56 #include <termios.h>
57 #include <unistd.h>
58
59 #include "extern.h"
60
61 void    obsolete(char *[]);
62 void    report(const char *, int, u_int);
63 void    usage(void);
64
65 struct termios mode, oldmode;
66
67 int     erasech;                /* new erase character */
68 int     intrchar;               /* new interrupt character */
69 int     isreset;                /* invoked as reset */
70 int     killch;                 /* new kill character */
71 int     Lines, Columns;         /* window size */
72 speed_t Ospeed;
73
74 int
75 main(int argc, char *argv[])
76 {
77 #ifdef TIOCGWINSZ
78         struct winsize win;
79 #endif
80         int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
81         char *p, *tcapbuf;
82         const char *ttype;
83
84         if (tcgetattr(STDERR_FILENO, &mode) < 0)
85                 err(1, "standard error");
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;
102         while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
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                         erasech = 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                         killch = 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         if (noset)
190                 (void)printf("%s\n", ttype);
191         else {
192                 if (showterm)
193                         (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
194                 /*
195                  * If erase, kill and interrupt characters could have been
196                  * modified and not -Q, display the changes.
197                  */
198                 if (!quiet) {
199                         report("Erase", VERASE, CERASE);
200                         report("Kill", VKILL, CKILL);
201                         report("Interrupt", VINTR, CINTR);
202                 }
203         }
204
205         if (Sflag) {
206                 (void)printf("%s ", ttype);
207                 if (strlen(tcapbuf) > 0)
208                         wrtermcap(tcapbuf);
209         }
210
211         if (sflag) {
212                 /*
213                  * Figure out what shell we're using.  A hack, we look for an
214                  * environmental variable SHELL ending in "csh".
215                  */
216                 if ((p = getenv("SHELL")) &&
217                     !strcmp(p + strlen(p) - 3, "csh")) {
218                         printf("set noglob;\nsetenv TERM %s;\n", ttype);
219                         if (strlen(tcapbuf) > 0) {
220                                 printf("setenv TERMCAP '");
221                                 wrtermcap(tcapbuf);
222                                 printf("';\n");
223                         }
224                         printf("unset noglob;\n");
225                 } else {
226                         printf("TERM=%s;\n", ttype);
227                         if (strlen(tcapbuf) > 0) {
228                                 printf("TERMCAP='");
229                                 wrtermcap(tcapbuf);
230                                 printf("';\nexport TERMCAP;\n");
231                         }
232                         printf("export TERM;\n");
233                 }
234         }
235
236         exit(0);
237 }
238
239 /*
240  * Tell the user if a control key has been changed from the default value.
241  */
242 void
243 report(const char *name, int which, u_int def)
244 {
245         u_int old, new;
246
247         new = mode.c_cc[which];
248         old = oldmode.c_cc[which];
249
250         if (old == new && old == def)
251                 return;
252
253         (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
254
255         if (new == 010)
256                 (void)fprintf(stderr, "backspace.\n");
257         else if (new == 0177)
258                 (void)fprintf(stderr, "delete.\n");
259         else if (new < 040) {
260                 new ^= 0100;
261                 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
262         } else
263                 (void)fprintf(stderr, "%c.\n", new);
264 }
265
266 /*
267  * Convert the obsolete argument form into something that getopt can handle.
268  * This means that -e, -i and -k get default arguments supplied for them.
269  */
270 void
271 obsolete(char *argv[])
272 {
273         for (; *argv; ++argv) {
274                 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
275                     (argv[0][1] != 'e' && argv[0][1] != 'i' && argv[0][1] != 'k') ||
276                         argv[0][2] != '\0')
277                         continue;
278                 switch(argv[0][1]) {
279                 case 'e':
280                         argv[0] = strdup("-e^H");
281                         break;
282                 case 'i':
283                         argv[0] = strdup("-i^C");
284                         break;
285                 case 'k':
286                         argv[0] = strdup("-k^U");
287                         break;
288                 }
289         }
290 }
291
292 void
293 usage(void)
294 {
295         (void)fprintf(stderr, "%s\n%s\n",
296 "usage: tset  [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]",
297 "       reset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]");
298         exit(1);
299 }
300