xref: /freebsd/contrib/ncurses/ncurses/tinfo/lib_termcap.c (revision f0a75d274af375d15b97b830966b99a02b7db911)
1 /****************************************************************************
2  * Copyright (c) 1998-2005,2006 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  *                                                                          *
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  ****************************************************************************/
38 
39 /* $FreeBSD$ */
40 
41 #define __INTERNAL_CAPS_VISIBLE
42 #include <curses.priv.h>
43 
44 #include <termcap.h>
45 #include <tic.h>
46 #include <ctype.h>
47 
48 #include <term_entry.h>
49 
50 MODULE_ID("$Id: lib_termcap.c,v 1.58 2006/09/02 19:39:46 Miroslav.Lichvar Exp $")
51 
52 NCURSES_EXPORT_VAR(char *) UP = 0;
53 NCURSES_EXPORT_VAR(char *) BC = 0;
54 
55 #ifdef FREEBSD_NATIVE
56 extern char _nc_termcap[];	/* buffer to copy out */
57 #endif
58 
59 typedef struct {
60     long sequence;
61     char *fix_sgr0;		/* this holds the filtered sgr0 string */
62     char *last_bufp;		/* help with fix_sgr0 leak */
63     TERMINAL *last_term;
64 } CACHE;
65 
66 #define MAX_CACHE 4
67 static CACHE cache[MAX_CACHE];
68 static int in_cache = 0;
69 
70 #define FIX_SGR0 cache[in_cache].fix_sgr0
71 #define LAST_TRM cache[in_cache].last_term
72 #define LAST_BUF cache[in_cache].last_bufp
73 #define LAST_SEQ cache[in_cache].sequence
74 
75 /***************************************************************************
76  *
77  * tgetent(bufp, term)
78  *
79  * In termcap, this function reads in the entry for terminal `term' into the
80  * buffer pointed to by bufp. It must be called before any of the functions
81  * below are called.
82  * In this terminfo emulation, tgetent() simply calls setupterm() (which
83  * does a bit more than tgetent() in termcap does), and returns its return
84  * value (1 if successful, 0 if no terminal with the given name could be
85  * found, or -1 if no terminal descriptions have been installed on the
86  * system).  The bufp argument is ignored.
87  *
88  ***************************************************************************/
89 
90 NCURSES_EXPORT(int)
91 tgetent(char *bufp, const char *name)
92 {
93     static long sequence;
94 
95     int errcode;
96     int n;
97     bool found_cache = FALSE;
98 
99     START_TRACE();
100     T((T_CALLED("tgetent()")));
101 
102     _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
103 
104     /*
105      * In general we cannot tell if the fixed sgr0 is still used by the
106      * caller, but if tgetent() is called with the same buffer, that is
107      * good enough, since the previous data would be invalidated by the
108      * current call.
109      */
110     for (n = 0; n < MAX_CACHE; ++n) {
111 	bool same_result = (bufp != 0 && cache[n].last_bufp == bufp);
112 	if (same_result) {
113 	    in_cache = n;
114 	    if (FIX_SGR0 != 0) {
115 		FreeAndNull(FIX_SGR0);
116 	    }
117 	    /*
118 	     * Also free the terminfo data that we loaded (much bigger leak).
119 	     */
120 	    if (LAST_TRM != 0 && LAST_TRM != cur_term) {
121 		TERMINAL *trm = LAST_TRM;
122 		del_curterm(LAST_TRM);
123 		for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache)
124 		    if (LAST_TRM == trm)
125 			LAST_TRM = 0;
126 		in_cache = n;
127 	    }
128 	    found_cache = TRUE;
129 	    break;
130 	}
131     }
132     if (!found_cache) {
133 	int best = 0;
134 
135 	for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache) {
136 	    if (LAST_SEQ < cache[best].sequence) {
137 		best = in_cache;
138 	    }
139 	}
140 	in_cache = best;
141     }
142     LAST_TRM = cur_term;
143     LAST_SEQ = ++sequence;
144 
145     PC = 0;
146     UP = 0;
147     BC = 0;
148     FIX_SGR0 = 0;		/* don't free it - application may still use */
149 
150     if (errcode == 1) {
151 
152 	if (cursor_left)
153 	    if ((backspaces_with_bs = !strcmp(cursor_left, "\b")) == 0)
154 		backspace_if_not_bs = cursor_left;
155 
156 	/* we're required to export these */
157 	if (pad_char != NULL)
158 	    PC = pad_char[0];
159 	if (cursor_up != NULL)
160 	    UP = cursor_up;
161 	if (backspace_if_not_bs != NULL)
162 	    BC = backspace_if_not_bs;
163 
164 	if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) {
165 	    if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
166 		if (FIX_SGR0 != exit_attribute_mode) {
167 		    free(FIX_SGR0);
168 		}
169 		FIX_SGR0 = 0;
170 	    }
171 	}
172 	LAST_BUF = bufp;
173 
174 	(void) baudrate();	/* sets ospeed as a side-effect */
175 
176 /* LINT_PREPRO
177 #if 0*/
178 #include <capdefaults.c>
179 /* LINT_PREPRO
180 #endif*/
181 
182     }
183 
184 #ifdef FREEBSD_NATIVE
185     /*
186      * This is a REALLY UGLY hack. Basically, if we originate with
187      * a termcap source, try and copy it out.
188      */
189     if (bufp && _nc_termcap[0])
190 	strncpy(bufp, _nc_termcap, 1024);
191 #endif
192 
193     returnCode(errcode);
194 }
195 
196 /***************************************************************************
197  *
198  * tgetflag(str)
199  *
200  * Look up boolean termcap capability str and return its value (TRUE=1 if
201  * present, FALSE=0 if not).
202  *
203  ***************************************************************************/
204 
205 NCURSES_EXPORT(int)
206 tgetflag(NCURSES_CONST char *id)
207 {
208     unsigned i;
209 
210     T((T_CALLED("tgetflag(%s)"), id));
211     if (cur_term != 0) {
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]);
218 	    }
219 	}
220     }
221     returnCode(0);		/* Solaris does this */
222 }
223 
224 /***************************************************************************
225  *
226  * tgetnum(str)
227  *
228  * Look up numeric termcap capability str and return its value, or -1 if
229  * not given.
230  *
231  ***************************************************************************/
232 
233 NCURSES_EXPORT(int)
234 tgetnum(NCURSES_CONST char *id)
235 {
236     unsigned i;
237 
238     T((T_CALLED("tgetnum(%s)"), id));
239     if (cur_term != 0) {
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]);
247 	    }
248 	}
249     }
250     returnCode(ABSENT_NUMERIC);
251 }
252 
253 /***************************************************************************
254  *
255  * tgetstr(str, area)
256  *
257  * Look up string termcap capability str and return a pointer to its value,
258  * or NULL if not given.
259  *
260  ***************************************************************************/
261 
262 NCURSES_EXPORT(char *)
263 tgetstr(NCURSES_CONST char *id, char **area)
264 {
265     unsigned i;
266     char *result = NULL;
267 
268     T((T_CALLED("tgetstr(%s,%p)"), id, area));
269     if (cur_term != 0) {
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
279 			&& FIX_SGR0 != 0) {
280 			result = FIX_SGR0;
281 			TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
282 		    }
283 		    if (area != 0
284 			&& *area != 0) {
285 			(void) strcpy(*area, result);
286 			result = *area;
287 			*area += strlen(*area) + 1;
288 		    }
289 		}
290 		break;
291 	    }
292 	}
293     }
294     returnPtr(result);
295 }
296 
297 #if NO_LEAKS
298 NCURSES_EXPORT(void)
299 _nc_tgetent_leaks(void)
300 {
301     for (in_cache = 0; in_cache < MAX_CACHE; ++in_cache) {
302 	FreeIfNeeded(FIX_SGR0);
303 	del_curterm(LAST_TRM);
304     }
305 }
306 #endif
307