term.c revision 1.12 1 /* $NetBSD: term.c,v 1.12 1999/11/09 15:06:37 drochner 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.12 1999/11/09 15:06:37 drochner 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 const char *askuser __P((const 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 const char *
65 get_termcap_entry(userarg, tcapbufp)
66 const char *userarg;
67 char **tcapbufp;
68 {
69 struct ttyent *t;
70 int rval;
71 char *p, *ttypath;
72 const char *ttype;
73
74 if (userarg) {
75 ttype = userarg;
76 goto found;
77 }
78
79 /* Try the environment. */
80 if ((ttype = getenv("TERM")) != NULL)
81 goto map;
82
83 /* Try ttyname(3); check for dialup or other mapping. */
84 if ((ttypath = ttyname(STDERR_FILENO)) != NULL) {
85 if ((p = strrchr(ttypath, '/')) != NULL)
86 ++p;
87 else
88 p = ttypath;
89 if ((t = getttynam(p))) {
90 ttype = t->ty_type;
91 goto map;
92 }
93 }
94
95 /* If still undefined, use "unknown". */
96 ttype = "unknown";
97
98 map: ttype = mapped(ttype);
99
100 /*
101 * If not a path, remove TERMCAP from the environment so we get a
102 * real entry from /etc/termcap. This prevents us from being fooled
103 * by out of date stuff in the environment.
104 */
105 found: if ((p = getenv("TERMCAP")) != NULL && *p != '/')
106 unsetenv("TERMCAP");
107
108 /*
109 * ttype now contains a pointer to the type of the terminal.
110 * If the first character is '?', ask the user.
111 */
112 if (ttype[0] == '?') {
113 if (ttype[1] != '\0')
114 ttype = askuser(ttype + 1);
115 else
116 ttype = askuser(NULL);
117 }
118 /* Find the termcap entry. If it doesn't exist, ask the user. */
119 while ((rval = tgetent(tbuf, ttype)) == 0) {
120 warnx("terminal type %s is unknown", ttype);
121 ttype = askuser(NULL);
122 }
123 if (rval == -1) {
124 if (!errno)
125 errno = ENOENT;
126 err(1, NULL);
127 }
128 *tcapbufp = tbuf;
129 return (ttype);
130 }
131
132 /* Prompt the user for a terminal type. */
133 const char *
134 askuser(dflt)
135 const char *dflt;
136 {
137 static char answer[256];
138 char *p;
139
140 /* We can get recalled; if so, don't continue uselessly. */
141 if (feof(stdin) || ferror(stdin)) {
142 (void)fprintf(stderr, "\n");
143 exit(1);
144 }
145 for (;;) {
146 if (dflt)
147 (void)fprintf(stderr, "Terminal type? [%s] ", dflt);
148 else
149 (void)fprintf(stderr, "Terminal type? ");
150 (void)fflush(stderr);
151
152 if (fgets(answer, sizeof(answer), stdin) == NULL) {
153 if (dflt == NULL) {
154 (void)fprintf(stderr, "\n");
155 exit(1);
156 }
157 return (dflt);
158 }
159
160 if ((p = strchr(answer, '\n')) != NULL)
161 *p = '\0';
162 if (answer[0])
163 return (answer);
164 if (dflt != NULL)
165 return (dflt);
166 }
167 }
168