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