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