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