Home | History | Annotate | Line # | Download | only in libterminfo
term_private.h revision 1.12
      1 /* $NetBSD: term_private.h,v 1.12 2020/03/13 15:19:25 roy Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2009, 2010, 2013, 2020 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 #ifndef _TERM_PRIVATE_H_
     31 #define	_TERM_PRIVATE_H_
     32 
     33 /* This header should only be used by libterminfo, tic and infocmp. */
     34 
     35 /* The terminfo database structure is private to us,
     36  * so it's documented here.
     37  *
     38  * Version 1 - types 1 and 2.
     39  * terminfo defines the largest number as 32767 and the largest
     40  * compiled entry as 4093 bytes long. Negative numbers are not allowed.
     41  * Thus, we store all numbers as uint16_t, including string length.
     42  * We reserve negative numbers -1 and -2 to mean absent or cancelled.
     43  * All strings are prefixed by length, including the null terminator.
     44  * The largest string length we can handle is 65535 bytes,
     45  * including the null terminator.
     46  * The largest capability block we can handle is 65535 bytes.
     47  *
     48  * Version 2 - type 3
     49  * Extends terminfo numbers upto 2147483647 by storing the value as a uint32_t.
     50  * This means that we exceed the current terminfo defined limits in every way.
     51  *
     52  * Type 1 capabilities are defined as:
     53  * header byte (always 1)
     54  * name
     55  * description,
     56  * cap length, num flags, index, char,
     57  * cap length, num numbers, index, number,
     58  * cap length, num strings, index, string,
     59  * cap length, num undefined caps, name, type (char), flag, number, string
     60  *
     61  * Type 2 entries are aliases and defined as:
     62  * header byte (always 2)
     63  * 32bit id of the corresponding terminal in the file
     64  * name
     65  *
     66  * Type 3 extends Type 1 so that it can store terminfo numbers
     67  * as uint32_t. All other numerics are still stored as uint16_t.
     68  *
     69  * The database itself is created using cdbw(3) and the numbers are
     70  * always stored as little endian.
     71  */
     72 
     73 #include <sys/types.h>
     74 
     75 #define _TERMINFO
     76 #define TERMINFO_RTYPE_O1	1
     77 #define TERMINFO_ALIAS		2
     78 #define TERMINFO_RTYPE		3
     79 
     80 /* We use the same ncurses tic macros so that our data is identical
     81  * when a caller uses the long name macros to access te terminfo data
     82  * directly. */
     83 #define ABSENT_BOOLEAN		((signed char)-1)       /* 255 */
     84 #define ABSENT_NUMERIC		(-1)
     85 #define ABSENT_STRING		(char *)0
     86 #define CANCELLED_BOOLEAN	((signed char)-2)       /* 254 */
     87 #define CANCELLED_NUMERIC	(-2)
     88 #define CANCELLED_STRING	(char *)(-1)
     89 #define VALID_BOOLEAN(s) ((unsigned char)(s) <= 1)	/* reject "-1" */
     90 #define VALID_NUMERIC(s) ((s) >= 0)
     91 #define VALID_STRING(s)  ((s) != CANCELLED_STRING && (s) != ABSENT_STRING)
     92 
     93 typedef struct {
     94 	const char *id;
     95 	char type;
     96 	char flag;
     97 	int num;
     98 	const char *str;
     99 } TERMUSERDEF;
    100 
    101 typedef struct {
    102 	int fildes;
    103 	/* We need to expose these so that the macros work */
    104 	const char *name;
    105 	const char *desc;
    106 	signed char *flags;
    107 	int *nums;
    108 	const char **strs;
    109 	/* Storage area for terminfo data */
    110 	char *_area;
    111 	size_t _arealen;
    112 	size_t _nuserdefs;
    113 	TERMUSERDEF *_userdefs;
    114 	/* So we don't rely on the global ospeed */
    115 	short _ospeed;
    116 	/* Output buffer for tparm */
    117 	char *_buf;
    118 	size_t _buflen;
    119 	size_t _bufpos;
    120 	/* A-Z static variables for tparm  */
    121 	long _snums[26];
    122 	/* aliases of the terminal, | separated */
    123 	const char *_alias;
    124 } TERMINAL;
    125 
    126 extern const char *	_ti_database;
    127 
    128 ssize_t		_ti_flagindex(const char *);
    129 ssize_t		_ti_numindex(const char *);
    130 ssize_t		_ti_strindex(const char *);
    131 const char *	_ti_flagid(ssize_t);
    132 const char *	_ti_numid(ssize_t);
    133 const char *	_ti_strid(ssize_t);
    134 int		_ti_getterm(TERMINAL *, const char *, int);
    135 void		_ti_setospeed(TERMINAL *);
    136 
    137 /* libterminfo can compile terminfo strings too */
    138 #define TIC_WARNING	(1 << 0)
    139 #define TIC_DESCRIPTION	(1 << 1)
    140 #define TIC_ALIAS	(1 << 2)
    141 #define TIC_COMMENT	(1 << 3)
    142 #define TIC_EXTRA	(1 << 4)
    143 
    144 #define UINT16_T_MAX 0xffff
    145 
    146 typedef struct {
    147 	char *buf;
    148 	size_t buflen;
    149 	size_t bufpos;
    150 	size_t entries;
    151 } TBUF;
    152 
    153 typedef struct {
    154 	char *name;
    155 	char *alias;
    156 	char *desc;
    157 	TBUF flags;
    158 	TBUF nums;
    159 	TBUF strs;
    160 	TBUF extras;
    161 } TIC;
    162 
    163 char *_ti_grow_tbuf(TBUF *, size_t);
    164 char *_ti_get_token(char **, char);
    165 char *_ti_find_cap(TBUF *, char,  short);
    166 char *_ti_find_extra(TBUF *, const char *);
    167 size_t _ti_store_extra(TIC *, int, char *, char, char, int,
    168     char *, size_t, int);
    169 TIC *_ti_compile(char *, int);
    170 ssize_t _ti_flatten(uint8_t **, const TIC *);
    171 void _ti_freetic(TIC *);
    172 
    173 #define TPARM_MAX 9	/* not likely to change */
    174 int _ti_parm_analyse(const char *, int *, int);
    175 #endif
    176