1 /* $NetBSD: tset.c,v 1.19 2010/02/10 10:34:31 roy Exp $ */ 2 3 /*- 4 * Copyright (c) 1980, 1991, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 __COPYRIGHT("@(#) Copyright (c) 1980, 1991, 1993\ 34 The Regents of the University of California. All rights reserved."); 35 __RCSID("$NetBSD: tset.c,v 1.19 2010/02/10 10:34:31 roy Exp $"); 36 37 #include <sys/types.h> 38 #include <sys/ioctl.h> 39 #include <ctype.h> 40 #include <err.h> 41 #include <errno.h> 42 #include <stdio.h> 43 #include <stdlib.h> 44 #include <string.h> 45 #include <term.h> 46 #include <termios.h> 47 #include <unistd.h> 48 #include "extern.h" 49 50 int main __P((int, char *[])); 51 void obsolete __P((char *[])); 52 void report __P((const char *, int, u_int)); 53 void usage __P((void)); 54 55 struct termios mode, oldmode; 56 57 int isreset; /* invoked as reset */ 58 int nlines, ncolumns; /* window size */ 59 60 int 61 main(argc, argv) 62 int argc; 63 char *argv[]; 64 { 65 #ifdef TIOCGWINSZ 66 struct winsize win; 67 #endif 68 int ch, noinit, noset, quiet, sflag, showterm; 69 int erasechar = 0, intrchar = 0, killchar = 0; 70 int usingupper; 71 char *p; 72 const char *k1, *k2; 73 const char *ttype; 74 75 if (tcgetattr(STDERR_FILENO, &mode) < 0) 76 err(1, "standard error"); 77 78 oldmode = mode; 79 ospeed = cfgetospeed(&mode); 80 81 if ((p = strrchr(*argv, '/')) != NULL) 82 ++p; 83 else 84 p = *argv; 85 usingupper = isupper((unsigned char)*p); 86 if (!strcasecmp(p, "reset")) { 87 isreset = 1; 88 reset_mode(); 89 } 90 91 obsolete(argv); 92 noinit = noset = quiet = sflag = showterm = 0; 93 while ((ch = getopt(argc, argv, "-a:d:e:EIi:k:m:np:QSrs")) != -1) { 94 switch (ch) { 95 case '-': /* display term only */ 96 noset = 1; 97 break; 98 case 'a': /* OBSOLETE: map identifier to type */ 99 add_mapping("arpanet", optarg); 100 break; 101 case 'd': /* OBSOLETE: map identifier to type */ 102 add_mapping("dialup", optarg); 103 break; 104 case 'e': /* erase character */ 105 erasechar = optarg[0] == '^' && optarg[1] != '\0' ? 106 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) : 107 optarg[0]; 108 break; 109 case 'E': /* -E does not make sense for terminfo 110 should this be noisy? */ 111 break; 112 case 'I': /* no initialization strings */ 113 noinit = 1; 114 break; 115 case 'i': /* interrupt character */ 116 intrchar = optarg[0] == '^' && optarg[1] != '\0' ? 117 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) : 118 optarg[0]; 119 break; 120 case 'k': /* kill character */ 121 killchar = optarg[0] == '^' && optarg[1] != '\0' ? 122 optarg[1] == '?' ? '\177' : CTRL(optarg[1]) : 123 optarg[0]; 124 break; 125 case 'm': /* map identifier to type */ 126 add_mapping(NULL, optarg); 127 break; 128 case 'n': /* OBSOLETE: set new tty driver */ 129 break; 130 case 'p': /* OBSOLETE: map identifier to type */ 131 add_mapping("plugboard", optarg); 132 break; 133 case 'Q': /* don't output control key settings */ 134 quiet = 1; 135 break; 136 case 'S': /* -S does not make sense for terminfo 137 should this be noisy? */ 138 break; 139 case 'r': /* display term on stderr */ 140 showterm = 1; 141 break; 142 case 's': /* output TERM string */ 143 sflag = 1; 144 break; 145 case '?': 146 default: 147 usage(); 148 } 149 } 150 argc -= optind; 151 argv += optind; 152 153 if (argc > 1) 154 usage(); 155 156 ttype = get_terminfo_entry(*argv); 157 158 if (!noset) { 159 ncolumns = columns; 160 nlines = lines; 161 162 #ifdef TIOCGWINSZ 163 /* Set window size */ 164 (void)ioctl(STDERR_FILENO, TIOCGWINSZ, &win); 165 if (win.ws_row > 0 && win.ws_col > 0) { 166 nlines = win.ws_row; 167 ncolumns = win.ws_col; 168 } else if (win.ws_row == 0 && win.ws_col == 0 && 169 nlines > 0 && columns > 0) { 170 win.ws_row = nlines; 171 win.ws_col = ncolumns; 172 (void)ioctl(STDERR_FILENO, TIOCSWINSZ, &win); 173 } 174 #endif 175 set_control_chars(erasechar, intrchar, killchar); 176 set_conversions(usingupper); 177 178 if (!noinit) 179 set_init(); 180 181 /* Set the modes if they've changed. */ 182 if (memcmp(&mode, &oldmode, sizeof(mode))) 183 tcsetattr(STDERR_FILENO, TCSADRAIN, &mode); 184 } 185 186 if (noset) 187 (void)printf("%s\n", ttype); 188 else { 189 if (showterm) 190 (void)fprintf(stderr, "Terminal type is %s.\n", ttype); 191 /* 192 * If erase, kill and interrupt characters could have been 193 * modified and not -Q, display the changes. 194 */ 195 if (!quiet) { 196 report("Erase", VERASE, CERASE); 197 report("Kill", VKILL, CKILL); 198 report("Interrupt", VINTR, CINTR); 199 } 200 } 201 202 if (sflag) { 203 /* 204 * Figure out what shell we're using. A hack, we look for an 205 * environmental variable SHELL ending in "csh". 206 */ 207 if ((p = getenv("SHELL")) && 208 !strcmp(p + strlen(p) - 3, "csh")) { 209 k1 = "set noglob;\nsetenv TERM "; 210 k2 = ";\nunset noglob;\n"; 211 } else { 212 k1 = "TERM="; 213 k2 = ";\nexport TERM;\n"; 214 } 215 (void)printf("%s%s%s", k1, ttype, k2); 216 } 217 218 exit(0); 219 } 220 221 /* 222 * Tell the user if a control key has been changed from the default value. 223 */ 224 void 225 report(name, which, def) 226 const char *name; 227 int which; 228 u_int def; 229 { 230 u_int old, new; 231 232 new = mode.c_cc[which]; 233 old = oldmode.c_cc[which]; 234 235 if (old == new && old == def) 236 return; 237 238 (void)fprintf(stderr, "%s %s ", name, old == new ? "is" : "set to"); 239 240 if (key_backspace != NULL && 241 new == (unsigned int)key_backspace[0] && 242 key_backspace[1] == '\0') 243 (void)fprintf(stderr, "backspace.\n"); 244 else if (new == 0177) 245 (void)fprintf(stderr, "delete.\n"); 246 else if (new < 040) { 247 new ^= 0100; 248 (void)fprintf(stderr, "control-%c (^%c).\n", new, new); 249 } else if (new == _POSIX_VDISABLE) 250 (void)fprintf(stderr, "<undef>.\n"); 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(argv) 261 char *argv[]; 262 { 263 static char earg[5] = { '-', 'e', '^', 'H', '\0' }; 264 static char iarg[5] = { '-', 'i', '^', 'C', '\0' }; 265 static char karg[5] = { '-', 'k', '^', 'U', '\0' }; 266 267 for (; *argv; ++argv) { 268 if (argv[0][0] != '-' || (argv[1] && argv[1][0] != '-') || 269 (argv[0][1] != 'e' && argv[0][1] != 'i' && 270 argv[0][1] != 'k') || argv[0][2] != '\0') 271 continue; 272 switch(argv[0][1]) { 273 case 'e': 274 argv[0] = earg; 275 break; 276 case 'i': 277 argv[0] = iarg; 278 break; 279 case 'k': 280 argv[0] = karg; 281 break; 282 } 283 } 284 } 285 286 void 287 usage() 288 { 289 (void)fprintf(stderr, 290 "usage: %s [-EIQrSs] [-] [-e ch] [-i ch] [-k ch] [-m mapping] [terminal]\n", 291 getprogname()); 292 exit(1); 293 } 294