xref: /netbsd-src/lib/libterminfo/term.c (revision b1c86f5f087524e68db12794ee9c3e3da1ab17a0)
1 /* $NetBSD: term.c,v 1.11 2010/02/26 00:09:00 roy Exp $ */
2 
3 /*
4  * Copyright (c) 2009, 2010 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: term.c,v 1.11 2010/02/26 00:09:00 roy Exp $");
32 
33 #include <sys/stat.h>
34 
35 #include <assert.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <limits.h>
40 #include <ndbm.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h>
44 #include <term_private.h>
45 #include <term.h>
46 
47 #define _PATH_TERMINFO		"/usr/share/misc/terminfo"
48 
49 static char database[PATH_MAX];
50 static char pathbuf[PATH_MAX];
51 const char *_ti_database;
52 
53 /* Include a generated list of pre-compiled terminfo descriptions. */
54 #include "compiled_terms.c"
55 
56 static int
57 _ti_readterm(TERMINAL *term, const char *cap, size_t caplen, int flags)
58 {
59 	uint8_t ver;
60 	uint16_t ind, num;
61 	size_t len;
62 	TERMUSERDEF *ud;
63 
64 	ver = *cap++;
65 	/* Only read version 1 and 2 structures */
66 	if (ver != 1 && ver != 2) {
67 		errno = EINVAL;
68 		return -1;
69 	}
70 
71 	term->flags = calloc(TIFLAGMAX + 1, sizeof(char));
72 	if (term->flags == NULL)
73 		goto err;
74 	term->nums = malloc((TINUMMAX + 1) * sizeof(short));
75 	if (term->nums == NULL)
76 		goto err;
77 	memset(term->nums, (short)-1, (TINUMMAX + 1) * sizeof(short));
78 	term->strs = calloc(TISTRMAX + 1, sizeof(char *));
79 	if (term->strs == NULL)
80 		goto err;
81 	term->_arealen = caplen;
82 	term->_area = malloc(term->_arealen);
83 	if (term->_area == NULL)
84 		goto err;
85 	memcpy(term->_area, cap, term->_arealen);
86 
87 	cap = term->_area;
88 	len = le16dec(cap);
89 	cap += sizeof(uint16_t);
90 	term->name = cap;
91 	cap += len;
92 	if (ver == 1)
93 		term->_alias = NULL;
94 	else {
95 		len = le16dec(cap);
96 		cap += sizeof(uint16_t);
97 		if (len == 0)
98 			term->_alias = NULL;
99 		else {
100 			term->_alias = cap;
101 			cap += len;
102 		}
103 	}
104 	len = le16dec(cap);
105 	cap += sizeof(uint16_t);
106 	if (len == 0)
107 		term->desc = NULL;
108 	else {
109 		term->desc = cap;
110 		cap += len;
111 	}
112 
113 	num = le16dec(cap);
114 	cap += sizeof(uint16_t);
115 	if (num != 0) {
116 		num = le16dec(cap);
117 		cap += sizeof(uint16_t);
118 		for (; num != 0; num--) {
119 			ind = le16dec(cap);
120 			cap += sizeof(uint16_t);
121 			term->flags[ind] = *cap++;
122 			if (flags == 0 && !VALID_BOOLEAN(term->flags[ind]))
123 				term->flags[ind] = 0;
124 		}
125 	}
126 
127 	num = le16dec(cap);
128 	cap += sizeof(uint16_t);
129 	if (num != 0) {
130 		num = le16dec(cap);
131 		cap += sizeof(uint16_t);
132 		for (; num != 0; num--) {
133 			ind = le16dec(cap);
134 			cap += sizeof(uint16_t);
135 			term->nums[ind] = le16dec(cap);
136 			if (flags == 0 && !VALID_NUMERIC(term->nums[ind]))
137 				term->nums[ind] = ABSENT_NUMERIC;
138 			cap += sizeof(uint16_t);
139 		}
140 	}
141 
142 	num = le16dec(cap);
143 	cap += sizeof(uint16_t);
144 	if (num != 0) {
145 		num = le16dec(cap);
146 		cap += sizeof(uint16_t);
147 		for (; num != 0; num--) {
148 			ind = le16dec(cap);
149 			cap += sizeof(uint16_t);
150 			len = le16dec(cap);
151 			cap += sizeof(uint16_t);
152 			if (len > 0)
153 				term->strs[ind] = cap;
154 			else if (flags == 0)
155 				term->strs[ind] = ABSENT_STRING;
156 			else
157 				term->strs[ind] = CANCELLED_STRING;
158 			cap += len;
159 		}
160 	}
161 
162 	num = le16dec(cap);
163 	cap += sizeof(uint16_t);
164 	if (num != 0) {
165 		term->_nuserdefs = le16dec(cap);
166 		term->_userdefs = malloc(sizeof(*term->_userdefs) * num);
167 		cap += sizeof(uint16_t);
168 		for (num = 0; num < term->_nuserdefs; num++) {
169 			ud = &term->_userdefs[num];
170 			len = le16dec(cap);
171 			cap += sizeof(uint16_t);
172 			ud->id = cap;
173 			cap += len;
174 			ud->type = *cap++;
175 			switch (ud->type) {
176 			case 'f':
177 				ud->flag = *cap++;
178 				if (flags == 0 &&
179 				    !VALID_BOOLEAN(ud->flag))
180 					ud->flag = 0;
181 				ud->num = ABSENT_NUMERIC;
182 				ud->str = ABSENT_STRING;
183 				break;
184 			case 'n':
185 				ud->flag = ABSENT_BOOLEAN;
186 				ud->num = le16dec(cap);
187 				if (flags == 0 &&
188 				    !VALID_NUMERIC(ud->num))
189 					ud->num = ABSENT_NUMERIC;
190 				ud->str = ABSENT_STRING;
191 				cap += sizeof(uint16_t);
192 				break;
193 			case 's':
194 				ud->flag = ABSENT_BOOLEAN;
195 				ud->num = ABSENT_NUMERIC;
196 				len = le16dec(cap);
197 				cap += sizeof(uint16_t);
198 				if (len > 0)
199 					ud->str = cap;
200 				else if (flags == 0)
201 					ud->str = ABSENT_STRING;
202 				else
203 					ud->str = CANCELLED_STRING;
204 				cap += len;
205 				break;
206 			default:
207 				errno = EINVAL;
208 				goto err;
209 			}
210 		}
211 	}
212 	return 1;
213 
214 err:
215 	_ti_freeterm(term);
216 	return -1;
217 }
218 
219 static int
220 _ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags)
221 {
222 	DBM *db;
223 	datum dt;
224 	char *p;
225 	int r;
226 
227 	db = dbm_open(path, O_RDONLY, 0644);
228 	if (db == NULL)
229 		return -1;
230 	strlcpy(database, path, sizeof(database));
231 	_ti_database = database;
232 	dt.dptr = (void *)__UNCONST(name);
233 	dt.dsize = strlen(name);
234 	dt = dbm_fetch(db, dt);
235 	if (dt.dptr == NULL) {
236 		dbm_close(db);
237 		return 0;
238 	}
239 
240 	for (;;) {
241 		p = (char *)dt.dptr;
242 		if (*p++ != 0) /* not alias */
243 			break;
244 		dt.dsize = le16dec(p) - 1;
245 		p += sizeof(uint16_t);
246 		dt.dptr = p;
247 		dt = dbm_fetch(db, dt);
248 		if (dt.dptr == NULL) {
249 			dbm_close(db);
250 			return 0;
251 		}
252 	}
253 
254 	r = _ti_readterm(term, (char *)dt.dptr, dt.dsize, flags);
255 	dbm_close(db);
256 	return r;
257 }
258 
259 static int
260 _ti_dbgettermp(TERMINAL *term, const char *path, const char *name, int flags)
261 {
262 	const char *p;
263 	size_t l;
264 	int r, e;
265 
266 	e = -1;
267 	r = 0;
268 	do {
269 		for (p = path; *path != '\0' && *path != ':'; path++)
270 			continue;
271 		l = path - p;
272 		if (l != 0 && l + 1 < sizeof(pathbuf)) {
273 			memcpy(pathbuf, p, l);
274 			pathbuf[l] = '\0';
275 			r = _ti_dbgetterm(term, pathbuf, name, flags);
276 			if (r == 1)
277 				return 1;
278 			if (r == 0)
279 				e = 0;
280 		}
281 	} while (*path++ == ':');
282 	return e;
283 }
284 
285 static int
286 ticcmp(const TIC *tic, const char *name)
287 {
288 	char *alias, *s;
289 	size_t len, l;
290 
291 	if (strcmp(tic->name, name) == 0)
292 		return 0;
293 	if (tic->alias == NULL)
294 		return -1;
295 
296 	len = strlen(name);
297 	alias = tic->alias;
298 	while (*alias != '\0') {
299 		s = strchr(alias, '|');
300 		if (s == NULL)
301 			l = strlen(alias);
302 		else
303 			l = s - alias;
304 		if (len == l && strncmp(alias, name, l) == 0)
305 			return 0;
306 		if (s == NULL)
307 			break;
308 		alias = s + 1;
309 	}
310 	return 1;
311 }
312 
313 static int
314 _ti_findterm(TERMINAL *term, const char *name, int flags)
315 {
316 	int r;
317 	char *c, *e, h[PATH_MAX];
318 	TIC *tic;
319 	uint8_t *f;
320 	ssize_t len;
321 
322 	_DIAGASSERT(term != NULL);
323 	_DIAGASSERT(name != NULL);
324 
325 	database[0] = '\0';
326 	_ti_database = NULL;
327 	r = 0;
328 
329 	if ((e = getenv("TERMINFO")) != NULL && *e != '\0')
330 		if (e[0] == '/')
331 			return _ti_dbgetterm(term, e, name, flags);
332 
333 	c = NULL;
334 	if (e == NULL && (c = getenv("TERMCAP")) != NULL) {
335 		if (*c != '\0' && *c != '/') {
336 			c = strdup(c);
337 			if (c != NULL) {
338 				e = captoinfo(c);
339 				free(c);
340 			}
341 		}
342 	}
343 
344 	if (e != NULL) {
345 		if (c == NULL)
346 			e = strdup(e); /* So we don't destroy env */
347 		if (e  == NULL)
348 			tic = NULL;
349 		else
350 			tic = _ti_compile(e, TIC_WARNING |
351 			    TIC_ALIAS | TIC_DESCRIPTION | TIC_EXTRA);
352 		if (c == NULL && e != NULL)
353 			free(e);
354 		if (tic != NULL && ticcmp(tic, name) == 0) {
355 			len = _ti_flatten(&f, tic);
356 			if (len != -1) {
357 				r = _ti_readterm(term, (char *)f, len, flags);
358 				free(f);
359 			}
360 		}
361 		_ti_freetic(tic);
362 		if (r == 1) {
363 			if (c == NULL)
364 				_ti_database = "$TERMINFO";
365 			else
366 				_ti_database = "$TERMCAP";
367 			return r;
368 		}
369 	}
370 
371 	if ((e = getenv("TERMINFO_DIRS")) != NULL)
372 		return _ti_dbgettermp(term, e, name, flags);
373 
374 	if ((e = getenv("HOME")) != NULL) {
375 		snprintf(h, sizeof(h), "%s/.terminfo", e);
376 		r = _ti_dbgetterm(term, h, name, flags);
377 	}
378 	if (r != 1)
379 		r = _ti_dbgettermp(term, _PATH_TERMINFO, name, flags);
380 
381 	return r;
382 
383 }
384 
385 int
386 _ti_getterm(TERMINAL *term, const char *name, int flags)
387 {
388 	int r;
389 	size_t i;
390 	const struct compiled_term *t;
391 
392 	r = _ti_findterm(term, name, flags);
393 	if (r == 1)
394 		return r;
395 
396 	for (i = 0; i < __arraycount(compiled_terms); i++) {
397 		t = &compiled_terms[i];
398 		if (strcmp(name, t->name) == 0) {
399 			r = _ti_readterm(term, t->cap, t->caplen, flags);
400 			break;
401 		}
402 	}
403 
404 	return r;
405 }
406 
407 void
408 _ti_freeterm(TERMINAL *term)
409 {
410 
411 	if (term != NULL) {
412 		free(term->_area);
413 		free(term->strs);
414 		free(term->nums);
415 		free(term->flags);
416 		free(term->_userdefs);
417 	}
418 }
419