Home | History | Annotate | Line # | Download | only in libterminfo
termcap.c revision 1.1
      1 /* $NetBSD: termcap.c,v 1.1 2010/02/03 15:16:32 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.1 2010/02/03 15:16:32 roy Exp $");
     32 
     33 #include <sys/types.h>
     34 
     35 #include <assert.h>
     36 #include <string.h>
     37 #include <term_private.h>
     38 #include <term.h>
     39 #include <termcap.h>
     40 #include <unistd.h>
     41 #include <stdio.h>
     42 
     43 #include "termcap_map.c"
     44 #include "termcap_hash.c"
     45 
     46 char *UP;
     47 char *BC;
     48 
     49 /* ARGSUSED */
     50 int
     51 tgetent(__unused char *bp, const char *name)
     52 {
     53 	int errret;
     54 	static TERMINAL *last = NULL;
     55 
     56 	_DIAGASSERT(name != NULL);
     57 
     58 	/* Free the old term */
     59 	if (last != NULL) {
     60 		del_curterm(last);
     61 		last = NULL;
     62 	}
     63 	errret = -1;
     64 	if (setupterm(name, STDOUT_FILENO, &errret) != 0)
     65 		return errret;
     66 	last = cur_term;
     67 
     68 	if (pad_char != NULL)
     69 		PC = pad_char[0];
     70 	UP = __UNCONST(cursor_up);
     71 	BC = __UNCONST(cursor_left);
     72 	return 1;
     73 }
     74 
     75 int
     76 tgetflag(const char *id)
     77 {
     78 	uint32_t ind;
     79 	size_t i;
     80 	TERMUSERDEF *ud;
     81 
     82 	_DIAGASSERT(id != NULL);
     83 
     84 	if (cur_term == NULL)
     85 		return 0;
     86 
     87 	ind = _t_flaghash((const unsigned char *)id, strlen(id));
     88 	if (ind <= __arraycount(_ti_cap_flagids)) {
     89 		if (strcmp(id, _ti_cap_flagids[ind].id) == 0)
     90 			return cur_term->flags[_ti_cap_flagids[ind].ti];
     91 	}
     92 	for (i = 0; i < cur_term->_nuserdefs; i++) {
     93 		ud = &cur_term->_userdefs[i];
     94 		if (ud->type == 'f' && strcmp(ud->id, id) == 0)
     95 			return ud->flag;
     96 	}
     97 	return 0;
     98 }
     99 
    100 int
    101 tgetnum(const char *id)
    102 {
    103 	uint32_t ind;
    104 	size_t i;
    105 	TERMUSERDEF *ud;
    106 	const TENTRY *te;
    107 
    108 	_DIAGASSERT(id != NULL);
    109 
    110 	if (cur_term == NULL)
    111 		return -1;
    112 
    113 	ind = _t_numhash((const unsigned char *)id, strlen(id));
    114 	if (ind <= __arraycount(_ti_cap_numids)) {
    115 		te = &_ti_cap_numids[ind];
    116 		if (strcmp(id, te->id) == 0) {
    117 			if (!VALID_NUMERIC(cur_term->nums[te->ti]))
    118 				return ABSENT_NUMERIC;
    119 			return cur_term->nums[te->ti];
    120 		}
    121 	}
    122 	for (i = 0; i < cur_term->_nuserdefs; i++) {
    123 		ud = &cur_term->_userdefs[i];
    124 		if (ud->type == 'n' && strcmp(ud->id, id) == 0) {
    125 			if (!VALID_NUMERIC(ud->num))
    126 				return ABSENT_NUMERIC;
    127 			return ud->num;
    128 		}
    129 	}
    130 	return -1;
    131 }
    132 
    133 char *
    134 tgetstr(const char *id, __unused char **area)
    135 {
    136 	uint32_t ind;
    137 	size_t i;
    138 	TERMUSERDEF *ud;
    139 	const char *str;
    140 
    141 	_DIAGASSERT(id != NULL);
    142 
    143 	if (cur_term == NULL)
    144 		return NULL;
    145 
    146 	str = NULL;
    147 	ind = _t_strhash((const unsigned char *)id, strlen(id));
    148 	if (ind <= __arraycount(_ti_cap_strids)) {
    149 		if (strcmp(id, _ti_cap_strids[ind].id) == 0) {
    150 			str = cur_term->strs[_ti_cap_strids[ind].ti];
    151 			if (str == NULL)
    152 				return NULL;
    153 		}
    154 	}
    155 	if (str != NULL)
    156 		for (i = 0; i < cur_term->_nuserdefs; i++) {
    157 			ud = &cur_term->_userdefs[i];
    158 			if (ud->type == 's' && strcmp(ud->id, id) == 0)
    159 				str = ud->str;
    160 		}
    161 
    162 	/* XXX: FXIXME
    163 	 * We should fix sgr0(me) as it has a slightly different meaning
    164 	 * for termcap. */
    165 
    166 	if (str != NULL && area != NULL && *area != NULL) {
    167 		char *s;
    168 		s = *area;
    169 		strcpy(*area, str);
    170 		*area += strlen(*area) + 1;
    171 		return s;
    172 	}
    173 
    174 	return __UNCONST(str);
    175 }
    176 
    177 char *
    178 tgoto(const char *cm, int destcol, int destline)
    179 {
    180 
    181 	_DIAGASSERT(cm != NULL);
    182 	return vtparm(cm, destline, destcol);
    183 }
    184