Home | History | Annotate | Line # | Download | only in tset
term.c revision 1.8
      1 /*	$NetBSD: term.c,v 1.8 1997/10/14 02:08:01 lukem 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.8 1997/10/14 02:08:01 lukem Exp $");
     42 #endif /* not lint */
     43 
     44 #include <sys/types.h>
     45 #include <errno.h>
     46 #include <stdio.h>
     47 #include <stdlib.h>
     48 #include <string.h>
     49 #include <termcap.h>
     50 #include <ttyent.h>
     51 #include <unistd.h>
     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")) != NULL)
     78 		goto map;
     79 
     80 	/* Try ttyname(3); check for dialup or other mapping. */
     81 	if ((ttypath = ttyname(STDERR_FILENO)) != NULL) {
     82 		if ((p = strrchr(ttypath, '/')) != NULL)
     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) {
    117 		(void)fprintf(stderr,
    118 		    "tset: terminal type %s is unknown\n", ttype);
    119 		ttype = askuser(NULL);
    120 	}
    121 	if (rval == -1)
    122 		err("termcap: %s", strerror(errno ? errno : ENOENT));
    123 	*tcapbufp = tbuf;
    124 	return (ttype);
    125 }
    126 
    127 /* Prompt the user for a terminal type. */
    128 char *
    129 askuser(dflt)
    130 	char *dflt;
    131 {
    132 	static char answer[256];
    133 	char *p;
    134 
    135 	/* We can get recalled; if so, don't continue uselessly. */
    136 	if (feof(stdin) || ferror(stdin)) {
    137 		(void)fprintf(stderr, "\n");
    138 		exit(1);
    139 	}
    140 	for (;;) {
    141 		if (dflt)
    142 			(void)fprintf(stderr, "Terminal type? [%s] ", dflt);
    143 		else
    144 			(void)fprintf(stderr, "Terminal type? ");
    145 		(void)fflush(stderr);
    146 
    147 		if (fgets(answer, sizeof(answer), stdin) == NULL) {
    148 			if (dflt == NULL) {
    149 				(void)fprintf(stderr, "\n");
    150 				exit(1);
    151 			}
    152 			return (dflt);
    153 		}
    154 
    155 		if ((p = strchr(answer, '\n')) != NULL)
    156 			*p = '\0';
    157 		if (answer[0])
    158 			return (answer);
    159 		if (dflt != NULL)
    160 			return (dflt);
    161 	}
    162 }
    163