Add __attribute__((__noreturn__)) to various function prototypes in usr.bin/.
[dragonfly.git] / usr.bin / tset / tset.c
blob7bc35cd8e3885ed934fb23efc4ab0b47e13421f9
1 /*-
2 * Copyright (c) 1980, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
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. 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.
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.
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 $
34 #include <sys/types.h>
35 #include <sys/ioctl.h>
36 #include <ctype.h>
37 #include <err.h>
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <string.h>
41 #include <unistd.h>
42 #include <termios.h>
43 #include "extern.h"
45 void obsolete(char *[]);
46 void report(const char *, int, u_int);
47 void usage(void) __dead2;
49 struct termios mode, oldmode;
51 int erasech; /* new erase character */
52 int intrchar; /* new interrupt character */
53 int isreset; /* invoked as reset */
54 int killch; /* new kill character */
55 int Lines, Columns; /* window size */
56 speed_t Ospeed;
58 int
59 main(int argc, char **argv)
61 #ifdef TIOCGWINSZ
62 struct winsize win;
63 #endif
64 int ch, noinit, noset, quiet, Sflag, sflag, showterm, usingupper;
65 char *p, *tcapbuf;
66 const char *ttype;
68 if (tcgetattr(STDERR_FILENO, &mode) < 0)
69 err(1, "standard error");
71 oldmode = mode;
72 Ospeed = cfgetospeed(&mode);
74 if ((p = strrchr(*argv, '/')))
75 ++p;
76 else
77 p = *argv;
78 usingupper = isupper(*p);
79 if (!strcasecmp(p, "reset")) {
80 isreset = 1;
81 reset_mode();
84 obsolete(argv);
85 noinit = noset = quiet = Sflag = sflag = showterm = 0;
86 while ((ch = getopt(argc, argv, "-a:d:e:Ii:k:m:np:QSrs")) != -1) {
87 switch (ch) {
88 case '-': /* display term only */
89 noset = 1;
90 break;
91 case 'a': /* OBSOLETE: map identifier to type */
92 add_mapping("arpanet", optarg);
93 break;
94 case 'd': /* OBSOLETE: map identifier to type */
95 add_mapping("dialup", optarg);
96 break;
97 case 'e': /* erase character */
98 erasech = optarg[0] == '^' && optarg[1] != '\0' ?
99 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
100 optarg[0];
101 break;
102 case 'I': /* no initialization strings */
103 noinit = 1;
104 break;
105 case 'i': /* interrupt character */
106 intrchar = optarg[0] == '^' && optarg[1] != '\0' ?
107 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
108 optarg[0];
109 break;
110 case 'k': /* kill character */
111 killch = optarg[0] == '^' && optarg[1] != '\0' ?
112 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) :
113 optarg[0];
114 break;
115 case 'm': /* map identifier to type */
116 add_mapping(NULL, optarg);
117 break;
118 case 'n': /* OBSOLETE: set new tty driver */
119 break;
120 case 'p': /* OBSOLETE: map identifier to type */
121 add_mapping("plugboard", optarg);
122 break;
123 case 'Q': /* don't output control key settings */
124 quiet = 1;
125 break;
126 case 'S': /* output TERM/TERMCAP strings */
127 Sflag = 1;
128 break;
129 case 'r': /* display term on stderr */
130 showterm = 1;
131 break;
132 case 's': /* output TERM/TERMCAP strings */
133 sflag = 1;
134 break;
135 case '?':
136 default:
137 usage();
140 argc -= optind;
141 argv += optind;
143 if (argc > 1)
144 usage();
146 ttype = get_termcap_entry(*argv, &tcapbuf);
148 if (!noset) {
149 Columns = tgetnum("co");
150 Lines = tgetnum("li");
152 #ifdef TIOCGWINSZ
153 /* Set window size */
154 (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win);
155 if (win.ws_row == 0 && win.ws_col == 0 &&
156 Lines > 0 && Columns > 0) {
157 win.ws_row = Lines;
158 win.ws_col = Columns;
159 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win);
161 #endif
162 set_control_chars();
163 set_conversions(usingupper);
165 if (!noinit)
166 set_init();
168 /* Set the modes if they've changed. */
169 if (memcmp(&mode, &oldmode, sizeof(mode)))
170 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode);
173 if (noset)
174 (void)printf("%s\n", ttype);
175 else {
176 if (showterm)
177 (void)fprintf(stderr, "Terminal type is %s.\n", ttype);
179 * If erase, kill and interrupt characters could have been
180 * modified and not -Q, display the changes.
182 if (!quiet) {
183 report("Erase", VERASE, CERASE);
184 report("Kill", VKILL, CKILL);
185 report("Interrupt", VINTR, CINTR);
189 if (Sflag) {
190 (void)printf("%s ", ttype);
191 if (strlen(tcapbuf) > 0)
192 wrtermcap(tcapbuf);
195 if (sflag) {
197 * Figure out what shell we're using. A hack, we look for an
198 * environmental variable SHELL ending in "csh".
200 if ((p = getenv("SHELL")) &&
201 !strcmp(p + strlen(p) - 3, "csh")) {
202 printf("set noglob;\nsetenv TERM %s;\n", ttype);
203 if (strlen(tcapbuf) > 0) {
204 printf("setenv TERMCAP '");
205 wrtermcap(tcapbuf);
206 printf("';\n");
208 printf("unset noglob;\n");
209 } else {
210 printf("TERM=%s;\n", ttype);
211 if (strlen(tcapbuf) > 0) {
212 printf("TERMCAP='");
213 wrtermcap(tcapbuf);
214 printf("';\nexport TERMCAP;\n");
216 printf("export TERM;\n");
220 exit(0);
224 * Tell the user if a control key has been changed from the default value.
226 void
227 report(const char *name, int which, u_int def)
229 u_int old, new;
231 new = mode.c_cc[which];
232 old = oldmode.c_cc[which];
234 if (old == new && old == def)
235 return;
237 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to");
239 if (new == 010)
240 (void)fprintf(stderr, "backspace.\n");
241 else if (new == 0177)
242 (void)fprintf(stderr, "delete.\n");
243 else if (new < 040) {
244 new ^= 0100;
245 (void)fprintf(stderr, "control-%c (^%c).\n", new, new);
246 } else
247 (void)fprintf(stderr, "%c.\n", new);
251 * Convert the obsolete argument form into something that getopt can handle.
252 * This means that -e, -i and -k get default arguments supplied for them.
254 void
255 obsolete(char **argv)
257 for (; *argv; ++argv) {
258 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') ||
259 (argv[0][1] != 'e' && argv[0][1] != 'i' && argv[0][1] != 'k') ||
260 argv[0][2] != '\0')
261 continue;
262 switch(argv[0][1]) {
263 case 'e':
264 argv[0] = strdup("-e^H");
265 break;
266 case 'i':
267 argv[0] = strdup("-i^C");
268 break;
269 case 'k':
270 argv[0] = strdup("-k^U");
271 break;
276 void
277 usage(void)
279 (void)fprintf(stderr, "%s\n%s\n",
280 "usage: tset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]",
281 " reset [-IQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]");
282 exit(1);