]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_termcap.c
ncurses 5.7 - patch 20090718
[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.69 2009/07/11 18:14:21 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 = ERR;
87     int n;
88     bool found_cache = FALSE;
89 #ifdef USE_TERM_DRIVER
90     TERMINAL *termp = 0;
91 #endif
92
93     START_TRACE();
94     T((T_CALLED("tgetent()")));
95
96 #ifdef USE_TERM_DRIVER
97     _nc_setupterm_ex(&termp, (NCURSES_CONST char *) name,
98                      STDOUT_FILENO, &errcode, TRUE);
99
100     if (termp == 0 ||
101         !((TERMINAL_CONTROL_BLOCK *) termp)->drv->isTerminfo)
102         return (errcode);
103 #else
104     _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
105 #endif
106
107     /*
108      * In general we cannot tell if the fixed sgr0 is still used by the
109      * caller, but if tgetent() is called with the same buffer, that is
110      * good enough, since the previous data would be invalidated by the
111      * current call.
112      *
113      * bufp may be a null pointer, e.g., GNU termcap.  That allocates data,
114      * which is good until the next tgetent() call.  The conventional termcap
115      * is inconvenient because of the fixed buffer size, but because it uses
116      * caller-supplied buffers, can have multiple terminal descriptions in
117      * use at a given time.
118      */
119     for (n = 0; n < TGETENT_MAX; ++n) {
120         bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
121         if (same_result) {
122             CacheInx = n;
123             if (FIX_SGR0 != 0) {
124                 FreeAndNull(FIX_SGR0);
125             }
126             /*
127              * Also free the terminfo data that we loaded (much bigger leak).
128              */
129             if (LAST_TRM != 0 && LAST_TRM != TerminalOf(SP_PARM)) {
130                 TERMINAL *trm = LAST_TRM;
131                 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx LAST_TRM);
132                 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx)
133                     if (LAST_TRM == trm)
134                         LAST_TRM = 0;
135                 CacheInx = n;
136             }
137             found_cache = TRUE;
138             break;
139         }
140     }
141     if (!found_cache) {
142         int best = 0;
143
144         for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
145             if (LAST_SEQ < MyCache[best].sequence) {
146                 best = CacheInx;
147             }
148         }
149         CacheInx = best;
150     }
151     LAST_TRM = TerminalOf(SP_PARM);
152     LAST_SEQ = ++CacheSeq;
153
154     PC = 0;
155     UP = 0;
156     BC = 0;
157     FIX_SGR0 = 0;               /* don't free it - application may still use */
158
159     if (errcode == 1) {
160
161         if (cursor_left)
162             if ((backspaces_with_bs = (char) !strcmp(cursor_left, "\b")) == 0)
163                 backspace_if_not_bs = cursor_left;
164
165         /* we're required to export these */
166         if (pad_char != NULL)
167             PC = pad_char[0];
168         if (cursor_up != NULL)
169             UP = cursor_up;
170         if (backspace_if_not_bs != NULL)
171             BC = backspace_if_not_bs;
172
173         if ((FIX_SGR0 = _nc_trim_sgr0(&(TerminalOf(SP_PARM)->type))) != 0) {
174             if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
175                 if (FIX_SGR0 != exit_attribute_mode) {
176                     free(FIX_SGR0);
177                 }
178                 FIX_SGR0 = 0;
179             }
180         }
181         LAST_BUF = bufp;
182         LAST_USE = TRUE;
183
184         SetNoPadding(SP_PARM);
185         (void) NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);      /* sets ospeed as a side-effect */
186
187 /* LINT_PREPRO
188 #if 0*/
189 #include <capdefaults.c>
190 /* LINT_PREPRO
191 #endif*/
192
193     }
194     returnCode(errcode);
195 }
196
197 #if NCURSES_SP_FUNCS
198 NCURSES_EXPORT(int)
199 tgetent(char *bufp, const char *name)
200 {
201     return NCURSES_SP_NAME(tgetent) (CURRENT_SCREEN, bufp, name);
202 }
203 #endif
204
205 #if 0
206 static bool
207 same_tcname(const char *a, const char *b)
208 {
209     fprintf(stderr, "compare(%s,%s)\n", a, b);
210     return !strncmp(a, b, 2);
211 }
212 #else
213 #define same_tcname(a,b) !strncmp(a,b,2)
214 #endif
215
216 /***************************************************************************
217  *
218  * tgetflag(str)
219  *
220  * Look up boolean termcap capability str and return its value (TRUE=1 if
221  * present, FALSE=0 if not).
222  *
223  ***************************************************************************/
224
225 NCURSES_EXPORT(int)
226 NCURSES_SP_NAME(tgetflag) (NCURSES_SP_DCLx NCURSES_CONST char *id)
227 {
228     int result = 0;             /* Solaris returns zero for missing flag */
229     int i, j;
230
231     T((T_CALLED("tgetflag(%p, %s)"), SP_PARM, id));
232     if (HasTInfoTerminal(SP_PARM)) {
233         TERMTYPE *tp = &(TerminalOf(SP_PARM)->type);
234         struct name_table_entry const *entry_ptr;
235
236         entry_ptr = _nc_find_type_entry(id, BOOLEAN, TRUE);
237         if (entry_ptr != 0) {
238             j = entry_ptr->nte_index;
239         }
240 #if NCURSES_XNAMES
241         else {
242             j = -1;
243             for_each_ext_boolean(i, tp) {
244                 const char *capname = ExtBoolname(tp, i, boolcodes);
245                 if (same_tcname(id, capname)) {
246                     j = i;
247                     break;
248                 }
249             }
250         }
251 #endif
252         if (j >= 0) {
253             /* note: setupterm forces invalid booleans to false */
254             result = tp->Booleans[j];
255         }
256     }
257     returnCode(result);
258 }
259
260 #if NCURSES_SP_FUNCS
261 NCURSES_EXPORT(int)
262 tgetflag(NCURSES_CONST char *id)
263 {
264     return NCURSES_SP_NAME(tgetflag) (CURRENT_SCREEN, id);
265 }
266 #endif
267
268 /***************************************************************************
269  *
270  * tgetnum(str)
271  *
272  * Look up numeric termcap capability str and return its value, or -1 if
273  * not given.
274  *
275  ***************************************************************************/
276
277 NCURSES_EXPORT(int)
278 NCURSES_SP_NAME(tgetnum) (NCURSES_SP_DCLx NCURSES_CONST char *id)
279 {
280     int result = ABSENT_NUMERIC;
281     int i, j;
282
283     T((T_CALLED("tgetnum(%p, %s)"), SP_PARM, id));
284     if (HasTInfoTerminal(SP_PARM)) {
285         TERMTYPE *tp = &(TerminalOf(SP_PARM)->type);
286         struct name_table_entry const *entry_ptr;
287
288         entry_ptr = _nc_find_type_entry(id, NUMBER, TRUE);
289         if (entry_ptr != 0) {
290             j = entry_ptr->nte_index;
291         }
292 #if NCURSES_XNAMES
293         else {
294             j = -1;
295             for_each_ext_number(i, tp) {
296                 const char *capname = ExtNumname(tp, i, numcodes);
297                 if (same_tcname(id, capname)) {
298                     j = i;
299                     break;
300                 }
301             }
302         }
303 #endif
304         if (j >= 0) {
305             if (VALID_NUMERIC(tp->Numbers[j]))
306                 result = tp->Numbers[j];
307         }
308     }
309     returnCode(result);
310 }
311
312 #if NCURSES_SP_FUNCS
313 NCURSES_EXPORT(int)
314 tgetnum(NCURSES_CONST char *id)
315 {
316     return NCURSES_SP_NAME(tgetnum) (CURRENT_SCREEN, id);
317 }
318 #endif
319
320 /***************************************************************************
321  *
322  * tgetstr(str, area)
323  *
324  * Look up string termcap capability str and return a pointer to its value,
325  * or NULL if not given.
326  *
327  ***************************************************************************/
328
329 NCURSES_EXPORT(char *)
330 NCURSES_SP_NAME(tgetstr) (NCURSES_SP_DCLx NCURSES_CONST char *id, char **area)
331 {
332     char *result = NULL;
333     int i, j;
334
335     T((T_CALLED("tgetstr(%s,%p)"), id, area));
336     if (HasTInfoTerminal(SP_PARM)) {
337         TERMTYPE *tp = &(TerminalOf(SP_PARM)->type);
338         struct name_table_entry const *entry_ptr;
339
340         entry_ptr = _nc_find_type_entry(id, STRING, TRUE);
341         if (entry_ptr != 0) {
342             j = entry_ptr->nte_index;
343         }
344 #if NCURSES_XNAMES
345         else {
346             j = -1;
347             for_each_ext_string(i, tp) {
348                 const char *capname = ExtStrname(tp, i, strcodes);
349                 if (same_tcname(id, capname)) {
350                     j = i;
351                     break;
352                 }
353             }
354         }
355 #endif
356         if (j >= 0) {
357             result = tp->Strings[j];
358             TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
359             /* setupterm forces canceled strings to null */
360             if (VALID_STRING(result)) {
361                 if (result == exit_attribute_mode
362                     && FIX_SGR0 != 0) {
363                     result = FIX_SGR0;
364                     TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
365                 }
366                 if (area != 0
367                     && *area != 0) {
368                     (void) strcpy(*area, result);
369                     result = *area;
370                     *area += strlen(*area) + 1;
371                 }
372             }
373         }
374     }
375     returnPtr(result);
376 }
377
378 #if NCURSES_SP_FUNCS
379 NCURSES_EXPORT(char *)
380 tgetstr(NCURSES_CONST char *id, char **area)
381 {
382     return NCURSES_SP_NAME(tgetstr) (CURRENT_SCREEN, id, area);
383 }
384 #endif
385
386 #if NO_LEAKS
387 NCURSES_EXPORT(void)
388 _nc_tgetent_leaks(void)
389 {
390     for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
391         FreeIfNeeded(FIX_SGR0);
392         if (LAST_TRM != 0)
393             del_curterm(LAST_TRM);
394     }
395 }
396 #endif