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