version 1.12, 2011/04/11 21:37:19 |
version 1.26, 2017/05/16 10:29:06 |
Line 33 __RCSID("$NetBSD$"); |
|
Line 33 __RCSID("$NetBSD$"); |
|
#include <sys/stat.h> |
#include <sys/stat.h> |
|
|
#include <assert.h> |
#include <assert.h> |
|
#include <cdbr.h> |
#include <ctype.h> |
#include <ctype.h> |
#include <errno.h> |
#include <errno.h> |
#include <fcntl.h> |
#include <fcntl.h> |
#include <limits.h> |
#include <limits.h> |
#include <ndbm.h> |
|
#include <stdio.h> |
#include <stdio.h> |
#include <stdlib.h> |
#include <stdlib.h> |
#include <string.h> |
#include <string.h> |
Line 46 __RCSID("$NetBSD$"); |
|
Line 46 __RCSID("$NetBSD$"); |
|
|
|
#define _PATH_TERMINFO "/usr/share/misc/terminfo" |
#define _PATH_TERMINFO "/usr/share/misc/terminfo" |
|
|
static char database[PATH_MAX]; |
static char __ti_database[PATH_MAX]; |
static char pathbuf[PATH_MAX]; |
|
const char *_ti_database; |
const char *_ti_database; |
|
|
/* Include a generated list of pre-compiled terminfo descriptions. */ |
/* Include a generated list of pre-compiled terminfo descriptions. */ |
#include "compiled_terms.c" |
#include "compiled_terms.c" |
|
|
static int |
static int |
|
allocset(void *pp, int init, size_t nelem, size_t elemsize) |
|
{ |
|
void **p = pp; |
|
if (*p) { |
|
memset(*p, init, nelem * elemsize); |
|
return 0; |
|
} |
|
|
|
if ((*p = calloc(nelem, elemsize)) == NULL) |
|
return -1; |
|
|
|
if (init != 0) |
|
memset(*p, init, nelem * elemsize); |
|
return 0; |
|
} |
|
|
|
static int |
_ti_readterm(TERMINAL *term, const char *cap, size_t caplen, int flags) |
_ti_readterm(TERMINAL *term, const char *cap, size_t caplen, int flags) |
{ |
{ |
uint8_t ver; |
char ver; |
uint16_t ind, num; |
uint16_t ind, num; |
size_t len; |
size_t len; |
TERMUSERDEF *ud; |
TERMUSERDEF *ud; |
|
|
|
if (caplen == 0) |
|
goto out; |
ver = *cap++; |
ver = *cap++; |
/* Only read version 1 and 2 structures */ |
caplen--; |
if (ver != 1 && ver != 2) { |
/* Only read version 1 structures */ |
errno = EINVAL; |
if (ver != 1) |
|
goto out; |
|
|
|
if (allocset(&term->flags, 0, TIFLAGMAX+1, sizeof(*term->flags)) == -1) |
|
return -1; |
|
|
|
if (allocset(&term->nums, -1, TINUMMAX+1, sizeof(*term->nums)) == -1) |
|
return -1; |
|
|
|
if (allocset(&term->strs, 0, TISTRMAX+1, sizeof(*term->strs)) == -1) |
return -1; |
return -1; |
} |
|
|
|
term->flags = calloc(TIFLAGMAX + 1, sizeof(char)); |
if (term->_arealen != caplen) { |
if (term->flags == NULL) |
term->_arealen = caplen; |
goto err; |
term->_area = realloc(term->_area, term->_arealen); |
term->nums = malloc((TINUMMAX + 1) * sizeof(short)); |
if (term->_area == NULL) |
if (term->nums == NULL) |
return -1; |
goto err; |
} |
memset(term->nums, (short)-1, (TINUMMAX + 1) * sizeof(short)); |
|
term->strs = calloc(TISTRMAX + 1, sizeof(char *)); |
|
if (term->strs == NULL) |
|
goto err; |
|
term->_arealen = caplen; |
|
term->_area = malloc(term->_arealen); |
|
if (term->_area == NULL) |
|
goto err; |
|
memcpy(term->_area, cap, term->_arealen); |
memcpy(term->_area, cap, term->_arealen); |
|
|
cap = term->_area; |
cap = term->_area; |
Line 89 _ti_readterm(TERMINAL *term, const char |
|
Line 107 _ti_readterm(TERMINAL *term, const char |
|
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
term->name = cap; |
term->name = cap; |
cap += len; |
cap += len; |
if (ver == 1) |
len = le16dec(cap); |
|
cap += sizeof(uint16_t); |
|
if (len == 0) |
term->_alias = NULL; |
term->_alias = NULL; |
else { |
else { |
len = le16dec(cap); |
term->_alias = cap; |
cap += sizeof(uint16_t); |
cap += len; |
if (len == 0) |
|
term->_alias = NULL; |
|
else { |
|
term->_alias = cap; |
|
cap += len; |
|
} |
|
} |
} |
len = le16dec(cap); |
len = le16dec(cap); |
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
Line 123 _ti_readterm(TERMINAL *term, const char |
|
Line 137 _ti_readterm(TERMINAL *term, const char |
|
term->flags[ind] = 0; |
term->flags[ind] = 0; |
} |
} |
} |
} |
|
|
num = le16dec(cap); |
num = le16dec(cap); |
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
if (num != 0) { |
if (num != 0) { |
Line 132 _ti_readterm(TERMINAL *term, const char |
|
Line 146 _ti_readterm(TERMINAL *term, const char |
|
for (; num != 0; num--) { |
for (; num != 0; num--) { |
ind = le16dec(cap); |
ind = le16dec(cap); |
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
term->nums[ind] = le16dec(cap); |
term->nums[ind] = (short)le16dec(cap); |
if (flags == 0 && !VALID_NUMERIC(term->nums[ind])) |
if (flags == 0 && !VALID_NUMERIC(term->nums[ind])) |
term->nums[ind] = ABSENT_NUMERIC; |
term->nums[ind] = ABSENT_NUMERIC; |
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
} |
} |
} |
} |
|
|
num = le16dec(cap); |
num = le16dec(cap); |
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
if (num != 0) { |
if (num != 0) { |
Line 158 _ti_readterm(TERMINAL *term, const char |
|
Line 172 _ti_readterm(TERMINAL *term, const char |
|
cap += len; |
cap += len; |
} |
} |
} |
} |
|
|
num = le16dec(cap); |
num = le16dec(cap); |
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
if (num != 0) { |
if (num != 0) { |
term->_nuserdefs = le16dec(cap); |
num = le16dec(cap); |
term->_userdefs = malloc(sizeof(*term->_userdefs) * num); |
|
cap += sizeof(uint16_t); |
cap += sizeof(uint16_t); |
|
if (num != term->_nuserdefs) { |
|
free(term->_userdefs); |
|
term->_userdefs = NULL; |
|
term->_nuserdefs = num; |
|
} |
|
if (allocset(&term->_userdefs, 0, term->_nuserdefs, |
|
sizeof(*term->_userdefs)) == -1) |
|
return -1; |
for (num = 0; num < term->_nuserdefs; num++) { |
for (num = 0; num < term->_nuserdefs; num++) { |
ud = &term->_userdefs[num]; |
ud = &term->_userdefs[num]; |
len = le16dec(cap); |
len = le16dec(cap); |
Line 183 _ti_readterm(TERMINAL *term, const char |
|
Line 204 _ti_readterm(TERMINAL *term, const char |
|
break; |
break; |
case 'n': |
case 'n': |
ud->flag = ABSENT_BOOLEAN; |
ud->flag = ABSENT_BOOLEAN; |
ud->num = le16dec(cap); |
ud->num = (short)le16dec(cap); |
if (flags == 0 && |
if (flags == 0 && |
!VALID_NUMERIC(ud->num)) |
!VALID_NUMERIC(ud->num)) |
ud->num = ABSENT_NUMERIC; |
ud->num = ABSENT_NUMERIC; |
Line 204 _ti_readterm(TERMINAL *term, const char |
|
Line 225 _ti_readterm(TERMINAL *term, const char |
|
cap += len; |
cap += len; |
break; |
break; |
default: |
default: |
errno = EINVAL; |
goto out; |
goto err; |
|
} |
} |
} |
} |
|
} else { |
|
term->_nuserdefs = 0; |
|
if (term->_userdefs) { |
|
free(term->_userdefs); |
|
term->_userdefs = NULL; |
|
} |
} |
} |
|
|
return 1; |
return 1; |
|
out: |
err: |
errno = EINVAL; |
_ti_freeterm(term); |
|
return -1; |
return -1; |
} |
} |
|
|
static int |
static int |
|
_ti_checkname(const TERMINAL *term, const char *name) |
|
{ |
|
const char *a, *p; |
|
size_t name_len; |
|
|
|
/* Check terminal name matches. */ |
|
if (strcmp(name, term->name) == 0) |
|
return 1; |
|
|
|
/* Check terminal aliases match. */ |
|
name_len = strlen(name); |
|
for (a = term->_alias; a != NULL && *a != '\0'; a = p) { |
|
for (p = a; *p != '\0'; p++) { |
|
if (*p == '|') |
|
break; |
|
} |
|
if ((size_t)(p - a) == name_len && |
|
memcmp(name, a, name_len) == 0) |
|
return 1; |
|
if (*p == '|') |
|
p++; |
|
} |
|
|
|
/* No match. */ |
|
return 0; |
|
} |
|
|
|
static int |
_ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags) |
_ti_dbgetterm(TERMINAL *term, const char *path, const char *name, int flags) |
{ |
{ |
DBM *db; |
struct cdbr *db; |
datum dt; |
const void *data; |
char *p; |
const uint8_t *data8; |
|
size_t len, klen; |
int r; |
int r; |
|
|
db = dbm_open(path, O_RDONLY, 0644); |
if (snprintf(__ti_database, sizeof(__ti_database), "%s.cdb", path) < 0) |
|
return -1; |
|
db = cdbr_open(__ti_database, CDBR_DEFAULT); |
if (db == NULL) |
if (db == NULL) |
return -1; |
return -1; |
strlcpy(database, path, sizeof(database)); |
|
_ti_database = database; |
|
dt.dptr = (void *)__UNCONST(name); |
|
dt.dsize = strlen(name); |
|
dt = dbm_fetch(db, dt); |
|
if (dt.dptr == NULL) { |
|
dbm_close(db); |
|
return 0; |
|
} |
|
|
|
for (;;) { |
r = 0; |
p = (char *)dt.dptr; |
klen = strlen(name) + 1; |
if (*p++ != 0) /* not alias */ |
if (cdbr_find(db, name, klen, &data, &len) == -1) |
break; |
goto out; |
dt.dsize = le16dec(p) - 1; |
data8 = data; |
p += sizeof(uint16_t); |
if (len == 0) |
dt.dptr = p; |
goto out; |
dt = dbm_fetch(db, dt); |
|
if (dt.dptr == NULL) { |
/* If the entry is an alias, load the indexed terminfo description. */ |
dbm_close(db); |
if (data8[0] == 2) { |
return 0; |
if (cdbr_get(db, le32dec(data8 + 1), &data, &len)) |
} |
goto out; |
} |
data8 = data; |
|
} |
r = _ti_readterm(term, (char *)dt.dptr, dt.dsize, flags); |
|
dbm_close(db); |
r = _ti_readterm(term, data, len, flags); |
|
/* Ensure that this is the right terminfo description. */ |
|
if (r == 1) |
|
r = _ti_checkname(term, name); |
|
/* Remember the database we read. */ |
|
if (r == 1) |
|
_ti_database = __ti_database; |
|
|
|
out: |
|
cdbr_close(db); |
return r; |
return r; |
} |
} |
|
|
|
|
_ti_dbgettermp(TERMINAL *term, const char *path, const char *name, int flags) |
_ti_dbgettermp(TERMINAL *term, const char *path, const char *name, int flags) |
{ |
{ |
const char *p; |
const char *p; |
|
char pathbuf[PATH_MAX]; |
size_t l; |
size_t l; |
int r, e; |
int r, e; |
|
|
Line 268 _ti_dbgettermp(TERMINAL *term, const cha |
|
Line 326 _ti_dbgettermp(TERMINAL *term, const cha |
|
do { |
do { |
for (p = path; *path != '\0' && *path != ':'; path++) |
for (p = path; *path != '\0' && *path != ':'; path++) |
continue; |
continue; |
l = path - p; |
l = (size_t)(path - p); |
if (l != 0 && l + 1 < sizeof(pathbuf)) { |
if (l != 0 && l + 1 < sizeof(pathbuf)) { |
memcpy(pathbuf, p, l); |
memcpy(pathbuf, p, l); |
pathbuf[l] = '\0'; |
pathbuf[l] = '\0'; |
Line 300 ticcmp(const TIC *tic, const char *name) |
|
Line 358 ticcmp(const TIC *tic, const char *name) |
|
if (s == NULL) |
if (s == NULL) |
l = strlen(alias); |
l = strlen(alias); |
else |
else |
l = s - alias; |
l = (size_t)(s - alias); |
if (len == l && strncmp(alias, name, l) == 0) |
if (len == l && memcmp(alias, name, l) == 0) |
return 0; |
return 0; |
if (s == NULL) |
if (s == NULL) |
break; |
break; |
Line 322 _ti_findterm(TERMINAL *term, const char |
|
Line 380 _ti_findterm(TERMINAL *term, const char |
|
_DIAGASSERT(term != NULL); |
_DIAGASSERT(term != NULL); |
_DIAGASSERT(name != NULL); |
_DIAGASSERT(name != NULL); |
|
|
database[0] = '\0'; |
__ti_database[0] = '\0'; |
_ti_database = NULL; |
_ti_database = NULL; |
r = 0; |
r = 0; |
|
|
if ((e = getenv("TERMINFO")) != NULL && *e != '\0') |
if ((e = getenv("TERMINFO")) != NULL && *e != '\0') { |
if (e[0] == '/') |
if (e[0] == '/') |
return _ti_dbgetterm(term, e, name, flags); |
return _ti_dbgetterm(term, e, name, flags); |
|
} |
|
|
c = NULL; |
c = NULL; |
if (e == NULL && (c = getenv("TERMCAP")) != NULL) { |
if (e == NULL && (c = getenv("TERMCAP")) != NULL) { |
Line 344 _ti_findterm(TERMINAL *term, const char |
|
Line 403 _ti_findterm(TERMINAL *term, const char |
|
if (e != NULL) { |
if (e != NULL) { |
if (c == NULL) |
if (c == NULL) |
e = strdup(e); /* So we don't destroy env */ |
e = strdup(e); /* So we don't destroy env */ |
if (e == NULL) |
if (e == NULL) |
tic = NULL; |
tic = NULL; |
else |
else { |
tic = _ti_compile(e, TIC_WARNING | |
tic = _ti_compile(e, TIC_WARNING | |
TIC_ALIAS | TIC_DESCRIPTION | TIC_EXTRA); |
TIC_ALIAS | TIC_DESCRIPTION | TIC_EXTRA); |
if (c == NULL && e != NULL) |
|
free(e); |
free(e); |
|
} |
if (tic != NULL && ticcmp(tic, name) == 0) { |
if (tic != NULL && ticcmp(tic, name) == 0) { |
len = _ti_flatten(&f, tic); |
len = _ti_flatten(&f, tic); |
if (len != -1) { |
if (len != -1) { |
Line 380 _ti_findterm(TERMINAL *term, const char |
|
Line 439 _ti_findterm(TERMINAL *term, const char |
|
r = _ti_dbgettermp(term, _PATH_TERMINFO, name, flags); |
r = _ti_dbgettermp(term, _PATH_TERMINFO, name, flags); |
|
|
return r; |
return r; |
|
|
} |
} |
|
|
int |
int |
Line 404 _ti_getterm(TERMINAL *term, const char * |
|
Line 462 _ti_getterm(TERMINAL *term, const char * |
|
|
|
return r; |
return r; |
} |
} |
|
|
void |
|
_ti_freeterm(TERMINAL *term) |
|
{ |
|
|
|
if (term != NULL) { |
|
free(term->_area); |
|
free(term->strs); |
|
free(term->nums); |
|
free(term->flags); |
|
free(term->_userdefs); |
|
} |
|
} |
|