Annotation of src/usr.bin/tset/term.c, Revision 1.4
1.4 ! mickey 1: /* $OpenBSD: term.c,v 1.3 1997/01/17 07:13:44 millert Exp $ */
1.1 deraadt 2: /* $NetBSD: term.c,v 1.6 1994/12/07 05:08:12 jtc Exp $ */
3:
4: /*-
5: * Copyright (c) 1991, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
16: * 3. All advertising materials mentioning features or use of this software
17: * must display the following acknowledgement:
18: * This product includes software developed by the University of
19: * California, Berkeley and its contributors.
20: * 4. Neither the name of the University nor the names of its contributors
21: * may be used to endorse or promote products derived from this software
22: * without specific prior written permission.
23: *
24: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34: * SUCH DAMAGE.
35: */
36:
37: #ifndef lint
38: #if 0
39: static char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93";
40: #endif
1.4 ! mickey 41: static char rcsid[] = "$OpenBSD: term.c,v 1.3 1997/01/17 07:13:44 millert Exp $";
1.1 deraadt 42: #endif /* not lint */
43:
44: #include <sys/types.h>
45: #include <errno.h>
46: #include <ttyent.h>
47: #include <unistd.h>
48: #include <stdio.h>
49: #include <stdlib.h>
50: #include <string.h>
1.4 ! mickey 51: #include <err.h>
1.1 deraadt 52: #include "extern.h"
53:
54: char tbuf[1024]; /* Termcap entry. */
55:
56: char *askuser __P((char *));
57: char *ttys __P((char *));
58:
59: /*
60: * Figure out what kind of terminal we're dealing with, and then read in
61: * its termcap entry.
62: */
63: char *
64: get_termcap_entry(userarg, tcapbufp)
65: char *userarg, **tcapbufp;
66: {
67: struct ttyent *t;
68: int rval;
69: char *p, *ttype, *ttypath;
70:
71: if (userarg) {
72: ttype = userarg;
73: goto found;
74: }
75:
76: /* Try the environment. */
77: if (ttype = getenv("TERM"))
78: goto map;
79:
80: /* Try ttyname(3); check for dialup or other mapping. */
81: if (ttypath = ttyname(STDERR_FILENO)) {
1.3 millert 82: if (p = strrchr(ttypath, '/'))
1.1 deraadt 83: ++p;
84: else
85: p = ttypath;
86: if ((t = getttynam(p))) {
87: ttype = t->ty_type;
88: goto map;
89: }
90: }
91:
92: /* If still undefined, use "unknown". */
93: ttype = "unknown";
94:
95: map: ttype = mapped(ttype);
96:
97: /*
98: * If not a path, remove TERMCAP from the environment so we get a
99: * real entry from /etc/termcap. This prevents us from being fooled
100: * by out of date stuff in the environment.
101: */
102: found: if ((p = getenv("TERMCAP")) != NULL && *p != '/')
103: unsetenv("TERMCAP");
104:
105: /*
106: * ttype now contains a pointer to the type of the terminal.
107: * If the first character is '?', ask the user.
108: */
109: if (ttype[0] == '?')
110: if (ttype[1] != '\0')
111: ttype = askuser(ttype + 1);
112: else
113: ttype = askuser(NULL);
114:
115: /* Find the termcap entry. If it doesn't exist, ask the user. */
116: while ((rval = tgetent(tbuf, ttype)) == 0) {
1.4 ! mickey 117: warnx("terminal type %s is unknown", ttype);
1.1 deraadt 118: ttype = askuser(NULL);
119: }
120: if (rval == -1)
1.4 ! mickey 121: warnx("termcap: %s", strerror(errno ? errno : ENOENT));
1.1 deraadt 122: *tcapbufp = tbuf;
123: return (ttype);
124: }
125:
126: /* Prompt the user for a terminal type. */
127: char *
128: askuser(dflt)
129: char *dflt;
130: {
131: static char answer[256];
132: char *p;
133:
134: /* We can get recalled; if so, don't continue uselessly. */
1.4 ! mickey 135: if (feof(stdin) || ferror(stdin))
1.1 deraadt 136: exit(1);
137: for (;;) {
138: if (dflt)
139: (void)fprintf(stderr, "Terminal type? [%s] ", dflt);
140: else
141: (void)fprintf(stderr, "Terminal type? ");
142: (void)fflush(stderr);
143:
144: if (fgets(answer, sizeof(answer), stdin) == NULL) {
145: if (dflt == NULL) {
146: (void)fprintf(stderr, "\n");
147: exit(1);
148: }
149: return (dflt);
150: }
151:
1.3 millert 152: if (p = strchr(answer, '\n'))
1.1 deraadt 153: *p = '\0';
154: if (answer[0])
155: return (answer);
156: if (dflt != NULL)
157: return (dflt);
158: }
159: }