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