]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_termcap.c
33e15c55c179e569a6580446baef555aad4185f4
[ncurses.git] / ncurses / tinfo / lib_termcap.c
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 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.58 2006/09/02 19:39:46 Miroslav.Lichvar Exp $")
49
50 NCURSES_EXPORT_VAR(char *) UP = 0;
51 NCURSES_EXPORT_VAR(char *) BC = 0;
52
53 typedef struct {
54     long sequence;
55     char *fix_sgr0;             /* this holds the filtered sgr0 string */
56     char *last_bufp;            /* help with fix_sgr0 leak */
57     TERMINAL *last_term;
58 } CACHE;
59
60 #define MAX_CACHE 4
61 static CACHE cache[MAX_CACHE];
62 static int in_cache = 0;
63
64 #define FIX_SGR0 cache[in_cache].fix_sgr0
65 #define LAST_TRM cache[in_cache].last_term
66 #define LAST_BUF cache[in_cache].last_bufp
67 #define LAST_SEQ cache[in_cache].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     static long sequence;
88
89     int errcode;
90     int n;
91     bool found_cache = FALSE;
92
93     START_TRACE();
94     T((T_CALLED("tgetent()")));
95
96     _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
97
98     /*
99      * In general we cannot tell if the fixed sgr0 is still used by the
100      * caller, but if tgetent() is called with the same buffer, that is
101      * good enough, since the previous data would be invalidated by the
102      * current call.
103      */
104     for (n = 0; n < MAX_CACHE; ++n) {
105         bool same_result = (bufp != 0 && cache[n].last_bufp == bufp);
106         if (same_result) {
107             in_cache = n;
108             if (FIX_SGR0 != 0) {
109                 FreeAndNull(FIX_SGR0);
110             }
111             /*
112              * Also free the terminfo data that we loaded (much bigger leak).
113              */
114             if (LAST_TRM != 0 && LAST_TRM != cur_term) {
115                 TERMINAL *trm = LAST_TRM;
116                 del_curterm(LAST_TRM);
117                 for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache)
118                     if (LAST_TRM == trm)
119                         LAST_TRM = 0;
120                 in_cache = n;
121             }
122             found_cache = TRUE;
123             break;
124         }
125     }
126     if (!found_cache) {
127         int best = 0;
128
129         for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache) {
130             if (LAST_SEQ < cache[best].sequence) {
131                 best = in_cache;
132             }
133         }
134         in_cache = best;
135     }
136     LAST_TRM = cur_term;
137     LAST_SEQ = ++sequence;
138
139     PC = 0;
140     UP = 0;
141     BC = 0;
142     FIX_SGR0 = 0;               /* don't free it - application may still use */
143
144     if (errcode == 1) {
145
146         if (cursor_left)
147             if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
148                 backspace_if_not_bs = cursor_left;
149
150         /* we're required to export these */
151         if (pad_char != NULL)
152             PC = pad_char[0];
153         if (cursor_up != NULL)
154             UP = cursor_up;
155         if (backspace_if_not_bs != NULL)
156             BC = backspace_if_not_bs;
157
158         if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) {
159             if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
160                 if (FIX_SGR0 != exit_attribute_mode) {
161                     free(FIX_SGR0);
162                 }
163                 FIX_SGR0 = 0;
164             }
165         }
166         LAST_BUF = bufp;
167
168         (void) baudrate();      /* sets ospeed as a side-effect */
169
170 /* LINT_PREPRO
171 #if 0*/
172 #include <capdefaults.c>
173 /* LINT_PREPRO
174 #endif*/
175
176     }
177     returnCode(errcode);
178 }
179
180 /***************************************************************************
181  *
182  * tgetflag(str)
183  *
184  * Look up boolean termcap capability str and return its value (TRUE=1 if
185  * present, FALSE=0 if not).
186  *
187  ***************************************************************************/
188
189 NCURSES_EXPORT(int)
190 tgetflag(NCURSES_CONST char *id)
191 {
192     unsigned i;
193
194     T((T_CALLED("tgetflag(%s)"), id));
195     if (cur_term != 0) {
196         TERMTYPE *tp = &(cur_term->type);
197         for_each_boolean(i, tp) {
198             const char *capname = ExtBoolname(tp, i, boolcodes);
199             if (!strncmp(id, capname, 2)) {
200                 /* setupterm forces invalid booleans to false */
201                 returnCode(tp->Booleans[i]);
202             }
203         }
204     }
205     returnCode(0);              /* Solaris does this */
206 }
207
208 /***************************************************************************
209  *
210  * tgetnum(str)
211  *
212  * Look up numeric termcap capability str and return its value, or -1 if
213  * not given.
214  *
215  ***************************************************************************/
216
217 NCURSES_EXPORT(int)
218 tgetnum(NCURSES_CONST char *id)
219 {
220     unsigned i;
221
222     T((T_CALLED("tgetnum(%s)"), id));
223     if (cur_term != 0) {
224         TERMTYPE *tp = &(cur_term->type);
225         for_each_number(i, tp) {
226             const char *capname = ExtNumname(tp, i, numcodes);
227             if (!strncmp(id, capname, 2)) {
228                 if (!VALID_NUMERIC(tp->Numbers[i]))
229                     returnCode(ABSENT_NUMERIC);
230                 returnCode(tp->Numbers[i]);
231             }
232         }
233     }
234     returnCode(ABSENT_NUMERIC);
235 }
236
237 /***************************************************************************
238  *
239  * tgetstr(str, area)
240  *
241  * Look up string termcap capability str and return a pointer to its value,
242  * or NULL if not given.
243  *
244  ***************************************************************************/
245
246 NCURSES_EXPORT(char *)
247 tgetstr(NCURSES_CONST char *id, char **area)
248 {
249     unsigned i;
250     char *result = NULL;
251
252     T((T_CALLED("tgetstr(%s,%p)"), id, area));
253     if (cur_term != 0) {
254         TERMTYPE *tp = &(cur_term->type);
255         for_each_string(i, tp) {
256             const char *capname = ExtStrname(tp, i, strcodes);
257             if (!strncmp(id, capname, 2)) {
258                 result = tp->Strings[i];
259                 TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
260                 /* setupterm forces canceled strings to null */
261                 if (VALID_STRING(result)) {
262                     if (result == exit_attribute_mode
263                         && FIX_SGR0 != 0) {
264                         result = FIX_SGR0;
265                         TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
266                     }
267                     if (area != 0
268                         && *area != 0) {
269                         (void) strcpy(*area, result);
270                         result = *area;
271                         *area += strlen(*area) + 1;
272                     }
273                 }
274                 break;
275             }
276         }
277     }
278     returnPtr(result);
279 }
280
281 #if NO_LEAKS
282 NCURSES_EXPORT(void)
283 _nc_tgetent_leaks(void)
284 {
285     for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache) {
286         FreeIfNeeded(FIX_SGR0);
287         del_curterm(LAST_TRM);
288     }
289 }
290 #endif