term.c revision 1590
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 341590Srgrimes#ifndef lint 351590Srgrimesstatic char sccsid[] = "@(#)term.c 8.1 (Berkeley) 6/9/93"; 361590Srgrimes#endif /* not lint */ 371590Srgrimes 381590Srgrimes#include <sys/types.h> 391590Srgrimes#include <errno.h> 401590Srgrimes#include <ttyent.h> 411590Srgrimes#include <unistd.h> 421590Srgrimes#include <stdio.h> 431590Srgrimes#include <stdlib.h> 441590Srgrimes#include <string.h> 451590Srgrimes#include "extern.h" 461590Srgrimes 471590Srgrimeschar tbuf[1024]; /* Termcap entry. */ 481590Srgrimes 491590Srgrimeschar *askuser __P((char *)); 501590Srgrimeschar *ttys __P((char *)); 511590Srgrimes 521590Srgrimes/* 531590Srgrimes * Figure out what kind of terminal we're dealing with, and then read in 541590Srgrimes * its termcap entry. 551590Srgrimes */ 561590Srgrimeschar * 571590Srgrimesget_termcap_entry(userarg, tcapbufp) 581590Srgrimes char *userarg, **tcapbufp; 591590Srgrimes{ 601590Srgrimes struct ttyent *t; 611590Srgrimes int rval; 621590Srgrimes char *p, *ttype, *ttypath; 631590Srgrimes 641590Srgrimes if (userarg) { 651590Srgrimes ttype = userarg; 661590Srgrimes goto found; 671590Srgrimes } 681590Srgrimes 691590Srgrimes /* Try the environment. */ 701590Srgrimes if (ttype = getenv("TERM")) 711590Srgrimes goto map; 721590Srgrimes 731590Srgrimes /* Try ttyname(3); check for dialup or other mapping. */ 741590Srgrimes if (ttypath = ttyname(STDERR_FILENO)) { 751590Srgrimes if (p = rindex(ttypath, '/')) 761590Srgrimes ++p; 771590Srgrimes else 781590Srgrimes p = ttypath; 791590Srgrimes if ((t = getttynam(p))) { 801590Srgrimes ttype = t->ty_type; 811590Srgrimes goto map; 821590Srgrimes } 831590Srgrimes } 841590Srgrimes 851590Srgrimes /* If still undefined, use "unknown". */ 861590Srgrimes ttype = "unknown"; 871590Srgrimes 881590Srgrimesmap: ttype = mapped(ttype); 891590Srgrimes 901590Srgrimes /* 911590Srgrimes * If not a path, remove TERMCAP from the environment so we get a 921590Srgrimes * real entry from /etc/termcap. This prevents us from being fooled 931590Srgrimes * by out of date stuff in the environment. 941590Srgrimes */ 951590Srgrimesfound: if ((p = getenv("TERMCAP")) != NULL && *p != '/') 961590Srgrimes unsetenv("TERMCAP"); 971590Srgrimes 981590Srgrimes /* 991590Srgrimes * ttype now contains a pointer to the type of the terminal. 1001590Srgrimes * If the first character is '?', ask the user. 1011590Srgrimes */ 1021590Srgrimes if (ttype[0] == '?') 1031590Srgrimes if (ttype[1] != '\0') 1041590Srgrimes ttype = askuser(ttype + 1); 1051590Srgrimes else 1061590Srgrimes ttype = askuser(NULL); 1071590Srgrimes 1081590Srgrimes /* Find the termcap entry. If it doesn't exist, ask the user. */ 1091590Srgrimes while ((rval = tgetent(tbuf, ttype)) == 0) { 1101590Srgrimes (void)fprintf(stderr, 1111590Srgrimes "tset: terminal type %s is unknown\n", ttype); 1121590Srgrimes ttype = askuser(NULL); 1131590Srgrimes } 1141590Srgrimes if (rval == -1) 1151590Srgrimes err("termcap: %s", strerror(errno ? errno : ENOENT)); 1161590Srgrimes *tcapbufp = tbuf; 1171590Srgrimes return (ttype); 1181590Srgrimes} 1191590Srgrimes 1201590Srgrimes/* Prompt the user for a terminal type. */ 1211590Srgrimeschar * 1221590Srgrimesaskuser(dflt) 1231590Srgrimes char *dflt; 1241590Srgrimes{ 1251590Srgrimes static char answer[256]; 1261590Srgrimes char *p; 1271590Srgrimes 1281590Srgrimes /* We can get recalled; if so, don't continue uselessly. */ 1291590Srgrimes if (feof(stdin) || ferror(stdin)) { 1301590Srgrimes (void)fprintf(stderr, "\n"); 1311590Srgrimes exit(1); 1321590Srgrimes } 1331590Srgrimes for (;;) { 1341590Srgrimes if (dflt) 1351590Srgrimes (void)fprintf(stderr, "Terminal type? [%s] ", dflt); 1361590Srgrimes else 1371590Srgrimes (void)fprintf(stderr, "Terminal type? "); 1381590Srgrimes (void)fflush(stderr); 1391590Srgrimes 1401590Srgrimes if (fgets(answer, sizeof(answer), stdin) == NULL) { 1411590Srgrimes if (dflt == NULL) { 1421590Srgrimes (void)fprintf(stderr, "\n"); 1431590Srgrimes exit(1); 1441590Srgrimes } 1451590Srgrimes return (dflt); 1461590Srgrimes } 1471590Srgrimes 1481590Srgrimes if (p = index(answer, '\n')) 1491590Srgrimes *p = '\0'; 1501590Srgrimes if (answer[0]) 1511590Srgrimes return (answer); 1521590Srgrimes if (dflt != NULL) 1531590Srgrimes return (dflt); 1541590Srgrimes } 1551590Srgrimes} 156