Home | History | Annotate | Line # | Download | only in libterminfo
term_private.h revision 1.18
      1  1.18       roy /* $NetBSD: term_private.h,v 1.18 2020/03/29 21:46:22 roy Exp $ */
      2   1.1       roy 
      3   1.1       roy /*
      4  1.12       roy  * Copyright (c) 2009, 2010, 2013, 2020 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 #ifndef _TERM_PRIVATE_H_
     31   1.1       roy #define	_TERM_PRIVATE_H_
     32   1.1       roy 
     33   1.1       roy /* This header should only be used by libterminfo, tic and infocmp. */
     34   1.1       roy 
     35   1.1       roy /* The terminfo database structure is private to us,
     36   1.1       roy  * so it's documented here.
     37  1.12       roy  *
     38  1.12       roy  * Version 1 - types 1 and 2.
     39   1.1       roy  * terminfo defines the largest number as 32767 and the largest
     40  1.12       roy  * compiled entry as 4093 bytes long. Negative numbers are not allowed.
     41   1.1       roy  * Thus, we store all numbers as uint16_t, including string length.
     42  1.12       roy  * We reserve negative numbers -1 and -2 to mean absent or cancelled.
     43   1.1       roy  * All strings are prefixed by length, including the null terminator.
     44   1.1       roy  * The largest string length we can handle is 65535 bytes,
     45   1.1       roy  * including the null terminator.
     46   1.2       snj  * The largest capability block we can handle is 65535 bytes.
     47  1.10     joerg  *
     48  1.12       roy  * Version 2 - type 3
     49  1.12       roy  * Extends terminfo numbers upto 2147483647 by storing the value as a uint32_t.
     50  1.12       roy  * This means that we exceed the current terminfo defined limits in every way.
     51  1.12       roy  *
     52  1.12       roy  * Type 1 capabilities are defined as:
     53  1.10     joerg  * header byte (always 1)
     54  1.10     joerg  * name
     55   1.1       roy  * description,
     56   1.1       roy  * cap length, num flags, index, char,
     57   1.1       roy  * cap length, num numbers, index, number,
     58   1.1       roy  * cap length, num strings, index, string,
     59  1.10     joerg  * cap length, num undefined caps, name, type (char), flag, number, string
     60  1.10     joerg  *
     61  1.12       roy  * Type 2 entries are aliases and defined as:
     62  1.10     joerg  * header byte (always 2)
     63  1.10     joerg  * 32bit id of the corresponding terminal in the file
     64  1.10     joerg  * name
     65  1.10     joerg  *
     66  1.12       roy  * Type 3 extends Type 1 so that it can store terminfo numbers
     67  1.12       roy  * as uint32_t. All other numerics are still stored as uint16_t.
     68  1.12       roy  *
     69  1.10     joerg  * The database itself is created using cdbw(3) and the numbers are
     70   1.1       roy  * always stored as little endian.
     71   1.1       roy  */
     72   1.1       roy 
     73   1.1       roy #include <sys/types.h>
     74  1.14  christos #include <assert.h>
     75  1.14  christos #include <limits.h>
     76   1.1       roy 
     77   1.1       roy #define _TERMINFO
     78  1.12       roy #define TERMINFO_RTYPE_O1	1
     79  1.12       roy #define TERMINFO_ALIAS		2
     80  1.12       roy #define TERMINFO_RTYPE		3
     81   1.1       roy 
     82  1.17       roy /* , and | are the two print characters now allowed
     83  1.17       roy  * in terminfo aliases or long descriptions.
     84  1.17       roy  * As | is generally used to delimit aliases inside the
     85  1.17       roy  * description, we use a comma. */
     86  1.17       roy #define TERMINFO_VDELIM		','
     87  1.17       roy #define TERMINFO_VDELIMSTR	","
     88  1.17       roy 
     89   1.1       roy /* We use the same ncurses tic macros so that our data is identical
     90   1.1       roy  * when a caller uses the long name macros to access te terminfo data
     91   1.1       roy  * directly. */
     92   1.1       roy #define ABSENT_BOOLEAN		((signed char)-1)       /* 255 */
     93   1.1       roy #define ABSENT_NUMERIC		(-1)
     94   1.1       roy #define ABSENT_STRING		(char *)0
     95   1.1       roy #define CANCELLED_BOOLEAN	((signed char)-2)       /* 254 */
     96   1.1       roy #define CANCELLED_NUMERIC	(-2)
     97   1.1       roy #define CANCELLED_STRING	(char *)(-1)
     98   1.1       roy #define VALID_BOOLEAN(s) ((unsigned char)(s) <= 1)	/* reject "-1" */
     99   1.1       roy #define VALID_NUMERIC(s) ((s) >= 0)
    100   1.1       roy #define VALID_STRING(s)  ((s) != CANCELLED_STRING && (s) != ABSENT_STRING)
    101   1.1       roy 
    102   1.8       roy typedef struct {
    103   1.1       roy 	const char *id;
    104   1.1       roy 	char type;
    105   1.1       roy 	char flag;
    106  1.12       roy 	int num;
    107   1.1       roy 	const char *str;
    108   1.1       roy } TERMUSERDEF;
    109   1.1       roy 
    110   1.8       roy typedef struct {
    111   1.1       roy 	int fildes;
    112   1.1       roy 	/* We need to expose these so that the macros work */
    113   1.5       roy 	const char *name;
    114   1.5       roy 	const char *desc;
    115   1.4        he 	signed char *flags;
    116  1.12       roy 	int *nums;
    117   1.1       roy 	const char **strs;
    118   1.1       roy 	/* Storage area for terminfo data */
    119   1.1       roy 	char *_area;
    120   1.5       roy 	size_t _arealen;
    121   1.1       roy 	size_t _nuserdefs;
    122   1.1       roy 	TERMUSERDEF *_userdefs;
    123   1.1       roy 	/* So we don't rely on the global ospeed */
    124   1.1       roy 	short _ospeed;
    125   1.1       roy 	/* Output buffer for tparm */
    126   1.1       roy 	char *_buf;
    127   1.1       roy 	size_t _buflen;
    128   1.1       roy 	size_t _bufpos;
    129   1.1       roy 	/* A-Z static variables for tparm  */
    130   1.1       roy 	long _snums[26];
    131   1.3       roy 	/* aliases of the terminal, | separated */
    132   1.5       roy 	const char *_alias;
    133   1.1       roy } TERMINAL;
    134   1.1       roy 
    135   1.1       roy extern const char *	_ti_database;
    136   1.1       roy 
    137   1.1       roy ssize_t		_ti_flagindex(const char *);
    138   1.1       roy ssize_t		_ti_numindex(const char *);
    139   1.1       roy ssize_t		_ti_strindex(const char *);
    140   1.1       roy const char *	_ti_flagid(ssize_t);
    141   1.1       roy const char *	_ti_numid(ssize_t);
    142   1.1       roy const char *	_ti_strid(ssize_t);
    143   1.1       roy int		_ti_getterm(TERMINAL *, const char *, int);
    144   1.1       roy void		_ti_setospeed(TERMINAL *);
    145   1.1       roy 
    146   1.6       roy /* libterminfo can compile terminfo strings too */
    147   1.6       roy #define TIC_WARNING	(1 << 0)
    148   1.6       roy #define TIC_DESCRIPTION	(1 << 1)
    149   1.6       roy #define TIC_ALIAS	(1 << 2)
    150   1.6       roy #define TIC_COMMENT	(1 << 3)
    151   1.6       roy #define TIC_EXTRA	(1 << 4)
    152  1.13  christos #define TIC_COMPAT_V1	(1 << 5)
    153   1.6       roy 
    154   1.8       roy typedef struct {
    155   1.6       roy 	char *buf;
    156   1.6       roy 	size_t buflen;
    157   1.6       roy 	size_t bufpos;
    158   1.6       roy 	size_t entries;
    159   1.6       roy } TBUF;
    160   1.6       roy 
    161   1.8       roy typedef struct {
    162   1.6       roy 	char *name;
    163   1.6       roy 	char *alias;
    164   1.6       roy 	char *desc;
    165  1.13  christos 	int  rtype;
    166   1.6       roy 	TBUF flags;
    167   1.6       roy 	TBUF nums;
    168   1.6       roy 	TBUF strs;
    169   1.6       roy 	TBUF extras;
    170   1.6       roy } TIC;
    171   1.6       roy 
    172  1.13  christos #define _ti_numsize(tic) \
    173  1.13  christos     ((tic)->rtype == TERMINFO_RTYPE_O1 ? sizeof(uint16_t) : sizeof(uint32_t))
    174  1.13  christos 
    175  1.18       roy int _ti_promote(TIC *);
    176   1.6       roy char *_ti_grow_tbuf(TBUF *, size_t);
    177   1.7       roy char *_ti_get_token(char **, char);
    178  1.14  christos const char *_ti_find_cap(TIC *, TBUF *, char,  short);
    179  1.14  christos const char *_ti_find_extra(TIC *, TBUF *, const char *);
    180  1.13  christos char *_ti_getname(int, const char *);
    181  1.13  christos size_t _ti_store_extra(TIC *, int, const char *, char, char, int,
    182  1.13  christos     const char *, size_t, int);
    183   1.6       roy TIC *_ti_compile(char *, int);
    184   1.6       roy ssize_t _ti_flatten(uint8_t **, const TIC *);
    185   1.6       roy void _ti_freetic(TIC *);
    186  1.11       roy 
    187  1.14  christos int _ti_encode_buf_id_num(TBUF *, int, int, size_t);
    188  1.14  christos int _ti_encode_buf_id_count_str(TBUF *, int, const void *, size_t);
    189  1.14  christos int _ti_encode_buf_id_flags(TBUF *, int, int);
    190  1.14  christos 
    191  1.11       roy #define TPARM_MAX 9	/* not likely to change */
    192  1.11       roy int _ti_parm_analyse(const char *, int *, int);
    193  1.14  christos 
    194  1.14  christos static __inline int
    195  1.14  christos _ti_decode_16(const char **cap)
    196  1.14  christos {
    197  1.16       roy 	int num = (int16_t)le16dec(*cap);
    198  1.14  christos 
    199  1.14  christos 	*cap += sizeof(uint16_t);
    200  1.14  christos 	return num;
    201  1.14  christos }
    202  1.15       roy 
    203  1.14  christos static __inline int
    204  1.14  christos _ti_decode_32(const char **cap)
    205  1.14  christos {
    206  1.16       roy 	int num = (int32_t)le32dec(*cap);
    207  1.14  christos 
    208  1.14  christos 	*cap += sizeof(uint32_t);
    209  1.14  christos 	return num;
    210  1.14  christos }
    211  1.14  christos 
    212  1.14  christos static __inline int
    213  1.14  christos _ti_decode_num(const char **cap, int rtype)
    214  1.14  christos {
    215  1.14  christos 	if (rtype == TERMINFO_RTYPE_O1) {
    216  1.14  christos 		return _ti_decode_16(cap);
    217  1.14  christos 	} else {
    218  1.14  christos 		return _ti_decode_32(cap);
    219  1.14  christos 	}
    220  1.14  christos }
    221  1.14  christos 
    222  1.14  christos static __inline void
    223  1.14  christos _ti_encode_16(char **cap, size_t num)
    224  1.14  christos {
    225  1.16       roy 	_DIAGASSERT(num <= UINT16_MAX);
    226  1.14  christos 	le16enc(*cap, (uint16_t)num);
    227  1.14  christos 	*cap += sizeof(uint16_t);
    228  1.14  christos }
    229  1.15       roy 
    230  1.14  christos static __inline void
    231  1.14  christos _ti_encode_32(char **cap, size_t num)
    232  1.14  christos {
    233  1.16       roy 	_DIAGASSERT(num <= UINT32_MAX);
    234  1.14  christos 	le32enc(*cap, (uint32_t)num);
    235  1.14  christos 	*cap += sizeof(uint32_t);
    236  1.14  christos }
    237  1.14  christos 
    238  1.14  christos static __inline void
    239  1.14  christos _ti_encode_str(char **cap, const void *buf, size_t len)
    240  1.14  christos {
    241  1.14  christos 	memcpy(*cap, buf, len);
    242  1.14  christos 	*cap += len;
    243  1.14  christos }
    244  1.14  christos 
    245  1.14  christos static __inline void
    246  1.14  christos _ti_encode_count_str(char **cap, const char *name, size_t len)
    247  1.14  christos {
    248  1.14  christos 	_ti_encode_16(cap, (uint16_t)len);
    249  1.14  christos 	if (name == NULL)
    250  1.14  christos 		return;
    251  1.14  christos 	_ti_encode_str(cap, name, len);
    252  1.14  christos }
    253  1.14  christos 
    254  1.14  christos static __inline void
    255  1.14  christos _ti_encode_buf_16(TBUF *tbuf, size_t num)
    256  1.14  christos {
    257  1.16       roy 	_DIAGASSERT(num <= UINT16_MAX);
    258  1.14  christos 	le16enc(tbuf->buf + tbuf->bufpos, (uint16_t)num);
    259  1.14  christos 	tbuf->bufpos += sizeof(uint16_t);
    260  1.14  christos }
    261  1.14  christos 
    262  1.14  christos static __inline void
    263  1.14  christos _ti_encode_buf_32(TBUF *tbuf, size_t num)
    264  1.14  christos {
    265  1.16       roy 	_DIAGASSERT(num <= UINT32_MAX);
    266  1.14  christos 	le32enc(tbuf->buf + tbuf->bufpos, (uint32_t)num);
    267  1.14  christos 	tbuf->bufpos += sizeof(uint32_t);
    268  1.14  christos }
    269  1.14  christos 
    270  1.14  christos static __inline void
    271  1.14  christos _ti_encode_buf_count_str(TBUF *tbuf, const void *buf, size_t len)
    272  1.14  christos {
    273  1.14  christos 	_ti_encode_buf_16(tbuf, len);
    274  1.14  christos 	memcpy(tbuf->buf + tbuf->bufpos, buf, len);
    275  1.14  christos 	tbuf->bufpos += len;
    276  1.14  christos }
    277  1.14  christos 
    278  1.14  christos static __inline void
    279  1.14  christos _ti_encode_buf_num(TBUF *tbuf, size_t num, int rtype)
    280  1.14  christos {
    281  1.14  christos 	if (rtype == TERMINFO_RTYPE_O1) {
    282  1.16       roy 		if (num > INT16_MAX)
    283  1.16       roy 			num = INT16_MAX;
    284  1.14  christos 		_ti_encode_buf_16(tbuf, num);
    285  1.14  christos 	} else {
    286  1.14  christos 		_ti_encode_buf_32(tbuf, num);
    287  1.14  christos 	}
    288  1.14  christos }
    289  1.14  christos 
    290   1.1       roy #endif
    291