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