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