xref: /freebsd/contrib/ncurses/ncurses/tinfo/read_termcap.c (revision 39f2269fcb4873fd97d70af944ec49f4230fadea)
10e3d5408SPeter Wemm /****************************************************************************
239f2269fSPeter Wemm  * Copyright (c) 1998,1999,2000,2001 Free Software Foundation, Inc.         *
30e3d5408SPeter Wemm  *                                                                          *
40e3d5408SPeter Wemm  * Permission is hereby granted, free of charge, to any person obtaining a  *
50e3d5408SPeter Wemm  * copy of this software and associated documentation files (the            *
60e3d5408SPeter Wemm  * "Software"), to deal in the Software without restriction, including      *
70e3d5408SPeter Wemm  * without limitation the rights to use, copy, modify, merge, publish,      *
80e3d5408SPeter Wemm  * distribute, distribute with modifications, sublicense, and/or sell       *
90e3d5408SPeter Wemm  * copies of the Software, and to permit persons to whom the Software is    *
100e3d5408SPeter Wemm  * furnished to do so, subject to the following conditions:                 *
110e3d5408SPeter Wemm  *                                                                          *
120e3d5408SPeter Wemm  * The above copyright notice and this permission notice shall be included  *
130e3d5408SPeter Wemm  * in all copies or substantial portions of the Software.                   *
140e3d5408SPeter Wemm  *                                                                          *
150e3d5408SPeter Wemm  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
160e3d5408SPeter Wemm  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
170e3d5408SPeter Wemm  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
180e3d5408SPeter Wemm  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
190e3d5408SPeter Wemm  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
200e3d5408SPeter Wemm  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
210e3d5408SPeter Wemm  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
220e3d5408SPeter Wemm  *                                                                          *
230e3d5408SPeter Wemm  * Except as contained in this notice, the name(s) of the above copyright   *
240e3d5408SPeter Wemm  * holders shall not be used in advertising or otherwise to promote the     *
250e3d5408SPeter Wemm  * sale, use or other dealings in this Software without prior written       *
260e3d5408SPeter Wemm  * authorization.                                                           *
270e3d5408SPeter Wemm  ****************************************************************************/
280e3d5408SPeter Wemm 
290e3d5408SPeter Wemm /****************************************************************************
300e3d5408SPeter Wemm  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
310e3d5408SPeter Wemm  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
320e3d5408SPeter Wemm  ****************************************************************************/
330e3d5408SPeter Wemm 
340e3d5408SPeter Wemm /*
350e3d5408SPeter Wemm  * Termcap compatibility support
360e3d5408SPeter Wemm  *
370e3d5408SPeter Wemm  * If your OS integrator didn't install a terminfo database, you can call
380e3d5408SPeter Wemm  * _nc_read_termcap_entry() to support reading and translating capabilities
390e3d5408SPeter Wemm  * from the system termcap file.  This is a kludge; it will bulk up and slow
400e3d5408SPeter Wemm  * down every program that uses ncurses, and translated termcap entries cannot
410e3d5408SPeter Wemm  * use full terminfo capabilities.  Don't use it unless you absolutely have to;
420e3d5408SPeter Wemm  * instead, get your system people to run tic(1) from root on the terminfo
430e3d5408SPeter Wemm  * master included with ncurses to translate it into a terminfo database.
440e3d5408SPeter Wemm  *
450e3d5408SPeter Wemm  * If USE_GETCAP is enabled, we use what is effectively a copy of the 4.4BSD
460e3d5408SPeter Wemm  * getcap code to fetch entries.  There are disadvantages to this; mainly that
470e3d5408SPeter Wemm  * getcap(3) does its own resolution, meaning that entries read in in this way
480e3d5408SPeter Wemm  * can't reference the terminfo tree.  The only thing it buys is faster startup
490e3d5408SPeter Wemm  * time, getcap(3) is much faster than our tic parser.
500e3d5408SPeter Wemm  */
510e3d5408SPeter Wemm 
520e3d5408SPeter Wemm #include <curses.priv.h>
530e3d5408SPeter Wemm 
540e3d5408SPeter Wemm #include <ctype.h>
5539f2269fSPeter Wemm #include <sys/types.h>
5639f2269fSPeter Wemm #include <sys/stat.h>
570e3d5408SPeter Wemm #include <tic.h>
580e3d5408SPeter Wemm #include <term_entry.h>
590e3d5408SPeter Wemm 
6039f2269fSPeter Wemm MODULE_ID("$Id: read_termcap.c,v 1.58 2001/10/28 01:11:34 tom Exp $")
610e3d5408SPeter Wemm 
6218259542SPeter Wemm #if !PURE_TERMINFO
630e3d5408SPeter Wemm 
640e3d5408SPeter Wemm #ifdef __EMX__
650e3d5408SPeter Wemm #define is_pathname(s) ((((s) != 0) && ((s)[0] == '/')) \
660e3d5408SPeter Wemm 		  || (((s)[0] != 0) && ((s)[1] == ':')))
670e3d5408SPeter Wemm #else
680e3d5408SPeter Wemm #define is_pathname(s) ((s) != 0 && (s)[0] == '/')
690e3d5408SPeter Wemm #endif
700e3d5408SPeter Wemm 
710e3d5408SPeter Wemm #define TC_SUCCESS     0
720e3d5408SPeter Wemm #define TC_UNRESOLVED -1
730e3d5408SPeter Wemm #define TC_NOT_FOUND  -2
740e3d5408SPeter Wemm #define TC_SYS_ERR    -3
750e3d5408SPeter Wemm #define TC_REF_LOOP   -4
760e3d5408SPeter Wemm 
7739f2269fSPeter Wemm static char *
7839f2269fSPeter Wemm get_termpath(void)
7939f2269fSPeter Wemm {
8039f2269fSPeter Wemm     char *result;
8139f2269fSPeter Wemm 
8239f2269fSPeter Wemm     if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0)
8339f2269fSPeter Wemm 	result = TERMPATH;
8439f2269fSPeter Wemm     T(("TERMPATH is %s", result));
8539f2269fSPeter Wemm     return result;
8639f2269fSPeter Wemm }
8739f2269fSPeter Wemm 
880e3d5408SPeter Wemm #if USE_GETCAP
890e3d5408SPeter Wemm 
900e3d5408SPeter Wemm #if HAVE_BSD_CGETENT
910e3d5408SPeter Wemm #define _nc_cgetcap   cgetcap
920e3d5408SPeter Wemm #define _nc_cgetent(buf, oline, db_array, name) cgetent(buf, db_array, name)
930e3d5408SPeter Wemm #define _nc_cgetmatch cgetmatch
940e3d5408SPeter Wemm #define _nc_cgetset   cgetset
950e3d5408SPeter Wemm #else
960e3d5408SPeter Wemm static int _nc_cgetmatch(char *, const char *);
9718259542SPeter Wemm static int _nc_getent(char **, unsigned *, int *, int, char **, int, const char
9818259542SPeter Wemm 		      *, int, char *);
990e3d5408SPeter Wemm static int _nc_nfcmp(const char *, char *);
1000e3d5408SPeter Wemm 
1010e3d5408SPeter Wemm /*-
1020e3d5408SPeter Wemm  * Copyright (c) 1992, 1993
1030e3d5408SPeter Wemm  *	The Regents of the University of California.  All rights reserved.
1040e3d5408SPeter Wemm  *
1050e3d5408SPeter Wemm  * This code is derived from software contributed to Berkeley by
1060e3d5408SPeter Wemm  * Casey Leedom of Lawrence Livermore National Laboratory.
1070e3d5408SPeter Wemm  *
1080e3d5408SPeter Wemm  * Redistribution and use in source and binary forms, with or without
1090e3d5408SPeter Wemm  * modification, are permitted provided that the following conditions
1100e3d5408SPeter Wemm  * are met:
1110e3d5408SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
1120e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
1130e3d5408SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
1140e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
1150e3d5408SPeter Wemm  *    documentation and/or other materials provided with the distribution.
1160e3d5408SPeter Wemm  * 3. All advertising materials mentioning features or use of this software
1170e3d5408SPeter Wemm  *    must display the following acknowledgment:
1180e3d5408SPeter Wemm  *	This product includes software developed by the University of
1190e3d5408SPeter Wemm  *	California, Berkeley and its contributors.
1200e3d5408SPeter Wemm  * 4. Neither the name of the University nor the names of its contributors
1210e3d5408SPeter Wemm  *    may be used to endorse or promote products derived from this software
1220e3d5408SPeter Wemm  *    without specific prior written permission.
1230e3d5408SPeter Wemm  *
1240e3d5408SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1250e3d5408SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1260e3d5408SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1270e3d5408SPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1280e3d5408SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1290e3d5408SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1300e3d5408SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1310e3d5408SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1320e3d5408SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1330e3d5408SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1340e3d5408SPeter Wemm  * SUCH DAMAGE.
1350e3d5408SPeter Wemm  */
1360e3d5408SPeter Wemm 
1370e3d5408SPeter Wemm /* static char sccsid[] = "@(#)getcap.c	8.3 (Berkeley) 3/25/94"; */
1380e3d5408SPeter Wemm 
1390e3d5408SPeter Wemm #define	BFRAG		1024
1400e3d5408SPeter Wemm #define	BSIZE		1024
1410e3d5408SPeter Wemm #define	MAX_RECURSION	32	/* maximum getent recursion */
1420e3d5408SPeter Wemm 
1430e3d5408SPeter Wemm static size_t topreclen;	/* toprec length */
1440e3d5408SPeter Wemm static char *toprec;		/* Additional record specified by cgetset() */
1450e3d5408SPeter Wemm static int gottoprec;		/* Flag indicating retrieval of toprecord */
1460e3d5408SPeter Wemm 
1470e3d5408SPeter Wemm /*
1480e3d5408SPeter Wemm  * Cgetset() allows the addition of a user specified buffer to be added to the
1490e3d5408SPeter Wemm  * database array, in effect "pushing" the buffer on top of the virtual
1500e3d5408SPeter Wemm  * database.  0 is returned on success, -1 on failure.
1510e3d5408SPeter Wemm  */
1520e3d5408SPeter Wemm static int
1530e3d5408SPeter Wemm _nc_cgetset(const char *ent)
1540e3d5408SPeter Wemm {
1550e3d5408SPeter Wemm     if (ent == 0) {
1560e3d5408SPeter Wemm 	FreeIfNeeded(toprec);
1570e3d5408SPeter Wemm 	toprec = 0;
1580e3d5408SPeter Wemm 	topreclen = 0;
1590e3d5408SPeter Wemm 	return (0);
1600e3d5408SPeter Wemm     }
1610e3d5408SPeter Wemm     topreclen = strlen(ent);
1620e3d5408SPeter Wemm     if ((toprec = typeMalloc(char, topreclen + 1)) == 0) {
1630e3d5408SPeter Wemm 	errno = ENOMEM;
1640e3d5408SPeter Wemm 	return (-1);
1650e3d5408SPeter Wemm     }
1660e3d5408SPeter Wemm     gottoprec = 0;
1670e3d5408SPeter Wemm     (void) strcpy(toprec, ent);
1680e3d5408SPeter Wemm     return (0);
1690e3d5408SPeter Wemm }
1700e3d5408SPeter Wemm 
1710e3d5408SPeter Wemm /*
1720e3d5408SPeter Wemm  * Cgetcap searches the capability record buf for the capability cap with type
1730e3d5408SPeter Wemm  * `type'.  A pointer to the value of cap is returned on success, 0 if the
1740e3d5408SPeter Wemm  * requested capability couldn't be found.
1750e3d5408SPeter Wemm  *
1760e3d5408SPeter Wemm  * Specifying a type of ':' means that nothing should follow cap (:cap:).  In
1770e3d5408SPeter Wemm  * this case a pointer to the terminating ':' or NUL will be returned if cap is
1780e3d5408SPeter Wemm  * found.
1790e3d5408SPeter Wemm  *
1800e3d5408SPeter Wemm  * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
1810e3d5408SPeter Wemm  * return 0.
1820e3d5408SPeter Wemm  */
1830e3d5408SPeter Wemm static char *
1840e3d5408SPeter Wemm _nc_cgetcap(char *buf, const char *cap, int type)
1850e3d5408SPeter Wemm {
1860e3d5408SPeter Wemm     register const char *cp;
1870e3d5408SPeter Wemm     register char *bp;
1880e3d5408SPeter Wemm 
1890e3d5408SPeter Wemm     bp = buf;
1900e3d5408SPeter Wemm     for (;;) {
1910e3d5408SPeter Wemm 	/*
1920e3d5408SPeter Wemm 	 * Skip past the current capability field - it's either the
1930e3d5408SPeter Wemm 	 * name field if this is the first time through the loop, or
1940e3d5408SPeter Wemm 	 * the remainder of a field whose name failed to match cap.
1950e3d5408SPeter Wemm 	 */
1960e3d5408SPeter Wemm 	for (;;) {
1970e3d5408SPeter Wemm 	    if (*bp == '\0')
1980e3d5408SPeter Wemm 		return (0);
1990e3d5408SPeter Wemm 	    else if (*bp++ == ':')
2000e3d5408SPeter Wemm 		break;
2010e3d5408SPeter Wemm 	}
2020e3d5408SPeter Wemm 
2030e3d5408SPeter Wemm 	/*
2040e3d5408SPeter Wemm 	 * Try to match (cap, type) in buf.
2050e3d5408SPeter Wemm 	 */
2060e3d5408SPeter Wemm 	for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
2070e3d5408SPeter Wemm 	    continue;
2080e3d5408SPeter Wemm 	if (*cp != '\0')
2090e3d5408SPeter Wemm 	    continue;
2100e3d5408SPeter Wemm 	if (*bp == '@')
2110e3d5408SPeter Wemm 	    return (0);
2120e3d5408SPeter Wemm 	if (type == ':') {
2130e3d5408SPeter Wemm 	    if (*bp != '\0' && *bp != ':')
2140e3d5408SPeter Wemm 		continue;
2150e3d5408SPeter Wemm 	    return (bp);
2160e3d5408SPeter Wemm 	}
2170e3d5408SPeter Wemm 	if (*bp != type)
2180e3d5408SPeter Wemm 	    continue;
2190e3d5408SPeter Wemm 	bp++;
2200e3d5408SPeter Wemm 	return (*bp == '@' ? 0 : bp);
2210e3d5408SPeter Wemm     }
2220e3d5408SPeter Wemm     /* NOTREACHED */
2230e3d5408SPeter Wemm }
2240e3d5408SPeter Wemm 
2250e3d5408SPeter Wemm /*
2260e3d5408SPeter Wemm  * Cgetent extracts the capability record name from the NULL terminated file
2270e3d5408SPeter Wemm  * array db_array and returns a pointer to a malloc'd copy of it in buf.  Buf
2280e3d5408SPeter Wemm  * must be retained through all subsequent calls to cgetcap, cgetnum, cgetflag,
2290e3d5408SPeter Wemm  * and cgetstr, but may then be freed.
2300e3d5408SPeter Wemm  *
2310e3d5408SPeter Wemm  * Returns:
2320e3d5408SPeter Wemm  *
2330e3d5408SPeter Wemm  * positive #    on success (i.e., the index in db_array)
2340e3d5408SPeter Wemm  * TC_UNRESOLVED if we had too many recurrences to resolve
2350e3d5408SPeter Wemm  * TC_NOT_FOUND  if the requested record couldn't be found
2360e3d5408SPeter Wemm  * TC_SYS_ERR    if a system error was encountered (e.g.,couldn't open a file)
2370e3d5408SPeter Wemm  * TC_REF_LOOP   if a potential reference loop is detected
2380e3d5408SPeter Wemm  */
2390e3d5408SPeter Wemm static int
2400e3d5408SPeter Wemm _nc_cgetent(char **buf, int *oline, char **db_array, const char *name)
2410e3d5408SPeter Wemm {
24215589c42SPeter Wemm     unsigned dummy;
2430e3d5408SPeter Wemm 
2440e3d5408SPeter Wemm     return (_nc_getent(buf, &dummy, oline, 0, db_array, -1, name, 0, 0));
2450e3d5408SPeter Wemm }
2460e3d5408SPeter Wemm 
2470e3d5408SPeter Wemm /*
2480e3d5408SPeter Wemm  * Getent implements the functions of cgetent.  If fd is non-negative,
2490e3d5408SPeter Wemm  * *db_array has already been opened and fd is the open file descriptor.  We
2500e3d5408SPeter Wemm  * do this to save time and avoid using up file descriptors for tc=
2510e3d5408SPeter Wemm  * recursions.
2520e3d5408SPeter Wemm  *
2530e3d5408SPeter Wemm  * Getent returns the same success/failure codes as cgetent.  On success, a
2540e3d5408SPeter Wemm  * pointer to a malloc'd capability record with all tc= capabilities fully
2550e3d5408SPeter Wemm  * expanded and its length (not including trailing ASCII NUL) are left in
2560e3d5408SPeter Wemm  * *cap and *len.
2570e3d5408SPeter Wemm  *
2580e3d5408SPeter Wemm  * Basic algorithm:
2590e3d5408SPeter Wemm  *	+ Allocate memory incrementally as needed in chunks of size BFRAG
2600e3d5408SPeter Wemm  *	  for capability buffer.
2610e3d5408SPeter Wemm  *	+ Recurse for each tc=name and interpolate result.  Stop when all
2620e3d5408SPeter Wemm  *	  names interpolated, a name can't be found, or depth exceeds
2630e3d5408SPeter Wemm  *	  MAX_RECURSION.
2640e3d5408SPeter Wemm  */
2650e3d5408SPeter Wemm #define DOALLOC(size) typeRealloc(char, size, record)
2660e3d5408SPeter Wemm static int
2670e3d5408SPeter Wemm _nc_getent(
2680e3d5408SPeter Wemm 	      char **cap,	/* termcap-content */
26915589c42SPeter Wemm 	      unsigned *len,	/* length, needed for recursion */
2700e3d5408SPeter Wemm 	      int *beginning,	/* line-number at match */
2710e3d5408SPeter Wemm 	      int in_array,	/* index in 'db_array[] */
2720e3d5408SPeter Wemm 	      char **db_array,	/* list of files to search */
2730e3d5408SPeter Wemm 	      int fd,
2740e3d5408SPeter Wemm 	      const char *name,
2750e3d5408SPeter Wemm 	      int depth,
2760e3d5408SPeter Wemm 	      char *nfield)
2770e3d5408SPeter Wemm {
2780e3d5408SPeter Wemm     register char *r_end, *rp;
2790e3d5408SPeter Wemm     int myfd = FALSE;
2800e3d5408SPeter Wemm     char *record = 0;
2810e3d5408SPeter Wemm     int tc_not_resolved;
2820e3d5408SPeter Wemm     int current;
2830e3d5408SPeter Wemm     int lineno;
2840e3d5408SPeter Wemm 
2850e3d5408SPeter Wemm     /*
2860e3d5408SPeter Wemm      * Return with ``loop detected'' error if we've recurred more than
2870e3d5408SPeter Wemm      * MAX_RECURSION times.
2880e3d5408SPeter Wemm      */
2890e3d5408SPeter Wemm     if (depth > MAX_RECURSION)
2900e3d5408SPeter Wemm 	return (TC_REF_LOOP);
2910e3d5408SPeter Wemm 
2920e3d5408SPeter Wemm     /*
2930e3d5408SPeter Wemm      * Check if we have a top record from cgetset().
2940e3d5408SPeter Wemm      */
2950e3d5408SPeter Wemm     if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) {
2960e3d5408SPeter Wemm 	if ((record = DOALLOC(topreclen + BFRAG)) == 0) {
2970e3d5408SPeter Wemm 	    errno = ENOMEM;
2980e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
2990e3d5408SPeter Wemm 	}
3000e3d5408SPeter Wemm 	(void) strcpy(record, toprec);
3010e3d5408SPeter Wemm 	rp = record + topreclen + 1;
3020e3d5408SPeter Wemm 	r_end = rp + BFRAG;
3030e3d5408SPeter Wemm 	current = in_array;
3040e3d5408SPeter Wemm     } else {
3050e3d5408SPeter Wemm 	int foundit;
3060e3d5408SPeter Wemm 
3070e3d5408SPeter Wemm 	/*
3080e3d5408SPeter Wemm 	 * Allocate first chunk of memory.
3090e3d5408SPeter Wemm 	 */
3100e3d5408SPeter Wemm 	if ((record = DOALLOC(BFRAG)) == 0) {
3110e3d5408SPeter Wemm 	    errno = ENOMEM;
3120e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
3130e3d5408SPeter Wemm 	}
3140e3d5408SPeter Wemm 	rp = r_end = record + BFRAG;
3150e3d5408SPeter Wemm 	foundit = FALSE;
3160e3d5408SPeter Wemm 
3170e3d5408SPeter Wemm 	/*
3180e3d5408SPeter Wemm 	 * Loop through database array until finding the record.
3190e3d5408SPeter Wemm 	 */
3200e3d5408SPeter Wemm 	for (current = in_array; db_array[current] != 0; current++) {
3210e3d5408SPeter Wemm 	    int eof = FALSE;
3220e3d5408SPeter Wemm 
3230e3d5408SPeter Wemm 	    /*
3240e3d5408SPeter Wemm 	     * Open database if not already open.
3250e3d5408SPeter Wemm 	     */
3260e3d5408SPeter Wemm 	    if (fd >= 0) {
3270e3d5408SPeter Wemm 		(void) lseek(fd, (off_t) 0, SEEK_SET);
3280e3d5408SPeter Wemm 	    } else if ((_nc_access(db_array[current], R_OK) < 0)
3290e3d5408SPeter Wemm 		       || (fd = open(db_array[current], O_RDONLY, 0)) < 0) {
3300e3d5408SPeter Wemm 		/* No error on unfound file. */
3310e3d5408SPeter Wemm 		if (errno == ENOENT)
3320e3d5408SPeter Wemm 		    continue;
3330e3d5408SPeter Wemm 		free(record);
3340e3d5408SPeter Wemm 		return (TC_SYS_ERR);
3350e3d5408SPeter Wemm 	    } else {
3360e3d5408SPeter Wemm 		myfd = TRUE;
3370e3d5408SPeter Wemm 	    }
3380e3d5408SPeter Wemm 	    lineno = 0;
3390e3d5408SPeter Wemm 
3400e3d5408SPeter Wemm 	    /*
3410e3d5408SPeter Wemm 	     * Find the requested capability record ...
3420e3d5408SPeter Wemm 	     */
3430e3d5408SPeter Wemm 	    {
3440e3d5408SPeter Wemm 		char buf[2048];
3450e3d5408SPeter Wemm 		register char *b_end = buf;
3460e3d5408SPeter Wemm 		register char *bp = buf;
3470e3d5408SPeter Wemm 		register int c;
3480e3d5408SPeter Wemm 
3490e3d5408SPeter Wemm 		/*
3500e3d5408SPeter Wemm 		 * Loop invariants:
3510e3d5408SPeter Wemm 		 *      There is always room for one more character in record.
3520e3d5408SPeter Wemm 		 *      R_end always points just past end of record.
3530e3d5408SPeter Wemm 		 *      Rp always points just past last character in record.
3540e3d5408SPeter Wemm 		 *      B_end always points just past last character in buf.
3550e3d5408SPeter Wemm 		 *      Bp always points at next character in buf.
3560e3d5408SPeter Wemm 		 */
3570e3d5408SPeter Wemm 
3580e3d5408SPeter Wemm 		for (;;) {
3590e3d5408SPeter Wemm 		    int first = lineno + 1;
3600e3d5408SPeter Wemm 
3610e3d5408SPeter Wemm 		    /*
3620e3d5408SPeter Wemm 		     * Read in a line implementing (\, newline)
3630e3d5408SPeter Wemm 		     * line continuation.
3640e3d5408SPeter Wemm 		     */
3650e3d5408SPeter Wemm 		    rp = record;
3660e3d5408SPeter Wemm 		    for (;;) {
3670e3d5408SPeter Wemm 			if (bp >= b_end) {
3680e3d5408SPeter Wemm 			    int n;
3690e3d5408SPeter Wemm 
3700e3d5408SPeter Wemm 			    n = read(fd, buf, sizeof(buf));
3710e3d5408SPeter Wemm 			    if (n <= 0) {
3720e3d5408SPeter Wemm 				if (myfd)
3730e3d5408SPeter Wemm 				    (void) close(fd);
3740e3d5408SPeter Wemm 				if (n < 0) {
3750e3d5408SPeter Wemm 				    free(record);
3760e3d5408SPeter Wemm 				    return (TC_SYS_ERR);
3770e3d5408SPeter Wemm 				}
3780e3d5408SPeter Wemm 				fd = -1;
3790e3d5408SPeter Wemm 				eof = TRUE;
3800e3d5408SPeter Wemm 				break;
3810e3d5408SPeter Wemm 			    }
3820e3d5408SPeter Wemm 			    b_end = buf + n;
3830e3d5408SPeter Wemm 			    bp = buf;
3840e3d5408SPeter Wemm 			}
3850e3d5408SPeter Wemm 
3860e3d5408SPeter Wemm 			c = *bp++;
3870e3d5408SPeter Wemm 			if (c == '\n') {
3880e3d5408SPeter Wemm 			    lineno++;
3890e3d5408SPeter Wemm 			    if (rp == record || *(rp - 1) != '\\')
3900e3d5408SPeter Wemm 				break;
3910e3d5408SPeter Wemm 			}
3920e3d5408SPeter Wemm 			*rp++ = c;
3930e3d5408SPeter Wemm 
3940e3d5408SPeter Wemm 			/*
3950e3d5408SPeter Wemm 			 * Enforce loop invariant: if no room
3960e3d5408SPeter Wemm 			 * left in record buffer, try to get
3970e3d5408SPeter Wemm 			 * some more.
3980e3d5408SPeter Wemm 			 */
3990e3d5408SPeter Wemm 			if (rp >= r_end) {
40015589c42SPeter Wemm 			    unsigned pos;
4010e3d5408SPeter Wemm 			    size_t newsize;
4020e3d5408SPeter Wemm 
4030e3d5408SPeter Wemm 			    pos = rp - record;
4040e3d5408SPeter Wemm 			    newsize = r_end - record + BFRAG;
4050e3d5408SPeter Wemm 			    record = DOALLOC(newsize);
4060e3d5408SPeter Wemm 			    if (record == 0) {
4070e3d5408SPeter Wemm 				if (myfd)
4080e3d5408SPeter Wemm 				    (void) close(fd);
4090e3d5408SPeter Wemm 				errno = ENOMEM;
4100e3d5408SPeter Wemm 				return (TC_SYS_ERR);
4110e3d5408SPeter Wemm 			    }
4120e3d5408SPeter Wemm 			    r_end = record + newsize;
4130e3d5408SPeter Wemm 			    rp = record + pos;
4140e3d5408SPeter Wemm 			}
4150e3d5408SPeter Wemm 		    }
4160e3d5408SPeter Wemm 		    /* loop invariant lets us do this */
4170e3d5408SPeter Wemm 		    *rp++ = '\0';
4180e3d5408SPeter Wemm 
4190e3d5408SPeter Wemm 		    /*
4200e3d5408SPeter Wemm 		     * If encountered eof check next file.
4210e3d5408SPeter Wemm 		     */
4220e3d5408SPeter Wemm 		    if (eof)
4230e3d5408SPeter Wemm 			break;
4240e3d5408SPeter Wemm 
4250e3d5408SPeter Wemm 		    /*
4260e3d5408SPeter Wemm 		     * Toss blank lines and comments.
4270e3d5408SPeter Wemm 		     */
4280e3d5408SPeter Wemm 		    if (*record == '\0' || *record == '#')
4290e3d5408SPeter Wemm 			continue;
4300e3d5408SPeter Wemm 
4310e3d5408SPeter Wemm 		    /*
4320e3d5408SPeter Wemm 		     * See if this is the record we want ...
4330e3d5408SPeter Wemm 		     */
4340e3d5408SPeter Wemm 		    if (_nc_cgetmatch(record, name) == 0
4350e3d5408SPeter Wemm 			&& (nfield == 0
4360e3d5408SPeter Wemm 			    || !_nc_nfcmp(nfield, record))) {
4370e3d5408SPeter Wemm 			foundit = TRUE;
4380e3d5408SPeter Wemm 			*beginning = first;
4390e3d5408SPeter Wemm 			break;	/* found it! */
4400e3d5408SPeter Wemm 		    }
4410e3d5408SPeter Wemm 		}
4420e3d5408SPeter Wemm 	    }
4430e3d5408SPeter Wemm 	    if (foundit)
4440e3d5408SPeter Wemm 		break;
4450e3d5408SPeter Wemm 	}
4460e3d5408SPeter Wemm 
4470e3d5408SPeter Wemm 	if (!foundit)
4480e3d5408SPeter Wemm 	    return (TC_NOT_FOUND);
4490e3d5408SPeter Wemm     }
4500e3d5408SPeter Wemm 
4510e3d5408SPeter Wemm     /*
4520e3d5408SPeter Wemm      * Got the capability record, but now we have to expand all tc=name
4530e3d5408SPeter Wemm      * references in it ...
4540e3d5408SPeter Wemm      */
4550e3d5408SPeter Wemm     {
4560e3d5408SPeter Wemm 	register char *newicap, *s;
4570e3d5408SPeter Wemm 	register int newilen;
45815589c42SPeter Wemm 	unsigned ilen;
4590e3d5408SPeter Wemm 	int diff, iret, tclen, oline;
4600e3d5408SPeter Wemm 	char *icap, *scan, *tc, *tcstart, *tcend;
4610e3d5408SPeter Wemm 
4620e3d5408SPeter Wemm 	/*
4630e3d5408SPeter Wemm 	 * Loop invariants:
4640e3d5408SPeter Wemm 	 *      There is room for one more character in record.
4650e3d5408SPeter Wemm 	 *      R_end points just past end of record.
4660e3d5408SPeter Wemm 	 *      Rp points just past last character in record.
4670e3d5408SPeter Wemm 	 *      Scan points at remainder of record that needs to be
4680e3d5408SPeter Wemm 	 *      scanned for tc=name constructs.
4690e3d5408SPeter Wemm 	 */
4700e3d5408SPeter Wemm 	scan = record;
4710e3d5408SPeter Wemm 	tc_not_resolved = FALSE;
4720e3d5408SPeter Wemm 	for (;;) {
4730e3d5408SPeter Wemm 	    if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0)
4740e3d5408SPeter Wemm 		break;
4750e3d5408SPeter Wemm 
4760e3d5408SPeter Wemm 	    /*
4770e3d5408SPeter Wemm 	     * Find end of tc=name and stomp on the trailing `:'
4780e3d5408SPeter Wemm 	     * (if present) so we can use it to call ourselves.
4790e3d5408SPeter Wemm 	     */
4800e3d5408SPeter Wemm 	    s = tc;
4810e3d5408SPeter Wemm 	    while (*s != '\0') {
4820e3d5408SPeter Wemm 		if (*s++ == ':') {
4830e3d5408SPeter Wemm 		    *(s - 1) = '\0';
4840e3d5408SPeter Wemm 		    break;
4850e3d5408SPeter Wemm 		}
4860e3d5408SPeter Wemm 	    }
4870e3d5408SPeter Wemm 	    tcstart = tc - 3;
4880e3d5408SPeter Wemm 	    tclen = s - tcstart;
4890e3d5408SPeter Wemm 	    tcend = s;
4900e3d5408SPeter Wemm 
49115589c42SPeter Wemm 	    iret = _nc_getent(&icap, &ilen, &oline, current, db_array, fd,
49215589c42SPeter Wemm 			      tc, depth + 1, 0);
4930e3d5408SPeter Wemm 	    newicap = icap;	/* Put into a register. */
4940e3d5408SPeter Wemm 	    newilen = ilen;
4950e3d5408SPeter Wemm 	    if (iret != TC_SUCCESS) {
4960e3d5408SPeter Wemm 		/* an error */
4970e3d5408SPeter Wemm 		if (iret < TC_NOT_FOUND) {
4980e3d5408SPeter Wemm 		    if (myfd)
4990e3d5408SPeter Wemm 			(void) close(fd);
5000e3d5408SPeter Wemm 		    free(record);
5010e3d5408SPeter Wemm 		    return (iret);
5020e3d5408SPeter Wemm 		}
5030e3d5408SPeter Wemm 		if (iret == TC_UNRESOLVED)
5040e3d5408SPeter Wemm 		    tc_not_resolved = TRUE;
5050e3d5408SPeter Wemm 		/* couldn't resolve tc */
5060e3d5408SPeter Wemm 		if (iret == TC_NOT_FOUND) {
5070e3d5408SPeter Wemm 		    *(s - 1) = ':';
5080e3d5408SPeter Wemm 		    scan = s - 1;
5090e3d5408SPeter Wemm 		    tc_not_resolved = TRUE;
5100e3d5408SPeter Wemm 		    continue;
5110e3d5408SPeter Wemm 		}
5120e3d5408SPeter Wemm 	    }
5130e3d5408SPeter Wemm 
5140e3d5408SPeter Wemm 	    /* not interested in name field of tc'ed record */
5150e3d5408SPeter Wemm 	    s = newicap;
51615589c42SPeter Wemm 	    while (*s != '\0' && *s++ != ':') ;
5170e3d5408SPeter Wemm 	    newilen -= s - newicap;
5180e3d5408SPeter Wemm 	    newicap = s;
5190e3d5408SPeter Wemm 
5200e3d5408SPeter Wemm 	    /* make sure interpolated record is `:'-terminated */
5210e3d5408SPeter Wemm 	    s += newilen;
5220e3d5408SPeter Wemm 	    if (*(s - 1) != ':') {
5230e3d5408SPeter Wemm 		*s = ':';	/* overwrite NUL with : */
5240e3d5408SPeter Wemm 		newilen++;
5250e3d5408SPeter Wemm 	    }
5260e3d5408SPeter Wemm 
5270e3d5408SPeter Wemm 	    /*
5280e3d5408SPeter Wemm 	     * Make sure there's enough room to insert the
5290e3d5408SPeter Wemm 	     * new record.
5300e3d5408SPeter Wemm 	     */
5310e3d5408SPeter Wemm 	    diff = newilen - tclen;
5320e3d5408SPeter Wemm 	    if (diff >= r_end - rp) {
53315589c42SPeter Wemm 		unsigned pos, tcpos, tcposend;
5340e3d5408SPeter Wemm 		size_t newsize;
5350e3d5408SPeter Wemm 
5360e3d5408SPeter Wemm 		pos = rp - record;
5370e3d5408SPeter Wemm 		newsize = r_end - record + diff + BFRAG;
5380e3d5408SPeter Wemm 		tcpos = tcstart - record;
5390e3d5408SPeter Wemm 		tcposend = tcend - record;
5400e3d5408SPeter Wemm 		record = DOALLOC(newsize);
5410e3d5408SPeter Wemm 		if (record == 0) {
5420e3d5408SPeter Wemm 		    if (myfd)
5430e3d5408SPeter Wemm 			(void) close(fd);
5440e3d5408SPeter Wemm 		    free(icap);
5450e3d5408SPeter Wemm 		    errno = ENOMEM;
5460e3d5408SPeter Wemm 		    return (TC_SYS_ERR);
5470e3d5408SPeter Wemm 		}
5480e3d5408SPeter Wemm 		r_end = record + newsize;
5490e3d5408SPeter Wemm 		rp = record + pos;
5500e3d5408SPeter Wemm 		tcstart = record + tcpos;
5510e3d5408SPeter Wemm 		tcend = record + tcposend;
5520e3d5408SPeter Wemm 	    }
5530e3d5408SPeter Wemm 
5540e3d5408SPeter Wemm 	    /*
5550e3d5408SPeter Wemm 	     * Insert tc'ed record into our record.
5560e3d5408SPeter Wemm 	     */
5570e3d5408SPeter Wemm 	    s = tcstart + newilen;
5580e3d5408SPeter Wemm 	    memmove(s, tcend, (size_t) (rp - tcend));
5590e3d5408SPeter Wemm 	    memmove(tcstart, newicap, (size_t) newilen);
5600e3d5408SPeter Wemm 	    rp += diff;
5610e3d5408SPeter Wemm 	    free(icap);
5620e3d5408SPeter Wemm 
5630e3d5408SPeter Wemm 	    /*
5640e3d5408SPeter Wemm 	     * Start scan on `:' so next cgetcap works properly
5650e3d5408SPeter Wemm 	     * (cgetcap always skips first field).
5660e3d5408SPeter Wemm 	     */
5670e3d5408SPeter Wemm 	    scan = s - 1;
5680e3d5408SPeter Wemm 	}
5690e3d5408SPeter Wemm     }
5700e3d5408SPeter Wemm 
5710e3d5408SPeter Wemm     /*
5720e3d5408SPeter Wemm      * Close file (if we opened it), give back any extra memory, and
5730e3d5408SPeter Wemm      * return capability, length and success.
5740e3d5408SPeter Wemm      */
5750e3d5408SPeter Wemm     if (myfd)
5760e3d5408SPeter Wemm 	(void) close(fd);
5770e3d5408SPeter Wemm     *len = rp - record - 1;	/* don't count NUL */
5780e3d5408SPeter Wemm     if (r_end > rp) {
5790e3d5408SPeter Wemm 	if ((record = DOALLOC((size_t) (rp - record))) == 0) {
5800e3d5408SPeter Wemm 	    errno = ENOMEM;
5810e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
5820e3d5408SPeter Wemm 	}
5830e3d5408SPeter Wemm     }
5840e3d5408SPeter Wemm 
5850e3d5408SPeter Wemm     *cap = record;
5860e3d5408SPeter Wemm     if (tc_not_resolved)
5870e3d5408SPeter Wemm 	return (TC_UNRESOLVED);
5880e3d5408SPeter Wemm     return (current);
5890e3d5408SPeter Wemm }
5900e3d5408SPeter Wemm 
5910e3d5408SPeter Wemm /*
5920e3d5408SPeter Wemm  * Cgetmatch will return 0 if name is one of the names of the capability
5930e3d5408SPeter Wemm  * record buf, -1 if not.
5940e3d5408SPeter Wemm  */
5950e3d5408SPeter Wemm static int
5960e3d5408SPeter Wemm _nc_cgetmatch(char *buf, const char *name)
5970e3d5408SPeter Wemm {
5980e3d5408SPeter Wemm     register const char *np;
5990e3d5408SPeter Wemm     register char *bp;
6000e3d5408SPeter Wemm 
6010e3d5408SPeter Wemm     /*
6020e3d5408SPeter Wemm      * Start search at beginning of record.
6030e3d5408SPeter Wemm      */
6040e3d5408SPeter Wemm     bp = buf;
6050e3d5408SPeter Wemm     for (;;) {
6060e3d5408SPeter Wemm 	/*
6070e3d5408SPeter Wemm 	 * Try to match a record name.
6080e3d5408SPeter Wemm 	 */
6090e3d5408SPeter Wemm 	np = name;
6100e3d5408SPeter Wemm 	for (;;) {
6110e3d5408SPeter Wemm 	    if (*np == '\0') {
6120e3d5408SPeter Wemm 		if (*bp == '|' || *bp == ':' || *bp == '\0')
6130e3d5408SPeter Wemm 		    return (0);
6140e3d5408SPeter Wemm 		else
6150e3d5408SPeter Wemm 		    break;
6160e3d5408SPeter Wemm 	    } else if (*bp++ != *np++) {
6170e3d5408SPeter Wemm 		break;
6180e3d5408SPeter Wemm 	    }
6190e3d5408SPeter Wemm 	}
6200e3d5408SPeter Wemm 
6210e3d5408SPeter Wemm 	/*
6220e3d5408SPeter Wemm 	 * Match failed, skip to next name in record.
6230e3d5408SPeter Wemm 	 */
6240e3d5408SPeter Wemm 	bp--;			/* a '|' or ':' may have stopped the match */
6250e3d5408SPeter Wemm 	for (;;) {
6260e3d5408SPeter Wemm 	    if (*bp == '\0' || *bp == ':')
6270e3d5408SPeter Wemm 		return (-1);	/* match failed totally */
6280e3d5408SPeter Wemm 	    else if (*bp++ == '|')
6290e3d5408SPeter Wemm 		break;		/* found next name */
6300e3d5408SPeter Wemm 	}
6310e3d5408SPeter Wemm     }
6320e3d5408SPeter Wemm }
6330e3d5408SPeter Wemm 
6340e3d5408SPeter Wemm /*
6350e3d5408SPeter Wemm  * Compare name field of record.
6360e3d5408SPeter Wemm  */
6370e3d5408SPeter Wemm static int
6380e3d5408SPeter Wemm _nc_nfcmp(const char *nf, char *rec)
6390e3d5408SPeter Wemm {
6400e3d5408SPeter Wemm     char *cp, tmp;
6410e3d5408SPeter Wemm     int ret;
6420e3d5408SPeter Wemm 
64315589c42SPeter Wemm     for (cp = rec; *cp != ':'; cp++) ;
6440e3d5408SPeter Wemm 
6450e3d5408SPeter Wemm     tmp = *(cp + 1);
6460e3d5408SPeter Wemm     *(cp + 1) = '\0';
6470e3d5408SPeter Wemm     ret = strcmp(nf, rec);
6480e3d5408SPeter Wemm     *(cp + 1) = tmp;
6490e3d5408SPeter Wemm 
6500e3d5408SPeter Wemm     return (ret);
6510e3d5408SPeter Wemm }
6520e3d5408SPeter Wemm #endif /* HAVE_BSD_CGETENT */
6530e3d5408SPeter Wemm 
6540e3d5408SPeter Wemm /*
6550e3d5408SPeter Wemm  * Since ncurses provides its own 'tgetent()', we cannot use the native one.
6560e3d5408SPeter Wemm  * So we reproduce the logic to get down to cgetent() -- or our cut-down
6570e3d5408SPeter Wemm  * version of that -- to circumvent the problem of configuring against the
6580e3d5408SPeter Wemm  * termcap library.
6590e3d5408SPeter Wemm  */
6600e3d5408SPeter Wemm #define USE_BSD_TGETENT 1
6610e3d5408SPeter Wemm 
6620e3d5408SPeter Wemm #if USE_BSD_TGETENT
6630e3d5408SPeter Wemm /*
6640e3d5408SPeter Wemm  * Copyright (c) 1980, 1993
6650e3d5408SPeter Wemm  *	The Regents of the University of California.  All rights reserved.
6660e3d5408SPeter Wemm  *
6670e3d5408SPeter Wemm  * Redistribution and use in source and binary forms, with or without
6680e3d5408SPeter Wemm  * modification, are permitted provided that the following conditions
6690e3d5408SPeter Wemm  * are met:
6700e3d5408SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
6710e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
6720e3d5408SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
6730e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
6740e3d5408SPeter Wemm  *    documentation and/or other materials provided with the distribution.
6750e3d5408SPeter Wemm  * 3. All advertising materials mentioning features or use of this software
6760e3d5408SPeter Wemm  *    must display the following acknowledgment:
6770e3d5408SPeter Wemm  *	This product includes software developed by the University of
6780e3d5408SPeter Wemm  *	California, Berkeley and its contributors.
6790e3d5408SPeter Wemm  * 4. Neither the name of the University nor the names of its contributors
6800e3d5408SPeter Wemm  *    may be used to endorse or promote products derived from this software
6810e3d5408SPeter Wemm  *    without specific prior written permission.
6820e3d5408SPeter Wemm  *
6830e3d5408SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6840e3d5408SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6850e3d5408SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6860e3d5408SPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6870e3d5408SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6880e3d5408SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6890e3d5408SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6900e3d5408SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6910e3d5408SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6920e3d5408SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6930e3d5408SPeter Wemm  * SUCH DAMAGE.
6940e3d5408SPeter Wemm  */
6950e3d5408SPeter Wemm 
6960e3d5408SPeter Wemm /* static char sccsid[] = "@(#)termcap.c	8.1 (Berkeley) 6/4/93" */
6970e3d5408SPeter Wemm 
6980e3d5408SPeter Wemm #define	PBUFSIZ		512	/* max length of filename path */
6990e3d5408SPeter Wemm #define	PVECSIZ		32	/* max number of names in path */
7000e3d5408SPeter Wemm #define TBUFSIZ (2048*2)
7010e3d5408SPeter Wemm 
7020e3d5408SPeter Wemm static char *tbuf;
7030e3d5408SPeter Wemm 
7040e3d5408SPeter Wemm /*
7050e3d5408SPeter Wemm  * On entry, srcp points to a non ':' character which is the beginning of the
7060e3d5408SPeter Wemm  * token, if any.  We'll try to return a string that doesn't end with a ':'.
7070e3d5408SPeter Wemm  */
7080e3d5408SPeter Wemm static char *
7090e3d5408SPeter Wemm get_tc_token(char **srcp, int *endp)
7100e3d5408SPeter Wemm {
7110e3d5408SPeter Wemm     int ch;
7120e3d5408SPeter Wemm     bool found = FALSE;
7130e3d5408SPeter Wemm     char *s, *base;
7140e3d5408SPeter Wemm     char *tok = 0;
7150e3d5408SPeter Wemm 
7160e3d5408SPeter Wemm     *endp = TRUE;
7170e3d5408SPeter Wemm     for (s = base = *srcp; *s != '\0';) {
7180e3d5408SPeter Wemm 	ch = *s++;
7190e3d5408SPeter Wemm 	if (ch == '\\') {
7200e3d5408SPeter Wemm 	    if (*s == '\0') {
7210e3d5408SPeter Wemm 		break;
7220e3d5408SPeter Wemm 	    } else if (*s++ == '\n') {
7230e3d5408SPeter Wemm 		while (isspace(*s))
7240e3d5408SPeter Wemm 		    s++;
7250e3d5408SPeter Wemm 	    } else {
7260e3d5408SPeter Wemm 		found = TRUE;
7270e3d5408SPeter Wemm 	    }
7280e3d5408SPeter Wemm 	} else if (ch == ':') {
7290e3d5408SPeter Wemm 	    if (found) {
7300e3d5408SPeter Wemm 		tok = base;
7310e3d5408SPeter Wemm 		s[-1] = '\0';
7320e3d5408SPeter Wemm 		*srcp = s;
7330e3d5408SPeter Wemm 		*endp = FALSE;
7340e3d5408SPeter Wemm 		break;
7350e3d5408SPeter Wemm 	    }
7360e3d5408SPeter Wemm 	    base = s;
7370e3d5408SPeter Wemm 	} else if (isgraph(ch)) {
7380e3d5408SPeter Wemm 	    found = TRUE;
7390e3d5408SPeter Wemm 	}
7400e3d5408SPeter Wemm     }
7410e3d5408SPeter Wemm 
7420e3d5408SPeter Wemm     /* malformed entry may end without a ':' */
7430e3d5408SPeter Wemm     if (tok == 0 && found) {
7440e3d5408SPeter Wemm 	tok = base;
7450e3d5408SPeter Wemm     }
7460e3d5408SPeter Wemm 
7470e3d5408SPeter Wemm     return tok;
7480e3d5408SPeter Wemm }
7490e3d5408SPeter Wemm 
7500e3d5408SPeter Wemm static char *
7510e3d5408SPeter Wemm copy_tc_token(char *dst, const char *src, size_t len)
7520e3d5408SPeter Wemm {
7530e3d5408SPeter Wemm     int ch;
7540e3d5408SPeter Wemm 
7550e3d5408SPeter Wemm     while ((ch = *src++) != '\0') {
7560e3d5408SPeter Wemm 	if (ch == '\\' && *src == '\n') {
7570e3d5408SPeter Wemm 	    while (isspace(*src))
7580e3d5408SPeter Wemm 		src++;
7590e3d5408SPeter Wemm 	    continue;
7600e3d5408SPeter Wemm 	}
7610e3d5408SPeter Wemm 	if (--len == 0) {
7620e3d5408SPeter Wemm 	    dst = 0;
7630e3d5408SPeter Wemm 	    break;
7640e3d5408SPeter Wemm 	}
7650e3d5408SPeter Wemm 	*dst++ = ch;
7660e3d5408SPeter Wemm     }
7670e3d5408SPeter Wemm     return dst;
7680e3d5408SPeter Wemm }
7690e3d5408SPeter Wemm 
7700e3d5408SPeter Wemm /*
7710e3d5408SPeter Wemm  * Get an entry for terminal name in buffer bp from the termcap file.
7720e3d5408SPeter Wemm  */
7730e3d5408SPeter Wemm static int
7740e3d5408SPeter Wemm _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
7750e3d5408SPeter Wemm {
7760e3d5408SPeter Wemm     static char *the_source;
7770e3d5408SPeter Wemm 
7780e3d5408SPeter Wemm     register char *p;
7790e3d5408SPeter Wemm     register char *cp;
7807a69bbfbSPeter Wemm     char *dummy = NULL;
7810e3d5408SPeter Wemm     char **fname;
7820e3d5408SPeter Wemm     char *home;
7830e3d5408SPeter Wemm     int i;
7840e3d5408SPeter Wemm     char pathbuf[PBUFSIZ];	/* holds raw path of filenames */
7850e3d5408SPeter Wemm     char *pathvec[PVECSIZ];	/* to point to names in pathbuf */
7860e3d5408SPeter Wemm     char **pvec;		/* holds usable tail of path vector */
7870e3d5408SPeter Wemm     char *termpath;
78818259542SPeter Wemm     string_desc desc;
7890e3d5408SPeter Wemm 
7900e3d5408SPeter Wemm     fname = pathvec;
7910e3d5408SPeter Wemm     pvec = pathvec;
7920e3d5408SPeter Wemm     tbuf = bp;
7930e3d5408SPeter Wemm     p = pathbuf;
79418259542SPeter Wemm     cp = use_terminfo_vars()? getenv("TERMCAP") : NULL;
7950e3d5408SPeter Wemm 
7960e3d5408SPeter Wemm     /*
79715589c42SPeter Wemm      * TERMCAP can have one of two things in it.  It can be the name of a file
79815589c42SPeter Wemm      * to use instead of /etc/termcap.  In this case it better start with a
79915589c42SPeter Wemm      * "/".  Or it can be an entry to use so we don't have to read the file.
80015589c42SPeter Wemm      * In this case it has to already have the newlines crunched out.  If
80115589c42SPeter Wemm      * TERMCAP does not hold a file name then a path of names is searched
80215589c42SPeter Wemm      * instead.  The path is found in the TERMPATH variable, or becomes
80315589c42SPeter Wemm      * "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
8040e3d5408SPeter Wemm      */
80518259542SPeter Wemm     _nc_str_init(&desc, pathbuf, sizeof(pathbuf));
80618259542SPeter Wemm     if (cp == NULL) {
80739f2269fSPeter Wemm 	_nc_safe_strcpy(&desc, get_termpath());
80818259542SPeter Wemm     } else if (!is_pathname(cp)) {	/* TERMCAP holds an entry */
80939f2269fSPeter Wemm 	if ((termpath = get_termpath()) != 0) {
81018259542SPeter Wemm 	    _nc_safe_strcat(&desc, termpath);
8110e3d5408SPeter Wemm 	} else {
81218259542SPeter Wemm 	    char temp[PBUFSIZ];
81318259542SPeter Wemm 	    temp[0] = 0;
81418259542SPeter Wemm 	    if ((home = getenv("HOME")) != 0 && *home != '\0'
81518259542SPeter Wemm 		&& strchr(home, ' ') == 0
81618259542SPeter Wemm 		&& strlen(home) < sizeof(temp) - 10) {	/* setup path */
81718259542SPeter Wemm 		sprintf(temp, "%s/", home);	/* $HOME first */
8180e3d5408SPeter Wemm 	    }
81918259542SPeter Wemm 	    /* if no $HOME look in current directory */
82018259542SPeter Wemm 	    strcat(temp, ".termcap");
82118259542SPeter Wemm 	    _nc_safe_strcat(&desc, temp);
82239f2269fSPeter Wemm 	    _nc_safe_strcat(&desc, " ");
82339f2269fSPeter Wemm 	    _nc_safe_strcat(&desc, get_termpath());
82418259542SPeter Wemm 	}
82518259542SPeter Wemm     } else {			/* user-defined name in TERMCAP */
82618259542SPeter Wemm 	_nc_safe_strcat(&desc, cp);	/* still can be tokenized */
82718259542SPeter Wemm     }
8280e3d5408SPeter Wemm 
8290e3d5408SPeter Wemm     *fname++ = pathbuf;		/* tokenize path into vector of names */
8300e3d5408SPeter Wemm     while (*++p) {
8317a69bbfbSPeter Wemm 	if (*p == ' ' || *p == NCURSES_PATHSEP) {
8320e3d5408SPeter Wemm 	    *p = '\0';
8330e3d5408SPeter Wemm 	    while (*++p)
8347a69bbfbSPeter Wemm 		if (*p != ' ' && *p != NCURSES_PATHSEP)
8350e3d5408SPeter Wemm 		    break;
8360e3d5408SPeter Wemm 	    if (*p == '\0')
8370e3d5408SPeter Wemm 		break;
8380e3d5408SPeter Wemm 	    *fname++ = p;
8390e3d5408SPeter Wemm 	    if (fname >= pathvec + PVECSIZ) {
8400e3d5408SPeter Wemm 		fname--;
8410e3d5408SPeter Wemm 		break;
8420e3d5408SPeter Wemm 	    }
8430e3d5408SPeter Wemm 	}
8440e3d5408SPeter Wemm     }
8450e3d5408SPeter Wemm     *fname = 0;			/* mark end of vector */
8460e3d5408SPeter Wemm     if (is_pathname(cp)) {
8470e3d5408SPeter Wemm 	if (_nc_cgetset(cp) < 0) {
8480e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
8490e3d5408SPeter Wemm 	}
8500e3d5408SPeter Wemm     }
8510e3d5408SPeter Wemm 
8520e3d5408SPeter Wemm     i = _nc_cgetent(&dummy, lineno, pathvec, name);
8530e3d5408SPeter Wemm 
8540e3d5408SPeter Wemm     /* ncurses' termcap-parsing routines cannot handle multiple adjacent
8550e3d5408SPeter Wemm      * empty fields, and mistakenly use the last valid cap entry instead of
8560e3d5408SPeter Wemm      * the first (breaks tc= includes)
8570e3d5408SPeter Wemm      */
8580e3d5408SPeter Wemm     if (i >= 0) {
8590e3d5408SPeter Wemm 	char *pd, *ps, *tok;
8600e3d5408SPeter Wemm 	int endflag = FALSE;
8610e3d5408SPeter Wemm 	char *list[1023];
8620e3d5408SPeter Wemm 	size_t n, count = 0;
8630e3d5408SPeter Wemm 
8640e3d5408SPeter Wemm 	pd = bp;
8650e3d5408SPeter Wemm 	ps = dummy;
8660e3d5408SPeter Wemm 	while (!endflag && (tok = get_tc_token(&ps, &endflag)) != 0) {
8670e3d5408SPeter Wemm 	    bool ignore = FALSE;
8680e3d5408SPeter Wemm 
8690e3d5408SPeter Wemm 	    for (n = 1; n < count; n++) {
8700e3d5408SPeter Wemm 		char *s = list[n];
8710e3d5408SPeter Wemm 		if (s[0] == tok[0]
8720e3d5408SPeter Wemm 		    && s[1] == tok[1]) {
8730e3d5408SPeter Wemm 		    ignore = TRUE;
8740e3d5408SPeter Wemm 		    break;
8750e3d5408SPeter Wemm 		}
8760e3d5408SPeter Wemm 	    }
8770e3d5408SPeter Wemm 	    if (ignore != TRUE) {
8780e3d5408SPeter Wemm 		list[count++] = tok;
8790e3d5408SPeter Wemm 		pd = copy_tc_token(pd, tok, TBUFSIZ - (2 + pd - bp));
8800e3d5408SPeter Wemm 		if (pd == 0) {
8810e3d5408SPeter Wemm 		    i = -1;
8820e3d5408SPeter Wemm 		    break;
8830e3d5408SPeter Wemm 		}
8840e3d5408SPeter Wemm 		*pd++ = ':';
8850e3d5408SPeter Wemm 		*pd = '\0';
8860e3d5408SPeter Wemm 	    }
8870e3d5408SPeter Wemm 	}
8880e3d5408SPeter Wemm     }
8890e3d5408SPeter Wemm 
8900e3d5408SPeter Wemm     FreeIfNeeded(dummy);
8910e3d5408SPeter Wemm     FreeIfNeeded(the_source);
8920e3d5408SPeter Wemm     the_source = 0;
8930e3d5408SPeter Wemm 
8940e3d5408SPeter Wemm     /* This is not related to the BSD cgetent(), but to fake up a suitable
8950e3d5408SPeter Wemm      * filename for ncurses' error reporting.  (If we are not using BSD
8960e3d5408SPeter Wemm      * cgetent, then it is the actual filename).
8970e3d5408SPeter Wemm      */
8980e3d5408SPeter Wemm     if (i >= 0) {
8990e3d5408SPeter Wemm 	if ((the_source = strdup(pathvec[i])) != 0)
9000e3d5408SPeter Wemm 	    *sourcename = the_source;
9010e3d5408SPeter Wemm     }
9020e3d5408SPeter Wemm 
9030e3d5408SPeter Wemm     return (i);
9040e3d5408SPeter Wemm }
9050e3d5408SPeter Wemm #endif /* USE_BSD_TGETENT */
9060e3d5408SPeter Wemm #endif /* USE_GETCAP */
9070e3d5408SPeter Wemm 
9080e3d5408SPeter Wemm #define MAXPATHS	32
9090e3d5408SPeter Wemm 
9100e3d5408SPeter Wemm /*
9110e3d5408SPeter Wemm  * Add a filename to the list in 'termpaths[]', checking that we really have
9120e3d5408SPeter Wemm  * a right to open the file.
9130e3d5408SPeter Wemm  */
9140e3d5408SPeter Wemm #if !USE_GETCAP
91515589c42SPeter Wemm static int
91615589c42SPeter Wemm add_tc(char *termpaths[], char *path, int count)
9170e3d5408SPeter Wemm {
91839f2269fSPeter Wemm     char *save = strchr(path, NCURSES_PATHSEP);
91939f2269fSPeter Wemm     if (save != 0)
92039f2269fSPeter Wemm 	*save = '\0';
9210e3d5408SPeter Wemm     if (count < MAXPATHS
92239f2269fSPeter Wemm 	&& _nc_access(path, R_OK) == 0) {
9230e3d5408SPeter Wemm 	termpaths[count++] = path;
92439f2269fSPeter Wemm 	T(("Adding termpath %s", path));
92539f2269fSPeter Wemm     }
9260e3d5408SPeter Wemm     termpaths[count] = 0;
92739f2269fSPeter Wemm     if (save != 0)
92839f2269fSPeter Wemm 	*save = NCURSES_PATHSEP;
9290e3d5408SPeter Wemm     return count;
9300e3d5408SPeter Wemm }
9310e3d5408SPeter Wemm #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count)
9320e3d5408SPeter Wemm #endif /* !USE_GETCAP */
9330e3d5408SPeter Wemm 
9347a69bbfbSPeter Wemm NCURSES_EXPORT(int)
93539f2269fSPeter Wemm _nc_read_termcap_entry(const char *const tn, TERMTYPE * const tp)
9360e3d5408SPeter Wemm {
9370e3d5408SPeter Wemm     int found = FALSE;
9380e3d5408SPeter Wemm     ENTRY *ep;
9390e3d5408SPeter Wemm #if USE_GETCAP_CACHE
9400e3d5408SPeter Wemm     char cwd_buf[PATH_MAX];
9410e3d5408SPeter Wemm #endif
9420e3d5408SPeter Wemm #if USE_GETCAP
94315589c42SPeter Wemm     char *p, tc[TBUFSIZ];
9440e3d5408SPeter Wemm     static char *source;
9450e3d5408SPeter Wemm     static int lineno;
9460e3d5408SPeter Wemm 
94739f2269fSPeter Wemm     T(("read termcap entry for %s", tn));
94818259542SPeter Wemm     if (use_terminfo_vars() && (p = getenv("TERMCAP")) != 0
94915589c42SPeter Wemm 	&& !is_pathname(p) && _nc_name_match(p, tn, "|:")) {
95015589c42SPeter Wemm 	/* TERMCAP holds a termcap entry */
95115589c42SPeter Wemm 	strncpy(tc, p, sizeof(tc) - 1);
95215589c42SPeter Wemm 	tc[sizeof(tc) - 1] = '\0';
95315589c42SPeter Wemm 	_nc_set_source("TERMCAP");
95415589c42SPeter Wemm     } else {
9550e3d5408SPeter Wemm 	/* we're using getcap(3) */
9560e3d5408SPeter Wemm 	if (_nc_tgetent(tc, &source, &lineno, tn) < 0)
9570e3d5408SPeter Wemm 	    return (ERR);
9580e3d5408SPeter Wemm 
9590e3d5408SPeter Wemm 	_nc_curr_line = lineno;
9600e3d5408SPeter Wemm 	_nc_set_source(source);
96115589c42SPeter Wemm     }
9620e3d5408SPeter Wemm     _nc_read_entry_source((FILE *) 0, tc, FALSE, FALSE, NULLHOOK);
9630e3d5408SPeter Wemm #else
9640e3d5408SPeter Wemm     /*
9650e3d5408SPeter Wemm      * Here is what the 4.4BSD termcap(3) page prescribes:
9660e3d5408SPeter Wemm      *
96715589c42SPeter Wemm      * It will look in the environment for a TERMCAP variable.  If found, and
96815589c42SPeter Wemm      * the value does not begin with a slash, and the terminal type name is the
96915589c42SPeter Wemm      * same as the environment string TERM, the TERMCAP string is used instead
97015589c42SPeter Wemm      * of reading a termcap file.  If it does begin with a slash, the string is
97115589c42SPeter Wemm      * used as a path name of the termcap file to search.  If TERMCAP does not
97215589c42SPeter Wemm      * begin with a slash and name is different from TERM, tgetent() searches
97315589c42SPeter Wemm      * the files $HOME/.termcap and /usr/share/misc/termcap, in that order,
97415589c42SPeter Wemm      * unless the environment variable TERMPATH exists, in which case it
97515589c42SPeter Wemm      * specifies a list of file pathnames (separated by spaces or colons) to be
97615589c42SPeter Wemm      * searched instead.
9770e3d5408SPeter Wemm      *
9780e3d5408SPeter Wemm      * It goes on to state:
9790e3d5408SPeter Wemm      *
9800e3d5408SPeter Wemm      * Whenever multiple files are searched and a tc field occurs in the
98115589c42SPeter Wemm      * requested entry, the entry it names must be found in the same file or
98215589c42SPeter Wemm      * one of the succeeding files.
9830e3d5408SPeter Wemm      *
9840e3d5408SPeter Wemm      * However, this restriction is relaxed in ncurses; tc references to
9850e3d5408SPeter Wemm      * previous files are permitted.
9860e3d5408SPeter Wemm      *
98715589c42SPeter Wemm      * This routine returns 1 if an entry is found, 0 if not found, and -1 if
98815589c42SPeter Wemm      * the database is not accessible.
9890e3d5408SPeter Wemm      */
9900e3d5408SPeter Wemm     FILE *fp;
9910e3d5408SPeter Wemm     char *tc, *termpaths[MAXPATHS];
9920e3d5408SPeter Wemm     int filecount = 0;
99339f2269fSPeter Wemm     int j, k;
9940e3d5408SPeter Wemm     bool use_buffer = FALSE;
99539f2269fSPeter Wemm     bool normal = TRUE;
9960e3d5408SPeter Wemm     char tc_buf[1024];
9970e3d5408SPeter Wemm     char pathbuf[PATH_MAX];
99839f2269fSPeter Wemm     char *copied = 0;
99939f2269fSPeter Wemm     char *cp;
100039f2269fSPeter Wemm     struct stat test_stat[MAXPATHS];
10010e3d5408SPeter Wemm 
10020e3d5408SPeter Wemm     termpaths[filecount] = 0;
100318259542SPeter Wemm     if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) {
100415589c42SPeter Wemm 	if (is_pathname(tc)) {	/* interpret as a filename */
10050e3d5408SPeter Wemm 	    ADD_TC(tc, 0);
100639f2269fSPeter Wemm 	    normal = FALSE;
100715589c42SPeter Wemm 	} else if (_nc_name_match(tc, tn, "|:")) {	/* treat as a capability file */
10080e3d5408SPeter Wemm 	    use_buffer = TRUE;
10090e3d5408SPeter Wemm 	    (void) sprintf(tc_buf, "%.*s\n", (int) sizeof(tc_buf) - 2, tc);
101039f2269fSPeter Wemm 	    normal = FALSE;
101139f2269fSPeter Wemm 	}
101239f2269fSPeter Wemm     }
10130e3d5408SPeter Wemm 
101439f2269fSPeter Wemm     if (normal) {		/* normal case */
101539f2269fSPeter Wemm 	char envhome[PATH_MAX], *h;
101639f2269fSPeter Wemm 
101739f2269fSPeter Wemm 	copied = strdup(get_termpath());
101839f2269fSPeter Wemm 	for (cp = copied; *cp; cp++) {
10197a69bbfbSPeter Wemm 	    if (*cp == NCURSES_PATHSEP)
10200e3d5408SPeter Wemm 		*cp = '\0';
102139f2269fSPeter Wemm 	    else if (cp == copied || cp[-1] == '\0') {
10220e3d5408SPeter Wemm 		ADD_TC(cp, filecount);
10230e3d5408SPeter Wemm 	    }
10240e3d5408SPeter Wemm 	}
10250e3d5408SPeter Wemm 
10260e3d5408SPeter Wemm #define PRIVATE_CAP "%s/.termcap"
10270e3d5408SPeter Wemm 
10287a69bbfbSPeter Wemm 	if (use_terminfo_vars() && (h = getenv("HOME")) != NULL && *h != '\0'
102915589c42SPeter Wemm 	    && (strlen(h) + sizeof(PRIVATE_CAP)) < PATH_MAX) {
10300e3d5408SPeter Wemm 	    /* user's .termcap, if any, should override it */
10310e3d5408SPeter Wemm 	    (void) strcpy(envhome, h);
10320e3d5408SPeter Wemm 	    (void) sprintf(pathbuf, PRIVATE_CAP, envhome);
10330e3d5408SPeter Wemm 	    ADD_TC(pathbuf, filecount);
10340e3d5408SPeter Wemm 	}
10350e3d5408SPeter Wemm     }
10360e3d5408SPeter Wemm 
103739f2269fSPeter Wemm     /*
103839f2269fSPeter Wemm      * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
103939f2269fSPeter Wemm      * Avoid reading the same file twice.
104039f2269fSPeter Wemm      */
104139f2269fSPeter Wemm #ifdef HAVE_LINK
104239f2269fSPeter Wemm     for (j = 0; j < filecount; j++) {
104339f2269fSPeter Wemm 	bool omit = FALSE;
104439f2269fSPeter Wemm 	if (stat(termpaths[j], &test_stat[j]) != 0
104539f2269fSPeter Wemm 	    || (test_stat[j].st_mode & S_IFMT) != S_IFREG) {
104639f2269fSPeter Wemm 	    omit = TRUE;
104739f2269fSPeter Wemm 	} else {
104839f2269fSPeter Wemm 	    for (k = 0; k < j; k++) {
104939f2269fSPeter Wemm 		if (test_stat[k].st_dev == test_stat[j].st_dev
105039f2269fSPeter Wemm 		    && test_stat[k].st_ino == test_stat[j].st_ino) {
105139f2269fSPeter Wemm 		    omit = TRUE;
105239f2269fSPeter Wemm 		    break;
105339f2269fSPeter Wemm 		}
105439f2269fSPeter Wemm 	    }
105539f2269fSPeter Wemm 	}
105639f2269fSPeter Wemm 	if (omit) {
105739f2269fSPeter Wemm 	    T(("Path %s is a duplicate", termpaths[j]));
105839f2269fSPeter Wemm 	    for (k = j + 1; k < filecount; k++) {
105939f2269fSPeter Wemm 		termpaths[k - 1] = termpaths[k];
106039f2269fSPeter Wemm 		test_stat[k - 1] = test_stat[k];
106139f2269fSPeter Wemm 	    }
106239f2269fSPeter Wemm 	    --filecount;
106339f2269fSPeter Wemm 	    --j;
106439f2269fSPeter Wemm 	}
106539f2269fSPeter Wemm     }
106639f2269fSPeter Wemm #endif
106739f2269fSPeter Wemm 
10680e3d5408SPeter Wemm     /* parse the sources */
106915589c42SPeter Wemm     if (use_buffer) {
10700e3d5408SPeter Wemm 	_nc_set_source("TERMCAP");
10710e3d5408SPeter Wemm 
10720e3d5408SPeter Wemm 	/*
10730e3d5408SPeter Wemm 	 * We don't suppress warning messages here.  The presumption is
10740e3d5408SPeter Wemm 	 * that since it's just a single entry, they won't be a pain.
10750e3d5408SPeter Wemm 	 */
10760e3d5408SPeter Wemm 	_nc_read_entry_source((FILE *) 0, tc_buf, FALSE, FALSE, NULLHOOK);
10770e3d5408SPeter Wemm     } else {
10780e3d5408SPeter Wemm 	int i;
10790e3d5408SPeter Wemm 
10800e3d5408SPeter Wemm 	for (i = 0; i < filecount; i++) {
10810e3d5408SPeter Wemm 
10820e3d5408SPeter Wemm 	    T(("Looking for %s in %s", tn, termpaths[i]));
108339f2269fSPeter Wemm 	    if (_nc_access(termpaths[i], R_OK) == 0
108439f2269fSPeter Wemm 		&& (fp = fopen(termpaths[i], "r")) != (FILE *) 0) {
10850e3d5408SPeter Wemm 		_nc_set_source(termpaths[i]);
10860e3d5408SPeter Wemm 
10870e3d5408SPeter Wemm 		/*
108815589c42SPeter Wemm 		 * Suppress warning messages.  Otherwise you get 400 lines of
108915589c42SPeter Wemm 		 * crap from archaic termcap files as ncurses complains about
109015589c42SPeter Wemm 		 * all the obsolete capabilities.
10910e3d5408SPeter Wemm 		 */
10920e3d5408SPeter Wemm 		_nc_read_entry_source(fp, (char *) 0, FALSE, TRUE, NULLHOOK);
10930e3d5408SPeter Wemm 
10940e3d5408SPeter Wemm 		(void) fclose(fp);
10950e3d5408SPeter Wemm 	    }
10960e3d5408SPeter Wemm 	}
10970e3d5408SPeter Wemm     }
109839f2269fSPeter Wemm     if (copied != 0)
109939f2269fSPeter Wemm 	free(copied);
11000e3d5408SPeter Wemm #endif /* USE_GETCAP */
11010e3d5408SPeter Wemm 
11020e3d5408SPeter Wemm     if (_nc_head == 0)
11030e3d5408SPeter Wemm 	return (ERR);
11040e3d5408SPeter Wemm 
11050e3d5408SPeter Wemm     /* resolve all use references */
110615589c42SPeter Wemm     _nc_resolve_uses(TRUE);
11070e3d5408SPeter Wemm 
11080e3d5408SPeter Wemm     /* find a terminal matching tn, if we can */
11090e3d5408SPeter Wemm #if USE_GETCAP_CACHE
111015589c42SPeter Wemm     if (getcwd(cwd_buf, sizeof(cwd_buf)) != 0) {
11110e3d5408SPeter Wemm 	_nc_set_writedir((char *) 0);	/* note: this does a chdir */
11120e3d5408SPeter Wemm #endif
11130e3d5408SPeter Wemm 	for_entry_list(ep) {
111415589c42SPeter Wemm 	    if (_nc_name_match(ep->tterm.term_names, tn, "|:")) {
11150e3d5408SPeter Wemm 		/*
111615589c42SPeter Wemm 		 * Make a local copy of the terminal capabilities.  Free all
111715589c42SPeter Wemm 		 * entry storage except the string table for the loaded type
111815589c42SPeter Wemm 		 * (which we disconnected from the list by NULLing out
11190e3d5408SPeter Wemm 		 * ep->tterm.str_table above).
11200e3d5408SPeter Wemm 		 */
11210e3d5408SPeter Wemm 		*tp = ep->tterm;
11220e3d5408SPeter Wemm 		ep->tterm.str_table = (char *) 0;
11230e3d5408SPeter Wemm 
11240e3d5408SPeter Wemm 		/*
112515589c42SPeter Wemm 		 * OK, now try to write the type to user's terminfo directory.
112615589c42SPeter Wemm 		 * Next time he loads this, it will come through terminfo.
11270e3d5408SPeter Wemm 		 *
112815589c42SPeter Wemm 		 * Advantage:  Second and subsequent fetches of this entry will
112915589c42SPeter Wemm 		 * be very fast.
11300e3d5408SPeter Wemm 		 *
113115589c42SPeter Wemm 		 * Disadvantage:  After the first time a termcap type is loaded
113215589c42SPeter Wemm 		 * by its user, editing it in the /etc/termcap file, or in
113315589c42SPeter Wemm 		 * TERMCAP, or in a local ~/.termcap, will be ineffective
113415589c42SPeter Wemm 		 * unless the terminfo entry is explicitly removed.
11350e3d5408SPeter Wemm 		 */
11360e3d5408SPeter Wemm #if USE_GETCAP_CACHE
11370e3d5408SPeter Wemm 		(void) _nc_write_entry(tp);
11380e3d5408SPeter Wemm #endif
11390e3d5408SPeter Wemm 		found = TRUE;
11400e3d5408SPeter Wemm 		break;
11410e3d5408SPeter Wemm 	    }
11420e3d5408SPeter Wemm 	}
11430e3d5408SPeter Wemm #if USE_GETCAP_CACHE
11440e3d5408SPeter Wemm 	chdir(cwd_buf);
11450e3d5408SPeter Wemm     }
11460e3d5408SPeter Wemm #endif
11470e3d5408SPeter Wemm 
11480e3d5408SPeter Wemm     _nc_free_entries(_nc_head);
11490e3d5408SPeter Wemm     return (found);
11500e3d5408SPeter Wemm }
11510e3d5408SPeter Wemm #else
11527a69bbfbSPeter Wemm extern
11537a69bbfbSPeter Wemm NCURSES_EXPORT(void)
11547a69bbfbSPeter Wemm _nc_read_termcap(void);
11557a69bbfbSPeter Wemm NCURSES_EXPORT(void)
115615589c42SPeter Wemm _nc_read_termcap(void)
115715589c42SPeter Wemm {
115815589c42SPeter Wemm }
11590e3d5408SPeter Wemm #endif /* PURE_TERMINFO */
1160