1 /****************************************************************************
2 * Copyright 2018-2024,2025 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
27 * authorization. *
28 ****************************************************************************/
29
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer *
35 * *
36 * some of the code in here was contributed by: *
37 * Magnus Bengtsson, d6mbeng@dtek.chalmers.se (Nov'93) *
38 * (but it has changed a lot) *
39 ****************************************************************************/
40
41 /* $FreeBSD$ */
42
43 #define __INTERNAL_CAPS_VISIBLE
44 #include <curses.priv.h>
45
46 #include <termcap.h>
47 #include <tic.h>
48 #include <ctype.h>
49
50 #ifndef CUR
51 #define CUR SP_TERMTYPE
52 #endif
53
54 MODULE_ID("$Id: lib_termcap.c,v 1.96 2025/12/27 12:33:34 tom Exp $")
55
56 NCURSES_EXPORT_VAR(char *) UP = NULL;
57 NCURSES_EXPORT_VAR(char *) BC = NULL;
58
59 #ifdef FREEBSD_NATIVE
60 extern char _nc_termcap[]; /* buffer to copy out */
61 #endif
62
63 #define MyCache _nc_globals.tgetent_cache
64 #define CacheInx _nc_globals.tgetent_index
65 #define CacheSeq _nc_globals.tgetent_sequence
66
67 #define FIX_SGR0 MyCache[CacheInx].fix_sgr0
68 #define LAST_TRM MyCache[CacheInx].last_term
69 #define LAST_BUF MyCache[CacheInx].last_bufp
70 #define LAST_USE MyCache[CacheInx].last_used
71 #define LAST_SEQ MyCache[CacheInx].sequence
72
73 /*
74 * Termcap names are matched only using the first two bytes.
75 * Ignore any extended names longer than two bytes, to avoid problems
76 * with legacy code which passes in parameters whose use is long forgotten.
77 */
78 #define ValidCap(cap) (((cap)[0] != '\0') && ((cap)[1] != '\0'))
79 #define SameCap(a,b) (((a)[0] == (b)[0]) && ((a)[1] == (b)[1]))
80 #define ValidExt(ext) (ValidCap(ext) && (ext)[2] == '\0')
81
82 /***************************************************************************
83 *
84 * tgetent(bufp, term)
85 *
86 * In termcap, this function reads in the entry for terminal `term' into the
87 * buffer pointed to by bufp. It must be called before any of the functions
88 * below are called.
89 * In this terminfo emulation, tgetent() simply calls setupterm() (which
90 * does a bit more than tgetent() in termcap does), and returns its return
91 * value (1 if successful, 0 if no terminal with the given name could be
92 * found, or -1 if no terminal descriptions have been installed on the
93 * system). The bufp argument is ignored.
94 *
95 ***************************************************************************/
96
97 NCURSES_EXPORT(int)
tgetent(NCURSES_SP_DCLx char * bufp,const char * name)98 NCURSES_SP_NAME(tgetent) (NCURSES_SP_DCLx char *bufp, const char *name)
99 {
100 int rc = ERR;
101 int n;
102 bool found_cache = FALSE;
103 #if USE_TERM_DRIVER
104 TERMINAL *termp = NULL;
105 #endif
106
107 START_TRACE();
108 T((T_CALLED("tgetent()")));
109
110 TINFO_SETUP_TERM(&termp, name, STDOUT_FILENO, &rc, TRUE);
111
112 #if USE_TERM_DRIVER
113 if (termp == NULL ||
114 !((TERMINAL_CONTROL_BLOCK *) termp)->drv->isTerminfo)
115 returnCode(rc);
116 #endif
117
118 /*
119 * In general we cannot tell if the fixed sgr0 is still used by the
120 * caller, but if tgetent() is called with the same buffer, that is
121 * good enough, since the previous data would be invalidated by the
122 * current call.
123 *
124 * bufp may be a null pointer, e.g., GNU termcap. That allocates data,
125 * which is good until the next tgetent() call. The conventional termcap
126 * is inconvenient because of the fixed buffer size, but because it uses
127 * caller-supplied buffers, can have multiple terminal descriptions in
128 * use at a given time.
129 */
130 for (n = 0; n < TGETENT_MAX; ++n) {
131 bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
132 if (same_result) {
133 CacheInx = n;
134 if (FIX_SGR0 != NULL) {
135 FreeAndNull(FIX_SGR0);
136 }
137 /*
138 * Also free the terminfo data that we loaded (much bigger leak).
139 */
140 if (LAST_TRM != NULL && LAST_TRM != TerminalOf(SP_PARM)) {
141 const TERMINAL *trm = LAST_TRM;
142 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx LAST_TRM);
143 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx)
144 if (LAST_TRM == trm)
145 LAST_TRM = NULL;
146 CacheInx = n;
147 }
148 found_cache = TRUE;
149 break;
150 }
151 }
152 if (!found_cache) {
153 int best = 0;
154
155 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
156 if (LAST_SEQ < MyCache[best].sequence) {
157 best = CacheInx;
158 }
159 }
160 CacheInx = best;
161 }
162 if (rc == 1) {
163 LAST_TRM = TerminalOf(SP_PARM);
164 LAST_SEQ = ++CacheSeq;
165 } else {
166 LAST_TRM = NULL;
167 }
168
169 PC = 0;
170 UP = NULL;
171 BC = NULL;
172 FIX_SGR0 = NULL; /* don't free it - application may still use */
173
174 if (rc == 1) {
175
176 if (cursor_left)
177 if ((backspaces_with_bs = (char) !strcmp(cursor_left, "\b")) == 0)
178 backspace_if_not_bs = cursor_left;
179
180 /* we're required to export these */
181 if (pad_char != NULL)
182 PC = pad_char[0];
183 if (cursor_up != NULL)
184 UP = cursor_up;
185 if (backspace_if_not_bs != NULL)
186 BC = backspace_if_not_bs;
187
188 if ((FIX_SGR0 = _nc_trim_sgr0(&TerminalType(TerminalOf(SP_PARM))))
189 != NULL) {
190 if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
191 if (FIX_SGR0 != exit_attribute_mode) {
192 free(FIX_SGR0);
193 }
194 FIX_SGR0 = NULL;
195 }
196 }
197 LAST_BUF = bufp;
198 LAST_USE = TRUE;
199
200 SetNoPadding(SP_PARM);
201 (void) NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG); /* sets ospeed as a side effect */
202
203 /* LINT_PREPRO
204 #if 0*/
205 #include <capdefaults.c>
206 /* LINT_PREPRO
207 #endif*/
208
209 }
210
211 #ifdef FREEBSD_NATIVE
212 /*
213 * This is a REALLY UGLY hack. Basically, if we originate with
214 * a termcap source, try and copy it out.
215 */
216 if (bufp && _nc_termcap[0])
217 strncpy(bufp, _nc_termcap, 1024);
218 #endif
219
220 returnCode(rc);
221 }
222
223 #if NCURSES_SP_FUNCS
224 NCURSES_EXPORT(int)
tgetent(char * bufp,const char * name)225 tgetent(char *bufp, const char *name)
226 {
227 return NCURSES_SP_NAME(tgetent) (CURRENT_SCREEN, bufp, name);
228 }
229 #endif
230
231 #if 0
232 static bool
233 same_tcname(const char *a, const char *b)
234 {
235 bool code = SameCap(a, b);
236 fprintf(stderr, "compare(%s,%s) %s\n", a, b, code ? "same" : "diff");
237 return code;
238 }
239
240 #else
241 #define same_tcname(a,b) SameCap(a,b)
242 #endif
243
244 /***************************************************************************
245 *
246 * tgetflag(str)
247 *
248 * Look up boolean termcap capability str and return its value (TRUE=1 if
249 * present, FALSE=0 if not).
250 *
251 ***************************************************************************/
252
253 NCURSES_EXPORT(int)
tgetflag(NCURSES_SP_DCLx const char * id)254 NCURSES_SP_NAME(tgetflag) (NCURSES_SP_DCLx const char *id)
255 {
256 int result = 0; /* Solaris returns zero for missing flag */
257
258 T((T_CALLED("tgetflag(%p, %s)"), (void *) SP_PARM, id));
259 if (HasTInfoTerminal(SP_PARM) && ValidCap(id)) {
260 TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
261 struct name_table_entry const *entry_ptr;
262 int j = -1;
263
264 entry_ptr = _nc_find_type_entry(id, BOOLEAN, TRUE);
265 if (entry_ptr != NULL) {
266 j = entry_ptr->nte_index;
267 }
268 #if NCURSES_XNAMES
269 else {
270 int i;
271 for_each_ext_boolean(i, tp) {
272 const char *capname = ExtBoolname(tp, i, boolcodes);
273 if (same_tcname(id, capname) && ValidExt(capname)) {
274 j = i;
275 break;
276 }
277 }
278 }
279 #endif
280 if (j >= 0) {
281 /* note: setupterm forces invalid booleans to false */
282 result = tp->Booleans[j];
283 }
284 }
285 returnCode(result);
286 }
287
288 #if NCURSES_SP_FUNCS
289 NCURSES_EXPORT(int)
tgetflag(const char * id)290 tgetflag(const char *id)
291 {
292 return NCURSES_SP_NAME(tgetflag) (CURRENT_SCREEN, id);
293 }
294 #endif
295
296 /***************************************************************************
297 *
298 * tgetnum(str)
299 *
300 * Look up numeric termcap capability str and return its value, or -1 if
301 * not given.
302 *
303 ***************************************************************************/
304
305 NCURSES_EXPORT(int)
tgetnum(NCURSES_SP_DCLx const char * id)306 NCURSES_SP_NAME(tgetnum) (NCURSES_SP_DCLx const char *id)
307 {
308 int result = ABSENT_NUMERIC;
309
310 T((T_CALLED("tgetnum(%p, %s)"), (void *) SP_PARM, id));
311 assert(result < 0);
312
313 if (HasTInfoTerminal(SP_PARM) && ValidCap(id)) {
314 TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
315 struct name_table_entry const *entry_ptr;
316 int j = -1;
317
318 entry_ptr = _nc_find_type_entry(id, NUMBER, TRUE);
319 if (entry_ptr != NULL) {
320 j = entry_ptr->nte_index;
321 }
322 #if NCURSES_XNAMES
323 else {
324 int i;
325 for_each_ext_number(i, tp) {
326 const char *capname = ExtNumname(tp, i, numcodes);
327 if (same_tcname(id, capname) && ValidExt(capname)) {
328 j = i;
329 break;
330 }
331 }
332 }
333 #endif
334 if (j >= 0) {
335 if (VALID_NUMERIC(tp->Numbers[j]))
336 result = tp->Numbers[j];
337 }
338 }
339 returnCode(result);
340 }
341
342 #if NCURSES_SP_FUNCS
343 NCURSES_EXPORT(int)
tgetnum(const char * id)344 tgetnum(const char *id)
345 {
346 return NCURSES_SP_NAME(tgetnum) (CURRENT_SCREEN, id);
347 }
348 #endif
349
350 /***************************************************************************
351 *
352 * tgetstr(str, area)
353 *
354 * Look up string termcap capability str and return a pointer to its value,
355 * or NULL if not given.
356 *
357 ***************************************************************************/
358
359 NCURSES_EXPORT(char *)
tgetstr(NCURSES_SP_DCLx const char * id,char ** area)360 NCURSES_SP_NAME(tgetstr) (NCURSES_SP_DCLx const char *id, char **area)
361 {
362 char *result = NULL;
363
364 T((T_CALLED("tgetstr(%s,%p)"), id, (void *) area));
365 if (HasTInfoTerminal(SP_PARM) && ValidCap(id)) {
366 TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
367 struct name_table_entry const *entry_ptr;
368 int j = -1;
369
370 entry_ptr = _nc_find_type_entry(id, STRING, TRUE);
371 if (entry_ptr != NULL) {
372 j = entry_ptr->nte_index;
373 }
374 #if NCURSES_XNAMES
375 else {
376 int i;
377 for_each_ext_string(i, tp) {
378 const char *capname = ExtStrname(tp, i, strcodes);
379 if (same_tcname(id, capname) && ValidExt(capname)) {
380 j = i;
381 break;
382 }
383 }
384 }
385 #endif
386 if (j >= 0) {
387 result = tp->Strings[j];
388 TR(TRACE_DATABASE, ("found match %d: %s", j, _nc_visbuf(result)));
389 /* setupterm forces canceled strings to null */
390 if (VALID_STRING(result)) {
391 if (result == exit_attribute_mode
392 && FIX_SGR0 != NULL) {
393 result = FIX_SGR0;
394 TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
395 }
396 if (area != NULL
397 && *area != NULL) {
398 _nc_STRCPY(*area, result, 1024);
399 result = *area;
400 *area += strlen(*area) + 1;
401 }
402 }
403 }
404 }
405 returnPtr(result);
406 }
407
408 #if NCURSES_SP_FUNCS
409 NCURSES_EXPORT(char *)
tgetstr(const char * id,char ** area)410 tgetstr(const char *id, char **area)
411 {
412 return NCURSES_SP_NAME(tgetstr) (CURRENT_SCREEN, id, area);
413 }
414 #endif
415
416 #if NO_LEAKS
417 #undef CacheInx
418 #define CacheInx num
419 NCURSES_EXPORT(void)
_nc_tgetent_leak(const TERMINAL * const termp)420 _nc_tgetent_leak(const TERMINAL *const termp)
421 {
422 if (termp != NULL) {
423 int num;
424 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
425 if (LAST_TRM == termp) {
426 FreeAndNull(FIX_SGR0);
427 if (LAST_TRM != NULL) {
428 LAST_TRM = NULL;
429 }
430 break;
431 }
432 }
433 }
434 }
435
436 NCURSES_EXPORT(void)
_nc_tgetent_leaks(void)437 _nc_tgetent_leaks(void)
438 {
439 int num;
440 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
441 if (LAST_TRM != NULL) {
442 del_curterm(LAST_TRM);
443 _nc_tgetent_leak(LAST_TRM);
444 }
445 }
446 }
447 #endif
448