]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_termcap.c
ncurses 5.6 - patch 20070303
[ncurses.git] / 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 #define __INTERNAL_CAPS_VISIBLE
40 #include <curses.priv.h>
41
42 #include <termcap.h>
43 #include <tic.h>
44 #include <ctype.h>
45
46 #include <term_entry.h>
47
48 MODULE_ID("$Id: lib_termcap.c,v 1.59 2007/01/28 00:33:11 tom Exp $")
49
50 NCURSES_EXPORT_VAR(char *) UP = 0;
51 NCURSES_EXPORT_VAR(char *) BC = 0;
52
53 typedef struct {
54     long sequence;
55     bool last_used;
56     char *fix_sgr0;             /* this holds the filtered sgr0 string */
57     char *last_bufp;            /* help with fix_sgr0 leak */
58     TERMINAL *last_term;
59 } CACHE;
60
61 #define MAX_CACHE 4
62 static CACHE cache[MAX_CACHE];
63 static int in_cache = 0;
64
65 #define FIX_SGR0 cache[in_cache].fix_sgr0
66 #define LAST_TRM cache[in_cache].last_term
67 #define LAST_BUF cache[in_cache].last_bufp
68 #define LAST_USE cache[in_cache].last_used
69 #define LAST_SEQ cache[in_cache].sequence
70
71 /***************************************************************************
72  *
73  * tgetent(bufp, term)
74  *
75  * In termcap, this function reads in the entry for terminal `term' into the
76  * buffer pointed to by bufp. It must be called before any of the functions
77  * below are called.
78  * In this terminfo emulation, tgetent() simply calls setupterm() (which
79  * does a bit more than tgetent() in termcap does), and returns its return
80  * value (1 if successful, 0 if no terminal with the given name could be
81  * found, or -1 if no terminal descriptions have been installed on the
82  * system).  The bufp argument is ignored.
83  *
84  ***************************************************************************/
85
86 NCURSES_EXPORT(int)
87 tgetent(char *bufp, const char *name)
88 {
89     static long sequence;
90
91     int errcode;
92     int n;
93     bool found_cache = FALSE;
94
95     START_TRACE();
96     T((T_CALLED("tgetent()")));
97
98     _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
99
100     /*
101      * In general we cannot tell if the fixed sgr0 is still used by the
102      * caller, but if tgetent() is called with the same buffer, that is
103      * good enough, since the previous data would be invalidated by the
104      * current call.
105      *
106      * bufp may be a null pointer, e.g., GNU termcap.  That allocates data,
107      * which is good until the next tgetent() call.  The conventional termcap
108      * is inconvenient because of the fixed buffer size, but because it uses
109      * caller-supplied buffers, can have multiple terminal descriptions in
110      * use at a given time.
111      */
112     for (n = 0; n < MAX_CACHE; ++n) {
113         bool same_result = (cache[n].last_used && cache[n].last_bufp == bufp);
114         if (same_result) {
115             in_cache = n;
116             if (FIX_SGR0 != 0) {
117                 FreeAndNull(FIX_SGR0);
118             }
119             /*
120              * Also free the terminfo data that we loaded (much bigger leak).
121              */
122             if (LAST_TRM != 0 && LAST_TRM != cur_term) {
123                 TERMINAL *trm = LAST_TRM;
124                 del_curterm(LAST_TRM);
125                 for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache)
126                     if (LAST_TRM == trm)
127                         LAST_TRM = 0;
128                 in_cache = n;
129             }
130             found_cache = TRUE;
131             break;
132         }
133     }
134     if (!found_cache) {
135         int best = 0;
136
137         for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache) {
138             if (LAST_SEQ < cache[best].sequence) {
139                 best = in_cache;
140             }
141         }
142         in_cache = best;
143     }
144     LAST_TRM = cur_term;
145     LAST_SEQ = ++sequence;
146
147     PC = 0;
148     UP = 0;
149     BC = 0;
150     FIX_SGR0 = 0;               /* don't free it - application may still use */
151
152     if (errcode == 1) {
153
154         if (cursor_left)
155             if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
156                 backspace_if_not_bs = cursor_left;
157
158         /* we're required to export these */
159         if (pad_char != NULL)
160             PC = pad_char[0];
161         if (cursor_up != NULL)
162             UP = cursor_up;
163         if (backspace_if_not_bs != NULL)
164             BC = backspace_if_not_bs;
165
166         if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) {
167             if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
168                 if (FIX_SGR0 != exit_attribute_mode) {
169                     free(FIX_SGR0);
170                 }
171                 FIX_SGR0 = 0;
172             }
173         }
174         LAST_BUF = bufp;
175         LAST_USE = TRUE;
176
177         (void) baudrate();      /* sets ospeed as a side-effect */
178
179 /* LINT_PREPRO
180 #if 0*/
181 #include <capdefaults.c>
182 /* LINT_PREPRO
183 #endif*/
184
185     }
186     returnCode(errcode);
187 }
188
189 /***************************************************************************
190  *
191  * tgetflag(str)
192  *
193  * Look up boolean termcap capability str and return its value (TRUE=1 if
194  * present, FALSE=0 if not).
195  *
196  ***************************************************************************/
197
198 NCURSES_EXPORT(int)
199 tgetflag(NCURSES_CONST char *id)
200 {
201     unsigned i;
202
203     T((T_CALLED("tgetflag(%s)"), id));
204     if (cur_term != 0) {
205         TERMTYPE *tp = &(cur_term->type);
206         for_each_boolean(i, tp) {
207             const char *capname = ExtBoolname(tp, i, boolcodes);
208             if (!strncmp(id, capname, 2)) {
209                 /* setupterm forces invalid booleans to false */
210                 returnCode(tp->Booleans[i]);
211             }
212         }
213     }
214     returnCode(0);              /* Solaris does this */
215 }
216
217 /***************************************************************************
218  *
219  * tgetnum(str)
220  *
221  * Look up numeric termcap capability str and return its value, or -1 if
222  * not given.
223  *
224  ***************************************************************************/
225
226 NCURSES_EXPORT(int)
227 tgetnum(NCURSES_CONST char *id)
228 {
229     unsigned i;
230
231     T((T_CALLED("tgetnum(%s)"), id));
232     if (cur_term != 0) {
233         TERMTYPE *tp = &(cur_term->type);
234         for_each_number(i, tp) {
235             const char *capname = ExtNumname(tp, i, numcodes);
236             if (!strncmp(id, capname, 2)) {
237                 if (!VALID_NUMERIC(tp->Numbers[i]))
238                     returnCode(ABSENT_NUMERIC);
239                 returnCode(tp->Numbers[i]);
240             }
241         }
242     }
243     returnCode(ABSENT_NUMERIC);
244 }
245
246 /***************************************************************************
247  *
248  * tgetstr(str, area)
249  *
250  * Look up string termcap capability str and return a pointer to its value,
251  * or NULL if not given.
252  *
253  ***************************************************************************/
254
255 NCURSES_EXPORT(char *)
256 tgetstr(NCURSES_CONST char *id, char **area)
257 {
258     unsigned i;
259     char *result = NULL;
260
261     T((T_CALLED("tgetstr(%s,%p)"), id, area));
262     if (cur_term != 0) {
263         TERMTYPE *tp = &(cur_term->type);
264         for_each_string(i, tp) {
265             const char *capname = ExtStrname(tp, i, strcodes);
266             if (!strncmp(id, capname, 2)) {
267                 result = tp->Strings[i];
268                 TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
269                 /* setupterm forces canceled strings to null */
270                 if (VALID_STRING(result)) {
271                     if (result == exit_attribute_mode
272                         && FIX_SGR0 != 0) {
273                         result = FIX_SGR0;
274                         TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
275                     }
276                     if (area != 0
277                         && *area != 0) {
278                         (void) strcpy(*area, result);
279                         result = *area;
280                         *area += strlen(*area) + 1;
281                     }
282                 }
283                 break;
284             }
285         }
286     }
287     returnPtr(result);
288 }
289
290 #if NO_LEAKS
291 NCURSES_EXPORT(void)
292 _nc_tgetent_leaks(void)
293 {
294     for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache) {
295         FreeIfNeeded(FIX_SGR0);
296         del_curterm(LAST_TRM);
297     }
298 }
299 #endif