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 |
✓✗✗✓
|
852 |
#define CacheInx _nc_globals.tgetent_index |
57 |
✓✗✗✓
|
852 |
#define CacheSeq _nc_globals.tgetent_sequence |
58 |
|
|
|
59 |
✓✗✓✓ ✗✓✗✓
|
752 |
#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 |
✓✗✓✗ ✓✗✗✓
|
1136 |
#define LAST_SEQ MyCache[CacheInx].sequence |
64 |
|
|
|
65 |
|
|
/*************************************************************************** |
66 |
|
|
* |
67 |
✓✗✓✗
|
660 |
* tgetent(bufp, term) |
68 |
✓✗✓✗
|
852 |
* |
69 |
✓✓✓✗
|
596 |
* In termcap, this function reads in the entry for terminal `term' into the |
70 |
✓✓✓✗
|
340 |
* buffer pointed to by bufp. It must be called before any of the functions |
71 |
|
92 |
* below are called. |
72 |
|
92 |
* In this terminfo emulation, tgetent() simply calls setupterm() (which |
73 |
|
92 |
* does a bit more than tgetent() in termcap does), and returns its return |
74 |
✗✗ |
284 |
* value (1 if successful, 0 if no terminal with the given name could be |
75 |
✓✗ |
568 |
* found, or -1 if no terminal descriptions have been installed on the |
76 |
✗✓✗✗
|
284 |
* system). The bufp argument is ignored. |
77 |
|
|
* |
78 |
|
|
***************************************************************************/ |
79 |
|
|
|
80 |
✓✗ |
852 |
NCURSES_EXPORT(int) |
81 |
✓✓ |
660 |
tgetent(char *bufp, const char *name) |
82 |
✓✗✗✓
|
852 |
{ |
83 |
✓✗✗✓
|
1420 |
int errcode; |
84 |
|
|
int n; |
85 |
✓✗✓✓ ✗✓✗✓
|
1008 |
bool found_cache = FALSE; |
86 |
|
|
|
87 |
|
|
START_TRACE(); |
88 |
|
|
T((T_CALLED("tgetent()"))); |
89 |
|
|
|
90 |
|
284 |
_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 |
✓✓ |
2840 |
for (n = 0; n < TGETENT_MAX; ++n) { |
105 |
✗✓ |
2272 |
bool same_result = (MyCache[n].last_used && MyCache[n].last_bufp == bufp); |
106 |
✗✓ |
1136 |
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 |
✓✗ |
1136 |
} |
126 |
✓✗ |
284 |
if (!found_cache) { |
127 |
|
|
int best = 0; |
128 |
|
|
|
129 |
✓✓ |
2840 |
for (CacheInx = 0; CacheInx < TGETENT_MAX; ++CacheInx) { |
130 |
✗✓ |
1136 |
if (LAST_SEQ < MyCache[best].sequence) { |
131 |
|
|
best = CacheInx; |
132 |
|
|
} |
133 |
|
|
} |
134 |
|
284 |
CacheInx = best; |
135 |
|
284 |
} |
136 |
|
284 |
LAST_TRM = cur_term; |
137 |
|
284 |
LAST_SEQ = ++CacheSeq; |
138 |
|
|
|
139 |
|
284 |
PC = 0; |
140 |
|
284 |
UP = 0; |
141 |
|
284 |
BC = 0; |
142 |
|
284 |
FIX_SGR0 = 0; /* don't free it - application may still use */ |
143 |
|
|
|
144 |
✓✗ |
284 |
if (errcode == 1) { |
145 |
|
|
|
146 |
✓✗ |
284 |
if (cursor_left) |
147 |
✗✓ |
284 |
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 |
✗✓ |
284 |
if (pad_char != NULL) |
152 |
|
|
PC = pad_char[0]; |
153 |
✓✓ |
284 |
if (cursor_up != NULL) |
154 |
|
220 |
UP = cursor_up; |
155 |
✗✓ |
284 |
if (backspace_if_not_bs != NULL) |
156 |
|
|
BC = backspace_if_not_bs; |
157 |
|
|
|
158 |
✓✓ |
284 |
if ((FIX_SGR0 = _nc_trim_sgr0(&(cur_term->type))) != 0) { |
159 |
✓✓ |
156 |
if (!strcmp(FIX_SGR0, exit_attribute_mode)) { |
160 |
✗✓ |
64 |
if (FIX_SGR0 != exit_attribute_mode) { |
161 |
|
|
free(FIX_SGR0); |
162 |
|
|
} |
163 |
|
64 |
FIX_SGR0 = 0; |
164 |
|
64 |
} |
165 |
|
|
} |
166 |
|
284 |
LAST_BUF = bufp; |
167 |
|
284 |
LAST_USE = TRUE; |
168 |
|
|
|
169 |
|
284 |
SetNoPadding(SP); |
170 |
|
284 |
(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 |
|
284 |
} |
179 |
|
568 |
returnCode(errcode); |
180 |
|
284 |
} |
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 |
✓✗ |
1232 |
if (cur_term != 0) { |
198 |
|
616 |
TERMTYPE *tp = &(cur_term->type); |
199 |
✓✗ |
20720 |
for_each_boolean(i, tp) { |
200 |
✗✓ |
20720 |
const char *capname = ExtBoolname(tp, i, boolcodes); |
201 |
✓✓ |
10360 |
if (!strncmp(id, capname, 2)) { |
202 |
|
|
/* setupterm forces invalid booleans to false */ |
203 |
|
616 |
returnCode(tp->Booleans[i]); |
204 |
|
|
} |
205 |
✓✓ |
9744 |
} |
206 |
|
|
} |
207 |
|
|
returnCode(0); /* Solaris does this */ |
208 |
|
616 |
} |
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 |
✓✗ |
156 |
if (cur_term != 0) { |
226 |
|
78 |
TERMTYPE *tp = &(cur_term->type); |
227 |
✓✗ |
840 |
for_each_number(i, tp) { |
228 |
✗✓ |
840 |
const char *capname = ExtNumname(tp, i, numcodes); |
229 |
✓✓ |
420 |
if (!strncmp(id, capname, 2)) { |
230 |
✗✓ |
78 |
if (!VALID_NUMERIC(tp->Numbers[i])) |
231 |
|
|
returnCode(ABSENT_NUMERIC); |
232 |
|
78 |
returnCode(tp->Numbers[i]); |
233 |
|
|
} |
234 |
✓✓ |
342 |
} |
235 |
|
|
} |
236 |
|
|
returnCode(ABSENT_NUMERIC); |
237 |
|
78 |
} |
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 |
✓✗✓✗
|
17070 |
if (area != 0 && *area != 0) |
255 |
|
5690 |
base = *area; |
256 |
|
|
T((T_CALLED("tgetstr(%s,%p)"), id, area)); |
257 |
✓✗ |
5690 |
if (cur_term != 0) { |
258 |
|
5690 |
TERMTYPE *tp = &(cur_term->type); |
259 |
✓✓ |
1250556 |
for_each_string(i, tp) { |
260 |
✓✓ |
1249208 |
const char *capname = ExtStrname(tp, i, strcodes); |
261 |
✓✓ |
624604 |
if (!strncmp(id, capname, 2)) { |
262 |
|
5016 |
result = tp->Strings[i]; |
263 |
|
|
TR(TRACE_DATABASE, ("found match : %s", _nc_visbuf(result))); |
264 |
|
|
/* setupterm forces canceled strings to null */ |
265 |
✓✓ |
5016 |
if (VALID_STRING(result)) { |
266 |
✓✓ |
3324 |
if (result == exit_attribute_mode |
267 |
✓✓ |
3324 |
&& FIX_SGR0 != 0) { |
268 |
|
|
result = FIX_SGR0; |
269 |
|
|
TR(TRACE_DATABASE, ("altered to : %s", _nc_visbuf(result))); |
270 |
|
92 |
} |
271 |
✓✗✓✗
|
6336 |
if (area != 0 && *area != 0) { |
272 |
|
3168 |
(void) strlcpy(*area, result, 1024 - (*area - base)); |
273 |
|
3168 |
result = *area; |
274 |
|
3168 |
*area += strlen(*area) + 1; |
275 |
|
3168 |
} |
276 |
|
|
} |
277 |
|
5016 |
break; |
278 |
|
|
} |
279 |
✓✓ |
619588 |
} |
280 |
|
5690 |
} |
281 |
|
5690 |
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 |