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