Home | History | Annotate | Line # | Download | only in tset
term.c revision 1.16
      1 /*	$NetBSD: term.c,v 1.16 2010/02/03 15:34:46 roy 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.16 2010/02/03 15:34:46 roy 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 char    *tbuf;      		/* Termcap entry. */
     52 
     53 const	char *askuser __P((const char *));
     54 char	*ttys __P((char *));
     55 
     56 /*
     57  * Figure out what kind of terminal we're dealing with, and then read in
     58  * its termcap entry.
     59  */
     60 const char *
     61 get_terminfo_entry(const char *userarg)
     62 {
     63 	struct ttyent *t;
     64 	int rval;
     65 	char *p, *ttypath;
     66 	const char *ttype;
     67 
     68 	if (userarg) {
     69 		ttype = userarg;
     70 		goto found;
     71 	}
     72 
     73 	/* Try the environment. */
     74 	if ((ttype = getenv("TERM")) != NULL)
     75 		goto map;
     76 
     77 	/* Try ttyname(3); check for dialup or other mapping. */
     78 	if ((ttypath = ttyname(STDERR_FILENO)) != NULL) {
     79 		if ((p = strrchr(ttypath, '/')) != NULL)
     80 			++p;
     81 		else
     82 			p = ttypath;
     83 		if ((t = getttynam(p))) {
     84 			ttype = t->ty_type;
     85 			goto map;
     86 		}
     87 	}
     88 
     89 	/* If still undefined, use "unknown". */
     90 	ttype = "unknown";
     91 
     92 map:	ttype = mapped(ttype);
     93 
     94 found:
     95 
     96 	/*
     97 	 * ttype now contains a pointer to the type of the terminal.
     98 	 * If the first character is '?', ask the user.
     99 	 */
    100 	if (ttype[0] == '?') {
    101 		if (ttype[1] != '\0')
    102 			ttype = askuser(ttype + 1);
    103 		else
    104 			ttype = askuser(NULL);
    105 	}
    106 
    107 	while (setupterm(ttype, 0, &rval) == ERR) {
    108 		switch (rval) {
    109 		case 1:
    110 			warnx("terminal type %s is hardcopy", ttype);
    111 			break;
    112 		case 0:
    113 			warnx("terminal type %s is unknown", ttype);
    114 			break;
    115 		default:
    116 			if (!errno)
    117 				errno = ENOENT;
    118 			err(1, NULL);
    119 		}
    120 		ttype = askuser(NULL);
    121 	}
    122 
    123 	return (ttype);
    124 }
    125 
    126 /* Prompt the user for a terminal type. */
    127 const char *
    128 askuser(dflt)
    129 	const char *dflt;
    130 {
    131 	static char answer[256];
    132 	char *p;
    133 
    134 	/* We can get recalled; if so, don't continue uselessly. */
    135 	if (feof(stdin) || ferror(stdin)) {
    136 		(void)fprintf(stderr, "\n");
    137 		exit(1);
    138 	}
    139 	for (;;) {
    140 		if (dflt)
    141 			(void)fprintf(stderr, "Terminal type? [%s] ", dflt);
    142 		else
    143 			(void)fprintf(stderr, "Terminal type? ");
    144 		(void)fflush(stderr);
    145 
    146 		if (fgets(answer, sizeof(answer), stdin) == NULL) {
    147 			if (dflt == NULL) {
    148 				(void)fprintf(stderr, "\n");
    149 				exit(1);
    150 			}
    151 			return (dflt);
    152 		}
    153 
    154 		if ((p = strchr(answer, '\n')) != NULL)
    155 			*p = '\0';
    156 		if (answer[0])
    157 			return (answer);
    158 		if (dflt != NULL)
    159 			return (dflt);
    160 	}
    161 }
    162