term.c revision 241737
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1991, 1993 31590Srgrimes * The Regents of the University of California. All rights reserved. 41590Srgrimes * 51590Srgrimes * Redistribution and use in source and binary forms, with or without 61590Srgrimes * modification, are permitted provided that the following conditions 71590Srgrimes * are met: 81590Srgrimes * 1. Redistributions of source code must retain the above copyright 91590Srgrimes * notice, this list of conditions and the following disclaimer. 101590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 111590Srgrimes * notice, this list of conditions and the following disclaimer in the 121590Srgrimes * documentation and/or other materials provided with the distribution. 131590Srgrimes * 4. Neither the name of the University nor the names of its contributors 141590Srgrimes * may be used to endorse or promote products derived from this software 151590Srgrimes * without specific prior written permission. 161590Srgrimes * 171590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 181590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 191590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 201590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 211590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 221590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 231590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 241590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 251590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 261590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 271590Srgrimes * SUCH DAMAGE. 281590Srgrimes */ 291590Srgrimes 3087701Smarkm#include <sys/cdefs.h> 3187701Smarkm 3287701Smarkm__FBSDID("$FreeBSD: head/usr.bin/tset/term.c 241737 2012-10-19 14:49:42Z ed $"); 3387701Smarkm 341590Srgrimes#ifndef lint 3587701Smarkmstatic const char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93"; 3628370Scharnier#endif 371590Srgrimes 381590Srgrimes#include <sys/types.h> 3928370Scharnier#include <err.h> 401590Srgrimes#include <errno.h> 411590Srgrimes#include <stdio.h> 421590Srgrimes#include <stdlib.h> 431590Srgrimes#include <string.h> 44200419Sdelphij#include <termcap.h> 4528370Scharnier#include <unistd.h> 4628370Scharnier#include <ttyent.h> 471590Srgrimes#include "extern.h" 481590Srgrimes 49241737Sedstatic char tbuf[1024]; /* Termcap entry. */ 501590Srgrimes 5192922Simpconst char *askuser(const char *); 5292922Simpchar *ttys(char *); 531590Srgrimes 541590Srgrimes/* 551590Srgrimes * Figure out what kind of terminal we're dealing with, and then read in 561590Srgrimes * its termcap entry. 571590Srgrimes */ 5887701Smarkmconst char * 59200419Sdelphijget_termcap_entry(char *userarg, char **tcapbufp) 601590Srgrimes{ 611590Srgrimes struct ttyent *t; 621590Srgrimes int rval; 6387701Smarkm char *p, *ttypath; 6487701Smarkm const char *ttype; 651590Srgrimes 661590Srgrimes if (userarg) { 671590Srgrimes ttype = userarg; 681590Srgrimes goto found; 691590Srgrimes } 701590Srgrimes 711590Srgrimes /* Try the environment. */ 7228370Scharnier if ((ttype = getenv("TERM"))) 731590Srgrimes goto map; 741590Srgrimes 751590Srgrimes /* Try ttyname(3); check for dialup or other mapping. */ 7628370Scharnier if ((ttypath = ttyname(STDERR_FILENO))) { 77229403Sed if ((p = strrchr(ttypath, '/'))) 781590Srgrimes ++p; 791590Srgrimes else 801590Srgrimes p = ttypath; 811590Srgrimes if ((t = getttynam(p))) { 821590Srgrimes ttype = t->ty_type; 831590Srgrimes goto map; 841590Srgrimes } 851590Srgrimes } 861590Srgrimes 871590Srgrimes /* If still undefined, use "unknown". */ 881590Srgrimes ttype = "unknown"; 891590Srgrimes 901590Srgrimesmap: ttype = mapped(ttype); 911590Srgrimes 921590Srgrimes /* 931590Srgrimes * If not a path, remove TERMCAP from the environment so we get a 941590Srgrimes * real entry from /etc/termcap. This prevents us from being fooled 951590Srgrimes * by out of date stuff in the environment. 961590Srgrimes */ 971590Srgrimesfound: if ((p = getenv("TERMCAP")) != NULL && *p != '/') 981590Srgrimes unsetenv("TERMCAP"); 991590Srgrimes 1001590Srgrimes /* 1011590Srgrimes * ttype now contains a pointer to the type of the terminal. 1021590Srgrimes * If the first character is '?', ask the user. 1031590Srgrimes */ 10448566Sbillf if (ttype[0] == '?') { 1051590Srgrimes if (ttype[1] != '\0') 1061590Srgrimes ttype = askuser(ttype + 1); 1071590Srgrimes else 1081590Srgrimes ttype = askuser(NULL); 10948566Sbillf } 1101590Srgrimes 1111590Srgrimes /* Find the termcap entry. If it doesn't exist, ask the user. */ 1121590Srgrimes while ((rval = tgetent(tbuf, ttype)) == 0) { 11328370Scharnier warnx("terminal type %s is unknown", ttype); 1141590Srgrimes ttype = askuser(NULL); 1151590Srgrimes } 1161590Srgrimes if (rval == -1) 11728370Scharnier errx(1, "termcap: %s", strerror(errno ? errno : ENOENT)); 1181590Srgrimes *tcapbufp = tbuf; 1191590Srgrimes return (ttype); 1201590Srgrimes} 1211590Srgrimes 1221590Srgrimes/* Prompt the user for a terminal type. */ 12387701Smarkmconst char * 124200419Sdelphijaskuser(const char *dflt) 1251590Srgrimes{ 1261590Srgrimes static char answer[256]; 1271590Srgrimes char *p; 1281590Srgrimes 1291590Srgrimes /* We can get recalled; if so, don't continue uselessly. */ 1301590Srgrimes if (feof(stdin) || ferror(stdin)) { 1311590Srgrimes (void)fprintf(stderr, "\n"); 1321590Srgrimes exit(1); 1331590Srgrimes } 1341590Srgrimes for (;;) { 1351590Srgrimes if (dflt) 1361590Srgrimes (void)fprintf(stderr, "Terminal type? [%s] ", dflt); 1371590Srgrimes else 1381590Srgrimes (void)fprintf(stderr, "Terminal type? "); 1391590Srgrimes (void)fflush(stderr); 1401590Srgrimes 1411590Srgrimes if (fgets(answer, sizeof(answer), stdin) == NULL) { 1421590Srgrimes if (dflt == NULL) { 1431590Srgrimes (void)fprintf(stderr, "\n"); 1441590Srgrimes exit(1); 1451590Srgrimes } 1461590Srgrimes return (dflt); 1471590Srgrimes } 1481590Srgrimes 149229403Sed if ((p = strchr(answer, '\n'))) 1501590Srgrimes *p = '\0'; 1511590Srgrimes if (answer[0]) 1521590Srgrimes return (answer); 1531590Srgrimes if (dflt != NULL) 1541590Srgrimes return (dflt); 1551590Srgrimes } 1561590Srgrimes} 157