termcap.c revision 1.5 1 /* $NetBSD: termcap.c,v 1.5 2010/03/02 14:11:11 roy Exp $ */
2
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: termcap.c,v 1.5 2010/03/02 14:11:11 roy Exp $");
32
33 #include <assert.h>
34 #include <ctype.h>
35 #include <errno.h>
36 #include <stdint.h>
37 #include <string.h>
38 #include <term_private.h>
39 #include <term.h>
40 #include <termcap.h>
41 #include <unistd.h>
42 #include <stdio.h>
43
44 #include "termcap_map.c"
45 #include "termcap_hash.c"
46
47 char *UP;
48 char *BC;
49
50 /* ARGSUSED */
51 int
52 tgetent(__unused char *bp, const char *name)
53 {
54 int errret;
55 static TERMINAL *last = NULL;
56
57 _DIAGASSERT(name != NULL);
58
59 /* Free the old term */
60 if (last != NULL) {
61 del_curterm(last);
62 last = NULL;
63 }
64 errret = -1;
65 if (setupterm(name, STDOUT_FILENO, &errret) != 0)
66 return errret;
67 last = cur_term;
68
69 if (pad_char != NULL)
70 PC = pad_char[0];
71 UP = __UNCONST(cursor_up);
72 BC = __UNCONST(cursor_left);
73 return 1;
74 }
75
76 int
77 tgetflag(const char *id)
78 {
79 uint32_t ind;
80 size_t i;
81 TERMUSERDEF *ud;
82
83 _DIAGASSERT(id != NULL);
84
85 if (cur_term == NULL)
86 return 0;
87
88 ind = _t_flaghash((const unsigned char *)id, strlen(id));
89 if (ind <= __arraycount(_ti_cap_flagids)) {
90 if (strcmp(id, _ti_cap_flagids[ind].id) == 0)
91 return cur_term->flags[_ti_cap_flagids[ind].ti];
92 }
93 for (i = 0; i < cur_term->_nuserdefs; i++) {
94 ud = &cur_term->_userdefs[i];
95 if (ud->type == 'f' && strcmp(ud->id, id) == 0)
96 return ud->flag;
97 }
98 return 0;
99 }
100
101 int
102 tgetnum(const char *id)
103 {
104 uint32_t ind;
105 size_t i;
106 TERMUSERDEF *ud;
107 const TENTRY *te;
108
109 _DIAGASSERT(id != NULL);
110
111 if (cur_term == NULL)
112 return -1;
113
114 ind = _t_numhash((const unsigned char *)id, strlen(id));
115 if (ind <= __arraycount(_ti_cap_numids)) {
116 te = &_ti_cap_numids[ind];
117 if (strcmp(id, te->id) == 0) {
118 if (!VALID_NUMERIC(cur_term->nums[te->ti]))
119 return ABSENT_NUMERIC;
120 return cur_term->nums[te->ti];
121 }
122 }
123 for (i = 0; i < cur_term->_nuserdefs; i++) {
124 ud = &cur_term->_userdefs[i];
125 if (ud->type == 'n' && strcmp(ud->id, id) == 0) {
126 if (!VALID_NUMERIC(ud->num))
127 return ABSENT_NUMERIC;
128 return ud->num;
129 }
130 }
131 return -1;
132 }
133
134 char *
135 tgetstr(const char *id, __unused char **area)
136 {
137 uint32_t ind;
138 size_t i;
139 TERMUSERDEF *ud;
140 const char *str;
141
142 _DIAGASSERT(id != NULL);
143
144 if (cur_term == NULL)
145 return NULL;
146
147 str = NULL;
148 ind = _t_strhash((const unsigned char *)id, strlen(id));
149 if (ind <= __arraycount(_ti_cap_strids)) {
150 if (strcmp(id, _ti_cap_strids[ind].id) == 0) {
151 str = cur_term->strs[_ti_cap_strids[ind].ti];
152 if (str == NULL)
153 return NULL;
154 }
155 }
156 if (str != NULL)
157 for (i = 0; i < cur_term->_nuserdefs; i++) {
158 ud = &cur_term->_userdefs[i];
159 if (ud->type == 's' && strcmp(ud->id, id) == 0)
160 str = ud->str;
161 }
162
163 /* XXX: FXIXME
164 * We should fix sgr0(me) as it has a slightly different meaning
165 * for termcap. */
166
167 if (str != NULL && area != NULL && *area != NULL) {
168 char *s;
169 s = *area;
170 strcpy(*area, str);
171 *area += strlen(*area) + 1;
172 return s;
173 }
174
175 return __UNCONST(str);
176 }
177
178 char *
179 tgoto(const char *cm, int destcol, int destline)
180 {
181
182 _DIAGASSERT(cm != NULL);
183 return vtparm(cm, destline, destcol);
184 }
185
186 static const char *
187 flagname(const char *key)
188 {
189 uint32_t idx;
190
191 idx = _t_flaghash((const unsigned char *)key, strlen(key));
192 if (idx <= __arraycount(_ti_cap_flagids) &&
193 strcmp(key, _ti_cap_flagids[idx].id) == 0)
194 return _ti_flagid(_ti_cap_flagids[idx].ti);
195 return key;
196 }
197
198 static const char *
199 numname(const char *key)
200 {
201 uint32_t idx;
202
203 idx = _t_numhash((const unsigned char *)key, strlen(key));
204 if (idx <= __arraycount(_ti_cap_numids) &&
205 strcmp(key, _ti_cap_numids[idx].id) == 0)
206 return _ti_numid(_ti_cap_numids[idx].ti);
207 return key;
208 }
209
210 static const char *
211 strname(const char *key)
212 {
213 uint32_t idx;
214
215 idx = _t_strhash((const unsigned char *)key, strlen(key));
216 if (idx <= __arraycount(_ti_cap_strids) &&
217 strcmp(key, _ti_cap_strids[idx].id) == 0)
218 return _ti_strid(_ti_cap_strids[idx].ti);
219
220 if (strcmp(key, "tc") == 0)
221 return "use";
222
223 return key;
224 }
225
226 /* We don't currently map %> %B %D
227 * That means no conversion for regent100, hz1500, act4, act5, mime terms. */
228 static char *
229 strval(const char *val)
230 {
231 char *info, *ip, c;
232 int p;
233 size_t len, l, n;
234
235 len = 1024; /* no single string should be bigger */
236 info = ip = malloc(len);
237 if (info == NULL)
238 return 0;
239
240 l = 0;
241 p = 1;
242 for (; *val != '\0'; val++) {
243 if (l + 2 > len)
244 goto elen;
245 if (*val != '%') {
246 if (*val == ',') {
247 if (l + 3 > len)
248 goto elen;
249 *ip++ = '\\';
250 l++;
251 }
252 *ip++ = *val;
253 l++;
254 continue;
255 }
256 switch (c = *(++val)) {
257 case 'd':
258 if (l + 6 > len)
259 goto elen;
260 *ip++ = '%';
261 *ip++ = 'p';
262 *ip++ = '0' + p;
263 *ip++ = '%';
264 *ip++ = 'd';
265 l += 5;
266 n += 5;
267 /* FALLTHROUGH */
268 case 'r':
269 p = 3 - p;
270 break;
271 default:
272 /* Hope it matches a terminfo command. */
273 *ip++ = '%';
274 *ip++ = c;
275 l += 2;
276 break;
277 }
278 }
279
280 /* \E\ is valid termcap.
281 * We need to escape the final \ for terminfo. */
282 if (l > 2 && info[l - 1] == '\\' &&
283 (info[l - 2] != '\\' && info[l - 2] != '^'))
284 {
285 if (l + 1 > len)
286 goto elen;
287 *ip++ = '\\';
288 }
289
290 *ip = '\0';
291 return info;
292
293 elen:
294 free(info);
295 errno = ENOMEM;
296 return NULL;
297 }
298
299 char *
300 captoinfo(char *cap)
301 {
302 char *info, *ip, *token, *val, *p, tok[3];
303 const char *name;
304 size_t len, lp, nl, vl, rl;
305
306 _DIAGASSERT(cap != NULL);
307
308 len = strlen(cap) * 2;
309 info = ip = malloc(len);
310 if (info == NULL)
311 return NULL;
312
313 lp = 0;
314 tok[2] = '\0';
315 for (token = _ti_get_token(&cap, ':');
316 token != NULL;
317 token = _ti_get_token(&cap, ':'))
318 {
319 if (token[0] == '\0')
320 continue;
321 name = token;
322 val = NULL;
323 if (token[1] != '\0') {
324 tok[0] = token[0];
325 tok[1] = token[1];
326 if (token[2] == '\0') {
327 name = flagname(tok);
328 val = NULL;
329 } else if (token[2] == '#') {
330 name = numname(tok);
331 val = token + 2;
332 } else if (token[2] == '=') {
333 name = strname(tok);
334 val = strval(token + 2);
335 }
336 }
337 nl = strlen(name);
338 if (val == NULL)
339 vl = 0;
340 else
341 vl = strlen(val);
342 rl = nl + vl + 3; /* , \0 */
343
344 if (lp + rl > len) {
345 if (rl < 256)
346 len += 256;
347 else
348 len += rl;
349 p = realloc(info, len);
350 if (p == NULL)
351 return NULL;
352 info = p;
353 }
354
355 if (ip != info) {
356 *ip++ = ',';
357 *ip++ = ' ';
358 }
359
360 strcpy(ip, name);
361 ip += nl;
362 if (val != NULL) {
363 strcpy(ip, val);
364 ip += vl;
365 if (token[2] == '=')
366 free(val);
367 }
368 }
369
370 *ip = '\0';
371 return info;
372 }
373
374