Home | History | Annotate | Line # | Download | only in libterminfo
termcap.c revision 1.3
      1 /* $NetBSD: termcap.c,v 1.3 2010/02/26 00:09:00 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.3 2010/02/26 00:09:00 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 			*ip++ = *val;
    247 			l++;
    248 			continue;
    249 		}
    250 		switch (c = *(++val)) {
    251 		case 'd':
    252 			if (l + 6 > len)
    253 				goto elen;
    254 			*ip++ = '%';
    255 			*ip++ = 'p';
    256 			*ip++ = '0' + p;
    257 			*ip++ = '%';
    258 			*ip++ = 'd';
    259 			l += 5;
    260 			n += 5;
    261 			/* FALLTHROUGH */
    262 		case 'r':
    263 			p = 3 - p;
    264 			break;
    265 		default:
    266 			/* Hope it matches a terminfo command. */
    267 			*ip++ = '%';
    268 			*ip++ = c;
    269 			l += 2;
    270 			break;
    271 		}
    272 	}
    273 
    274 	*ip = '\0';
    275 	return info;
    276 
    277 elen:
    278 	free(info);
    279 	errno = ENOMEM;
    280 	return NULL;
    281 }
    282 
    283 char *
    284 captoinfo(char *cap)
    285 {
    286 	char *info, *ip, *token, *val, *p, tok[3];
    287 	const char *name;
    288 	size_t len, lp, nl, vl, rl;
    289 
    290 	_DIAGASSERT(cap != NULL);
    291 
    292 	len = strlen(cap) * 2;
    293 	info = ip = malloc(len);
    294 	if (info == NULL)
    295 		return NULL;
    296 
    297 	lp = 0;
    298 	tok[2] = '\0';
    299 	while ((token = strsep(&cap, ":")) != NULL) {
    300 		/* Trim whitespace */
    301 		while (isspace((unsigned char)*token))
    302 			token++;
    303 		if (token[0] == '\0')
    304 			continue;
    305 		name = token;
    306 		val = NULL;
    307 		if (token[1] != '\0') {
    308 			tok[0] = token[0];
    309 			tok[1] = token[1];
    310 			if (token[2] == '\0') {
    311 				name = flagname(tok);
    312 				val = NULL;
    313 			} else if (token[2] == '#') {
    314 				name = numname(tok);
    315 				val = token + 2;
    316 			} else if (token[2] == '=') {
    317 				name = strname(tok);
    318 				val = strval(token + 2);
    319 			}
    320 		}
    321 		nl = strlen(name);
    322 		if (val == NULL)
    323 			vl = 0;
    324 		else
    325 			vl = strlen(val);
    326 		rl = nl + vl + 3; /* , \0 */
    327 
    328 		if (lp + rl > len) {
    329 			if (rl < 256)
    330 				len += 256;
    331 			else
    332 				len += rl;
    333 			p = realloc(info, len);
    334 			if (p == NULL)
    335 				return NULL;
    336 			info = p;
    337 		}
    338 
    339 		if (ip != info) {
    340 			*ip++ = ',';
    341 			*ip++ = ' ';
    342 		}
    343 
    344 		strcpy(ip, name);
    345 		ip += nl;
    346 		if (val != NULL) {
    347 			strcpy(ip, val);
    348 			ip += vl;
    349 			if (token[2] == '=')
    350 				free(val);
    351 		}
    352 	}
    353 
    354 	*ip = '\0';
    355 	return info;
    356 }
    357 
    358