1 /* $NetBSD: term.c,v 1.13 2011/10/03 19:18:55 roy Exp $ */ 2 3 /* 4 * Copyright (c) 2009, 2010, 2011 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.13 2011/10/03 19:18:55 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 return -1; 74 term->nums = malloc((TINUMMAX + 1) * sizeof(short)); 75 if (term->nums == NULL) 76 return -1; 77 memset(term->nums, (short)-1, (TINUMMAX + 1) * sizeof(short)); 78 term->strs = calloc(TISTRMAX + 1, sizeof(char *)); 79 if (term->strs == NULL) 80 return -1; 81 term->_arealen = caplen; 82 term->_area = malloc(term->_arealen); 83 if (term->_area == NULL) 84 return -1; 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 return -1; 209 } 210 } 211 } 212 return 1; 213 } 214 215 static int 216 _ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags) 217 { 218 DBM *db; 219 datum dt; 220 char *p; 221 int r; 222 223 db = dbm_open(path, O_RDONLY, 0644); 224 if (db == NULL) 225 return -1; 226 strlcpy(database, path, sizeof(database)); 227 _ti_database = database; 228 dt.dptr = (void *)__UNCONST(name); 229 dt.dsize = strlen(name); 230 dt = dbm_fetch(db, dt); 231 if (dt.dptr == NULL) { 232 dbm_close(db); 233 return 0; 234 } 235 236 for (;;) { 237 p = (char *)dt.dptr; 238 if (*p++ != 0) /* not alias */ 239 break; 240 dt.dsize = le16dec(p) - 1; 241 p += sizeof(uint16_t); 242 dt.dptr = p; 243 dt = dbm_fetch(db, dt); 244 if (dt.dptr == NULL) { 245 dbm_close(db); 246 return 0; 247 } 248 } 249 250 r = _ti_readterm(term, (char *)dt.dptr, dt.dsize, flags); 251 dbm_close(db); 252 return r; 253 } 254 255 static int 256 _ti_dbgettermp(TERMINAL *term, const char *path, const char *name, int flags) 257 { 258 const char *p; 259 size_t l; 260 int r, e; 261 262 e = -1; 263 r = 0; 264 do { 265 for (p = path; *path != '\0' && *path != ':'; path++) 266 continue; 267 l = path - p; 268 if (l != 0 && l + 1 < sizeof(pathbuf)) { 269 memcpy(pathbuf, p, l); 270 pathbuf[l] = '\0'; 271 r = _ti_dbgetterm(term, pathbuf, name, flags); 272 if (r == 1) 273 return 1; 274 if (r == 0) 275 e = 0; 276 } 277 } while (*path++ == ':'); 278 return e; 279 } 280 281 static int 282 ticcmp(const TIC *tic, const char *name) 283 { 284 char *alias, *s; 285 size_t len, l; 286 287 if (strcmp(tic->name, name) == 0) 288 return 0; 289 if (tic->alias == NULL) 290 return -1; 291 292 len = strlen(name); 293 alias = tic->alias; 294 while (*alias != '\0') { 295 s = strchr(alias, '|'); 296 if (s == NULL) 297 l = strlen(alias); 298 else 299 l = s - alias; 300 if (len == l && strncmp(alias, name, l) == 0) 301 return 0; 302 if (s == NULL) 303 break; 304 alias = s + 1; 305 } 306 return 1; 307 } 308 309 static int 310 _ti_findterm(TERMINAL *term, const char *name, int flags) 311 { 312 int r; 313 char *c, *e, h[PATH_MAX]; 314 TIC *tic; 315 uint8_t *f; 316 ssize_t len; 317 318 _DIAGASSERT(term != NULL); 319 _DIAGASSERT(name != NULL); 320 321 database[0] = '\0'; 322 _ti_database = NULL; 323 r = 0; 324 325 if ((e = getenv("TERMINFO")) != NULL && *e != '\0') 326 if (e[0] == '/') 327 return _ti_dbgetterm(term, e, name, flags); 328 329 c = NULL; 330 if (e == NULL && (c = getenv("TERMCAP")) != NULL) { 331 if (*c != '\0' && *c != '/') { 332 c = strdup(c); 333 if (c != NULL) { 334 e = captoinfo(c); 335 free(c); 336 } 337 } 338 } 339 340 if (e != NULL) { 341 if (c == NULL) 342 e = strdup(e); /* So we don't destroy env */ 343 if (e == NULL) 344 tic = NULL; 345 else 346 tic = _ti_compile(e, TIC_WARNING | 347 TIC_ALIAS | TIC_DESCRIPTION | TIC_EXTRA); 348 if (c == NULL && e != NULL) 349 free(e); 350 if (tic != NULL && ticcmp(tic, name) == 0) { 351 len = _ti_flatten(&f, tic); 352 if (len != -1) { 353 r = _ti_readterm(term, (char *)f, (size_t)len, 354 flags); 355 free(f); 356 } 357 } 358 _ti_freetic(tic); 359 if (r == 1) { 360 if (c == NULL) 361 _ti_database = "$TERMINFO"; 362 else 363 _ti_database = "$TERMCAP"; 364 return r; 365 } 366 } 367 368 if ((e = getenv("TERMINFO_DIRS")) != NULL) 369 return _ti_dbgettermp(term, e, name, flags); 370 371 if ((e = getenv("HOME")) != NULL) { 372 snprintf(h, sizeof(h), "%s/.terminfo", e); 373 r = _ti_dbgetterm(term, h, name, flags); 374 } 375 if (r != 1) 376 r = _ti_dbgettermp(term, _PATH_TERMINFO, name, flags); 377 378 return r; 379 380 } 381 382 int 383 _ti_getterm(TERMINAL *term, const char *name, int flags) 384 { 385 int r; 386 size_t i; 387 const struct compiled_term *t; 388 389 r = _ti_findterm(term, name, flags); 390 if (r == 1) 391 return r; 392 393 for (i = 0; i < __arraycount(compiled_terms); i++) { 394 t = &compiled_terms[i]; 395 if (strcmp(name, t->name) == 0) { 396 r = _ti_readterm(term, t->cap, t->caplen, flags); 397 break; 398 } 399 } 400 401 return r; 402 } 403