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