2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
32 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
37 static const char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93";
40 #include <sys/types.h>
51 static char tbuf[1024]; /* Termcap entry. */
53 const char *askuser(const char *);
57 * Figure out what kind of terminal we're dealing with, and then read in
61 get_termcap_entry(char *userarg, char **tcapbufp)
73 /* Try the environment. */
74 if ((ttype = getenv("TERM")))
77 /* Try ttyname(3); check for dialup or other mapping. */
78 if ((ttypath = ttyname(STDERR_FILENO))) {
79 if ((p = strrchr(ttypath, '/')))
83 if ((t = getttynam(p))) {
89 /* If still undefined, use "unknown". */
92 map: ttype = mapped(ttype);
95 * If not a path, remove TERMCAP from the environment so we get a
96 * real entry from /etc/termcap. This prevents us from being fooled
97 * by out of date stuff in the environment.
99 found: if ((p = getenv("TERMCAP")) != NULL && *p != '/')
103 * ttype now contains a pointer to the type of the terminal.
104 * If the first character is '?', ask the user.
106 if (ttype[0] == '?') {
107 if (ttype[1] != '\0')
108 ttype = askuser(ttype + 1);
110 ttype = askuser(NULL);
113 /* Find the termcap entry. If it doesn't exist, ask the user. */
114 while ((rval = tgetent(tbuf, ttype)) == 0) {
115 warnx("terminal type %s is unknown", ttype);
116 ttype = askuser(NULL);
119 errx(1, "termcap: %s", strerror(errno ? errno : ENOENT));
124 /* Prompt the user for a terminal type. */
126 askuser(const char *dflt)
128 static char answer[256];
131 /* We can get recalled; if so, don't continue uselessly. */
132 if (feof(stdin) || ferror(stdin)) {
133 (void)fprintf(stderr, "\n");
138 (void)fprintf(stderr, "Terminal type? [%s] ", dflt);
140 (void)fprintf(stderr, "Terminal type? ");
141 (void)fflush(stderr);
143 if (fgets(answer, sizeof(answer), stdin) == NULL) {
145 (void)fprintf(stderr, "\n");
151 if ((p = strchr(answer, '\n')))