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