1 /*- 2 * Copyright (c) 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. 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. 16 * 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. 28 * 29 * @(#)term.c 8.1 (Berkeley) 6/9/93 30 * $FreeBSD: src/usr.bin/tset/term.c,v 1.4 1999/08/28 01:06:58 peter Exp $ 31 * $DragonFly: src/usr.bin/tset/term.c,v 1.4 2004/08/30 18:06:50 eirikn Exp $ 32 */ 33 34 #include <sys/types.h> 35 #include <err.h> 36 #include <errno.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <unistd.h> 41 #include <ttyent.h> 42 #include "extern.h" 43 44 char tbuf[1024]; /* Termcap entry. */ 45 46 const char *askuser(const char *); 47 char *ttys(char *); 48 49 /* 50 * Figure out what kind of terminal we're dealing with, and then read in 51 * its termcap entry. 52 */ 53 const char * 54 get_termcap_entry(char *userarg, char **tcapbufp) 55 { 56 struct ttyent *t; 57 int rval; 58 char *p, *ttypath; 59 const char *ttype; 60 61 if (userarg) { 62 ttype = userarg; 63 goto found; 64 } 65 66 /* Try the environment. */ 67 if ((ttype = getenv("TERM"))) 68 goto map; 69 70 /* Try ttyname(3); check for dialup or other mapping. */ 71 if ((ttypath = ttyname(STDERR_FILENO))) { 72 if ((p = strrchr(ttypath, '/'))) 73 ++p; 74 else 75 p = ttypath; 76 if ((t = getttynam(p))) { 77 ttype = t->ty_type; 78 goto map; 79 } 80 } 81 82 /* If still undefined, use "unknown". */ 83 ttype = "unknown"; 84 85 map: ttype = mapped(ttype); 86 87 /* 88 * If not a path, remove TERMCAP from the environment so we get a 89 * real entry from /etc/termcap. This prevents us from being fooled 90 * by out of date stuff in the environment. 91 */ 92 found: if ((p = getenv("TERMCAP")) != NULL && *p != '/') 93 unsetenv("TERMCAP"); 94 95 /* 96 * ttype now contains a pointer to the type of the terminal. 97 * If the first character is '?', ask the user. 98 */ 99 if (ttype[0] == '?') { 100 if (ttype[1] != '\0') 101 ttype = askuser(ttype + 1); 102 else 103 ttype = askuser(NULL); 104 } 105 106 /* Find the termcap entry. If it doesn't exist, ask the user. */ 107 while ((rval = tgetent(tbuf, ttype)) == 0) { 108 warnx("terminal type %s is unknown", ttype); 109 ttype = askuser(NULL); 110 } 111 if (rval == -1) 112 errx(1, "termcap: %s", strerror(errno ? errno : ENOENT)); 113 *tcapbufp = tbuf; 114 return (ttype); 115 } 116 117 /* Prompt the user for a terminal type. */ 118 const char * 119 askuser(const char *dflt) 120 { 121 static char answer[256]; 122 char *p; 123 124 /* We can get recalled; if so, don't continue uselessly. */ 125 if (feof(stdin) || ferror(stdin)) { 126 (void)fprintf(stderr, "\n"); 127 exit(1); 128 } 129 for (;;) { 130 if (dflt) 131 (void)fprintf(stderr, "Terminal type? [%s] ", dflt); 132 else 133 (void)fprintf(stderr, "Terminal type? "); 134 (void)fflush(stderr); 135 136 if (fgets(answer, sizeof(answer), stdin) == NULL) { 137 if (dflt == NULL) { 138 (void)fprintf(stderr, "\n"); 139 exit(1); 140 } 141 return (dflt); 142 } 143 144 if ((p = strchr(answer, '\n'))) 145 *p = '\0'; 146 if (answer[0]) 147 return (answer); 148 if (dflt != NULL) 149 return (dflt); 150 } 151 } 152