]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tinfo/lib_termcap.c
ncurses 6.1 - patch 20181215
[ncurses.git] / ncurses / tinfo / lib_termcap.c
1 /****************************************************************************
2  * Copyright (c) 1998-2017,2018 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 #ifndef CUR
48 #define CUR SP_TERMTYPE
49 #endif
50
51 MODULE_ID("$Id: lib_termcap.c,v 1.87 2018/04/07 21:11:15 tom Exp $")
52
53 NCURSES_EXPORT_VAR(char *) UP = 0;
54 NCURSES_EXPORT_VAR(char *) BC = 0;
55
56 #define MyCache  _nc_globals.tgetent_cache
57 #define CacheInx _nc_globals.tgetent_index
58 #define CacheSeq _nc_globals.tgetent_sequence
59
60 #define FIX_SGR0 MyCache[CacheInx].fix_sgr0
61 #define LAST_TRM MyCache[CacheInx].last_term
62 #define LAST_BUF MyCache[CacheInx].last_bufp
63 #define LAST_USE MyCache[CacheInx].last_used
64 #define LAST_SEQ MyCache[CacheInx].sequence
65
66 /*
67  * Termcap names are matched only using the first two bytes.
68  * Ignore any extended names longer than two bytes, to avoid problems
69  * with legacy code which passes in parameters whose use is long forgotten.
70  */
71 #define ValidCap(cap) (((cap)[0] != '\0') && ((cap)[1] != '\0'))
72 #define SameCap(a,b)  (((a)[0] == (b)[0]) && ((a)[1] == (b)[1]))
73 #define ValidExt(ext) (ValidCap(ext) && (ext)[2] == '\0')
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 NCURSES_SP_NAME(tgetent) (NCURSES_SP_DCLx char *bufp, const char *name)
92 {
93     int rc = ERR;
94     int n;
95     bool found_cache = FALSE;
96 #ifdef USE_TERM_DRIVER
97     TERMINAL *termp = 0;
98 #endif
99
100     START_TRACE();
101     T((T_CALLED("tgetent()")));
102
103     TINFO_SETUP_TERM(&termp, name, STDOUT_FILENO, &rc, TRUE);
104
105 #ifdef USE_TERM_DRIVER
106     if (termp == 0 ||
107         !((TERMINAL_CONTROL_BLOCK *) termp)->drv->isTerminfo)
108         returnCode(rc);
109 #endif
110
111     /*
112      * In general we cannot tell if the fixed sgr0 is still used by the
113      * caller, but if tgetent() is called with the same buffer, that is
114      * good enough, since the previous data would be invalidated by the
115      * current call.
116      *
117      * bufp may be a null pointer, e.g., GNU termcap.  That allocates data,
118      * which is good until the next tgetent() call.  The conventional termcap
119      * is inconvenient because of the fixed buffer size, but because it uses
120      * caller-supplied buffers, can have multiple terminal descriptions in
121      * use at a given time.
122      */
123     for (n = 0; n < TGETENT_MAX; ++n) {
124         bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
125         if (same_result) {
126             CacheInx = n;
127             if (FIX_SGR0 != 0) {
128                 FreeAndNull(FIX_SGR0);
129             }
130             /*
131              * Also free the terminfo data that we loaded (much bigger leak).
132              */
133             if (LAST_TRM != 0 && LAST_TRM != TerminalOf(SP_PARM)) {
134                 TERMINAL *trm = LAST_TRM;
135                 NCURSES_SP_NAME(del_curterm) (NCURSES_SP_ARGx LAST_TRM);
136                 for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx)
137                     if (LAST_TRM == trm)
138                         LAST_TRM = 0;
139                 CacheInx = n;
140             }
141             found_cache = TRUE;
142             break;
143         }
144     }
145     if (!found_cache) {
146         int best = 0;
147
148         for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
149             if (LAST_SEQ < MyCache[best].sequence) {
150                 best = CacheInx;
151             }
152         }
153         CacheInx = best;
154     }
155     if (rc == 1) {
156         LAST_TRM = TerminalOf(SP_PARM);
157         LAST_SEQ = ++CacheSeq;
158     } else {
159         LAST_TRM = 0;
160     }
161
162     PC = 0;
163     UP = 0;
164     BC = 0;
165     FIX_SGR0 = 0;               /* don't free it - application may still use */
166
167     if (rc == 1) {
168
169         if (cursor_left)
170             if ((backspaces_with_bs = (char) !strcmp(cursor_left, "\b")) == 0)
171                 backspace_if_not_bs = cursor_left;
172
173         /* we're required to export these */
174         if (pad_char != NULL)
175             PC = pad_char[0];
176         if (cursor_up != NULL)
177             UP = cursor_up;
178         if (backspace_if_not_bs != NULL)
179             BC = backspace_if_not_bs;
180
181         if ((FIX_SGR0 = _nc_trim_sgr0(&TerminalType(TerminalOf(SP_PARM))))
182             != 0) {
183             if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
184                 if (FIX_SGR0 != exit_attribute_mode) {
185                     free(FIX_SGR0);
186                 }
187                 FIX_SGR0 = 0;
188             }
189         }
190         LAST_BUF = bufp;
191         LAST_USE = TRUE;
192
193         SetNoPadding(SP_PARM);
194         (void) NCURSES_SP_NAME(baudrate) (NCURSES_SP_ARG);      /* sets ospeed as a side-effect */
195
196 /* LINT_PREPRO
197 #if 0*/
198 #include <capdefaults.c>
199 /* LINT_PREPRO
200 #endif*/
201
202     }
203     returnCode(rc);
204 }
205
206 #if NCURSES_SP_FUNCS
207 NCURSES_EXPORT(int)
208 tgetent(char *bufp, const char *name)
209 {
210     return NCURSES_SP_NAME(tgetent) (CURRENT_SCREEN, bufp, name);
211 }
212 #endif
213
214 #if 0
215 static bool
216 same_tcname(const char *a, const char *b)
217 {
218     bool code = SameCap(a, b);
219     fprintf(stderr, "compare(%s,%s) %s\n", a, b, code ? "same" : "diff");
220     return code;
221 }
222
223 #else
224 #define same_tcname(a,b) SameCap(a,b)
225 #endif
226
227 /***************************************************************************
228  *
229  * tgetflag(str)
230  *
231  * Look up boolean termcap capability str and return its value (TRUE=1 if
232  * present, FALSE=0 if not).
233  *
234  ***************************************************************************/
235
236 NCURSES_EXPORT(int)
237 NCURSES_SP_NAME(tgetflag) (NCURSES_SP_DCLx const char *id)
238 {
239     int result = 0;             /* Solaris returns zero for missing flag */
240
241     T((T_CALLED("tgetflag(%p, %s)"), (void *) SP_PARM, id));
242     if (HasTInfoTerminal(SP_PARM) && ValidCap(id)) {
243         TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
244         struct name_table_entry const *entry_ptr;
245         int j = -1;
246
247         entry_ptr = _nc_find_type_entry(id, BOOLEAN, TRUE);
248         if (entry_ptr != 0) {
249             j = entry_ptr->nte_index;
250         }
251 #if NCURSES_XNAMES
252         else {
253             int i;
254             for_each_ext_boolean(i, tp) {
255                 const char *capname = ExtBoolname(tp, i, boolcodes);
256                 if (same_tcname(id, capname) && ValidExt(capname)) {
257                     j = i;
258                     break;
259                 }
260             }
261         }
262 #endif
263         if (j >= 0) {
264             /* note: setupterm forces invalid booleans to false */
265             result = tp->Booleans[j];
266         }
267     }
268     returnCode(result);
269 }
270
271 #if NCURSES_SP_FUNCS
272 NCURSES_EXPORT(int)
273 tgetflag(const char *id)
274 {
275     return NCURSES_SP_NAME(tgetflag) (CURRENT_SCREEN, id);
276 }
277 #endif
278
279 /***************************************************************************
280  *
281  * tgetnum(str)
282  *
283  * Look up numeric termcap capability str and return its value, or -1 if
284  * not given.
285  *
286  ***************************************************************************/
287
288 NCURSES_EXPORT(int)
289 NCURSES_SP_NAME(tgetnum) (NCURSES_SP_DCLx const char *id)
290 {
291     int result = ABSENT_NUMERIC;
292
293     T((T_CALLED("tgetnum(%p, %s)"), (void *) SP_PARM, id));
294     if (HasTInfoTerminal(SP_PARM) && ValidCap(id)) {
295         TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
296         struct name_table_entry const *entry_ptr;
297         int j = -1;
298
299         entry_ptr = _nc_find_type_entry(id, NUMBER, TRUE);
300         if (entry_ptr != 0) {
301             j = entry_ptr->nte_index;
302         }
303 #if NCURSES_XNAMES
304         else {
305             int i;
306             for_each_ext_number(i, tp) {
307                 const char *capname = ExtNumname(tp, i, numcodes);
308                 if (same_tcname(id, capname) && ValidExt(capname)) {
309                     j = i;
310                     break;
311                 }
312             }
313         }
314 #endif
315         if (j >= 0) {
316             if (VALID_NUMERIC(tp->Numbers[j]))
317                 result = tp->Numbers[j];
318         }
319     }
320     returnCode(result);
321 }
322
323 #if NCURSES_SP_FUNCS
324 NCURSES_EXPORT(int)
325 tgetnum(const char *id)
326 {
327     return NCURSES_SP_NAME(tgetnum) (CURRENT_SCREEN, id);
328 }
329 #endif
330
331 /***************************************************************************
332  *
333  * tgetstr(str, area)
334  *
335  * Look up string termcap capability str and return a pointer to its value,
336  * or NULL if not given.
337  *
338  ***************************************************************************/
339
340 NCURSES_EXPORT(char *)
341 NCURSES_SP_NAME(tgetstr) (NCURSES_SP_DCLx const char *id, char **area)
342 {
343     char *result = NULL;
344
345     T((T_CALLED("tgetstr(%s,%p)"), id, (void *) area));
346     if (HasTInfoTerminal(SP_PARM) && ValidCap(id)) {
347         TERMTYPE2 *tp = &TerminalType(TerminalOf(SP_PARM));
348         struct name_table_entry const *entry_ptr;
349         int j = -1;
350
351         entry_ptr = _nc_find_type_entry(id, STRING, TRUE);
352         if (entry_ptr != 0) {
353             j = entry_ptr->nte_index;
354         }
355 #if NCURSES_XNAMES
356         else {
357             int i;
358             for_each_ext_string(i, tp) {
359                 const char *capname = ExtStrname(tp, i, strcodes);
360                 if (same_tcname(id, capname) && ValidExt(capname)) {
361                     j = i;
362                     break;
363                 }
364             }
365         }
366 #endif
367         if (j >= 0) {
368             result = tp->Strings[j];
369             TR(TRACE_DATABASE, ("found match %d: %s", j, _nc_visbuf(result)));
370             /* setupterm forces canceled strings to null */
371             if (VALID_STRING(result)) {
372                 if (result == exit_attribute_mode
373                     && FIX_SGR0 != 0) {
374                     result = FIX_SGR0;
375                     TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
376                 }
377                 if (area != 0
378                     && *area != 0) {
379                     _nc_STRCPY(*area, result, 1024);
380                     result = *area;
381                     *area += strlen(*area) + 1;
382                 }
383             }
384         }
385     }
386     returnPtr(result);
387 }
388
389 #if NCURSES_SP_FUNCS
390 NCURSES_EXPORT(char *)
391 tgetstr(const char *id, char **area)
392 {
393     return NCURSES_SP_NAME(tgetstr) (CURRENT_SCREEN, id, area);
394 }
395 #endif
396
397 #if NO_LEAKS
398 #undef CacheInx
399 #define CacheInx num
400 NCURSES_EXPORT(void)
401 _nc_tgetent_leak(TERMINAL *termp)
402 {
403     if (termp != 0) {
404         int num;
405         for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
406             if (LAST_TRM == termp) {
407                 FreeAndNull(FIX_SGR0);
408                 if (LAST_TRM != 0) {
409                     LAST_TRM = 0;
410                 }
411                 break;
412             }
413         }
414     }
415 }
416
417 NCURSES_EXPORT(void)
418 _nc_tgetent_leaks(void)
419 {
420     int num;
421     for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
422         if (LAST_TRM != 0) {
423             del_curterm(LAST_TRM);
424             _nc_tgetent_leak(LAST_TRM);
425         }
426     }
427 }
428 #endif