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