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