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