1 /****************************************************************************
2 * Copyright (c) 1998-2007,2008 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 *
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 ****************************************************************************/
41 #define __INTERNAL_CAPS_VISIBLE
42 #include <curses.priv.h>
48 #include <term_entry.h>
50 MODULE_ID("$Id: lib_termcap.c,v 1.63 2008/08/16 19:22:55 tom Exp $")
52 NCURSES_EXPORT_VAR(char *) UP = 0;
53 NCURSES_EXPORT_VAR(char *) BC = 0;
56 extern char _nc_termcap[]; /* buffer to copy out */
59 #define MyCache _nc_globals.tgetent_cache
60 #define CacheInx _nc_globals.tgetent_index
61 #define CacheSeq _nc_globals.tgetent_sequence
63 #define FIX_SGR0 MyCache[CacheInx].fix_sgr0
64 #define LAST_TRM MyCache[CacheInx].last_term
65 #define LAST_BUF MyCache[CacheInx].last_bufp
66 #define LAST_USE MyCache[CacheInx].last_used
67 #define LAST_SEQ MyCache[CacheInx].sequence
69 /***************************************************************************
73 * In termcap, this function reads in the entry for terminal `term' into the
74 * buffer pointed to by bufp. It must be called before any of the functions
76 * In this terminfo emulation, tgetent() simply calls setupterm() (which
77 * does a bit more than tgetent() in termcap does), and returns its return
78 * value (1 if successful, 0 if no terminal with the given name could be
79 * found, or -1 if no terminal descriptions have been installed on the
80 * system). The bufp argument is ignored.
82 ***************************************************************************/
85 tgetent(char *bufp, const char *name)
89 bool found_cache = FALSE;
92 T((T_CALLED("tgetent()")));
94 _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
97 * In general we cannot tell if the fixed sgr0 is still used by the
98 * caller, but if tgetent() is called with the same buffer, that is
99 * good enough, since the previous data would be invalidated by the
102 * bufp may be a null pointer, e.g., GNU termcap. That allocates data,
103 * which is good until the next tgetent() call. The conventional termcap
104 * is inconvenient because of the fixed buffer size, but because it uses
105 * caller-supplied buffers, can have multiple terminal descriptions in
106 * use at a given time.
108 for (n = 0; n < TGETENT_MAX; ++n) {
109 bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
113 FreeAndNull(FIX_SGR0);
116 * Also free the terminfo data that we loaded (much bigger leak).
118 if (LAST_TRM != 0 && LAST_TRM != cur_term) {
119 TERMINAL *trm = LAST_TRM;
120 del_curterm(LAST_TRM);
121 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx)
133 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
134 if (LAST_SEQ < MyCache[best].sequence) {
141 LAST_SEQ = ++CacheSeq;
146 FIX_SGR0 = 0; /* don't free it - application may still use */
151 if ((backspaces_with_bs = (char) !strcmp(cursor_left, "\b")) == 0)
152 backspace_if_not_bs = cursor_left;
154 /* we're required to export these */
155 if (pad_char != NULL)
157 if (cursor_up != NULL)
159 if (backspace_if_not_bs != NULL)
160 BC = backspace_if_not_bs;
162 if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) {
163 if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
164 if (FIX_SGR0 != exit_attribute_mode) {
174 (void) baudrate(); /* sets ospeed as a side-effect */
178 #include <capdefaults.c>
184 #ifdef FREEBSD_NATIVE
186 * This is a REALLY UGLY hack. Basically, if we originate with
187 * a termcap source, try and copy it out.
189 if (bufp && _nc_termcap[0])
190 strncpy(bufp, _nc_termcap, 1024);
196 /***************************************************************************
200 * Look up boolean termcap capability str and return its value (TRUE=1 if
201 * present, FALSE=0 if not).
203 ***************************************************************************/
206 tgetflag(NCURSES_CONST char *id)
210 T((T_CALLED("tgetflag(%s)"), id));
212 TERMTYPE *tp = &(cur_term->type);
213 for_each_boolean(i, tp) {
214 const char *capname = ExtBoolname(tp, i, boolcodes);
215 if (!strncmp(id, capname, 2)) {
216 /* setupterm forces invalid booleans to false */
217 returnCode(tp->Booleans[i]);
221 returnCode(0); /* Solaris does this */
224 /***************************************************************************
228 * Look up numeric termcap capability str and return its value, or -1 if
231 ***************************************************************************/
234 tgetnum(NCURSES_CONST char *id)
238 T((T_CALLED("tgetnum(%s)"), id));
240 TERMTYPE *tp = &(cur_term->type);
241 for_each_number(i, tp) {
242 const char *capname = ExtNumname(tp, i, numcodes);
243 if (!strncmp(id, capname, 2)) {
244 if (!VALID_NUMERIC(tp->Numbers[i]))
245 returnCode(ABSENT_NUMERIC);
246 returnCode(tp->Numbers[i]);
250 returnCode(ABSENT_NUMERIC);
253 /***************************************************************************
257 * Look up string termcap capability str and return a pointer to its value,
258 * or NULL if not given.
260 ***************************************************************************/
262 NCURSES_EXPORT(char *)
263 tgetstr(NCURSES_CONST char *id, char **area)
268 T((T_CALLED("tgetstr(%s,%p)"), id, area));
270 TERMTYPE *tp = &(cur_term->type);
271 for_each_string(i, tp) {
272 const char *capname = ExtStrname(tp, i, strcodes);
273 if (!strncmp(id, capname, 2)) {
274 result = tp->Strings[i];
275 TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
276 /* setupterm forces canceled strings to null */
277 if (VALID_STRING(result)) {
278 if (result == exit_attribute_mode
281 TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
285 (void) strcpy(*area, result);
287 *area += strlen(*area) + 1;
299 _nc_tgetent_leaks(void)
301 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
302 FreeIfNeeded(FIX_SGR0);
304 del_curterm(LAST_TRM);