1 /****************************************************************************
2 * Copyright (c) 1998-2008,2009 Free Software Foundation, Inc. *
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: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
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. *
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 *
27 ****************************************************************************/
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 *
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 ****************************************************************************/
40 #define __INTERNAL_CAPS_VISIBLE
41 #include <curses.priv.h>
47 #include <term_entry.h>
50 #define CUR SP_TERMTYPE
53 MODULE_ID("$Id: lib_termcap.c,v 1.67 2009/05/30 20:05:20 tom Exp $")
55 NCURSES_EXPORT_VAR(char *) UP = 0;
56 NCURSES_EXPORT_VAR(char *) BC = 0;
58 #define MyCache _nc_globals.tgetent_cache
59 #define CacheInx _nc_globals.tgetent_index
60 #define CacheSeq _nc_globals.tgetent_sequence
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
68 /***************************************************************************
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
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.
81 ***************************************************************************/
84 NCURSES_SP_NAME(tgetent) (NCURSES_SP_DCLx char *bufp, const char *name)
88 bool found_cache = FALSE;
89 #ifdef USE_TERM_DRIVER
94 T((T_CALLED("tgetent()")));
96 #ifdef USE_TERM_DRIVER
97 _nc_setupterm_ex(&termp, (NCURSES_CONST char *) name,
98 STDOUT_FILENO, &errcode, TRUE);
101 !((TERMINAL_CONTROL_BLOCK *) termp)->drv->isTerminfo)
104 _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
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
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.
119 for (n = 0; n < TGETENT_MAX; ++n) {
120 bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
124 FreeAndNull(FIX_SGR0);
127 * Also free the terminfo data that we loaded (much bigger leak).
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)
144 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
145 if (LAST_SEQ < MyCache[best].sequence) {
151 LAST_TRM = TerminalOf(SP_PARM);
152 LAST_SEQ = ++CacheSeq;
157 FIX_SGR0 = 0; /* don't free it - application may still use */
162 if ((backspaces_with_bs = (char) !strcmp(cursor_left, "\b")) == 0)
163 backspace_if_not_bs = cursor_left;
165 /* we're required to export these */
166 if (pad_char != NULL)
168 if (cursor_up != NULL)
170 if (backspace_if_not_bs != NULL)
171 BC = backspace_if_not_bs;
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) {
184 SetNoPadding(SP_PARM);
185 (void) NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets ospeed as a side-effect */
189 #include <capdefaults.c>
199 tgetent(char *bufp, const char *name)
201 return NCURSES_SP_NAME(tgetent) (CURRENT_SCREEN, bufp, name);
205 /***************************************************************************
209 * Look up boolean termcap capability str and return its value (TRUE=1 if
210 * present, FALSE=0 if not).
212 ***************************************************************************/
215 NCURSES_SP_NAME(tgetflag) (NCURSES_SP_DCLx NCURSES_CONST char *id)
219 T((T_CALLED("tgetflag(%p, %s)"), SP_PARM, id));
220 if (HasTInfoTerminal(SP_PARM)) {
221 TERMTYPE *tp = &(TerminalOf(SP_PARM)->type);
222 for_each_boolean(i, tp) {
223 const char *capname = ExtBoolname(tp, i, boolcodes);
224 if (!strncmp(id, capname, 2)) {
225 /* setupterm forces invalid booleans to false */
226 returnCode(tp->Booleans[i]);
230 returnCode(0); /* Solaris does this */
235 tgetflag(NCURSES_CONST char *id)
237 return NCURSES_SP_NAME(tgetflag) (CURRENT_SCREEN, id);
241 /***************************************************************************
245 * Look up numeric termcap capability str and return its value, or -1 if
248 ***************************************************************************/
251 NCURSES_SP_NAME(tgetnum) (NCURSES_SP_DCLx NCURSES_CONST char *id)
255 T((T_CALLED("tgetnum(%p, %s)"), SP_PARM, id));
256 if (HasTInfoTerminal(SP_PARM)) {
257 TERMTYPE *tp = &(TerminalOf(SP_PARM)->type);
258 for_each_number(i, tp) {
259 const char *capname = ExtNumname(tp, i, numcodes);
260 if (!strncmp(id, capname, 2)) {
261 if (!VALID_NUMERIC(tp->Numbers[i]))
262 returnCode(ABSENT_NUMERIC);
263 returnCode(tp->Numbers[i]);
267 returnCode(ABSENT_NUMERIC);
272 tgetnum(NCURSES_CONST char *id)
274 return NCURSES_SP_NAME(tgetnum) (CURRENT_SCREEN, id);
278 /***************************************************************************
282 * Look up string termcap capability str and return a pointer to its value,
283 * or NULL if not given.
285 ***************************************************************************/
287 NCURSES_EXPORT(char *)
288 NCURSES_SP_NAME(tgetstr) (NCURSES_SP_DCLx NCURSES_CONST char *id, char **area)
293 T((T_CALLED("tgetstr(%s,%p)"), id, area));
294 if (HasTInfoTerminal(SP_PARM)) {
295 TERMTYPE *tp = &(TerminalOf(SP_PARM)->type);
296 for_each_string(i, tp) {
297 const char *capname = ExtStrname(tp, i, strcodes);
298 if (!strncmp(id, capname, 2)) {
299 result = tp->Strings[i];
300 TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
301 /* setupterm forces canceled strings to null */
302 if (VALID_STRING(result)) {
303 if (result == exit_attribute_mode
306 TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
310 (void) strcpy(*area, result);
312 *area += strlen(*area) + 1;
323 NCURSES_EXPORT(char *)
324 tgetstr(NCURSES_CONST char *id, char **area)
326 return NCURSES_SP_NAME(tgetstr) (CURRENT_SCREEN, id, area);
332 _nc_tgetent_leaks(void)
334 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
335 FreeIfNeeded(FIX_SGR0);
337 del_curterm(LAST_TRM);