xref: /freebsd/contrib/ncurses/ncurses/tinfo/read_termcap.c (revision 4a1a95108dd76c4259fe6c37c4471f7969b17983)
10e3d5408SPeter Wemm /****************************************************************************
24a1a9510SRong-En Fan  * Copyright (c) 1998-2005,2006 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>                         *
324a1a9510SRong-En Fan  *     and: Thomas E. Dickey                        1996-on                 *
330e3d5408SPeter Wemm  ****************************************************************************/
340e3d5408SPeter Wemm 
350e3d5408SPeter Wemm /*
360e3d5408SPeter Wemm  * Termcap compatibility support
370e3d5408SPeter Wemm  *
380e3d5408SPeter Wemm  * If your OS integrator didn't install a terminfo database, you can call
390e3d5408SPeter Wemm  * _nc_read_termcap_entry() to support reading and translating capabilities
400e3d5408SPeter Wemm  * from the system termcap file.  This is a kludge; it will bulk up and slow
410e3d5408SPeter Wemm  * down every program that uses ncurses, and translated termcap entries cannot
420e3d5408SPeter Wemm  * use full terminfo capabilities.  Don't use it unless you absolutely have to;
430e3d5408SPeter Wemm  * instead, get your system people to run tic(1) from root on the terminfo
440e3d5408SPeter Wemm  * master included with ncurses to translate it into a terminfo database.
450e3d5408SPeter Wemm  *
460e3d5408SPeter Wemm  * If USE_GETCAP is enabled, we use what is effectively a copy of the 4.4BSD
470e3d5408SPeter Wemm  * getcap code to fetch entries.  There are disadvantages to this; mainly that
480e3d5408SPeter Wemm  * getcap(3) does its own resolution, meaning that entries read in in this way
490e3d5408SPeter Wemm  * can't reference the terminfo tree.  The only thing it buys is faster startup
500e3d5408SPeter Wemm  * time, getcap(3) is much faster than our tic parser.
510e3d5408SPeter Wemm  */
520e3d5408SPeter Wemm 
530e3d5408SPeter Wemm #include <curses.priv.h>
540e3d5408SPeter Wemm 
550e3d5408SPeter Wemm #include <ctype.h>
5639f2269fSPeter Wemm #include <sys/types.h>
5739f2269fSPeter Wemm #include <sys/stat.h>
580e3d5408SPeter Wemm #include <tic.h>
590e3d5408SPeter Wemm #include <term_entry.h>
600e3d5408SPeter Wemm 
614a1a9510SRong-En Fan MODULE_ID("$Id: read_termcap.c,v 1.71 2006/07/29 12:06:51 tom Exp $")
620e3d5408SPeter Wemm 
6318259542SPeter Wemm #if !PURE_TERMINFO
640e3d5408SPeter Wemm 
650e3d5408SPeter Wemm #define TC_SUCCESS     0
664a1a9510SRong-En Fan #define TC_NOT_FOUND  -1
674a1a9510SRong-En Fan #define TC_SYS_ERR    -2
684a1a9510SRong-En Fan #define TC_REF_LOOP   -3
694a1a9510SRong-En Fan #define TC_UNRESOLVED -4	/* this is not returned by BSD cgetent */
700e3d5408SPeter Wemm 
714a1a9510SRong-En Fan static NCURSES_CONST char *
7239f2269fSPeter Wemm get_termpath(void)
7339f2269fSPeter Wemm {
744a1a9510SRong-En Fan     NCURSES_CONST char *result;
7539f2269fSPeter Wemm 
7639f2269fSPeter Wemm     if (!use_terminfo_vars() || (result = getenv("TERMPATH")) == 0)
7739f2269fSPeter Wemm 	result = TERMPATH;
7839f2269fSPeter Wemm     T(("TERMPATH is %s", result));
7939f2269fSPeter Wemm     return result;
8039f2269fSPeter Wemm }
8139f2269fSPeter Wemm 
820e3d5408SPeter Wemm #if USE_GETCAP
830e3d5408SPeter Wemm 
840e3d5408SPeter Wemm #if HAVE_BSD_CGETENT
850e3d5408SPeter Wemm #define _nc_cgetcap   cgetcap
860e3d5408SPeter Wemm #define _nc_cgetent(buf, oline, db_array, name) cgetent(buf, db_array, name)
870e3d5408SPeter Wemm #define _nc_cgetmatch cgetmatch
880e3d5408SPeter Wemm #define _nc_cgetset   cgetset
890e3d5408SPeter Wemm #else
900e3d5408SPeter Wemm static int _nc_cgetmatch(char *, const char *);
9118259542SPeter Wemm static int _nc_getent(char **, unsigned *, int *, int, char **, int, const char
9218259542SPeter Wemm 		      *, int, char *);
930e3d5408SPeter Wemm static int _nc_nfcmp(const char *, char *);
940e3d5408SPeter Wemm 
950e3d5408SPeter Wemm /*-
960e3d5408SPeter Wemm  * Copyright (c) 1992, 1993
970e3d5408SPeter Wemm  *	The Regents of the University of California.  All rights reserved.
980e3d5408SPeter Wemm  *
990e3d5408SPeter Wemm  * This code is derived from software contributed to Berkeley by
1000e3d5408SPeter Wemm  * Casey Leedom of Lawrence Livermore National Laboratory.
1010e3d5408SPeter Wemm  *
1020e3d5408SPeter Wemm  * Redistribution and use in source and binary forms, with or without
1030e3d5408SPeter Wemm  * modification, are permitted provided that the following conditions
1040e3d5408SPeter Wemm  * are met:
1050e3d5408SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
1060e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
1070e3d5408SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
1080e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
1090e3d5408SPeter Wemm  *    documentation and/or other materials provided with the distribution.
1100e3d5408SPeter Wemm  * 3. All advertising materials mentioning features or use of this software
1110e3d5408SPeter Wemm  *    must display the following acknowledgment:
1120e3d5408SPeter Wemm  *	This product includes software developed by the University of
1130e3d5408SPeter Wemm  *	California, Berkeley and its contributors.
1140e3d5408SPeter Wemm  * 4. Neither the name of the University nor the names of its contributors
1150e3d5408SPeter Wemm  *    may be used to endorse or promote products derived from this software
1160e3d5408SPeter Wemm  *    without specific prior written permission.
1170e3d5408SPeter Wemm  *
1180e3d5408SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1190e3d5408SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1200e3d5408SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1210e3d5408SPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
1220e3d5408SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1230e3d5408SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1240e3d5408SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1250e3d5408SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1260e3d5408SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1270e3d5408SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1280e3d5408SPeter Wemm  * SUCH DAMAGE.
1290e3d5408SPeter Wemm  */
1300e3d5408SPeter Wemm 
1310e3d5408SPeter Wemm /* static char sccsid[] = "@(#)getcap.c	8.3 (Berkeley) 3/25/94"; */
1320e3d5408SPeter Wemm 
1330e3d5408SPeter Wemm #define	BFRAG		1024
1340e3d5408SPeter Wemm #define	BSIZE		1024
1350e3d5408SPeter Wemm #define	MAX_RECURSION	32	/* maximum getent recursion */
1360e3d5408SPeter Wemm 
1370e3d5408SPeter Wemm static size_t topreclen;	/* toprec length */
1380e3d5408SPeter Wemm static char *toprec;		/* Additional record specified by cgetset() */
1390e3d5408SPeter Wemm static int gottoprec;		/* Flag indicating retrieval of toprecord */
1400e3d5408SPeter Wemm 
1410e3d5408SPeter Wemm /*
1420e3d5408SPeter Wemm  * Cgetset() allows the addition of a user specified buffer to be added to the
1430e3d5408SPeter Wemm  * database array, in effect "pushing" the buffer on top of the virtual
1440e3d5408SPeter Wemm  * database.  0 is returned on success, -1 on failure.
1450e3d5408SPeter Wemm  */
1460e3d5408SPeter Wemm static int
1470e3d5408SPeter Wemm _nc_cgetset(const char *ent)
1480e3d5408SPeter Wemm {
1490e3d5408SPeter Wemm     if (ent == 0) {
1500e3d5408SPeter Wemm 	FreeIfNeeded(toprec);
1510e3d5408SPeter Wemm 	toprec = 0;
1520e3d5408SPeter Wemm 	topreclen = 0;
1530e3d5408SPeter Wemm 	return (0);
1540e3d5408SPeter Wemm     }
1550e3d5408SPeter Wemm     topreclen = strlen(ent);
1560e3d5408SPeter Wemm     if ((toprec = typeMalloc(char, topreclen + 1)) == 0) {
1570e3d5408SPeter Wemm 	errno = ENOMEM;
1580e3d5408SPeter Wemm 	return (-1);
1590e3d5408SPeter Wemm     }
1600e3d5408SPeter Wemm     gottoprec = 0;
1610e3d5408SPeter Wemm     (void) strcpy(toprec, ent);
1620e3d5408SPeter Wemm     return (0);
1630e3d5408SPeter Wemm }
1640e3d5408SPeter Wemm 
1650e3d5408SPeter Wemm /*
1660e3d5408SPeter Wemm  * Cgetcap searches the capability record buf for the capability cap with type
1670e3d5408SPeter Wemm  * `type'.  A pointer to the value of cap is returned on success, 0 if the
1680e3d5408SPeter Wemm  * requested capability couldn't be found.
1690e3d5408SPeter Wemm  *
1700e3d5408SPeter Wemm  * Specifying a type of ':' means that nothing should follow cap (:cap:).  In
1710e3d5408SPeter Wemm  * this case a pointer to the terminating ':' or NUL will be returned if cap is
1720e3d5408SPeter Wemm  * found.
1730e3d5408SPeter Wemm  *
1740e3d5408SPeter Wemm  * If (cap, '@') or (cap, terminator, '@') is found before (cap, terminator)
1750e3d5408SPeter Wemm  * return 0.
1760e3d5408SPeter Wemm  */
1770e3d5408SPeter Wemm static char *
1780e3d5408SPeter Wemm _nc_cgetcap(char *buf, const char *cap, int type)
1790e3d5408SPeter Wemm {
1800e3d5408SPeter Wemm     register const char *cp;
1810e3d5408SPeter Wemm     register char *bp;
1820e3d5408SPeter Wemm 
1830e3d5408SPeter Wemm     bp = buf;
1840e3d5408SPeter Wemm     for (;;) {
1850e3d5408SPeter Wemm 	/*
1860e3d5408SPeter Wemm 	 * Skip past the current capability field - it's either the
1870e3d5408SPeter Wemm 	 * name field if this is the first time through the loop, or
1880e3d5408SPeter Wemm 	 * the remainder of a field whose name failed to match cap.
1890e3d5408SPeter Wemm 	 */
1900e3d5408SPeter Wemm 	for (;;) {
1910e3d5408SPeter Wemm 	    if (*bp == '\0')
1920e3d5408SPeter Wemm 		return (0);
1930e3d5408SPeter Wemm 	    else if (*bp++ == ':')
1940e3d5408SPeter Wemm 		break;
1950e3d5408SPeter Wemm 	}
1960e3d5408SPeter Wemm 
1970e3d5408SPeter Wemm 	/*
1980e3d5408SPeter Wemm 	 * Try to match (cap, type) in buf.
1990e3d5408SPeter Wemm 	 */
2000e3d5408SPeter Wemm 	for (cp = cap; *cp == *bp && *bp != '\0'; cp++, bp++)
2010e3d5408SPeter Wemm 	    continue;
2020e3d5408SPeter Wemm 	if (*cp != '\0')
2030e3d5408SPeter Wemm 	    continue;
2040e3d5408SPeter Wemm 	if (*bp == '@')
2050e3d5408SPeter Wemm 	    return (0);
2060e3d5408SPeter Wemm 	if (type == ':') {
2070e3d5408SPeter Wemm 	    if (*bp != '\0' && *bp != ':')
2080e3d5408SPeter Wemm 		continue;
2090e3d5408SPeter Wemm 	    return (bp);
2100e3d5408SPeter Wemm 	}
2110e3d5408SPeter Wemm 	if (*bp != type)
2120e3d5408SPeter Wemm 	    continue;
2130e3d5408SPeter Wemm 	bp++;
2140e3d5408SPeter Wemm 	return (*bp == '@' ? 0 : bp);
2150e3d5408SPeter Wemm     }
2160e3d5408SPeter Wemm     /* NOTREACHED */
2170e3d5408SPeter Wemm }
2180e3d5408SPeter Wemm 
2190e3d5408SPeter Wemm /*
2200e3d5408SPeter Wemm  * Cgetent extracts the capability record name from the NULL terminated file
2210e3d5408SPeter Wemm  * array db_array and returns a pointer to a malloc'd copy of it in buf.  Buf
2220e3d5408SPeter Wemm  * must be retained through all subsequent calls to cgetcap, cgetnum, cgetflag,
2230e3d5408SPeter Wemm  * and cgetstr, but may then be freed.
2240e3d5408SPeter Wemm  *
2250e3d5408SPeter Wemm  * Returns:
2260e3d5408SPeter Wemm  *
2270e3d5408SPeter Wemm  * positive #    on success (i.e., the index in db_array)
2280e3d5408SPeter Wemm  * TC_NOT_FOUND  if the requested record couldn't be found
2290e3d5408SPeter Wemm  * TC_SYS_ERR    if a system error was encountered (e.g.,couldn't open a file)
2300e3d5408SPeter Wemm  * TC_REF_LOOP   if a potential reference loop is detected
2314a1a9510SRong-En Fan  * TC_UNRESOLVED if we had too many recurrences to resolve
2320e3d5408SPeter Wemm  */
2330e3d5408SPeter Wemm static int
2340e3d5408SPeter Wemm _nc_cgetent(char **buf, int *oline, char **db_array, const char *name)
2350e3d5408SPeter Wemm {
23615589c42SPeter Wemm     unsigned dummy;
2370e3d5408SPeter Wemm 
2380e3d5408SPeter Wemm     return (_nc_getent(buf, &dummy, oline, 0, db_array, -1, name, 0, 0));
2390e3d5408SPeter Wemm }
2400e3d5408SPeter Wemm 
2410e3d5408SPeter Wemm /*
2420e3d5408SPeter Wemm  * Getent implements the functions of cgetent.  If fd is non-negative,
2430e3d5408SPeter Wemm  * *db_array has already been opened and fd is the open file descriptor.  We
2440e3d5408SPeter Wemm  * do this to save time and avoid using up file descriptors for tc=
2450e3d5408SPeter Wemm  * recursions.
2460e3d5408SPeter Wemm  *
2470e3d5408SPeter Wemm  * Getent returns the same success/failure codes as cgetent.  On success, a
2480e3d5408SPeter Wemm  * pointer to a malloc'd capability record with all tc= capabilities fully
2490e3d5408SPeter Wemm  * expanded and its length (not including trailing ASCII NUL) are left in
2500e3d5408SPeter Wemm  * *cap and *len.
2510e3d5408SPeter Wemm  *
2520e3d5408SPeter Wemm  * Basic algorithm:
2530e3d5408SPeter Wemm  *	+ Allocate memory incrementally as needed in chunks of size BFRAG
2540e3d5408SPeter Wemm  *	  for capability buffer.
2550e3d5408SPeter Wemm  *	+ Recurse for each tc=name and interpolate result.  Stop when all
2560e3d5408SPeter Wemm  *	  names interpolated, a name can't be found, or depth exceeds
2570e3d5408SPeter Wemm  *	  MAX_RECURSION.
2580e3d5408SPeter Wemm  */
2590e3d5408SPeter Wemm #define DOALLOC(size) typeRealloc(char, size, record)
2600e3d5408SPeter Wemm static int
2610e3d5408SPeter Wemm _nc_getent(
2620e3d5408SPeter Wemm 	      char **cap,	/* termcap-content */
26315589c42SPeter Wemm 	      unsigned *len,	/* length, needed for recursion */
2640e3d5408SPeter Wemm 	      int *beginning,	/* line-number at match */
2650e3d5408SPeter Wemm 	      int in_array,	/* index in 'db_array[] */
2660e3d5408SPeter Wemm 	      char **db_array,	/* list of files to search */
2670e3d5408SPeter Wemm 	      int fd,
2680e3d5408SPeter Wemm 	      const char *name,
2690e3d5408SPeter Wemm 	      int depth,
2700e3d5408SPeter Wemm 	      char *nfield)
2710e3d5408SPeter Wemm {
2720e3d5408SPeter Wemm     register char *r_end, *rp;
2730e3d5408SPeter Wemm     int myfd = FALSE;
2740e3d5408SPeter Wemm     char *record = 0;
2750e3d5408SPeter Wemm     int tc_not_resolved;
2760e3d5408SPeter Wemm     int current;
2770e3d5408SPeter Wemm     int lineno;
2780e3d5408SPeter Wemm 
2790e3d5408SPeter Wemm     /*
2800e3d5408SPeter Wemm      * Return with ``loop detected'' error if we've recurred more than
2810e3d5408SPeter Wemm      * MAX_RECURSION times.
2820e3d5408SPeter Wemm      */
2830e3d5408SPeter Wemm     if (depth > MAX_RECURSION)
2840e3d5408SPeter Wemm 	return (TC_REF_LOOP);
2850e3d5408SPeter Wemm 
2860e3d5408SPeter Wemm     /*
2870e3d5408SPeter Wemm      * Check if we have a top record from cgetset().
2880e3d5408SPeter Wemm      */
2890e3d5408SPeter Wemm     if (depth == 0 && toprec != 0 && _nc_cgetmatch(toprec, name) == 0) {
2900e3d5408SPeter Wemm 	if ((record = DOALLOC(topreclen + BFRAG)) == 0) {
2910e3d5408SPeter Wemm 	    errno = ENOMEM;
2920e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
2930e3d5408SPeter Wemm 	}
2940e3d5408SPeter Wemm 	(void) strcpy(record, toprec);
2950e3d5408SPeter Wemm 	rp = record + topreclen + 1;
2960e3d5408SPeter Wemm 	r_end = rp + BFRAG;
2970e3d5408SPeter Wemm 	current = in_array;
2980e3d5408SPeter Wemm     } else {
2990e3d5408SPeter Wemm 	int foundit;
3000e3d5408SPeter Wemm 
3010e3d5408SPeter Wemm 	/*
3020e3d5408SPeter Wemm 	 * Allocate first chunk of memory.
3030e3d5408SPeter Wemm 	 */
3040e3d5408SPeter Wemm 	if ((record = DOALLOC(BFRAG)) == 0) {
3050e3d5408SPeter Wemm 	    errno = ENOMEM;
3060e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
3070e3d5408SPeter Wemm 	}
3080e3d5408SPeter Wemm 	rp = r_end = record + BFRAG;
3090e3d5408SPeter Wemm 	foundit = FALSE;
3100e3d5408SPeter Wemm 
3110e3d5408SPeter Wemm 	/*
3120e3d5408SPeter Wemm 	 * Loop through database array until finding the record.
3130e3d5408SPeter Wemm 	 */
3140e3d5408SPeter Wemm 	for (current = in_array; db_array[current] != 0; current++) {
3150e3d5408SPeter Wemm 	    int eof = FALSE;
3160e3d5408SPeter Wemm 
3170e3d5408SPeter Wemm 	    /*
3180e3d5408SPeter Wemm 	     * Open database if not already open.
3190e3d5408SPeter Wemm 	     */
3200e3d5408SPeter Wemm 	    if (fd >= 0) {
3210e3d5408SPeter Wemm 		(void) lseek(fd, (off_t) 0, SEEK_SET);
3220e3d5408SPeter Wemm 	    } else if ((_nc_access(db_array[current], R_OK) < 0)
3230e3d5408SPeter Wemm 		       || (fd = open(db_array[current], O_RDONLY, 0)) < 0) {
3240e3d5408SPeter Wemm 		/* No error on unfound file. */
3250e3d5408SPeter Wemm 		if (errno == ENOENT)
3260e3d5408SPeter Wemm 		    continue;
3270e3d5408SPeter Wemm 		free(record);
3280e3d5408SPeter Wemm 		return (TC_SYS_ERR);
3290e3d5408SPeter Wemm 	    } else {
3300e3d5408SPeter Wemm 		myfd = TRUE;
3310e3d5408SPeter Wemm 	    }
3320e3d5408SPeter Wemm 	    lineno = 0;
3330e3d5408SPeter Wemm 
3340e3d5408SPeter Wemm 	    /*
3350e3d5408SPeter Wemm 	     * Find the requested capability record ...
3360e3d5408SPeter Wemm 	     */
3370e3d5408SPeter Wemm 	    {
3380e3d5408SPeter Wemm 		char buf[2048];
3390e3d5408SPeter Wemm 		register char *b_end = buf;
3400e3d5408SPeter Wemm 		register char *bp = buf;
3410e3d5408SPeter Wemm 		register int c;
3420e3d5408SPeter Wemm 
3430e3d5408SPeter Wemm 		/*
3440e3d5408SPeter Wemm 		 * Loop invariants:
3450e3d5408SPeter Wemm 		 *      There is always room for one more character in record.
3460e3d5408SPeter Wemm 		 *      R_end always points just past end of record.
3470e3d5408SPeter Wemm 		 *      Rp always points just past last character in record.
3480e3d5408SPeter Wemm 		 *      B_end always points just past last character in buf.
3490e3d5408SPeter Wemm 		 *      Bp always points at next character in buf.
3500e3d5408SPeter Wemm 		 */
3510e3d5408SPeter Wemm 
3520e3d5408SPeter Wemm 		for (;;) {
3530e3d5408SPeter Wemm 		    int first = lineno + 1;
3540e3d5408SPeter Wemm 
3550e3d5408SPeter Wemm 		    /*
3560e3d5408SPeter Wemm 		     * Read in a line implementing (\, newline)
3570e3d5408SPeter Wemm 		     * line continuation.
3580e3d5408SPeter Wemm 		     */
3590e3d5408SPeter Wemm 		    rp = record;
3600e3d5408SPeter Wemm 		    for (;;) {
3610e3d5408SPeter Wemm 			if (bp >= b_end) {
3620e3d5408SPeter Wemm 			    int n;
3630e3d5408SPeter Wemm 
3640e3d5408SPeter Wemm 			    n = read(fd, buf, sizeof(buf));
3650e3d5408SPeter Wemm 			    if (n <= 0) {
3660e3d5408SPeter Wemm 				if (myfd)
3670e3d5408SPeter Wemm 				    (void) close(fd);
3680e3d5408SPeter Wemm 				if (n < 0) {
3690e3d5408SPeter Wemm 				    free(record);
3700e3d5408SPeter Wemm 				    return (TC_SYS_ERR);
3710e3d5408SPeter Wemm 				}
3720e3d5408SPeter Wemm 				fd = -1;
3730e3d5408SPeter Wemm 				eof = TRUE;
3740e3d5408SPeter Wemm 				break;
3750e3d5408SPeter Wemm 			    }
3760e3d5408SPeter Wemm 			    b_end = buf + n;
3770e3d5408SPeter Wemm 			    bp = buf;
3780e3d5408SPeter Wemm 			}
3790e3d5408SPeter Wemm 
3800e3d5408SPeter Wemm 			c = *bp++;
3810e3d5408SPeter Wemm 			if (c == '\n') {
3820e3d5408SPeter Wemm 			    lineno++;
3830e3d5408SPeter Wemm 			    if (rp == record || *(rp - 1) != '\\')
3840e3d5408SPeter Wemm 				break;
3850e3d5408SPeter Wemm 			}
3860e3d5408SPeter Wemm 			*rp++ = c;
3870e3d5408SPeter Wemm 
3880e3d5408SPeter Wemm 			/*
3890e3d5408SPeter Wemm 			 * Enforce loop invariant: if no room
3900e3d5408SPeter Wemm 			 * left in record buffer, try to get
3910e3d5408SPeter Wemm 			 * some more.
3920e3d5408SPeter Wemm 			 */
3930e3d5408SPeter Wemm 			if (rp >= r_end) {
39415589c42SPeter Wemm 			    unsigned pos;
3950e3d5408SPeter Wemm 			    size_t newsize;
3960e3d5408SPeter Wemm 
3970e3d5408SPeter Wemm 			    pos = rp - record;
3980e3d5408SPeter Wemm 			    newsize = r_end - record + BFRAG;
3990e3d5408SPeter Wemm 			    record = DOALLOC(newsize);
4000e3d5408SPeter Wemm 			    if (record == 0) {
4010e3d5408SPeter Wemm 				if (myfd)
4020e3d5408SPeter Wemm 				    (void) close(fd);
4030e3d5408SPeter Wemm 				errno = ENOMEM;
4040e3d5408SPeter Wemm 				return (TC_SYS_ERR);
4050e3d5408SPeter Wemm 			    }
4060e3d5408SPeter Wemm 			    r_end = record + newsize;
4070e3d5408SPeter Wemm 			    rp = record + pos;
4080e3d5408SPeter Wemm 			}
4090e3d5408SPeter Wemm 		    }
4100e3d5408SPeter Wemm 		    /* loop invariant lets us do this */
4110e3d5408SPeter Wemm 		    *rp++ = '\0';
4120e3d5408SPeter Wemm 
4130e3d5408SPeter Wemm 		    /*
4140e3d5408SPeter Wemm 		     * If encountered eof check next file.
4150e3d5408SPeter Wemm 		     */
4160e3d5408SPeter Wemm 		    if (eof)
4170e3d5408SPeter Wemm 			break;
4180e3d5408SPeter Wemm 
4190e3d5408SPeter Wemm 		    /*
4200e3d5408SPeter Wemm 		     * Toss blank lines and comments.
4210e3d5408SPeter Wemm 		     */
4220e3d5408SPeter Wemm 		    if (*record == '\0' || *record == '#')
4230e3d5408SPeter Wemm 			continue;
4240e3d5408SPeter Wemm 
4250e3d5408SPeter Wemm 		    /*
4260e3d5408SPeter Wemm 		     * See if this is the record we want ...
4270e3d5408SPeter Wemm 		     */
4280e3d5408SPeter Wemm 		    if (_nc_cgetmatch(record, name) == 0
4290e3d5408SPeter Wemm 			&& (nfield == 0
4300e3d5408SPeter Wemm 			    || !_nc_nfcmp(nfield, record))) {
4310e3d5408SPeter Wemm 			foundit = TRUE;
4320e3d5408SPeter Wemm 			*beginning = first;
4330e3d5408SPeter Wemm 			break;	/* found it! */
4340e3d5408SPeter Wemm 		    }
4350e3d5408SPeter Wemm 		}
4360e3d5408SPeter Wemm 	    }
4370e3d5408SPeter Wemm 	    if (foundit)
4380e3d5408SPeter Wemm 		break;
4390e3d5408SPeter Wemm 	}
4400e3d5408SPeter Wemm 
4410e3d5408SPeter Wemm 	if (!foundit)
4420e3d5408SPeter Wemm 	    return (TC_NOT_FOUND);
4430e3d5408SPeter Wemm     }
4440e3d5408SPeter Wemm 
4450e3d5408SPeter Wemm     /*
4460e3d5408SPeter Wemm      * Got the capability record, but now we have to expand all tc=name
4470e3d5408SPeter Wemm      * references in it ...
4480e3d5408SPeter Wemm      */
4490e3d5408SPeter Wemm     {
4500e3d5408SPeter Wemm 	register char *newicap, *s;
4510e3d5408SPeter Wemm 	register int newilen;
45215589c42SPeter Wemm 	unsigned ilen;
4530e3d5408SPeter Wemm 	int diff, iret, tclen, oline;
4540e3d5408SPeter Wemm 	char *icap, *scan, *tc, *tcstart, *tcend;
4550e3d5408SPeter Wemm 
4560e3d5408SPeter Wemm 	/*
4570e3d5408SPeter Wemm 	 * Loop invariants:
4580e3d5408SPeter Wemm 	 *      There is room for one more character in record.
4590e3d5408SPeter Wemm 	 *      R_end points just past end of record.
4600e3d5408SPeter Wemm 	 *      Rp points just past last character in record.
4610e3d5408SPeter Wemm 	 *      Scan points at remainder of record that needs to be
4620e3d5408SPeter Wemm 	 *      scanned for tc=name constructs.
4630e3d5408SPeter Wemm 	 */
4640e3d5408SPeter Wemm 	scan = record;
4650e3d5408SPeter Wemm 	tc_not_resolved = FALSE;
4660e3d5408SPeter Wemm 	for (;;) {
4670e3d5408SPeter Wemm 	    if ((tc = _nc_cgetcap(scan, "tc", '=')) == 0)
4680e3d5408SPeter Wemm 		break;
4690e3d5408SPeter Wemm 
4700e3d5408SPeter Wemm 	    /*
4710e3d5408SPeter Wemm 	     * Find end of tc=name and stomp on the trailing `:'
4720e3d5408SPeter Wemm 	     * (if present) so we can use it to call ourselves.
4730e3d5408SPeter Wemm 	     */
4740e3d5408SPeter Wemm 	    s = tc;
4750e3d5408SPeter Wemm 	    while (*s != '\0') {
4760e3d5408SPeter Wemm 		if (*s++ == ':') {
4770e3d5408SPeter Wemm 		    *(s - 1) = '\0';
4780e3d5408SPeter Wemm 		    break;
4790e3d5408SPeter Wemm 		}
4800e3d5408SPeter Wemm 	    }
4810e3d5408SPeter Wemm 	    tcstart = tc - 3;
4820e3d5408SPeter Wemm 	    tclen = s - tcstart;
4830e3d5408SPeter Wemm 	    tcend = s;
4840e3d5408SPeter Wemm 
48515589c42SPeter Wemm 	    iret = _nc_getent(&icap, &ilen, &oline, current, db_array, fd,
48615589c42SPeter Wemm 			      tc, depth + 1, 0);
4870e3d5408SPeter Wemm 	    newicap = icap;	/* Put into a register. */
4880e3d5408SPeter Wemm 	    newilen = ilen;
4890e3d5408SPeter Wemm 	    if (iret != TC_SUCCESS) {
4900e3d5408SPeter Wemm 		/* an error */
4910e3d5408SPeter Wemm 		if (iret < TC_NOT_FOUND) {
4920e3d5408SPeter Wemm 		    if (myfd)
4930e3d5408SPeter Wemm 			(void) close(fd);
4940e3d5408SPeter Wemm 		    free(record);
4950e3d5408SPeter Wemm 		    return (iret);
4960e3d5408SPeter Wemm 		}
4970e3d5408SPeter Wemm 		if (iret == TC_UNRESOLVED)
4980e3d5408SPeter Wemm 		    tc_not_resolved = TRUE;
4990e3d5408SPeter Wemm 		/* couldn't resolve tc */
5000e3d5408SPeter Wemm 		if (iret == TC_NOT_FOUND) {
5010e3d5408SPeter Wemm 		    *(s - 1) = ':';
5020e3d5408SPeter Wemm 		    scan = s - 1;
5030e3d5408SPeter Wemm 		    tc_not_resolved = TRUE;
5040e3d5408SPeter Wemm 		    continue;
5050e3d5408SPeter Wemm 		}
5060e3d5408SPeter Wemm 	    }
5070e3d5408SPeter Wemm 
5080e3d5408SPeter Wemm 	    /* not interested in name field of tc'ed record */
5090e3d5408SPeter Wemm 	    s = newicap;
51015589c42SPeter Wemm 	    while (*s != '\0' && *s++ != ':') ;
5110e3d5408SPeter Wemm 	    newilen -= s - newicap;
5120e3d5408SPeter Wemm 	    newicap = s;
5130e3d5408SPeter Wemm 
5140e3d5408SPeter Wemm 	    /* make sure interpolated record is `:'-terminated */
5150e3d5408SPeter Wemm 	    s += newilen;
5160e3d5408SPeter Wemm 	    if (*(s - 1) != ':') {
5170e3d5408SPeter Wemm 		*s = ':';	/* overwrite NUL with : */
5180e3d5408SPeter Wemm 		newilen++;
5190e3d5408SPeter Wemm 	    }
5200e3d5408SPeter Wemm 
5210e3d5408SPeter Wemm 	    /*
5220e3d5408SPeter Wemm 	     * Make sure there's enough room to insert the
5230e3d5408SPeter Wemm 	     * new record.
5240e3d5408SPeter Wemm 	     */
5250e3d5408SPeter Wemm 	    diff = newilen - tclen;
5260e3d5408SPeter Wemm 	    if (diff >= r_end - rp) {
52715589c42SPeter Wemm 		unsigned pos, tcpos, tcposend;
5280e3d5408SPeter Wemm 		size_t newsize;
5290e3d5408SPeter Wemm 
5300e3d5408SPeter Wemm 		pos = rp - record;
5310e3d5408SPeter Wemm 		newsize = r_end - record + diff + BFRAG;
5320e3d5408SPeter Wemm 		tcpos = tcstart - record;
5330e3d5408SPeter Wemm 		tcposend = tcend - record;
5340e3d5408SPeter Wemm 		record = DOALLOC(newsize);
5350e3d5408SPeter Wemm 		if (record == 0) {
5360e3d5408SPeter Wemm 		    if (myfd)
5370e3d5408SPeter Wemm 			(void) close(fd);
5380e3d5408SPeter Wemm 		    free(icap);
5390e3d5408SPeter Wemm 		    errno = ENOMEM;
5400e3d5408SPeter Wemm 		    return (TC_SYS_ERR);
5410e3d5408SPeter Wemm 		}
5420e3d5408SPeter Wemm 		r_end = record + newsize;
5430e3d5408SPeter Wemm 		rp = record + pos;
5440e3d5408SPeter Wemm 		tcstart = record + tcpos;
5450e3d5408SPeter Wemm 		tcend = record + tcposend;
5460e3d5408SPeter Wemm 	    }
5470e3d5408SPeter Wemm 
5480e3d5408SPeter Wemm 	    /*
5490e3d5408SPeter Wemm 	     * Insert tc'ed record into our record.
5500e3d5408SPeter Wemm 	     */
5510e3d5408SPeter Wemm 	    s = tcstart + newilen;
5520e3d5408SPeter Wemm 	    memmove(s, tcend, (size_t) (rp - tcend));
5530e3d5408SPeter Wemm 	    memmove(tcstart, newicap, (size_t) newilen);
5540e3d5408SPeter Wemm 	    rp += diff;
5550e3d5408SPeter Wemm 	    free(icap);
5560e3d5408SPeter Wemm 
5570e3d5408SPeter Wemm 	    /*
5580e3d5408SPeter Wemm 	     * Start scan on `:' so next cgetcap works properly
5590e3d5408SPeter Wemm 	     * (cgetcap always skips first field).
5600e3d5408SPeter Wemm 	     */
5610e3d5408SPeter Wemm 	    scan = s - 1;
5620e3d5408SPeter Wemm 	}
5630e3d5408SPeter Wemm     }
5640e3d5408SPeter Wemm 
5650e3d5408SPeter Wemm     /*
5660e3d5408SPeter Wemm      * Close file (if we opened it), give back any extra memory, and
5670e3d5408SPeter Wemm      * return capability, length and success.
5680e3d5408SPeter Wemm      */
5690e3d5408SPeter Wemm     if (myfd)
5700e3d5408SPeter Wemm 	(void) close(fd);
5710e3d5408SPeter Wemm     *len = rp - record - 1;	/* don't count NUL */
5720e3d5408SPeter Wemm     if (r_end > rp) {
5730e3d5408SPeter Wemm 	if ((record = DOALLOC((size_t) (rp - record))) == 0) {
5740e3d5408SPeter Wemm 	    errno = ENOMEM;
5750e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
5760e3d5408SPeter Wemm 	}
5770e3d5408SPeter Wemm     }
5780e3d5408SPeter Wemm 
5790e3d5408SPeter Wemm     *cap = record;
5800e3d5408SPeter Wemm     if (tc_not_resolved)
5810e3d5408SPeter Wemm 	return (TC_UNRESOLVED);
5820e3d5408SPeter Wemm     return (current);
5830e3d5408SPeter Wemm }
5840e3d5408SPeter Wemm 
5850e3d5408SPeter Wemm /*
5860e3d5408SPeter Wemm  * Cgetmatch will return 0 if name is one of the names of the capability
5870e3d5408SPeter Wemm  * record buf, -1 if not.
5880e3d5408SPeter Wemm  */
5890e3d5408SPeter Wemm static int
5900e3d5408SPeter Wemm _nc_cgetmatch(char *buf, const char *name)
5910e3d5408SPeter Wemm {
5920e3d5408SPeter Wemm     register const char *np;
5930e3d5408SPeter Wemm     register char *bp;
5940e3d5408SPeter Wemm 
5950e3d5408SPeter Wemm     /*
5960e3d5408SPeter Wemm      * Start search at beginning of record.
5970e3d5408SPeter Wemm      */
5980e3d5408SPeter Wemm     bp = buf;
5990e3d5408SPeter Wemm     for (;;) {
6000e3d5408SPeter Wemm 	/*
6010e3d5408SPeter Wemm 	 * Try to match a record name.
6020e3d5408SPeter Wemm 	 */
6030e3d5408SPeter Wemm 	np = name;
6040e3d5408SPeter Wemm 	for (;;) {
6050e3d5408SPeter Wemm 	    if (*np == '\0') {
6060e3d5408SPeter Wemm 		if (*bp == '|' || *bp == ':' || *bp == '\0')
6070e3d5408SPeter Wemm 		    return (0);
6080e3d5408SPeter Wemm 		else
6090e3d5408SPeter Wemm 		    break;
6100e3d5408SPeter Wemm 	    } else if (*bp++ != *np++) {
6110e3d5408SPeter Wemm 		break;
6120e3d5408SPeter Wemm 	    }
6130e3d5408SPeter Wemm 	}
6140e3d5408SPeter Wemm 
6150e3d5408SPeter Wemm 	/*
6160e3d5408SPeter Wemm 	 * Match failed, skip to next name in record.
6170e3d5408SPeter Wemm 	 */
6180e3d5408SPeter Wemm 	bp--;			/* a '|' or ':' may have stopped the match */
6190e3d5408SPeter Wemm 	for (;;) {
6200e3d5408SPeter Wemm 	    if (*bp == '\0' || *bp == ':')
6210e3d5408SPeter Wemm 		return (-1);	/* match failed totally */
6220e3d5408SPeter Wemm 	    else if (*bp++ == '|')
6230e3d5408SPeter Wemm 		break;		/* found next name */
6240e3d5408SPeter Wemm 	}
6250e3d5408SPeter Wemm     }
6260e3d5408SPeter Wemm }
6270e3d5408SPeter Wemm 
6280e3d5408SPeter Wemm /*
6290e3d5408SPeter Wemm  * Compare name field of record.
6300e3d5408SPeter Wemm  */
6310e3d5408SPeter Wemm static int
6320e3d5408SPeter Wemm _nc_nfcmp(const char *nf, char *rec)
6330e3d5408SPeter Wemm {
6340e3d5408SPeter Wemm     char *cp, tmp;
6350e3d5408SPeter Wemm     int ret;
6360e3d5408SPeter Wemm 
63715589c42SPeter Wemm     for (cp = rec; *cp != ':'; cp++) ;
6380e3d5408SPeter Wemm 
6390e3d5408SPeter Wemm     tmp = *(cp + 1);
6400e3d5408SPeter Wemm     *(cp + 1) = '\0';
6410e3d5408SPeter Wemm     ret = strcmp(nf, rec);
6420e3d5408SPeter Wemm     *(cp + 1) = tmp;
6430e3d5408SPeter Wemm 
6440e3d5408SPeter Wemm     return (ret);
6450e3d5408SPeter Wemm }
6460e3d5408SPeter Wemm #endif /* HAVE_BSD_CGETENT */
6470e3d5408SPeter Wemm 
6480e3d5408SPeter Wemm /*
6490e3d5408SPeter Wemm  * Since ncurses provides its own 'tgetent()', we cannot use the native one.
6500e3d5408SPeter Wemm  * So we reproduce the logic to get down to cgetent() -- or our cut-down
6510e3d5408SPeter Wemm  * version of that -- to circumvent the problem of configuring against the
6520e3d5408SPeter Wemm  * termcap library.
6530e3d5408SPeter Wemm  */
6540e3d5408SPeter Wemm #define USE_BSD_TGETENT 1
6550e3d5408SPeter Wemm 
6560e3d5408SPeter Wemm #if USE_BSD_TGETENT
6570e3d5408SPeter Wemm /*
6580e3d5408SPeter Wemm  * Copyright (c) 1980, 1993
6590e3d5408SPeter Wemm  *	The Regents of the University of California.  All rights reserved.
6600e3d5408SPeter Wemm  *
6610e3d5408SPeter Wemm  * Redistribution and use in source and binary forms, with or without
6620e3d5408SPeter Wemm  * modification, are permitted provided that the following conditions
6630e3d5408SPeter Wemm  * are met:
6640e3d5408SPeter Wemm  * 1. Redistributions of source code must retain the above copyright
6650e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer.
6660e3d5408SPeter Wemm  * 2. Redistributions in binary form must reproduce the above copyright
6670e3d5408SPeter Wemm  *    notice, this list of conditions and the following disclaimer in the
6680e3d5408SPeter Wemm  *    documentation and/or other materials provided with the distribution.
6690e3d5408SPeter Wemm  * 3. All advertising materials mentioning features or use of this software
6700e3d5408SPeter Wemm  *    must display the following acknowledgment:
6710e3d5408SPeter Wemm  *	This product includes software developed by the University of
6720e3d5408SPeter Wemm  *	California, Berkeley and its contributors.
6730e3d5408SPeter Wemm  * 4. Neither the name of the University nor the names of its contributors
6740e3d5408SPeter Wemm  *    may be used to endorse or promote products derived from this software
6750e3d5408SPeter Wemm  *    without specific prior written permission.
6760e3d5408SPeter Wemm  *
6770e3d5408SPeter Wemm  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
6780e3d5408SPeter Wemm  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
6790e3d5408SPeter Wemm  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
6800e3d5408SPeter Wemm  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
6810e3d5408SPeter Wemm  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
6820e3d5408SPeter Wemm  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
6830e3d5408SPeter Wemm  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
6840e3d5408SPeter Wemm  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6850e3d5408SPeter Wemm  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6860e3d5408SPeter Wemm  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6870e3d5408SPeter Wemm  * SUCH DAMAGE.
6880e3d5408SPeter Wemm  */
6890e3d5408SPeter Wemm 
6900e3d5408SPeter Wemm /* static char sccsid[] = "@(#)termcap.c	8.1 (Berkeley) 6/4/93" */
6910e3d5408SPeter Wemm 
6920e3d5408SPeter Wemm #define	PBUFSIZ		512	/* max length of filename path */
6930e3d5408SPeter Wemm #define	PVECSIZ		32	/* max number of names in path */
6940e3d5408SPeter Wemm #define TBUFSIZ (2048*2)
6950e3d5408SPeter Wemm 
6960e3d5408SPeter Wemm static char *tbuf;
6970e3d5408SPeter Wemm 
6980e3d5408SPeter Wemm /*
6990e3d5408SPeter Wemm  * On entry, srcp points to a non ':' character which is the beginning of the
7000e3d5408SPeter Wemm  * token, if any.  We'll try to return a string that doesn't end with a ':'.
7010e3d5408SPeter Wemm  */
7020e3d5408SPeter Wemm static char *
7030e3d5408SPeter Wemm get_tc_token(char **srcp, int *endp)
7040e3d5408SPeter Wemm {
7050e3d5408SPeter Wemm     int ch;
7060e3d5408SPeter Wemm     bool found = FALSE;
7070e3d5408SPeter Wemm     char *s, *base;
7080e3d5408SPeter Wemm     char *tok = 0;
7090e3d5408SPeter Wemm 
7100e3d5408SPeter Wemm     *endp = TRUE;
7110e3d5408SPeter Wemm     for (s = base = *srcp; *s != '\0';) {
7120e3d5408SPeter Wemm 	ch = *s++;
7130e3d5408SPeter Wemm 	if (ch == '\\') {
7140e3d5408SPeter Wemm 	    if (*s == '\0') {
7150e3d5408SPeter Wemm 		break;
7160e3d5408SPeter Wemm 	    } else if (*s++ == '\n') {
7174a1a9510SRong-En Fan 		while (isspace(UChar(*s)))
7180e3d5408SPeter Wemm 		    s++;
7190e3d5408SPeter Wemm 	    } else {
7200e3d5408SPeter Wemm 		found = TRUE;
7210e3d5408SPeter Wemm 	    }
7220e3d5408SPeter Wemm 	} else if (ch == ':') {
7230e3d5408SPeter Wemm 	    if (found) {
7240e3d5408SPeter Wemm 		tok = base;
7250e3d5408SPeter Wemm 		s[-1] = '\0';
7260e3d5408SPeter Wemm 		*srcp = s;
7270e3d5408SPeter Wemm 		*endp = FALSE;
7280e3d5408SPeter Wemm 		break;
7290e3d5408SPeter Wemm 	    }
7300e3d5408SPeter Wemm 	    base = s;
7314a1a9510SRong-En Fan 	} else if (isgraph(UChar(ch))) {
7320e3d5408SPeter Wemm 	    found = TRUE;
7330e3d5408SPeter Wemm 	}
7340e3d5408SPeter Wemm     }
7350e3d5408SPeter Wemm 
7360e3d5408SPeter Wemm     /* malformed entry may end without a ':' */
7370e3d5408SPeter Wemm     if (tok == 0 && found) {
7380e3d5408SPeter Wemm 	tok = base;
7390e3d5408SPeter Wemm     }
7400e3d5408SPeter Wemm 
7410e3d5408SPeter Wemm     return tok;
7420e3d5408SPeter Wemm }
7430e3d5408SPeter Wemm 
7440e3d5408SPeter Wemm static char *
7450e3d5408SPeter Wemm copy_tc_token(char *dst, const char *src, size_t len)
7460e3d5408SPeter Wemm {
7470e3d5408SPeter Wemm     int ch;
7480e3d5408SPeter Wemm 
7490e3d5408SPeter Wemm     while ((ch = *src++) != '\0') {
7500e3d5408SPeter Wemm 	if (ch == '\\' && *src == '\n') {
7514a1a9510SRong-En Fan 	    while (isspace(UChar(*src)))
7520e3d5408SPeter Wemm 		src++;
7530e3d5408SPeter Wemm 	    continue;
7540e3d5408SPeter Wemm 	}
7550e3d5408SPeter Wemm 	if (--len == 0) {
7560e3d5408SPeter Wemm 	    dst = 0;
7570e3d5408SPeter Wemm 	    break;
7580e3d5408SPeter Wemm 	}
7590e3d5408SPeter Wemm 	*dst++ = ch;
7600e3d5408SPeter Wemm     }
7610e3d5408SPeter Wemm     return dst;
7620e3d5408SPeter Wemm }
7630e3d5408SPeter Wemm 
7640e3d5408SPeter Wemm /*
7650e3d5408SPeter Wemm  * Get an entry for terminal name in buffer bp from the termcap file.
7660e3d5408SPeter Wemm  */
7670e3d5408SPeter Wemm static int
7680e3d5408SPeter Wemm _nc_tgetent(char *bp, char **sourcename, int *lineno, const char *name)
7690e3d5408SPeter Wemm {
7700e3d5408SPeter Wemm     static char *the_source;
7710e3d5408SPeter Wemm 
7720e3d5408SPeter Wemm     register char *p;
7730e3d5408SPeter Wemm     register char *cp;
7747a69bbfbSPeter Wemm     char *dummy = NULL;
7750e3d5408SPeter Wemm     char **fname;
7760e3d5408SPeter Wemm     char *home;
7770e3d5408SPeter Wemm     int i;
7780e3d5408SPeter Wemm     char pathbuf[PBUFSIZ];	/* holds raw path of filenames */
7790e3d5408SPeter Wemm     char *pathvec[PVECSIZ];	/* to point to names in pathbuf */
7800e3d5408SPeter Wemm     char **pvec;		/* holds usable tail of path vector */
7814a1a9510SRong-En Fan     NCURSES_CONST char *termpath;
78218259542SPeter Wemm     string_desc desc;
7830e3d5408SPeter Wemm 
7840e3d5408SPeter Wemm     fname = pathvec;
7850e3d5408SPeter Wemm     pvec = pathvec;
7860e3d5408SPeter Wemm     tbuf = bp;
7870e3d5408SPeter Wemm     p = pathbuf;
78818259542SPeter Wemm     cp = use_terminfo_vars()? getenv("TERMCAP") : NULL;
7890e3d5408SPeter Wemm 
7900e3d5408SPeter Wemm     /*
79115589c42SPeter Wemm      * TERMCAP can have one of two things in it.  It can be the name of a file
79215589c42SPeter Wemm      * to use instead of /etc/termcap.  In this case it better start with a
79315589c42SPeter Wemm      * "/".  Or it can be an entry to use so we don't have to read the file.
79415589c42SPeter Wemm      * In this case it has to already have the newlines crunched out.  If
79515589c42SPeter Wemm      * TERMCAP does not hold a file name then a path of names is searched
79615589c42SPeter Wemm      * instead.  The path is found in the TERMPATH variable, or becomes
79715589c42SPeter Wemm      * "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
7980e3d5408SPeter Wemm      */
79918259542SPeter Wemm     _nc_str_init(&desc, pathbuf, sizeof(pathbuf));
80018259542SPeter Wemm     if (cp == NULL) {
80139f2269fSPeter Wemm 	_nc_safe_strcpy(&desc, get_termpath());
8024a1a9510SRong-En Fan     } else if (!_nc_is_abs_path(cp)) {	/* TERMCAP holds an entry */
80339f2269fSPeter Wemm 	if ((termpath = get_termpath()) != 0) {
80418259542SPeter Wemm 	    _nc_safe_strcat(&desc, termpath);
8050e3d5408SPeter Wemm 	} else {
80618259542SPeter Wemm 	    char temp[PBUFSIZ];
80718259542SPeter Wemm 	    temp[0] = 0;
80818259542SPeter Wemm 	    if ((home = getenv("HOME")) != 0 && *home != '\0'
80918259542SPeter Wemm 		&& strchr(home, ' ') == 0
81018259542SPeter Wemm 		&& strlen(home) < sizeof(temp) - 10) {	/* setup path */
81118259542SPeter Wemm 		sprintf(temp, "%s/", home);	/* $HOME first */
8120e3d5408SPeter Wemm 	    }
81318259542SPeter Wemm 	    /* if no $HOME look in current directory */
81418259542SPeter Wemm 	    strcat(temp, ".termcap");
81518259542SPeter Wemm 	    _nc_safe_strcat(&desc, temp);
81639f2269fSPeter Wemm 	    _nc_safe_strcat(&desc, " ");
81739f2269fSPeter Wemm 	    _nc_safe_strcat(&desc, get_termpath());
81818259542SPeter Wemm 	}
81918259542SPeter Wemm     } else {			/* user-defined name in TERMCAP */
82018259542SPeter Wemm 	_nc_safe_strcat(&desc, cp);	/* still can be tokenized */
82118259542SPeter Wemm     }
8220e3d5408SPeter Wemm 
8230e3d5408SPeter Wemm     *fname++ = pathbuf;		/* tokenize path into vector of names */
8240e3d5408SPeter Wemm     while (*++p) {
8257a69bbfbSPeter Wemm 	if (*p == ' ' || *p == NCURSES_PATHSEP) {
8260e3d5408SPeter Wemm 	    *p = '\0';
8270e3d5408SPeter Wemm 	    while (*++p)
8287a69bbfbSPeter Wemm 		if (*p != ' ' && *p != NCURSES_PATHSEP)
8290e3d5408SPeter Wemm 		    break;
8300e3d5408SPeter Wemm 	    if (*p == '\0')
8310e3d5408SPeter Wemm 		break;
8320e3d5408SPeter Wemm 	    *fname++ = p;
8330e3d5408SPeter Wemm 	    if (fname >= pathvec + PVECSIZ) {
8340e3d5408SPeter Wemm 		fname--;
8350e3d5408SPeter Wemm 		break;
8360e3d5408SPeter Wemm 	    }
8370e3d5408SPeter Wemm 	}
8380e3d5408SPeter Wemm     }
8390e3d5408SPeter Wemm     *fname = 0;			/* mark end of vector */
8404a1a9510SRong-En Fan     if (_nc_is_abs_path(cp)) {
8410e3d5408SPeter Wemm 	if (_nc_cgetset(cp) < 0) {
8420e3d5408SPeter Wemm 	    return (TC_SYS_ERR);
8430e3d5408SPeter Wemm 	}
8440e3d5408SPeter Wemm     }
8450e3d5408SPeter Wemm 
8460e3d5408SPeter Wemm     i = _nc_cgetent(&dummy, lineno, pathvec, name);
8470e3d5408SPeter Wemm 
8480e3d5408SPeter Wemm     /* ncurses' termcap-parsing routines cannot handle multiple adjacent
8490e3d5408SPeter Wemm      * empty fields, and mistakenly use the last valid cap entry instead of
8500e3d5408SPeter Wemm      * the first (breaks tc= includes)
8510e3d5408SPeter Wemm      */
8520e3d5408SPeter Wemm     if (i >= 0) {
8530e3d5408SPeter Wemm 	char *pd, *ps, *tok;
8540e3d5408SPeter Wemm 	int endflag = FALSE;
8550e3d5408SPeter Wemm 	char *list[1023];
8560e3d5408SPeter Wemm 	size_t n, count = 0;
8570e3d5408SPeter Wemm 
8580e3d5408SPeter Wemm 	pd = bp;
8590e3d5408SPeter Wemm 	ps = dummy;
8600e3d5408SPeter Wemm 	while (!endflag && (tok = get_tc_token(&ps, &endflag)) != 0) {
8610e3d5408SPeter Wemm 	    bool ignore = FALSE;
8620e3d5408SPeter Wemm 
8630e3d5408SPeter Wemm 	    for (n = 1; n < count; n++) {
8640e3d5408SPeter Wemm 		char *s = list[n];
8650e3d5408SPeter Wemm 		if (s[0] == tok[0]
8660e3d5408SPeter Wemm 		    && s[1] == tok[1]) {
8670e3d5408SPeter Wemm 		    ignore = TRUE;
8680e3d5408SPeter Wemm 		    break;
8690e3d5408SPeter Wemm 		}
8700e3d5408SPeter Wemm 	    }
8710e3d5408SPeter Wemm 	    if (ignore != TRUE) {
8720e3d5408SPeter Wemm 		list[count++] = tok;
8730e3d5408SPeter Wemm 		pd = copy_tc_token(pd, tok, TBUFSIZ - (2 + pd - bp));
8740e3d5408SPeter Wemm 		if (pd == 0) {
8750e3d5408SPeter Wemm 		    i = -1;
8760e3d5408SPeter Wemm 		    break;
8770e3d5408SPeter Wemm 		}
8780e3d5408SPeter Wemm 		*pd++ = ':';
8790e3d5408SPeter Wemm 		*pd = '\0';
8800e3d5408SPeter Wemm 	    }
8810e3d5408SPeter Wemm 	}
8820e3d5408SPeter Wemm     }
8830e3d5408SPeter Wemm 
8840e3d5408SPeter Wemm     FreeIfNeeded(dummy);
8850e3d5408SPeter Wemm     FreeIfNeeded(the_source);
8860e3d5408SPeter Wemm     the_source = 0;
8870e3d5408SPeter Wemm 
8880e3d5408SPeter Wemm     /* This is not related to the BSD cgetent(), but to fake up a suitable
8890e3d5408SPeter Wemm      * filename for ncurses' error reporting.  (If we are not using BSD
8900e3d5408SPeter Wemm      * cgetent, then it is the actual filename).
8910e3d5408SPeter Wemm      */
8920e3d5408SPeter Wemm     if (i >= 0) {
8934a1a9510SRong-En Fan #if HAVE_BSD_CGETENT
8944a1a9510SRong-En Fan 	char temp[PATH_MAX];
8954a1a9510SRong-En Fan 
8964a1a9510SRong-En Fan 	_nc_str_init(&desc, temp, sizeof(temp));
8974a1a9510SRong-En Fan 	_nc_safe_strcpy(&desc, pathvec[i]);
8984a1a9510SRong-En Fan 	_nc_safe_strcat(&desc, ".db");
8994a1a9510SRong-En Fan 	if (_nc_access(temp, R_OK) == 0) {
9004a1a9510SRong-En Fan 	    _nc_safe_strcpy(&desc, pathvec[i]);
9014a1a9510SRong-En Fan 	}
9024a1a9510SRong-En Fan 	if ((the_source = strdup(temp)) != 0)
9034a1a9510SRong-En Fan 	    *sourcename = the_source;
9044a1a9510SRong-En Fan #else
9050e3d5408SPeter Wemm 	if ((the_source = strdup(pathvec[i])) != 0)
9060e3d5408SPeter Wemm 	    *sourcename = the_source;
9074a1a9510SRong-En Fan #endif
9080e3d5408SPeter Wemm     }
9090e3d5408SPeter Wemm 
9100e3d5408SPeter Wemm     return (i);
9110e3d5408SPeter Wemm }
9120e3d5408SPeter Wemm #endif /* USE_BSD_TGETENT */
9130e3d5408SPeter Wemm #endif /* USE_GETCAP */
9140e3d5408SPeter Wemm 
9150e3d5408SPeter Wemm #define MAXPATHS	32
9160e3d5408SPeter Wemm 
9170e3d5408SPeter Wemm /*
9180e3d5408SPeter Wemm  * Add a filename to the list in 'termpaths[]', checking that we really have
9190e3d5408SPeter Wemm  * a right to open the file.
9200e3d5408SPeter Wemm  */
9210e3d5408SPeter Wemm #if !USE_GETCAP
92215589c42SPeter Wemm static int
92315589c42SPeter Wemm add_tc(char *termpaths[], char *path, int count)
9240e3d5408SPeter Wemm {
92539f2269fSPeter Wemm     char *save = strchr(path, NCURSES_PATHSEP);
92639f2269fSPeter Wemm     if (save != 0)
92739f2269fSPeter Wemm 	*save = '\0';
9280e3d5408SPeter Wemm     if (count < MAXPATHS
92939f2269fSPeter Wemm 	&& _nc_access(path, R_OK) == 0) {
9300e3d5408SPeter Wemm 	termpaths[count++] = path;
93139f2269fSPeter Wemm 	T(("Adding termpath %s", path));
93239f2269fSPeter Wemm     }
9330e3d5408SPeter Wemm     termpaths[count] = 0;
93439f2269fSPeter Wemm     if (save != 0)
93539f2269fSPeter Wemm 	*save = NCURSES_PATHSEP;
9360e3d5408SPeter Wemm     return count;
9370e3d5408SPeter Wemm }
9380e3d5408SPeter Wemm #define ADD_TC(path, count) filecount = add_tc(termpaths, path, count)
9390e3d5408SPeter Wemm #endif /* !USE_GETCAP */
9400e3d5408SPeter Wemm 
9417a69bbfbSPeter Wemm NCURSES_EXPORT(int)
94239f2269fSPeter Wemm _nc_read_termcap_entry(const char *const tn, TERMTYPE *const tp)
9430e3d5408SPeter Wemm {
9444a1a9510SRong-En Fan     int found = TGETENT_NO;
9450e3d5408SPeter Wemm     ENTRY *ep;
9460e3d5408SPeter Wemm #if USE_GETCAP_CACHE
9470e3d5408SPeter Wemm     char cwd_buf[PATH_MAX];
9480e3d5408SPeter Wemm #endif
9490e3d5408SPeter Wemm #if USE_GETCAP
95015589c42SPeter Wemm     char *p, tc[TBUFSIZ];
9514a1a9510SRong-En Fan     int status;
9520e3d5408SPeter Wemm     static char *source;
9530e3d5408SPeter Wemm     static int lineno;
9540e3d5408SPeter Wemm 
95539f2269fSPeter Wemm     T(("read termcap entry for %s", tn));
9564a1a9510SRong-En Fan 
9574a1a9510SRong-En Fan     if (strlen(tn) == 0
9584a1a9510SRong-En Fan 	|| strcmp(tn, ".") == 0
9594a1a9510SRong-En Fan 	|| strcmp(tn, "..") == 0
9604a1a9510SRong-En Fan 	|| _nc_pathlast(tn) != 0) {
9614a1a9510SRong-En Fan 	T(("illegal or missing entry name '%s'", tn));
9624a1a9510SRong-En Fan 	return TGETENT_NO;
9634a1a9510SRong-En Fan     }
9644a1a9510SRong-En Fan 
96518259542SPeter Wemm     if (use_terminfo_vars() && (p = getenv("TERMCAP")) != 0
9664a1a9510SRong-En Fan 	&& !_nc_is_abs_path(p) && _nc_name_match(p, tn, "|:")) {
96715589c42SPeter Wemm 	/* TERMCAP holds a termcap entry */
96815589c42SPeter Wemm 	strncpy(tc, p, sizeof(tc) - 1);
96915589c42SPeter Wemm 	tc[sizeof(tc) - 1] = '\0';
97015589c42SPeter Wemm 	_nc_set_source("TERMCAP");
97115589c42SPeter Wemm     } else {
9720e3d5408SPeter Wemm 	/* we're using getcap(3) */
9734a1a9510SRong-En Fan 	if ((status = _nc_tgetent(tc, &source, &lineno, tn)) < 0)
9744a1a9510SRong-En Fan 	    return (status == TC_NOT_FOUND ? TGETENT_NO : TGETENT_ERR);
9750e3d5408SPeter Wemm 
9760e3d5408SPeter Wemm 	_nc_curr_line = lineno;
9770e3d5408SPeter Wemm 	_nc_set_source(source);
97815589c42SPeter Wemm     }
9790e3d5408SPeter Wemm     _nc_read_entry_source((FILE *) 0, tc, FALSE, FALSE, NULLHOOK);
9800e3d5408SPeter Wemm #else
9810e3d5408SPeter Wemm     /*
9820e3d5408SPeter Wemm      * Here is what the 4.4BSD termcap(3) page prescribes:
9830e3d5408SPeter Wemm      *
98415589c42SPeter Wemm      * It will look in the environment for a TERMCAP variable.  If found, and
98515589c42SPeter Wemm      * the value does not begin with a slash, and the terminal type name is the
98615589c42SPeter Wemm      * same as the environment string TERM, the TERMCAP string is used instead
98715589c42SPeter Wemm      * of reading a termcap file.  If it does begin with a slash, the string is
98815589c42SPeter Wemm      * used as a path name of the termcap file to search.  If TERMCAP does not
98915589c42SPeter Wemm      * begin with a slash and name is different from TERM, tgetent() searches
99015589c42SPeter Wemm      * the files $HOME/.termcap and /usr/share/misc/termcap, in that order,
99115589c42SPeter Wemm      * unless the environment variable TERMPATH exists, in which case it
99215589c42SPeter Wemm      * specifies a list of file pathnames (separated by spaces or colons) to be
99315589c42SPeter Wemm      * searched instead.
9940e3d5408SPeter Wemm      *
9950e3d5408SPeter Wemm      * It goes on to state:
9960e3d5408SPeter Wemm      *
9970e3d5408SPeter Wemm      * Whenever multiple files are searched and a tc field occurs in the
99815589c42SPeter Wemm      * requested entry, the entry it names must be found in the same file or
99915589c42SPeter Wemm      * one of the succeeding files.
10000e3d5408SPeter Wemm      *
10010e3d5408SPeter Wemm      * However, this restriction is relaxed in ncurses; tc references to
10020e3d5408SPeter Wemm      * previous files are permitted.
10030e3d5408SPeter Wemm      *
100415589c42SPeter Wemm      * This routine returns 1 if an entry is found, 0 if not found, and -1 if
100515589c42SPeter Wemm      * the database is not accessible.
10060e3d5408SPeter Wemm      */
10070e3d5408SPeter Wemm     FILE *fp;
10080e3d5408SPeter Wemm     char *tc, *termpaths[MAXPATHS];
10090e3d5408SPeter Wemm     int filecount = 0;
101039f2269fSPeter Wemm     int j, k;
10110e3d5408SPeter Wemm     bool use_buffer = FALSE;
101239f2269fSPeter Wemm     bool normal = TRUE;
10130e3d5408SPeter Wemm     char tc_buf[1024];
10140e3d5408SPeter Wemm     char pathbuf[PATH_MAX];
101539f2269fSPeter Wemm     char *copied = 0;
101639f2269fSPeter Wemm     char *cp;
101739f2269fSPeter Wemm     struct stat test_stat[MAXPATHS];
10180e3d5408SPeter Wemm 
10190e3d5408SPeter Wemm     termpaths[filecount] = 0;
102018259542SPeter Wemm     if (use_terminfo_vars() && (tc = getenv("TERMCAP")) != 0) {
10214a1a9510SRong-En Fan 	if (_nc_is_abs_path(tc)) {	/* interpret as a filename */
10220e3d5408SPeter Wemm 	    ADD_TC(tc, 0);
102339f2269fSPeter Wemm 	    normal = FALSE;
102415589c42SPeter Wemm 	} else if (_nc_name_match(tc, tn, "|:")) {	/* treat as a capability file */
10250e3d5408SPeter Wemm 	    use_buffer = TRUE;
10260e3d5408SPeter Wemm 	    (void) sprintf(tc_buf, "%.*s\n", (int) sizeof(tc_buf) - 2, tc);
102739f2269fSPeter Wemm 	    normal = FALSE;
102839f2269fSPeter Wemm 	}
102939f2269fSPeter Wemm     }
10300e3d5408SPeter Wemm 
103139f2269fSPeter Wemm     if (normal) {		/* normal case */
103239f2269fSPeter Wemm 	char envhome[PATH_MAX], *h;
103339f2269fSPeter Wemm 
103439f2269fSPeter Wemm 	copied = strdup(get_termpath());
103539f2269fSPeter Wemm 	for (cp = copied; *cp; cp++) {
10367a69bbfbSPeter Wemm 	    if (*cp == NCURSES_PATHSEP)
10370e3d5408SPeter Wemm 		*cp = '\0';
103839f2269fSPeter Wemm 	    else if (cp == copied || cp[-1] == '\0') {
10390e3d5408SPeter Wemm 		ADD_TC(cp, filecount);
10400e3d5408SPeter Wemm 	    }
10410e3d5408SPeter Wemm 	}
10420e3d5408SPeter Wemm 
10430e3d5408SPeter Wemm #define PRIVATE_CAP "%s/.termcap"
10440e3d5408SPeter Wemm 
10457a69bbfbSPeter Wemm 	if (use_terminfo_vars() && (h = getenv("HOME")) != NULL && *h != '\0'
104615589c42SPeter Wemm 	    && (strlen(h) + sizeof(PRIVATE_CAP)) < PATH_MAX) {
10470e3d5408SPeter Wemm 	    /* user's .termcap, if any, should override it */
10480e3d5408SPeter Wemm 	    (void) strcpy(envhome, h);
10490e3d5408SPeter Wemm 	    (void) sprintf(pathbuf, PRIVATE_CAP, envhome);
10500e3d5408SPeter Wemm 	    ADD_TC(pathbuf, filecount);
10510e3d5408SPeter Wemm 	}
10520e3d5408SPeter Wemm     }
10530e3d5408SPeter Wemm 
105439f2269fSPeter Wemm     /*
105539f2269fSPeter Wemm      * Probably /etc/termcap is a symlink to /usr/share/misc/termcap.
105639f2269fSPeter Wemm      * Avoid reading the same file twice.
105739f2269fSPeter Wemm      */
10584a1a9510SRong-En Fan #if HAVE_LINK
105939f2269fSPeter Wemm     for (j = 0; j < filecount; j++) {
106039f2269fSPeter Wemm 	bool omit = FALSE;
106139f2269fSPeter Wemm 	if (stat(termpaths[j], &test_stat[j]) != 0
106239f2269fSPeter Wemm 	    || (test_stat[j].st_mode & S_IFMT) != S_IFREG) {
106339f2269fSPeter Wemm 	    omit = TRUE;
106439f2269fSPeter Wemm 	} else {
106539f2269fSPeter Wemm 	    for (k = 0; k < j; k++) {
106639f2269fSPeter Wemm 		if (test_stat[k].st_dev == test_stat[j].st_dev
106739f2269fSPeter Wemm 		    && test_stat[k].st_ino == test_stat[j].st_ino) {
106839f2269fSPeter Wemm 		    omit = TRUE;
106939f2269fSPeter Wemm 		    break;
107039f2269fSPeter Wemm 		}
107139f2269fSPeter Wemm 	    }
107239f2269fSPeter Wemm 	}
107339f2269fSPeter Wemm 	if (omit) {
107439f2269fSPeter Wemm 	    T(("Path %s is a duplicate", termpaths[j]));
107539f2269fSPeter Wemm 	    for (k = j + 1; k < filecount; k++) {
107639f2269fSPeter Wemm 		termpaths[k - 1] = termpaths[k];
107739f2269fSPeter Wemm 		test_stat[k - 1] = test_stat[k];
107839f2269fSPeter Wemm 	    }
107939f2269fSPeter Wemm 	    --filecount;
108039f2269fSPeter Wemm 	    --j;
108139f2269fSPeter Wemm 	}
108239f2269fSPeter Wemm     }
108339f2269fSPeter Wemm #endif
108439f2269fSPeter Wemm 
10850e3d5408SPeter Wemm     /* parse the sources */
108615589c42SPeter Wemm     if (use_buffer) {
10870e3d5408SPeter Wemm 	_nc_set_source("TERMCAP");
10880e3d5408SPeter Wemm 
10890e3d5408SPeter Wemm 	/*
10900e3d5408SPeter Wemm 	 * We don't suppress warning messages here.  The presumption is
10910e3d5408SPeter Wemm 	 * that since it's just a single entry, they won't be a pain.
10920e3d5408SPeter Wemm 	 */
10930e3d5408SPeter Wemm 	_nc_read_entry_source((FILE *) 0, tc_buf, FALSE, FALSE, NULLHOOK);
10940e3d5408SPeter Wemm     } else {
10950e3d5408SPeter Wemm 	int i;
10960e3d5408SPeter Wemm 
10970e3d5408SPeter Wemm 	for (i = 0; i < filecount; i++) {
10980e3d5408SPeter Wemm 
10990e3d5408SPeter Wemm 	    T(("Looking for %s in %s", tn, termpaths[i]));
110039f2269fSPeter Wemm 	    if (_nc_access(termpaths[i], R_OK) == 0
110139f2269fSPeter Wemm 		&& (fp = fopen(termpaths[i], "r")) != (FILE *) 0) {
11020e3d5408SPeter Wemm 		_nc_set_source(termpaths[i]);
11030e3d5408SPeter Wemm 
11040e3d5408SPeter Wemm 		/*
110515589c42SPeter Wemm 		 * Suppress warning messages.  Otherwise you get 400 lines of
110615589c42SPeter Wemm 		 * crap from archaic termcap files as ncurses complains about
110715589c42SPeter Wemm 		 * all the obsolete capabilities.
11080e3d5408SPeter Wemm 		 */
11090e3d5408SPeter Wemm 		_nc_read_entry_source(fp, (char *) 0, FALSE, TRUE, NULLHOOK);
11100e3d5408SPeter Wemm 
11110e3d5408SPeter Wemm 		(void) fclose(fp);
11120e3d5408SPeter Wemm 	    }
11130e3d5408SPeter Wemm 	}
11140e3d5408SPeter Wemm     }
111539f2269fSPeter Wemm     if (copied != 0)
111639f2269fSPeter Wemm 	free(copied);
11170e3d5408SPeter Wemm #endif /* USE_GETCAP */
11180e3d5408SPeter Wemm 
11190e3d5408SPeter Wemm     if (_nc_head == 0)
11204a1a9510SRong-En Fan 	return (TGETENT_ERR);
11210e3d5408SPeter Wemm 
11220e3d5408SPeter Wemm     /* resolve all use references */
11234a1a9510SRong-En Fan     _nc_resolve_uses2(TRUE, FALSE);
11240e3d5408SPeter Wemm 
11250e3d5408SPeter Wemm     /* find a terminal matching tn, if we can */
11260e3d5408SPeter Wemm #if USE_GETCAP_CACHE
112715589c42SPeter Wemm     if (getcwd(cwd_buf, sizeof(cwd_buf)) != 0) {
11280e3d5408SPeter Wemm 	_nc_set_writedir((char *) 0);	/* note: this does a chdir */
11290e3d5408SPeter Wemm #endif
11300e3d5408SPeter Wemm 	for_entry_list(ep) {
113115589c42SPeter Wemm 	    if (_nc_name_match(ep->tterm.term_names, tn, "|:")) {
11320e3d5408SPeter Wemm 		/*
11334a1a9510SRong-En Fan 		 * Make a local copy of the terminal capabilities, delinked
11344a1a9510SRong-En Fan 		 * from the list.
11350e3d5408SPeter Wemm 		 */
11360e3d5408SPeter Wemm 		*tp = ep->tterm;
11374a1a9510SRong-En Fan 		_nc_delink_entry(_nc_head, &(ep->tterm));
11384a1a9510SRong-En Fan 		free(ep);
11390e3d5408SPeter Wemm 
11400e3d5408SPeter Wemm 		/*
114115589c42SPeter Wemm 		 * OK, now try to write the type to user's terminfo directory.
114215589c42SPeter Wemm 		 * Next time he loads this, it will come through terminfo.
11430e3d5408SPeter Wemm 		 *
114415589c42SPeter Wemm 		 * Advantage:  Second and subsequent fetches of this entry will
114515589c42SPeter Wemm 		 * be very fast.
11460e3d5408SPeter Wemm 		 *
114715589c42SPeter Wemm 		 * Disadvantage:  After the first time a termcap type is loaded
114815589c42SPeter Wemm 		 * by its user, editing it in the /etc/termcap file, or in
114915589c42SPeter Wemm 		 * TERMCAP, or in a local ~/.termcap, will be ineffective
115015589c42SPeter Wemm 		 * unless the terminfo entry is explicitly removed.
11510e3d5408SPeter Wemm 		 */
11520e3d5408SPeter Wemm #if USE_GETCAP_CACHE
11530e3d5408SPeter Wemm 		(void) _nc_write_entry(tp);
11540e3d5408SPeter Wemm #endif
11554a1a9510SRong-En Fan 		found = TGETENT_YES;
11560e3d5408SPeter Wemm 		break;
11570e3d5408SPeter Wemm 	    }
11580e3d5408SPeter Wemm 	}
11590e3d5408SPeter Wemm #if USE_GETCAP_CACHE
11600e3d5408SPeter Wemm 	chdir(cwd_buf);
11610e3d5408SPeter Wemm     }
11620e3d5408SPeter Wemm #endif
11630e3d5408SPeter Wemm 
11640e3d5408SPeter Wemm     return (found);
11650e3d5408SPeter Wemm }
11660e3d5408SPeter Wemm #else
11677a69bbfbSPeter Wemm extern
11687a69bbfbSPeter Wemm NCURSES_EXPORT(void)
11697a69bbfbSPeter Wemm _nc_read_termcap(void);
11707a69bbfbSPeter Wemm NCURSES_EXPORT(void)
117115589c42SPeter Wemm _nc_read_termcap(void)
117215589c42SPeter Wemm {
117315589c42SPeter Wemm }
11740e3d5408SPeter Wemm #endif /* PURE_TERMINFO */
1175