termcap.c revision 1.13 1 /* $NetBSD: termcap.c,v 1.13 2011/03/11 13:28:52 christos 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.13 2011/03/11 13:28:52 christos 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, 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 _DIAGASSERT(cm != NULL);
182 return vtparm(cm, destline, destcol);
183 }
184
185 static const char *
186 flagname(const char *key)
187 {
188 uint32_t idx;
189
190 idx = _t_flaghash((const unsigned char *)key, strlen(key));
191 if (idx <= __arraycount(_ti_cap_flagids) &&
192 strcmp(key, _ti_cap_flagids[idx].id) == 0)
193 return _ti_flagid(_ti_cap_flagids[idx].ti);
194 return key;
195 }
196
197 static const char *
198 numname(const char *key)
199 {
200 uint32_t idx;
201
202 idx = _t_numhash((const unsigned char *)key, strlen(key));
203 if (idx <= __arraycount(_ti_cap_numids) &&
204 strcmp(key, _ti_cap_numids[idx].id) == 0)
205 return _ti_numid(_ti_cap_numids[idx].ti);
206 return key;
207 }
208
209 static const char *
210 strname(const char *key)
211 {
212 uint32_t idx;
213
214 idx = _t_strhash((const unsigned char *)key, strlen(key));
215 if (idx <= __arraycount(_ti_cap_strids) &&
216 strcmp(key, _ti_cap_strids[idx].id) == 0)
217 return _ti_strid(_ti_cap_strids[idx].ti);
218
219 if (strcmp(key, "tc") == 0)
220 return "use";
221
222 return key;
223 }
224
225 /* Convert a termcap character into terminfo equivalents */
226 static int
227 printchar(char **dst, const char **src)
228 {
229 unsigned char v;
230 int l;
231
232 l = 4;
233 v = (unsigned char) *++(*src);
234 if (v == '\\') {
235 v = (unsigned char) *++(*src);
236 switch (v) {
237 case '0':
238 case '1':
239 case '2':
240 case '3':
241 v = 0;
242 while (isdigit((unsigned char) **src))
243 v = 8 * v + ((unsigned char) *(*src)++ - '0');
244 (*src)--;
245 break;
246 case '\0':
247 v = '\\';
248 break;
249 }
250 } else if (v == '^')
251 v = (unsigned char) (*++(*src) & 0x1f);
252 *(*dst)++ = '%';
253 if (isgraph(v) && v != ',' && v != '\'' && v != '\\' && v != ':') {
254 *(*dst)++ = '\'';
255 *(*dst)++ = v;
256 *(*dst)++ = '\'';
257 } else {
258 *(*dst)++ = '{';
259 if (v > 99) {
260 *(*dst)++ = '0'+ v / 100;
261 l++;
262 }
263 if (v > 9) {
264 *(*dst)++ = '0' + ((int) (v / 10)) % 10;
265 l++;
266 }
267 *(*dst)++ = '0' + v % 10;
268 *(*dst)++ = '}';
269 }
270 return l;
271 }
272
273 /* Convert termcap commands into terminfo commands */
274 static char *
275 strval(const char *val)
276 {
277 char *info, *ip, c;
278 const char *ps, *pe;
279 int p, nop;
280 size_t len, l;
281
282 len = 1024; /* no single string should be bigger */
283 info = ip = malloc(len);
284 if (info == NULL)
285 return 0;
286
287 /* Move the = */
288 *ip++ = *val++;
289
290 /* Set ps and pe to point to the start and end of the padding */
291 if (isdigit((unsigned char)*val)) {
292 for (ps = pe = val;
293 isdigit((unsigned char)*val) || *val == '.';
294 val++)
295 pe++;
296 if (*val == '*') {
297 val++;
298 pe++;
299 }
300 } else
301 ps = pe = NULL;
302
303 l = nop = 0;
304 p = 1;
305 for (; *val != '\0'; val++) {
306 if (l + 2 > len)
307 goto elen;
308 if (*val != '%') {
309 if (*val == ',') {
310 if (l + 3 > len)
311 goto elen;
312 *ip++ = '\\';
313 l++;
314 }
315 *ip++ = *val;
316 l++;
317 continue;
318 }
319 switch (c = *++(val)) {
320 case 'B':
321 if (l + 30 > len)
322 goto elen;
323 *ip++ = '%';
324 *ip++ = 'p';
325 *ip++ = '0' + p;
326 strcpy(ip, "%{10}%/%{16}%*%p");
327 ip += 16;
328 *ip++ = '0' + p;
329 strcpy(ip, "%{10}%m%+");
330 ip += 9;
331 l += 29;
332 nop = 1;
333 continue;
334 case 'D':
335 if (l + 15 > len)
336 goto elen;
337 *ip++ = '%';
338 *ip++ = 'p';
339 *ip++ = '0' + p;
340 *ip++ = '%';
341 *ip++ = 'p';
342 *ip++ = '0' + p;
343 strcpy(ip, "%{2}%*%-");
344 ip += 8;
345 l += 14;
346 nop = 1;
347 continue;
348 case 'r':
349 /* non op as switched below */
350 break;
351 case '2': /* FALLTHROUGH */
352 case '3': /* FALLTHROUGH */
353 case 'd':
354 if (l + 7 > len)
355 goto elen;
356 if (nop == 0) {
357 *ip++ = '%';
358 *ip++ = 'p';
359 *ip++ = '0' + p;
360 l += 3;
361 } else
362 nop = 0;
363 *ip++ = '%';
364 if (c != 'd') {
365 *ip++ = c;
366 l++;
367 }
368 *ip++ = 'd';
369 l += 2;
370 break;
371 case '+':
372 if (l + 13 > len)
373 goto elen;
374 if (nop == 0) {
375 *ip++ = '%';
376 *ip++ = 'p';
377 *ip++ = '0' + p;
378 l += 3;
379 } else
380 nop = 0;
381 l += printchar(&ip, &val);
382 *ip++ = '%';
383 *ip++ = c;
384 *ip++ = '%';
385 *ip++ = 'c';
386 l += 7;
387 break;
388 case '>':
389 if (l + 29 > len)
390 goto elen;
391 *ip++ = '%';
392 *ip++ = 'p';
393 *ip++ = '0' + p;
394 *ip++ = '%';
395 *ip++ = 'p';
396 *ip++ = '0' + p;
397 *ip++ = '%';
398 *ip++ = '?';
399 l += printchar(&ip, &val);
400 *ip++ = '%';
401 *ip++ = '>';
402 *ip++ = '%';
403 *ip++ = 't';
404 l += printchar(&ip, &val);
405 *ip++ = '%';
406 *ip++ = '+';
407 *ip++ = '%';
408 *ip++ = ';';
409 l += 16;
410 nop = 1;
411 continue;
412 case '.':
413 if (l + 6 > len)
414 goto elen;
415 if (nop == 0) {
416 *ip++ = '%';
417 *ip++ = 'p';
418 *ip++ = '0' + p;
419 l += 3;
420 } else
421 nop = 0;
422 *ip++ = '%';
423 *ip++ = 'c';
424 l += 2;
425 break;
426 default:
427 /* Hope it matches a terminfo command. */
428 *ip++ = '%';
429 *ip++ = c;
430 l += 2;
431 if (c == 'i')
432 continue;
433 break;
434 }
435 /* Swap p1 and p2 */
436 p = 3 - p;
437 }
438
439 /* \E\ is valid termcap.
440 * We need to escape the final \ for terminfo. */
441 if (l > 2 && info[l - 1] == '\\' &&
442 (info[l - 2] != '\\' && info[l - 2] != '^'))
443 {
444 if (l + 1 > len)
445 goto elen;
446 *ip++ = '\\';
447 }
448
449 /* Add our padding at the end. */
450 if (ps != NULL) {
451 size_t n = pe - ps;
452 if (l + n + 4 > len)
453 goto elen;
454 *ip++ = '$';
455 *ip++ = '<';
456 strncpy(ip, ps, n);
457 ip += n;
458 *ip++ = '/';
459 *ip++ = '>';
460 }
461
462 *ip = '\0';
463 return info;
464
465 elen:
466 free(info);
467 errno = ENOMEM;
468 return NULL;
469 }
470
471 typedef struct {
472 const char *name;
473 const char *cap;
474 } DEF_INFO;
475
476 static DEF_INFO def_infos[] = {
477 { "bel", "^G" },
478 { "cr", "^M" },
479 { "cud1", "^J" },
480 { "ht", "^I" },
481 { "ind", "^J" },
482 { "kbs", "^H" },
483 { "kcub1", "^H" },
484 { "kcud1", "^J" },
485 { "nel", "^M^J" }
486 };
487
488 char *
489 captoinfo(char *cap)
490 {
491 char *info, *ip, *token, *val, *p, tok[3];
492 const char *name;
493 size_t len, lp, nl, vl, rl;
494 int defs[__arraycount(def_infos)], fv;
495
496 _DIAGASSERT(cap != NULL);
497
498 len = strlen(cap) * 2;
499 len += __arraycount(def_infos) * (5 + 4 + 3); /* reserve for defs */
500 info = ip = malloc(len);
501 if (info == NULL)
502 return NULL;
503
504 memset(defs, 0, sizeof(defs));
505 lp = 0;
506 tok[2] = '\0';
507 for (token = _ti_get_token(&cap, ':');
508 token != NULL;
509 token = _ti_get_token(&cap, ':'))
510 {
511 if (token[0] == '\0')
512 continue;
513 name = token;
514 val = p = NULL;
515 fv = nl = 0;
516 if (token[1] != '\0') {
517 tok[0] = token[0];
518 tok[1] = token[1];
519 nl = 1;
520 if (token[2] == '\0') {
521 name = flagname(tok);
522 val = NULL;
523 } else if (token[2] == '#') {
524 name = numname(tok);
525 val = token + 2;
526 } else if (token[2] == '=') {
527 name = strname(tok);
528 val = strval(token + 2);
529 fv = 1;
530 } else
531 nl = 0;
532 }
533 /* If not matched we may need to convert padding still. */
534 if (nl == 0) {
535 p = strchr(name, '=');
536 if (p != NULL) {
537 val = strval(p);
538 *p = '\0';
539 fv = 1;
540 }
541 }
542
543 /* See if this sets a default. */
544 for (nl = 0; nl < __arraycount(def_infos); nl++) {
545 if (strcmp(name, def_infos[nl].name) == 0) {
546 defs[nl] = 1;
547 break;
548 }
549 }
550
551 nl = strlen(name);
552 if (val == NULL)
553 vl = 0;
554 else
555 vl = strlen(val);
556 rl = nl + vl + 3; /* , \0 */
557
558 if (lp + rl > len) {
559 if (rl < 256)
560 len += 256;
561 else
562 len += rl;
563 p = realloc(info, len);
564 if (p == NULL)
565 return NULL;
566 info = p;
567 }
568
569 if (ip != info) {
570 *ip++ = ',';
571 *ip++ = ' ';
572 }
573
574 strcpy(ip, name);
575 ip += nl;
576 if (val != NULL) {
577 strcpy(ip, val);
578 ip += vl;
579 if (fv == 1)
580 free(val);
581 }
582 }
583
584 /* Add any defaults not set above. */
585 for (nl = 0; nl < __arraycount(def_infos); nl++) {
586 if (defs[nl] == 0) {
587 *ip++ = ',';
588 *ip++ = ' ';
589 strcpy(ip, def_infos[nl].name);
590 ip += strlen(def_infos[nl].name);
591 *ip++ = '=';
592 strcpy(ip, def_infos[nl].cap);
593 ip += strlen(def_infos[nl].cap);
594 }
595 }
596
597 *ip = '\0';
598 return info;
599 }
600
601