Home | History | Annotate | Line # | Download | only in tset
term.c revision 1.14
      1 /*	$NetBSD: term.c,v 1.14 2000/05/31 05:50:06 blymn 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.14 2000/05/31 05:50:06 blymn 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;      		/* 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, extended)
     66 	const char *userarg;
     67 	char **tcapbufp;
     68 	int extended;
     69 {
     70 	struct ttyent *t;
     71 	int rval;
     72 	char *p, *ttypath;
     73 	const char *ttype;
     74 	char zz[1024], *zz_ptr;
     75 	char *ext_tc, *newptr;
     76 
     77 	if (userarg) {
     78 		ttype = userarg;
     79 		goto found;
     80 	}
     81 
     82 	/* Try the environment. */
     83 	if ((ttype = getenv("TERM")) != NULL)
     84 		goto map;
     85 
     86 	/* Try ttyname(3); check for dialup or other mapping. */
     87 	if ((ttypath = ttyname(STDERR_FILENO)) != NULL) {
     88 		if ((p = strrchr(ttypath, '/')) != NULL)
     89 			++p;
     90 		else
     91 			p = ttypath;
     92 		if ((t = getttynam(p))) {
     93 			ttype = t->ty_type;
     94 			goto map;
     95 		}
     96 	}
     97 
     98 	/* If still undefined, use "unknown". */
     99 	ttype = "unknown";
    100 
    101 map:	ttype = mapped(ttype);
    102 
    103 	/*
    104 	 * If not a path, remove TERMCAP from the environment so we get a
    105 	 * real entry from /etc/termcap.  This prevents us from being fooled
    106 	 * by out of date stuff in the environment.
    107 	 */
    108 found:	if ((p = getenv("TERMCAP")) != NULL && *p != '/')
    109 		unsetenv("TERMCAP");
    110 
    111 	/*
    112 	 * ttype now contains a pointer to the type of the terminal.
    113 	 * If the first character is '?', ask the user.
    114 	 */
    115 	if (ttype[0] == '?') {
    116 		if (ttype[1] != '\0')
    117 			ttype = askuser(ttype + 1);
    118 		else
    119 			ttype = askuser(NULL);
    120 	}
    121 	/* Find the termcap entry.  If it doesn't exist, ask the user. */
    122 	if ((tbuf = (char *) malloc(1024)) == NULL) {
    123 		fprintf(stderr, "Could not malloc termcap buffer\n");
    124 		exit(1);
    125 	}
    126 
    127 	while ((rval = tgetent(tbuf, ttype)) == 0) {
    128 		warnx("terminal type %s is unknown", ttype);
    129 		ttype = askuser(NULL);
    130 	}
    131 	if (rval == -1) {
    132 		if (!errno)
    133 			errno = ENOENT;
    134 		err(1, NULL);
    135 	}
    136 
    137 	  /* check if we get a truncated termcap entry, fish back the full
    138 	   * one if need be and the user has asked for it.
    139 	   */
    140 	zz_ptr = zz;
    141 	if ((extended == 1) && (tgetstr("ZZ", &zz_ptr) != NULL)) {
    142 			  /* it was, fish back the full termcap */
    143 		sscanf(zz, "%p", &ext_tc);
    144 		if ((newptr = (char *) realloc(tbuf, strlen(ext_tc) + 1))
    145 		    == NULL) {
    146 			fprintf(stderr,
    147 				"reallocate of termcap falied\n");
    148 			exit (1);
    149 		}
    150 
    151 		strcpy(newptr, ext_tc);
    152 		tbuf = newptr;
    153 	}
    154 
    155 	*tcapbufp = tbuf;
    156 	return (ttype);
    157 }
    158 
    159 /* Prompt the user for a terminal type. */
    160 const char *
    161 askuser(dflt)
    162 	const char *dflt;
    163 {
    164 	static char answer[256];
    165 	char *p;
    166 
    167 	/* We can get recalled; if so, don't continue uselessly. */
    168 	if (feof(stdin) || ferror(stdin)) {
    169 		(void)fprintf(stderr, "\n");
    170 		exit(1);
    171 	}
    172 	for (;;) {
    173 		if (dflt)
    174 			(void)fprintf(stderr, "Terminal type? [%s] ", dflt);
    175 		else
    176 			(void)fprintf(stderr, "Terminal type? ");
    177 		(void)fflush(stderr);
    178 
    179 		if (fgets(answer, sizeof(answer), stdin) == NULL) {
    180 			if (dflt == NULL) {
    181 				(void)fprintf(stderr, "\n");
    182 				exit(1);
    183 			}
    184 			return (dflt);
    185 		}
    186 
    187 		if ((p = strchr(answer, '\n')) != NULL)
    188 			*p = '\0';
    189 		if (answer[0])
    190 			return (answer);
    191 		if (dflt != NULL)
    192 			return (dflt);
    193 	}
    194 }
    195