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