xref: /netbsd-src/usr.bin/tset/term.c (revision be0c033d10de50f9dd7b8e5007ed807884682fdf)
1 /*	$NetBSD: term.c,v 1.17 2011/09/06 18:34:12 joerg 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. 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.
18  *
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
29  * SUCH DAMAGE.
30  */
31 
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)term.c	8.1 (Berkeley) 6/9/93";
36 #endif
37 __RCSID("$NetBSD: term.c,v 1.17 2011/09/06 18:34:12 joerg Exp $");
38 #endif /* not lint */
39 
40 #include <sys/types.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <term.h>
47 #include <ttyent.h>
48 #include <unistd.h>
49 #include "extern.h"
50 
51 static const	char *askuser(const char *);
52 
53 /*
54  * Figure out what kind of terminal we're dealing with, and then read in
55  * its termcap entry.
56  */
57 const char *
get_terminfo_entry(const char * userarg)58 get_terminfo_entry(const char *userarg)
59 {
60 	struct ttyent *t;
61 	int rval;
62 	char *p, *ttypath;
63 	const char *ttype;
64 
65 	if (userarg) {
66 		ttype = userarg;
67 		goto found;
68 	}
69 
70 	/* Try the environment. */
71 	if ((ttype = getenv("TERM")) != NULL)
72 		goto map;
73 
74 	/* Try ttyname(3); check for dialup or other mapping. */
75 	if ((ttypath = ttyname(STDERR_FILENO)) != NULL) {
76 		if ((p = strrchr(ttypath, '/')) != NULL)
77 			++p;
78 		else
79 			p = ttypath;
80 		if ((t = getttynam(p))) {
81 			ttype = t->ty_type;
82 			goto map;
83 		}
84 	}
85 
86 	/* If still undefined, use "unknown". */
87 	ttype = "unknown";
88 
89 map:	ttype = mapped(ttype);
90 
91 found:
92 
93 	/*
94 	 * ttype now contains a pointer to the type of the terminal.
95 	 * If the first character is '?', ask the user.
96 	 */
97 	if (ttype[0] == '?') {
98 		if (ttype[1] != '\0')
99 			ttype = askuser(ttype + 1);
100 		else
101 			ttype = askuser(NULL);
102 	}
103 
104 	while (setupterm(ttype, 0, &rval) == ERR) {
105 		switch (rval) {
106 		case 1:
107 			warnx("terminal type %s is hardcopy", ttype);
108 			break;
109 		case 0:
110 			warnx("terminal type %s is unknown", ttype);
111 			break;
112 		default:
113 			if (!errno)
114 				errno = ENOENT;
115 			err(1, NULL);
116 		}
117 		ttype = askuser(NULL);
118 	}
119 
120 	return (ttype);
121 }
122 
123 /* Prompt the user for a terminal type. */
124 static const char *
askuser(const char * dflt)125 askuser(const char *dflt)
126 {
127 	static char answer[256];
128 	char *p;
129 
130 	/* We can get recalled; if so, don't continue uselessly. */
131 	if (feof(stdin) || ferror(stdin)) {
132 		(void)fprintf(stderr, "\n");
133 		exit(1);
134 	}
135 	for (;;) {
136 		if (dflt)
137 			(void)fprintf(stderr, "Terminal type? [%s] ", dflt);
138 		else
139 			(void)fprintf(stderr, "Terminal type? ");
140 		(void)fflush(stderr);
141 
142 		if (fgets(answer, sizeof(answer), stdin) == NULL) {
143 			if (dflt == NULL) {
144 				(void)fprintf(stderr, "\n");
145 				exit(1);
146 			}
147 			return (dflt);
148 		}
149 
150 		if ((p = strchr(answer, '\n')) != NULL)
151 			*p = '\0';
152 		if (answer[0])
153 			return (answer);
154 		if (dflt != NULL)
155 			return (dflt);
156 	}
157 }
158