term.c revision 200419
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 * 3. All advertising materials mentioning features or use of this software 141590Srgrimes * must display the following acknowledgement: 151590Srgrimes * This product includes software developed by the University of 161590Srgrimes * California, Berkeley and its contributors. 171590Srgrimes * 4. Neither the name of the University nor the names of its contributors 181590Srgrimes * may be used to endorse or promote products derived from this software 191590Srgrimes * without specific prior written permission. 201590Srgrimes * 211590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 221590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 231590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 241590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 251590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 261590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 271590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 281590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 291590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 301590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 311590Srgrimes * SUCH DAMAGE. 321590Srgrimes */ 331590Srgrimes 3487701Smarkm#include <sys/cdefs.h> 3587701Smarkm 3687701Smarkm__FBSDID("$FreeBSD: head/usr.bin/tset/term.c 200419 2009-12-11 23:30:22Z delphij $"); 3787701Smarkm 381590Srgrimes#ifndef lint 3987701Smarkmstatic const char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93"; 4028370Scharnier#endif 411590Srgrimes 421590Srgrimes#include <sys/types.h> 4328370Scharnier#include <err.h> 441590Srgrimes#include <errno.h> 451590Srgrimes#include <stdio.h> 461590Srgrimes#include <stdlib.h> 471590Srgrimes#include <string.h> 48200419Sdelphij#include <termcap.h> 4928370Scharnier#include <unistd.h> 5028370Scharnier#include <ttyent.h> 511590Srgrimes#include "extern.h" 521590Srgrimes 531590Srgrimeschar tbuf[1024]; /* Termcap entry. */ 541590Srgrimes 5592922Simpconst char *askuser(const char *); 5692922Simpchar *ttys(char *); 571590Srgrimes 581590Srgrimes/* 591590Srgrimes * Figure out what kind of terminal we're dealing with, and then read in 601590Srgrimes * its termcap entry. 611590Srgrimes */ 6287701Smarkmconst char * 63200419Sdelphijget_termcap_entry(char *userarg, char **tcapbufp) 641590Srgrimes{ 651590Srgrimes struct ttyent *t; 661590Srgrimes int rval; 6787701Smarkm char *p, *ttypath; 6887701Smarkm const char *ttype; 691590Srgrimes 701590Srgrimes if (userarg) { 711590Srgrimes ttype = userarg; 721590Srgrimes goto found; 731590Srgrimes } 741590Srgrimes 751590Srgrimes /* Try the environment. */ 7628370Scharnier if ((ttype = getenv("TERM"))) 771590Srgrimes goto map; 781590Srgrimes 791590Srgrimes /* Try ttyname(3); check for dialup or other mapping. */ 8028370Scharnier if ((ttypath = ttyname(STDERR_FILENO))) { 8128370Scharnier if ((p = rindex(ttypath, '/'))) 821590Srgrimes ++p; 831590Srgrimes else 841590Srgrimes p = ttypath; 851590Srgrimes if ((t = getttynam(p))) { 861590Srgrimes ttype = t->ty_type; 871590Srgrimes goto map; 881590Srgrimes } 891590Srgrimes } 901590Srgrimes 911590Srgrimes /* If still undefined, use "unknown". */ 921590Srgrimes ttype = "unknown"; 931590Srgrimes 941590Srgrimesmap: ttype = mapped(ttype); 951590Srgrimes 961590Srgrimes /* 971590Srgrimes * If not a path, remove TERMCAP from the environment so we get a 981590Srgrimes * real entry from /etc/termcap. This prevents us from being fooled 991590Srgrimes * by out of date stuff in the environment. 1001590Srgrimes */ 1011590Srgrimesfound: if ((p = getenv("TERMCAP")) != NULL && *p != '/') 1021590Srgrimes unsetenv("TERMCAP"); 1031590Srgrimes 1041590Srgrimes /* 1051590Srgrimes * ttype now contains a pointer to the type of the terminal. 1061590Srgrimes * If the first character is '?', ask the user. 1071590Srgrimes */ 10848566Sbillf if (ttype[0] == '?') { 1091590Srgrimes if (ttype[1] != '\0') 1101590Srgrimes ttype = askuser(ttype + 1); 1111590Srgrimes else 1121590Srgrimes ttype = askuser(NULL); 11348566Sbillf } 1141590Srgrimes 1151590Srgrimes /* Find the termcap entry. If it doesn't exist, ask the user. */ 1161590Srgrimes while ((rval = tgetent(tbuf, ttype)) == 0) { 11728370Scharnier warnx("terminal type %s is unknown", ttype); 1181590Srgrimes ttype = askuser(NULL); 1191590Srgrimes } 1201590Srgrimes if (rval == -1) 12128370Scharnier errx(1, "termcap: %s", strerror(errno ? errno : ENOENT)); 1221590Srgrimes *tcapbufp = tbuf; 1231590Srgrimes return (ttype); 1241590Srgrimes} 1251590Srgrimes 1261590Srgrimes/* Prompt the user for a terminal type. */ 12787701Smarkmconst char * 128200419Sdelphijaskuser(const char *dflt) 1291590Srgrimes{ 1301590Srgrimes static char answer[256]; 1311590Srgrimes char *p; 1321590Srgrimes 1331590Srgrimes /* We can get recalled; if so, don't continue uselessly. */ 1341590Srgrimes if (feof(stdin) || ferror(stdin)) { 1351590Srgrimes (void)fprintf(stderr, "\n"); 1361590Srgrimes exit(1); 1371590Srgrimes } 1381590Srgrimes for (;;) { 1391590Srgrimes if (dflt) 1401590Srgrimes (void)fprintf(stderr, "Terminal type? [%s] ", dflt); 1411590Srgrimes else 1421590Srgrimes (void)fprintf(stderr, "Terminal type? "); 1431590Srgrimes (void)fflush(stderr); 1441590Srgrimes 1451590Srgrimes if (fgets(answer, sizeof(answer), stdin) == NULL) { 1461590Srgrimes if (dflt == NULL) { 1471590Srgrimes (void)fprintf(stderr, "\n"); 1481590Srgrimes exit(1); 1491590Srgrimes } 1501590Srgrimes return (dflt); 1511590Srgrimes } 1521590Srgrimes 15328370Scharnier if ((p = index(answer, '\n'))) 1541590Srgrimes *p = '\0'; 1551590Srgrimes if (answer[0]) 1561590Srgrimes return (answer); 1571590Srgrimes if (dflt != NULL) 1581590Srgrimes return (dflt); 1591590Srgrimes } 1601590Srgrimes} 161