GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: lib/libcurses/tinfo/lib_termcap.c Lines: 89 113 78.8 %
Date: 2017-11-13 Branches: 82 160 51.3 %

Line Branch Exec Source
1
/* $OpenBSD: lib_termcap.c,v 1.10 2010/01/12 23:22:06 nicm Exp $ */
2
3
/****************************************************************************
4
 * Copyright (c) 1998-2007,2008 Free Software Foundation, Inc.              *
5
 *                                                                          *
6
 * Permission is hereby granted, free of charge, to any person obtaining a  *
7
 * copy of this software and associated documentation files (the            *
8
 * "Software"), to deal in the Software without restriction, including      *
9
 * without limitation the rights to use, copy, modify, merge, publish,      *
10
 * distribute, distribute with modifications, sublicense, and/or sell       *
11
 * copies of the Software, and to permit persons to whom the Software is    *
12
 * furnished to do so, subject to the following conditions:                 *
13
 *                                                                          *
14
 * The above copyright notice and this permission notice shall be included  *
15
 * in all copies or substantial portions of the Software.                   *
16
 *                                                                          *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20
 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22
 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23
 * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24
 *                                                                          *
25
 * Except as contained in this notice, the name(s) of the above copyright   *
26
 * holders shall not be used in advertising or otherwise to promote the     *
27
 * sale, use or other dealings in this Software without prior written       *
28
 * authorization.                                                           *
29
 ****************************************************************************/
30
31
/****************************************************************************
32
 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33
 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34
 *     and: Thomas E. Dickey                        1996-on                 *
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
#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.10 2010/01/12 23:22:06 nicm Exp $")
51
52
NCURSES_EXPORT_VAR(char *) UP = 0;
53
NCURSES_EXPORT_VAR(char *) BC = 0;
54
55
#define MyCache  _nc_globals.tgetent_cache
56

9
#define CacheInx _nc_globals.tgetent_index
57

9
#define CacheSeq _nc_globals.tgetent_sequence
58
59


12
#define FIX_SGR0 MyCache[CacheInx].fix_sgr0
60
#define LAST_TRM MyCache[CacheInx].last_term
61
#define LAST_BUF MyCache[CacheInx].last_bufp
62
#define LAST_USE MyCache[CacheInx].last_used
63


12
#define LAST_SEQ MyCache[CacheInx].sequence
64
65
/***************************************************************************
66
 *
67

9
 * tgetent(bufp, term)
68

9
 *
69

9
 * In termcap, this function reads in the entry for terminal `term' into the
70

9
 * buffer pointed to by bufp. It must be called before any of the functions
71
3
 * below are called.
72
3
 * In this terminfo emulation, tgetent() simply calls setupterm() (which
73
3
 * does a bit more than tgetent() in termcap does), and returns its return
74
3
 * value (1 if successful, 0 if no terminal with the given name could be
75
6
 * found, or -1 if no terminal descriptions have been installed on the
76

3
 * system).  The bufp argument is ignored.
77
 *
78
 ***************************************************************************/
79
80
9
NCURSES_EXPORT(int)
81
9
tgetent(char *bufp, const char *name)
82

9
{
83

15
    int errcode;
84
    int n;
85


12
    bool found_cache = FALSE;
86
87
    START_TRACE();
88
    T((T_CALLED("tgetent()")));
89
90
3
    _nc_setupterm((NCURSES_CONST char *) name, STDOUT_FILENO, &errcode, TRUE);
91
92
    /*
93
     * In general we cannot tell if the fixed sgr0 is still used by the
94
     * caller, but if tgetent() is called with the same buffer, that is
95
     * good enough, since the previous data would be invalidated by the
96
     * current call.
97
     *
98
     * bufp may be a null pointer, e.g., GNU termcap.  That allocates data,
99
     * which is good until the next tgetent() call.  The conventional termcap
100
     * is inconvenient because of the fixed buffer size, but because it uses
101
     * caller-supplied buffers, can have multiple terminal descriptions in
102
     * use at a given time.
103
     */
104
30
    for (n = 0; n < TGETENT_MAX; ++n) {
105
24
	bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp);
106
12
	if (same_result) {
107
	    CacheInx = n;
108
	    if (FIX_SGR0 != 0) {
109
		FreeAndNull(FIX_SGR0);
110
	    }
111
	    /*
112
	     * Also free the terminfo data that we loaded (much bigger leak).
113
	     */
114
	    if (LAST_TRM != 0 && LAST_TRM != cur_term) {
115
		TERMINAL *trm = LAST_TRM;
116
		del_curterm(LAST_TRM);
117
		for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx)
118
		    if (LAST_TRM == trm)
119
			LAST_TRM = 0;
120
		CacheInx = n;
121
	    }
122
	    found_cache = TRUE;
123
	    break;
124
	}
125
12
    }
126
3
    if (!found_cache) {
127
	int best = 0;
128
129
30
	for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
130
12
	    if (LAST_SEQ < MyCache[best].sequence) {
131
		best = CacheInx;
132
	    }
133
	}
134
3
	CacheInx = best;
135
3
    }
136
3
    LAST_TRM = cur_term;
137
3
    LAST_SEQ = ++CacheSeq;
138
139
3
    PC = 0;
140
3
    UP = 0;
141
3
    BC = 0;
142
3
    FIX_SGR0 = 0;		/* don't free it - application may still use */
143
144
3
    if (errcode == 1) {
145
146
3
	if (cursor_left)
147
3
	    if ((backspaces_with_bs = (char) !strcmp(cursor_left, "\b")) == 0)
148
		backspace_if_not_bs = cursor_left;
149
150
	/* we're required to export these */
151
3
	if (pad_char != NULL)
152
	    PC = pad_char[0];
153
3
	if (cursor_up != NULL)
154
3
	    UP = cursor_up;
155
3
	if (backspace_if_not_bs != NULL)
156
	    BC = backspace_if_not_bs;
157
158
3
	if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) {
159
3
	    if (!strcmp(FIX_SGR0, exit_attribute_mode)) {
160
		if (FIX_SGR0 != exit_attribute_mode) {
161
		    free(FIX_SGR0);
162
		}
163
		FIX_SGR0 = 0;
164
	    }
165
	}
166
3
	LAST_BUF = bufp;
167
3
	LAST_USE = TRUE;
168
169
3
	SetNoPadding(SP);
170
3
	(void) baudrate();	/* sets ospeed as a side-effect */
171
172
/* LINT_PREPRO
173
#if 0*/
174
#include <capdefaults.c>
175
/* LINT_PREPRO
176
#endif*/
177
178
3
    }
179
6
    returnCode(errcode);
180
3
}
181
182
/***************************************************************************
183
 *
184
 * tgetflag(str)
185
 *
186
 * Look up boolean termcap capability str and return its value (TRUE=1 if
187
 * present, FALSE=0 if not).
188
 *
189
 ***************************************************************************/
190
191
NCURSES_EXPORT(int)
192
tgetflag(NCURSES_CONST char *id)
193
{
194
    unsigned i;
195
196
    T((T_CALLED("tgetflag(%s)"), id));
197
36
    if (cur_term != 0) {
198
18
	TERMTYPE *tp = &(cur_term->type);
199
708
	for_each_boolean(i, tp) {
200
1062
	    const char *capname = ExtBoolname(tp, i, boolcodes);
201
354
	    if (!strncmp(id, capname, 2)) {
202
		/* setupterm forces invalid booleans to false */
203
18
		returnCode(tp->Booleans[i]);
204
	    }
205
336
	}
206
    }
207
    returnCode(0);		/* Solaris does this */
208
18
}
209
210
/***************************************************************************
211
 *
212
 * tgetnum(str)
213
 *
214
 * Look up numeric termcap capability str and return its value, or -1 if
215
 * not given.
216
 *
217
 ***************************************************************************/
218
219
NCURSES_EXPORT(int)
220
tgetnum(NCURSES_CONST char *id)
221
{
222
    unsigned i;
223
224
    T((T_CALLED("tgetnum(%s)"), id));
225
12
    if (cur_term != 0) {
226
6
	TERMTYPE *tp = &(cur_term->type);
227
24
	for_each_number(i, tp) {
228
36
	    const char *capname = ExtNumname(tp, i, numcodes);
229
12
	    if (!strncmp(id, capname, 2)) {
230
6
		if (!VALID_NUMERIC(tp->Numbers[i]))
231
		    returnCode(ABSENT_NUMERIC);
232
6
		returnCode(tp->Numbers[i]);
233
	    }
234
6
	}
235
    }
236
    returnCode(ABSENT_NUMERIC);
237
6
}
238
239
/***************************************************************************
240
 *
241
 * tgetstr(str, area)
242
 *
243
 * Look up string termcap capability str and return a pointer to its value,
244
 * or NULL if not given.
245
 *
246
 ***************************************************************************/
247
248
NCURSES_EXPORT(char *)
249
tgetstr(NCURSES_CONST char *id, char **area)
250
{
251
    unsigned i;
252
    char *result = NULL, *base;
253
254

342
    if (area != 0 && *area != 0)
255
114
        base = *area;
256
    T((T_CALLED("tgetstr(%s,%p)"), id, area));
257
114
    if (cur_term != 0) {
258
114
	TERMTYPE *tp = &(cur_term->type);
259
12696
	for_each_string(i, tp) {
260
19044
	    const char *capname = ExtStrname(tp, i, strcodes);
261
6348
	    if (!strncmp(id, capname, 2)) {
262
114
		result = tp->Strings[i];
263
		TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result)));
264
		/* setupterm forces canceled strings to null */
265
114
		if (VALID_STRING(result)) {
266
96
		    if (result == exit_attribute_mode
267
96
			&& FIX_SGR0 != 0) {
268
			result = FIX_SGR0;
269
			TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result)));
270
3
		    }
271

186
		    if (area != 0 && *area != 0) {
272
93
			(void) strlcpy(*area, result, 1024 - (*area - base));
273
93
			result = *area;
274
93
			*area += strlen(*area) + 1;
275
93
		    }
276
		}
277
114
		break;
278
	    }
279
6234
	}
280
114
    }
281
114
    returnPtr(result);
282
}
283
284
#if NO_LEAKS
285
NCURSES_EXPORT(void)
286
_nc_tgetent_leaks(void)
287
{
288
    for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) {
289
	FreeIfNeeded(FIX_SGR0);
290
	if (LAST_TRM != 0)
291
	    del_curterm(LAST_TRM);
292
    }
293
}
294
#endif