2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 4. 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.
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
30 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
35 static const char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93";
38 #include <sys/types.h>
49 char tbuf[1024]; /* Termcap entry. */
51 const char *askuser(const char *);
55 * Figure out what kind of terminal we're dealing with, and then read in
59 get_termcap_entry(char *userarg, char **tcapbufp)
71 /* Try the environment. */
72 if ((ttype = getenv("TERM")))
75 /* Try ttyname(3); check for dialup or other mapping. */
76 if ((ttypath = ttyname(STDERR_FILENO))) {
77 if ((p = rindex(ttypath, '/')))
81 if ((t = getttynam(p))) {
87 /* If still undefined, use "unknown". */
90 map: ttype = mapped(ttype);
93 * If not a path, remove TERMCAP from the environment so we get a
94 * real entry from /etc/termcap. This prevents us from being fooled
95 * by out of date stuff in the environment.
97 found: if ((p = getenv("TERMCAP")) != NULL && *p != '/')
101 * ttype now contains a pointer to the type of the terminal.
102 * If the first character is '?', ask the user.
104 if (ttype[0] == '?') {
105 if (ttype[1] != '\0')
106 ttype = askuser(ttype + 1);
108 ttype = askuser(NULL);
111 /* Find the termcap entry. If it doesn't exist, ask the user. */
112 while ((rval = tgetent(tbuf, ttype)) == 0) {
113 warnx("terminal type %s is unknown", ttype);
114 ttype = askuser(NULL);
117 errx(1, "termcap: %s", strerror(errno ? errno : ENOENT));
122 /* Prompt the user for a terminal type. */
124 askuser(const char *dflt)
126 static char answer[256];
129 /* We can get recalled; if so, don't continue uselessly. */
130 if (feof(stdin) || ferror(stdin)) {
131 (void)fprintf(stderr, "\n");
136 (void)fprintf(stderr, "Terminal type? [%s] ", dflt);
138 (void)fprintf(stderr, "Terminal type? ");
139 (void)fflush(stderr);
141 if (fgets(answer, sizeof(answer), stdin) == NULL) {
143 (void)fprintf(stderr, "\n");
149 if ((p = index(answer, '\n')))