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