termcap.c revision 330897
1/* A portion of this file is from ncurses: */
2/***************************************************************************
3*                            COPYRIGHT NOTICE                              *
4****************************************************************************
5*                ncurses is copyright (C) 1992-1995                        *
6*                          Zeyd M. Ben-Halim                               *
7*                          zmbenhal@netcom.com                             *
8*                          Eric S. Raymond                                 *
9*                          esr@snark.thyrsus.com                           *
10*                                                                          *
11*        Permission is hereby granted to reproduce and distribute ncurses  *
12*        by any means and for any fee, whether alone or as part of a       *
13*        larger distribution, in source or in binary form, PROVIDED        *
14*        this notice is included with any such distribution, and is not    *
15*        removed from any of its header files. Mention of ncurses in any   *
16*        applications linked with it is highly appreciated.                *
17*                                                                          *
18*        ncurses comes AS IS with no warranty, implied or expressed.       *
19*                                                                          *
20***************************************************************************/
21
22#include <curses.priv.h>
23
24#include <string.h>
25#include <term.h>
26#include <tic.h>
27#include <term_entry.h>
28
29/* The rest is from BSD */
30/*-
31 * SPDX-License-Identifier: BSD-3-Clause
32 *
33 * Copyright (c) 1980, 1993
34 *	The Regents of the University of California.  All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 *    notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 *    notice, this list of conditions and the following disclaimer in the
43 *    documentation and/or other materials provided with the distribution.
44 * 4. Neither the name of the University nor the names of its contributors
45 *    may be used to endorse or promote products derived from this software
46 *    without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 */
60
61#include <sys/cdefs.h>
62__FBSDID("$FreeBSD: stable/11/lib/ncurses/ncurses/termcap.c 330897 2018-03-14 03:19:51Z eadler $");
63
64#if 0
65#ifndef lint
66static const char sccsid[] = "@(#)termcap.c	8.1 (Berkeley) 6/4/93";
67#endif /* not lint */
68#endif
69
70#include <stdio.h>
71#include <ctype.h>
72#include <stdlib.h>
73#include <string.h>
74#include <unistd.h>
75#include <sys/param.h>
76#include "pathnames.h"
77
78#define	PBUFSIZ	MAXPATHLEN	/* max length of filename path */
79#define	PVECSIZ	32		/* max number of names in path */
80#define	TBUFSIZ 1024		/* max length of _nc_tgetent buffer */
81
82char _nc_termcap[TBUFSIZ + 1]; /* Last getcap, provided to tgetent() emul */
83
84/*
85 * termcap - routines for dealing with the terminal capability data base
86 *
87 * BUG:		Should use a "last" pointer in tbuf, so that searching
88 *		for capabilities alphabetically would not be a n**2/2
89 *		process when large numbers of capabilities are given.
90 * Note:	If we add a last pointer now we will screw up the
91 *		tc capability. We really should compile termcap.
92 *
93 * Essentially all the work here is scanning and decoding escapes
94 * in string capabilities.  We don't use stdio because the editor
95 * doesn't, and because living w/o it is not hard.
96 */
97
98/*
99 * Get an entry for terminal name in buffer _nc_termcap from the termcap
100 * file.
101 */
102int
103_nc_read_termcap_entry(const char *const name, TERMTYPE *const tp)
104{
105	ENTRY	*ep;
106	char *p;
107	char *cp;
108	char  *dummy;
109	char **fname;
110	char  *home;
111	int    i;
112	char   pathbuf[PBUFSIZ];	/* holds raw path of filenames */
113	char  *pathvec[PVECSIZ];	/* to point to names in pathbuf */
114	char **pvec;			/* holds usable tail of path vector */
115	char  *termpath;
116
117	_nc_termcap[0] = '\0';		/* in case */
118	dummy = NULL;
119	fname = pathvec;
120	pvec = pathvec;
121	p = pathbuf;
122	cp = getenv("TERMCAP");
123	/*
124	 * TERMCAP can have one of two things in it. It can be the
125	 * name of a file to use instead of /etc/termcap. In this
126	 * case it better start with a "/". Or it can be an entry to
127	 * use so we don't have to read the file. In this case it
128	 * has to already have the newlines crunched out.  If TERMCAP
129	 * does not hold a file name then a path of names is searched
130	 * instead.  The path is found in the TERMPATH variable, or
131	 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
132	 */
133	if (!cp || *cp != '/') {	/* no TERMCAP or it holds an entry */
134		if ( (termpath = getenv("TERMPATH")) )
135			strncpy(pathbuf, termpath, PBUFSIZ);
136		else {
137			if ( (home = getenv("HOME")) ) {/* set up default */
138				strncpy(pathbuf, home, PBUFSIZ - 1); /* $HOME first */
139				pathbuf[PBUFSIZ - 2] = '\0'; /* -2 because we add a slash */
140				p += strlen(pathbuf);	/* path, looking in */
141				*p++ = '/';
142			}	/* if no $HOME look in current directory */
143			strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf));
144		}
145	}
146	else				/* user-defined name in TERMCAP */
147		strncpy(pathbuf, cp, PBUFSIZ);	/* still can be tokenized */
148
149	/* For safety */
150	if (issetugid())
151		strcpy(pathbuf, _PATH_DEF_SEC);
152
153	pathbuf[PBUFSIZ - 1] = '\0';
154
155	*fname++ = pathbuf;	/* tokenize path into vector of names */
156	while (*++p)
157		if (*p == ' ' || *p == ':') {
158			*p = '\0';
159			while (*++p)
160				if (*p != ' ' && *p != ':')
161					break;
162			if (*p == '\0')
163				break;
164			*fname++ = p;
165			if (fname >= pathvec + PVECSIZ) {
166				fname--;
167				break;
168			}
169		}
170	*fname = (char *) 0;			/* mark end of vector */
171	if (cp && *cp && *cp != '/')
172		if (cgetset(cp) < 0)
173			return(-2);
174
175	i = cgetent(&dummy, pathvec, (char *)name);
176
177	if (i == 0) {
178		char *pd, *ps, *tok, *s, *tcs;
179		size_t len;
180
181		pd = _nc_termcap;
182		ps = dummy;
183		if ((tok = strchr(ps, ':')) == NULL) {
184			len = strlen(ps);
185			if (len >= TBUFSIZ)
186				i = -1;
187			else
188				strcpy(pd, ps);
189			goto done;
190		}
191		len = tok - ps + 1;
192		if (pd + len + 1 - _nc_termcap >= TBUFSIZ) {
193			i = -1;
194			goto done;
195		}
196		memcpy(pd, ps, len);
197		ps += len;
198		pd += len;
199		*pd = '\0';
200		tcs = pd - 1;
201		for (;;) {
202			while ((tok = strsep(&ps, ":")) != NULL &&
203			       *(tok - 2) != '\\' &&
204			       (*tok == '\0' || *tok == '\\' || !isgraph(UChar(*tok))))
205				;
206			if (tok == NULL)
207				break;
208			for (s = tcs; s != NULL && s[1] != '\0';
209			     s = strchr(s, ':')) {
210				s++;
211				if (s[0] == tok[0] && s[1] == tok[1])
212					goto skip_it;
213			}
214			len = strlen(tok);
215			if (pd + len + 1 - _nc_termcap >= TBUFSIZ) {
216				i = -1;
217				break;
218			}
219			memcpy(pd, tok, len);
220			pd += len;
221			*pd++ = ':';
222			*pd = '\0';
223		skip_it: ;
224		}
225	}
226done:
227	if (dummy)
228		free(dummy);
229
230
231/*
232 * From here on is ncurses-specific glue code
233 */
234
235	if (i < 0)
236		return(TGETENT_ERR);
237
238	_nc_set_source("TERMCAP");
239	_nc_read_entry_source((FILE *)NULL, _nc_termcap, FALSE, TRUE, NULLHOOK);
240
241	if (_nc_head == (ENTRY *)NULL)
242		return(TGETENT_ERR);
243
244	/* resolve all use references */
245	_nc_resolve_uses2(TRUE, FALSE);
246
247	for_entry_list(ep)
248		if (_nc_name_match(ep->tterm.term_names, name, "|:"))
249		{
250			/*
251			 * Make a local copy of the terminal capabilities, delinked
252			 * from the list.
253			 */
254			memcpy(tp, &ep->tterm, sizeof(TERMTYPE));
255			_nc_delink_entry(_nc_head, &(ep->tterm));
256			free(ep);
257			_nc_free_entries(_nc_head);
258			_nc_head = _nc_tail = NULL;	/* do not reuse! */
259
260			return TGETENT_YES;	/* OK */
261		}
262
263	_nc_free_entries(_nc_head);
264	_nc_head = _nc_tail = NULL;	/* do not reuse! */
265	return(TGETENT_NO);	/* not found */
266}
267