2103e84ce58a664d7ebb76fdc66d77461d2e77aa
[dragonfly.git] / lib / libncurses / libncurses / termcap.c
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  * @(#)termcap.c        8.1 (Berkeley) 6/4/93
63  * $FreeBSD: src/lib/libncurses/termcap.c,v 1.2.2.2 2002/08/15 08:30:26 ru Exp $
64  * $DragonFly: src/lib/libncurses/libncurses/termcap.c,v 1.1 2005/03/12 19:13:54 eirikn Exp $
65  */
66
67 #include <stdio.h>
68 #include <ctype.h>
69 #include <stdlib.h>
70 #include <string.h>
71 #include <unistd.h>
72 #include <sys/param.h>
73 #include "pathnames.h"
74
75 #define PBUFSIZ MAXPATHLEN      /* max length of filename path */
76 #define PVECSIZ 32              /* max number of names in path */
77 #define TBUFSIZ 1024            /* max length of _nc_tgetent buffer */
78
79 char _nc_termcap[TBUFSIZ + 1]; /* Last getcap, provided to tgetent() emul */
80
81 /*
82  * termcap - routines for dealing with the terminal capability data base
83  *
84  * BUG:         Should use a "last" pointer in tbuf, so that searching
85  *              for capabilities alphabetically would not be a n**2/2
86  *              process when large numbers of capabilities are given.
87  * Note:        If we add a last pointer now we will screw up the
88  *              tc capability. We really should compile termcap.
89  *
90  * Essentially all the work here is scanning and decoding escapes
91  * in string capabilities.  We don't use stdio because the editor
92  * doesn't, and because living w/o it is not hard.
93  */
94
95 /*
96  * Get an entry for terminal name in buffer _nc_termcap from the termcap
97  * file.
98  */
99 int
100 _nc_read_termcap_entry(const char *const name, TERMTYPE *const tp)
101 {
102         ENTRY   *ep;
103         char *p;
104         char *cp;
105         char  *dummy;
106         char **fname;
107         char  *home;
108         int    i;
109         char   pathbuf[PBUFSIZ];        /* holds raw path of filenames */
110         char  *pathvec[PVECSIZ];        /* to point to names in pathbuf */
111         char **pvec;                    /* holds usable tail of path vector */
112         char  *termpath;
113
114         _nc_termcap[0] = '\0';          /* in case */
115         dummy = NULL;
116         fname = pathvec;
117         pvec = pathvec;
118         p = pathbuf;
119         cp = getenv("TERMCAP");
120         /*
121          * TERMCAP can have one of two things in it. It can be the
122          * name of a file to use instead of /etc/termcap. In this
123          * case it better start with a "/". Or it can be an entry to
124          * use so we don't have to read the file. In this case it
125          * has to already have the newlines crunched out.  If TERMCAP
126          * does not hold a file name then a path of names is searched
127          * instead.  The path is found in the TERMPATH variable, or
128          * becomes "$HOME/.termcap /etc/termcap" if no TERMPATH exists.
129          */
130         if (!cp || *cp != '/') {        /* no TERMCAP or it holds an entry */
131                 if ( (termpath = getenv("TERMPATH")) )
132                         strncpy(pathbuf, termpath, PBUFSIZ);
133                 else {
134                         if ( (home = getenv("HOME")) ) {/* set up default */
135                                 strncpy(pathbuf, home, PBUFSIZ - 1); /* $HOME first */
136                                 pathbuf[PBUFSIZ - 2] = '\0'; /* -2 because we add a slash */
137                                 p += strlen(pathbuf);   /* path, looking in */
138                                 *p++ = '/';
139                         }       /* if no $HOME look in current directory */
140                         strncpy(p, _PATH_DEF, PBUFSIZ - (p - pathbuf));
141                 }
142         }
143         else                            /* user-defined name in TERMCAP */
144                 strncpy(pathbuf, cp, PBUFSIZ);  /* still can be tokenized */
145
146         /* For safety */
147         if (issetugid())
148                 strcpy(pathbuf, _PATH_DEF_SEC);
149
150         pathbuf[PBUFSIZ - 1] = '\0';
151
152         *fname++ = pathbuf;     /* tokenize path into vector of names */
153         while (*++p)
154                 if (*p == ' ' || *p == ':') {
155                         *p = '\0';
156                         while (*++p)
157                                 if (*p != ' ' && *p != ':')
158                                         break;
159                         if (*p == '\0')
160                                 break;
161                         *fname++ = p;
162                         if (fname >= pathvec + PVECSIZ) {
163                                 fname--;
164                                 break;
165                         }
166                 }
167         *fname = (char *) 0;                    /* mark end of vector */
168         if (cp && *cp && *cp != '/')
169                 if (cgetset(cp) < 0)
170                         return(-2);
171
172         i = cgetent(&dummy, pathvec, (char *)name);
173
174         if (i == 0) {
175                 char *pd, *ps, *tok, *s, *tcs;
176                 size_t len;
177
178                 pd = _nc_termcap;
179                 ps = dummy;
180                 if ((tok = strchr(ps, ':')) == NULL) {
181                         len = strlen(ps);
182                         if (len >= TBUFSIZ)
183                                 i = -1;
184                         else
185                                 strcpy(pd, ps);
186                         goto done;
187                 }
188                 len = tok - ps + 1;
189                 if (pd + len + 1 - _nc_termcap >= TBUFSIZ) {
190                         i = -1;
191                         goto done;
192                 }
193                 memcpy(pd, ps, len);
194                 ps += len;
195                 pd += len;
196                 *pd = '\0';
197                 tcs = pd - 1;
198                 for (;;) {
199                         while ((tok = strsep(&ps, ":")) != NULL &&
200                                *(tok - 2) != '\\' &&
201                                (*tok == '\0' || *tok == '\\' || !isgraph(*tok)))
202                                 ;
203                         if (tok == NULL)
204                                 break;
205                         for (s = tcs; s != NULL && s[1] != '\0';
206                              s = strchr(s, ':')) {
207                                 s++;
208                                 if (s[0] == tok[0] && s[1] == tok[1])
209                                         goto skip_it;
210                         }
211                         len = strlen(tok);
212                         if (pd + len + 1 - _nc_termcap >= TBUFSIZ) {
213                                 i = -1;
214                                 break;
215                         }
216                         memcpy(pd, tok, len);
217                         pd += len;
218                         *pd++ = ':';
219                         *pd = '\0';
220                 skip_it: ;
221                 }
222         }
223 done:
224         if (dummy)
225                 free(dummy);
226
227
228 /*
229  * From here on is ncurses-specific glue code
230  */
231
232         if (i < 0)
233                 return(ERR);
234
235         _nc_set_source("TERMCAP");
236         _nc_read_entry_source((FILE *)NULL, _nc_termcap, FALSE, TRUE, NULLHOOK);
237
238         if (_nc_head == (ENTRY *)NULL)
239                 return(ERR);
240
241         /* resolve all use references */
242         _nc_resolve_uses(TRUE);
243
244         for_entry_list(ep)
245                 if (_nc_name_match(ep->tterm.term_names, name, "|:"))
246                 {
247                         /*
248                          * Make a local copy of the terminal capabilities. free
249                          * all entry storage except the string table for the
250                          * loaded type (which we disconnected from the list by
251                          * NULLing out ep->tterm.str_table above).
252                          */
253                         memcpy(tp, &ep->tterm, sizeof(TERMTYPE));
254                         ep->tterm.str_table = (char *)NULL;
255                         _nc_free_entries(_nc_head);
256                         _nc_head = _nc_tail = NULL;     /* do not reuse! */
257
258                         return 1;       /* OK */
259                 }
260
261         _nc_free_entries(_nc_head);
262         _nc_head = _nc_tail = NULL;     /* do not reuse! */
263         return(0);      /* not found */
264 }