#ifndef _TERM_PRIVATE_H_
#define _TERM_PRIVATE_H_
#if HAVE_NBTOOL_CONFIG_H
# include "nbtool_config.h"
#endif
#if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
# include <sys/endian.h>
#endif
#include <sys/types.h>
#include <assert.h>
#include <limits.h>
#define _TERMINFO
#define TERMINFO_RTYPE_O1 1
#define TERMINFO_ALIAS 2
#define TERMINFO_RTYPE 3
#define TERMINFO_VDELIM ','
#define TERMINFO_VDELIMSTR ","
#define ABSENT_BOOLEAN ((signed char)-1)
#define ABSENT_NUMERIC (-1)
#define ABSENT_STRING (char *)0
#define CANCELLED_BOOLEAN ((signed char)-2)
#define CANCELLED_NUMERIC (-2)
#define CANCELLED_STRING (char *)(-1)
#define VALID_BOOLEAN(s) ((unsigned char)(s) <= 1)
#define VALID_NUMERIC(s) ((s) >= 0)
#define VALID_STRING(s) ((s) != CANCELLED_STRING && (s) != ABSENT_STRING)
typedef struct {
const char *id;
char type;
char flag;
int num;
const char *str;
} TERMUSERDEF;
typedef struct {
int fildes;
const char *name;
const char *desc;
signed char *flags;
int *nums;
const char **strs;
char *_area;
size_t _arealen;
size_t _nuserdefs;
TERMUSERDEF *_userdefs;
short _ospeed;
char *_buf;
size_t _buflen;
size_t _bufpos;
long _snums[26];
const char *_alias;
} TERMINAL;
extern const char * _ti_database;
ssize_t _ti_flagindex(const char *);
ssize_t _ti_numindex(const char *);
ssize_t _ti_strindex(const char *);
const char * _ti_flagid(ssize_t);
const char * _ti_numid(ssize_t);
const char * _ti_strid(ssize_t);
int _ti_getterm(TERMINAL *, const char *, int);
void _ti_setospeed(TERMINAL *);
#define TIC_WARNING (1 << 0)
#define TIC_DESCRIPTION (1 << 1)
#define TIC_ALIAS (1 << 2)
#define TIC_COMMENT (1 << 3)
#define TIC_EXTRA (1 << 4)
#define TIC_COMPAT_V1 (1 << 5)
typedef struct {
char *buf;
size_t buflen;
size_t bufpos;
size_t entries;
} TBUF;
typedef struct {
char *name;
char *alias;
char *desc;
int rtype;
TBUF flags;
TBUF nums;
TBUF strs;
TBUF extras;
} TIC;
#define _ti_numsize(tic) \
((tic)->rtype == TERMINFO_RTYPE_O1 ? sizeof(uint16_t) : sizeof(uint32_t))
int _ti_promote(TIC *);
char *_ti_grow_tbuf(TBUF *, size_t);
char *_ti_get_token(char **, char);
const char *_ti_find_cap(TIC *, TBUF *, char, short);
const char *_ti_find_extra(TIC *, TBUF *, const char *);
char *_ti_getname(int, const char *);
size_t _ti_store_extra(TIC *, int, const char *, char, char, int,
const char *, size_t, int);
TIC *_ti_compile(char *, int);
ssize_t _ti_flatten(uint8_t **, const TIC *);
void _ti_freetic(TIC *);
int _ti_encode_buf_id_num(TBUF *, int, int, size_t);
int _ti_encode_buf_id_count_str(TBUF *, int, const void *, size_t);
int _ti_encode_buf_id_flags(TBUF *, int, int);
#define TPARM_MAX 9
int _ti_parm_analyse(const char *, int *, int);
static __inline int
_ti_decode_16(const char **cap)
{
int num = (int16_t)le16dec(*cap);
*cap += sizeof(uint16_t);
return num;
}
static __inline int
_ti_decode_32(const char **cap)
{
int num = (int32_t)le32dec(*cap);
*cap += sizeof(uint32_t);
return num;
}
static __inline int
_ti_decode_num(const char **cap, int rtype)
{
if (rtype == TERMINFO_RTYPE_O1) {
return _ti_decode_16(cap);
} else {
return _ti_decode_32(cap);
}
}
static __inline void
_ti_encode_16(char **cap, size_t num)
{
_DIAGASSERT(num <= UINT16_MAX);
le16enc(*cap, (uint16_t)num);
*cap += sizeof(uint16_t);
}
static __inline void
_ti_encode_32(char **cap, size_t num)
{
_DIAGASSERT(num <= UINT32_MAX);
le32enc(*cap, (uint32_t)num);
*cap += sizeof(uint32_t);
}
static __inline void
_ti_encode_str(char **cap, const void *buf, size_t len)
{
memcpy(*cap, buf, len);
*cap += len;
}
static __inline void
_ti_encode_count_str(char **cap, const char *name, size_t len)
{
_ti_encode_16(cap, (uint16_t)len);
if (name == NULL)
return;
_ti_encode_str(cap, name, len);
}
static __inline void
_ti_encode_buf_16(TBUF *tbuf, size_t num)
{
_DIAGASSERT(num <= UINT16_MAX);
le16enc(tbuf->buf + tbuf->bufpos, (uint16_t)num);
tbuf->bufpos += sizeof(uint16_t);
}
static __inline void
_ti_encode_buf_32(TBUF *tbuf, size_t num)
{
_DIAGASSERT(num <= UINT32_MAX);
le32enc(tbuf->buf + tbuf->bufpos, (uint32_t)num);
tbuf->bufpos += sizeof(uint32_t);
}
static __inline void
_ti_encode_buf_count_str(TBUF *tbuf, const void *buf, size_t len)
{
_ti_encode_buf_16(tbuf, len);
memcpy(tbuf->buf + tbuf->bufpos, buf, len);
tbuf->bufpos += len;
}
static __inline void
_ti_encode_buf_num(TBUF *tbuf, int num, int rtype)
{
if (rtype == TERMINFO_RTYPE_O1) {
if (num > INT16_MAX)
num = INT16_MAX;
_ti_encode_buf_16(tbuf, (uint16_t)num);
} else {
if (num > INT32_MAX)
num = INT32_MAX;
_ti_encode_buf_32(tbuf, (uint32_t)num);
}
}
#endif