kernel - TMPFS - Bug fixing pass - paging to/from swap, vnode recycling
[dragonfly.git] / lib / libncurses / libncurses / termcap.c
blob1a8cbcc4a947945993eb9f7b65351fa7c8f75ed2
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 ***************************************************************************/
22 #include <curses.priv.h>
24 #include <string.h>
25 #include <term.h>
26 #include <tic.h>
27 #include <term_entry.h>
29 /* The rest is from BSD */
31 * Copyright (c) 1980, 1993
32 * The Regents of the University of California. All rights reserved.
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 * 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.
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.
59 #include <stdio.h>
60 #include <ctype.h>
61 #include <stdlib.h>
62 #include <string.h>
63 #include <unistd.h>
64 #include <sys/param.h>
65 #include "pathnames.h"
67 #define PBUFSIZ MAXPATHLEN /* max length of filename path */
68 #define PVECSIZ 32 /* max number of names in path */
69 #define TBUFSIZ 1024 /* max length of _nc_tgetent buffer */
71 char _nc_termcap[TBUFSIZ + 1]; /* Last getcap, provided to tgetent() emul */
74 * termcap - routines for dealing with the terminal capability data base
76 * BUG: Should use a "last" pointer in tbuf, so that searching
77 * for capabilities alphabetically would not be a n**2/2
78 * process when large numbers of capabilities are given.
79 * Note: If we add a last pointer now we will screw up the
80 * tc capability. We really should compile termcap.
82 * Essentially all the work here is scanning and decoding escapes
83 * in string capabilities. We don't use stdio because the editor
84 * doesn't, and because living w/o it is not hard.
88 * Get an entry for terminal name in buffer _nc_termcap from the termcap
89 * file.
91 int
92 _nc_read_termcap_entry(const char *const name, TERMTYPE *const tp)
94 ENTRY *ep;
95 char *p;
96 char *cp;
97 char *dummy;
98 char **fname;
99 char *home;
100 int i;
101 char pathbuf[PBUFSIZ]; /* holds raw path of filenames */
102 char *pathvec[PVECSIZ]; /* to point to names in pathbuf */
103 char **pvec; /* holds usable tail of path vector */
104 char *termpath;
106 _nc_termcap[0] = '\0'; /* in case */
107 dummy = NULL;
108 fname = pathvec;
109 pvec = pathvec;
110 p = pathbuf;
111 cp = getenv("TERMCAP");
113 * TERMCAP can have one of two things in it. It can be the
114 * name of a file to use instead of /etc/termcap. In this
115 * case it better start with a "/". Or it can be an entry to
116 * use so we don't have to read the file. In this case it
117 * has to already have the newlines crunched out. If TERMCAP
118 * does not hold a file name then a path of names is searched
119 * instead. The path is found in the TERMPATH variable, or
120 * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
122 if (!cp || *cp != '/') { /* no TERMCAP or it holds an entry */
123 if ( (termpath = getenv("TERMPATH")) )
124 strncpy(pathbuf, termpath, PBUFSIZ);
125 else {
126 if ( (home = getenv("HOME")) ) {/* set up default */
127 strncpy(pathbuf, home, PBUFSIZ - 1); /* $HOME first */
128 pathbuf[PBUFSIZ - 2] = '\0'; /* -2 because we add a slash */
129 p += strlen(pathbuf); /* path, looking in */
130 *p++ = '/';
131 } /* if no $HOME look in current directory */
132 strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf));
135 else /* user-defined name in TERMCAP */
136 strncpy(pathbuf, cp, PBUFSIZ); /* still can be tokenized */
138 /* For safety */
139 if (issetugid())
140 strcpy(pathbuf, _PATH_DEF_SEC);
142 pathbuf[PBUFSIZ - 1] = '\0';
144 *fname++ = pathbuf; /* tokenize path into vector of names */
145 while (*++p)
146 if (*p == ' ' || *p == ':') {
147 *p = '\0';
148 while (*++p)
149 if (*p != ' ' && *p != ':')
150 break;
151 if (*p == '\0')
152 break;
153 *fname++ = p;
154 if (fname >= pathvec + PVECSIZ) {
155 fname--;
156 break;
159 *fname = (char *) 0; /* mark end of vector */
160 if (cp && *cp && *cp != '/')
161 if (cgetset(cp) < 0)
162 return(-2);
164 i = cgetent(&dummy, pathvec, (char *)name);
166 if (i == 0) {
167 char *pd, *ps, *tok, *s, *tcs;
168 size_t len;
170 pd = _nc_termcap;
171 ps = dummy;
172 if ((tok = strchr(ps, ':')) == NULL) {
173 len = strlen(ps);
174 if (len >= TBUFSIZ)
175 i = -1;
176 else
177 strcpy(pd, ps);
178 goto done;
180 len = tok - ps + 1;
181 if (pd + len + 1 - _nc_termcap >= TBUFSIZ) {
182 i = -1;
183 goto done;
185 memcpy(pd, ps, len);
186 ps += len;
187 pd += len;
188 *pd = '\0';
189 tcs = pd - 1;
190 for (;;) {
191 while ((tok = strsep(&ps, ":")) != NULL &&
192 *(tok - 2) != '\\' &&
193 (*tok == '\0' || *tok == '\\' || !isgraph(UChar(*tok))))
195 if (tok == NULL)
196 break;
197 for (s = tcs; s != NULL && s[1] != '\0';
198 s = strchr(s, ':')) {
199 s++;
200 if (s[0] == tok[0] && s[1] == tok[1])
201 goto skip_it;
203 len = strlen(tok);
204 if (pd + len + 1 - _nc_termcap >= TBUFSIZ) {
205 i = -1;
206 break;
208 memcpy(pd, tok, len);
209 pd += len;
210 *pd++ = ':';
211 *pd = '\0';
212 skip_it: ;
215 done:
216 if (dummy)
217 free(dummy);
221 * From here on is ncurses-specific glue code
224 if (i < 0)
225 return(TGETENT_ERR);
227 _nc_set_source("TERMCAP");
228 _nc_read_entry_source((FILE *)NULL, _nc_termcap, FALSE, TRUE, NULLHOOK);
230 if (_nc_head == (ENTRY *)NULL)
231 return(TGETENT_ERR);
233 /* resolve all use references */
234 _nc_resolve_uses2(TRUE, FALSE);
236 for_entry_list(ep)
237 if (_nc_name_match(ep->tterm.term_names, name, "|:"))
240 * Make a local copy of the terminal capabilities, delinked
241 * from the list.
243 memcpy(tp, &ep->tterm, sizeof(TERMTYPE));
244 _nc_delink_entry(_nc_head, &(ep->tterm));
245 free(ep);
246 _nc_free_entries(_nc_head);
247 _nc_head = _nc_tail = NULL; /* do not reuse! */
249 return TGETENT_YES; /* OK */
252 _nc_free_entries(_nc_head);
253 _nc_head = _nc_tail = NULL; /* do not reuse! */
254 return(TGETENT_NO); /* not found */