]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - contrib/ncurses/ncurses/tinfo/lib_termcap.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / contrib / ncurses / ncurses / tinfo / lib_termcap.c
1 /****************************************************************************
2  * Copyright (c) 1998-2006,2007 Free Software Foundation, Inc.              *
3  *                                                                          *
4  * Permission is hereby granted, free of charge, to any person obtaining a  *
5  * copy of this software and associated documentation files (the            *
6  * "Software"), to deal in the Software without restriction, including      *
7  * without limitation the rights to use, copy, modify, merge, publish,      *
8  * distribute, distribute with modifications, sublicense, and/or sell       *
9  * copies of the Software, and to permit persons to whom the Software is    *
10  * furnished to do so, subject to the following conditions:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
16  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
18  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
19  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
20  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
21  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
22  *                                                                          *
23  * Except as contained in this notice, the name(s) of the above copyright   *
24  * holders shall not be used in advertising or otherwise to promote the     *
25  * sale, use or other dealings in this Software without prior written       *
26  * authorization.                                                           *
27  ****************************************************************************/
28
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  *                                                                          *
34  * some of the code in here was contributed by:                             *
35  * Magnus Bengtsson, d6mbeng@dtek.chalmers.se (Nov'93)                      *
36  * (but it has changed a lot)                                               *
37  ****************************************************************************/
38
39 /* $FreeBSD$ */
40
41 #define __INTERNAL_CAPS_VISIBLE
42 #include <curses.priv.h>
43
44 #include <termcap.h>
45 #include <tic.h>
46 #include <ctype.h>
47
48 #include <term_entry.h>
49
50 MODULE_ID("$Id: lib_termcap.c,v 1.61 2007/06/02 19:36:03 tom Exp $")
51
52 NCURSES_EXPORT_VAR(char *) UP = 0;
53 NCURSES_EXPORT_VAR(char *) BC = 0;
54
55 #ifdef FREEBSD_NATIVE
56 extern char _nc_termcap[];      /* buffer to copy out */
57 #endif
58
59 #define MyCache  _nc_globals.tgetent_cache
60 #define CacheInx _nc_globals.tgetent_index
61 #define CacheSeq _nc_globals.tgetent_sequence
62
63 #define FIX_SGR0 MyCache[CacheInx].fix_sgr0
64 #define LAST_TRM MyCache[CacheInx].last_term
65 #define LAST_BUF MyCache[CacheInx].last_bufp
66 #define LAST_USE MyCache[CacheInx].last_used
67 #define LAST_SEQ MyCache[CacheInx].sequence
68
69 /***************************************************************************
70  *
71  * tgetent(bufp, term)
72  *
73  * In termcap, this function reads in the entry for terminal `term' into the
74  * buffer pointed to by bufp. It must be called before any of the functions
75  * below are called.
76  * In this terminfo emulation, tgetent() simply calls setupterm() (which
77  * does a bit more than tgetent() in termcap does), and returns its return
78  * value (1 if successful, 0 if no terminal with the given name could be
79  * found, or -1 if no terminal descriptions have been installed on the
80  * system).  The bufp argument is ignored.
81  *
82  ***************************************************************************/
83
84 NCURSES_EXPORT(int)
85 tgetent(char *bufp, const char *name)
86 {
87     int errcode;
88     int n;
89     bool found_cache = FALSE;
90
91     START_TRACE();
92     T((T_CALLED("tgetent()")));
93
94     _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
95
96     /*
97      * In general we cannot tell if the fixed sgr0 is still used by the
98      * caller, but if tgetent() is called with the same buffer, that is
99      * good enough, since the previous data would be invalidated by the
100      * current call.
101      *
102      * bufp may be a null pointer, e.g., GNU termcap.  That allocates data,
103      * which is good until the next tgetent() call.  The conventional termcap
104      * is inconvenient because of the fixed buffer size, but because it uses
105      * caller-supplied buffers, can have multiple terminal descriptions in
106      * use at a given time.
107      */
108     for (n = 0; n < TGETENT_MAX; ++n) {
109         bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
110         if (same_result) {
111             CacheInx = n;
112             if (FIX_SGR0 != 0) {
113                 FreeAndNull(FIX_SGR0);
114             }
115             /*
116              * Also free the terminfo data that we loaded (much bigger leak).
117              */
118             if (LAST_TRM != 0 && LAST_TRM != cur_term) {
119                 TERMINAL *trm = LAST_TRM;
120                 del_curterm(LAST_TRM);
121                 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx)
122                     if (LAST_TRM == trm)
123                         LAST_TRM = 0;
124                 CacheInx = n;
125             }
126             found_cache = TRUE;
127             break;
128         }
129     }
130     if (!found_cache) {
131         int best = 0;
132
133         for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
134             if (LAST_SEQ < MyCache[best].sequence) {
135                 best = CacheInx;
136             }
137         }
138         CacheInx = best;
139     }
140     LAST_TRM = cur_term;
141     LAST_SEQ = ++CacheSeq;
142
143     PC = 0;
144     UP = 0;
145     BC = 0;
146     FIX_SGR0 = 0;               /* don't free it - application may still use */
147
148     if (errcode == 1) {
149
150         if (cursor_left)
151             if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
152                 backspace_if_not_bs = cursor_left;
153
154         /* we're required to export these */
155         if (pad_char != NULL)
156             PC = pad_char[0];
157         if (cursor_up != NULL)
158             UP = cursor_up;
159         if (backspace_if_not_bs != NULL)
160             BC = backspace_if_not_bs;
161
162         if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) {
163             if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
164                 if (FIX_SGR0 != exit_attribute_mode) {
165                     free(FIX_SGR0);
166                 }
167                 FIX_SGR0 = 0;
168             }
169         }
170         LAST_BUF = bufp;
171         LAST_USE = TRUE;
172
173         (void) baudrate();      /* sets ospeed as a side-effect */
174
175 /* LINT_PREPRO
176 #if 0*/
177 #include <capdefaults.c>
178 /* LINT_PREPRO
179 #endif*/
180
181     }
182
183 #ifdef FREEBSD_NATIVE
184     /*
185      * This is a REALLY UGLY hack. Basically, if we originate with
186      * a termcap source, try and copy it out.
187      */
188     if (bufp && _nc_termcap[0])
189         strncpy(bufp, _nc_termcap, 1024);
190 #endif
191
192     returnCode(errcode);
193 }
194
195 /***************************************************************************
196  *
197  * tgetflag(str)
198  *
199  * Look up boolean termcap capability str and return its value (TRUE=1 if
200  * present, FALSE=0 if not).
201  *
202  ***************************************************************************/
203
204 NCURSES_EXPORT(int)
205 tgetflag(NCURSES_CONST char *id)
206 {
207     unsigned i;
208
209     T((T_CALLED("tgetflag(%s)"), id));
210     if (cur_term != 0) {
211         TERMTYPE *tp = &(cur_term->type);
212         for_each_boolean(i, tp) {
213             const char *capname = ExtBoolname(tp, i, boolcodes);
214             if (!strncmp(id, capname, 2)) {
215                 /* setupterm forces invalid booleans to false */
216                 returnCode(tp->Booleans[i]);
217             }
218         }
219     }
220     returnCode(0);              /* Solaris does this */
221 }
222
223 /***************************************************************************
224  *
225  * tgetnum(str)
226  *
227  * Look up numeric termcap capability str and return its value, or -1 if
228  * not given.
229  *
230  ***************************************************************************/
231
232 NCURSES_EXPORT(int)
233 tgetnum(NCURSES_CONST char *id)
234 {
235     unsigned i;
236
237     T((T_CALLED("tgetnum(%s)"), id));
238     if (cur_term != 0) {
239         TERMTYPE *tp = &(cur_term->type);
240         for_each_number(i, tp) {
241             const char *capname = ExtNumname(tp, i, numcodes);
242             if (!strncmp(id, capname, 2)) {
243                 if (!VALID_NUMERIC(tp->Numbers[i]))
244                     returnCode(ABSENT_NUMERIC);
245                 returnCode(tp->Numbers[i]);
246             }
247         }
248     }
249     returnCode(ABSENT_NUMERIC);
250 }
251
252 /***************************************************************************
253  *
254  * tgetstr(str, area)
255  *
256  * Look up string termcap capability str and return a pointer to its value,
257  * or NULL if not given.
258  *
259  ***************************************************************************/
260
261 NCURSES_EXPORT(char *)
262 tgetstr(NCURSES_CONST char *id, char **area)
263 {
264     unsigned i;
265     char *result = NULL;
266
267     T((T_CALLED("tgetstr(%s,%p)"), id, area));
268     if (cur_term != 0) {
269         TERMTYPE *tp = &(cur_term->type);
270         for_each_string(i, tp) {
271             const char *capname = ExtStrname(tp, i, strcodes);
272             if (!strncmp(id, capname, 2)) {
273                 result = tp->Strings[i];
274                 TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
275                 /* setupterm forces canceled strings to null */
276                 if (VALID_STRING(result)) {
277                     if (result == exit_attribute_mode
278                         && FIX_SGR0 != 0) {
279                         result = FIX_SGR0;
280                         TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
281                     }
282                     if (area != 0
283                         && *area != 0) {
284                         (void) strcpy(*area, result);
285                         result = *area;
286                         *area += strlen(*area) + 1;
287                     }
288                 }
289                 break;
290             }
291         }
292     }
293     returnPtr(result);
294 }
295
296 #if NO_LEAKS
297 NCURSES_EXPORT(void)
298 _nc_tgetent_leaks(void)
299 {
300     for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
301         FreeIfNeeded(FIX_SGR0);
302         if (LAST_TRM != 0)
303             del_curterm(LAST_TRM);
304     }
305 }
306 #endif