Home | History | Annotate | Line # | Download | only in libterminfo
compile.c revision 1.21
      1  1.21       roy /* $NetBSD: compile.c,v 1.21 2020/03/29 18:54:57 roy Exp $ */
      2   1.1       roy 
      3   1.1       roy /*
      4  1.14       roy  * Copyright (c) 2009, 2010, 2011, 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 #if HAVE_NBTOOL_CONFIG_H
     31   1.1       roy #include "nbtool_config.h"
     32   1.1       roy #endif
     33   1.1       roy 
     34   1.1       roy #include <sys/cdefs.h>
     35  1.21       roy __RCSID("$NetBSD: compile.c,v 1.21 2020/03/29 18:54:57 roy Exp $");
     36   1.3  dholland 
     37   1.3  dholland #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
     38   1.3  dholland #include <sys/endian.h>
     39   1.3  dholland #endif
     40   1.1       roy 
     41   1.1       roy #include <assert.h>
     42   1.1       roy #include <ctype.h>
     43   1.1       roy #include <err.h>
     44   1.1       roy #include <errno.h>
     45   1.1       roy #include <limits.h>
     46   1.1       roy #include <stdarg.h>
     47   1.1       roy #include <stdlib.h>
     48   1.1       roy #include <stdint.h>
     49   1.1       roy #include <stdio.h>
     50   1.1       roy #include <string.h>
     51   1.1       roy #include <term_private.h>
     52   1.1       roy #include <term.h>
     53   1.1       roy 
     54   1.6     joerg static void __printflike(2, 3)
     55   1.1       roy dowarn(int flags, const char *fmt, ...)
     56   1.1       roy {
     57   1.1       roy 	va_list va;
     58   1.1       roy 
     59   1.1       roy 	errno = EINVAL;
     60   1.1       roy 	if (flags & TIC_WARNING) {
     61   1.1       roy 		va_start(va, fmt);
     62   1.1       roy 		vwarnx(fmt, va);
     63   1.1       roy 		va_end(va);
     64   1.1       roy 	}
     65   1.1       roy }
     66   1.1       roy 
     67   1.1       roy char *
     68   1.1       roy _ti_grow_tbuf(TBUF *tbuf, size_t len)
     69   1.1       roy {
     70   1.1       roy 	char *buf;
     71   1.1       roy 	size_t l;
     72   1.1       roy 
     73   1.1       roy 	_DIAGASSERT(tbuf != NULL);
     74   1.1       roy 
     75   1.1       roy 	l = tbuf->bufpos + len;
     76   1.1       roy 	if (l > tbuf->buflen) {
     77   1.7     joerg 		if (tbuf->buflen == 0)
     78   1.1       roy 			buf = malloc(l);
     79   1.1       roy 		else
     80   1.1       roy 			buf = realloc(tbuf->buf, l);
     81   1.1       roy 		if (buf == NULL)
     82   1.1       roy 			return NULL;
     83   1.1       roy 		tbuf->buf = buf;
     84   1.1       roy 		tbuf->buflen = l;
     85   1.1       roy 	}
     86   1.1       roy 	return tbuf->buf;
     87   1.1       roy }
     88   1.1       roy 
     89  1.16  christos const char *
     90  1.15  christos _ti_find_cap(TIC *tic, TBUF *tbuf, char type, short ind)
     91   1.1       roy {
     92   1.1       roy 	size_t n;
     93  1.12       roy 	uint16_t num;
     94  1.16  christos 	const char *cap;
     95   1.1       roy 
     96   1.1       roy 	_DIAGASSERT(tbuf != NULL);
     97   1.1       roy 
     98   1.1       roy 	cap = tbuf->buf;
     99   1.1       roy 	for (n = tbuf->entries; n > 0; n--) {
    100  1.16  christos 		num = _ti_decode_16(&cap);
    101  1.12       roy 		if ((short)num == ind)
    102   1.1       roy 			return cap;
    103   1.1       roy 		switch (type) {
    104   1.1       roy 		case 'f':
    105   1.1       roy 			cap++;
    106   1.1       roy 			break;
    107   1.1       roy 		case 'n':
    108  1.15  christos 			cap += _ti_numsize(tic);
    109   1.1       roy 			break;
    110   1.1       roy 		case 's':
    111  1.16  christos 			num = _ti_decode_16(&cap);
    112   1.1       roy 			cap += num;
    113   1.1       roy 			break;
    114   1.1       roy 		}
    115   1.1       roy 	}
    116   1.9       roy 
    117   1.1       roy 	errno = ESRCH;
    118   1.1       roy 	return NULL;
    119   1.1       roy }
    120   1.1       roy 
    121  1.16  christos const char *
    122  1.15  christos _ti_find_extra(TIC *tic, TBUF *tbuf, const char *code)
    123   1.1       roy {
    124   1.1       roy 	size_t n;
    125  1.12       roy 	uint16_t num;
    126  1.16  christos 	const char *cap;
    127   1.1       roy 
    128   1.1       roy 	_DIAGASSERT(tbuf != NULL);
    129   1.1       roy 	_DIAGASSERT(code != NULL);
    130   1.1       roy 
    131   1.1       roy 	cap = tbuf->buf;
    132   1.1       roy 	for (n = tbuf->entries; n > 0; n--) {
    133  1.16  christos 		num = _ti_decode_16(&cap);
    134   1.1       roy 		if (strcmp(cap, code) == 0)
    135   1.1       roy 			return cap + num;
    136   1.1       roy 		cap += num;
    137   1.1       roy 		switch (*cap++) {
    138   1.1       roy 		case 'f':
    139   1.1       roy 			cap++;
    140   1.1       roy 			break;
    141   1.1       roy 		case 'n':
    142  1.15  christos 			cap += _ti_numsize(tic);
    143   1.1       roy 			break;
    144   1.1       roy 		case 's':
    145  1.16  christos 			num = _ti_decode_16(&cap);
    146   1.1       roy 			cap += num;
    147   1.1       roy 			break;
    148   1.1       roy 		}
    149   1.1       roy 	}
    150   1.9       roy 
    151   1.1       roy 	errno = ESRCH;
    152   1.1       roy 	return NULL;
    153   1.1       roy }
    154   1.1       roy 
    155  1.15  christos char *
    156  1.15  christos _ti_getname(int rtype, const char *orig)
    157  1.15  christos {
    158  1.21       roy 	const char *delim;
    159  1.15  christos 	char *name;
    160  1.21       roy 	const char *verstr;
    161  1.21       roy 	size_t diff, vlen;
    162  1.15  christos 
    163  1.21       roy 	switch (rtype) {
    164  1.21       roy 	case TERMINFO_RTYPE:
    165  1.21       roy 		verstr = TERMINFO_VDELIMSTR "v3";
    166  1.21       roy 		break;
    167  1.21       roy 	case TERMINFO_RTYPE_O1:
    168  1.21       roy 		verstr = "";
    169  1.21       roy 		break;
    170  1.21       roy 	default:
    171  1.21       roy 		errno = EINVAL;
    172  1.21       roy 		return NULL;
    173  1.15  christos 	}
    174  1.21       roy 
    175  1.21       roy 	delim = orig;
    176  1.21       roy 	while (*delim != '\0' && *delim != TERMINFO_VDELIM)
    177  1.21       roy 		delim++;
    178  1.21       roy 	diff = delim - orig;
    179  1.21       roy 	vlen = strlen(verstr);
    180  1.21       roy 	name = malloc(diff + vlen + 1);
    181  1.21       roy 	if (name == NULL)
    182  1.21       roy 		return NULL;
    183  1.21       roy 
    184  1.21       roy 	memcpy(name, orig, diff);
    185  1.21       roy 	memcpy(name + diff, verstr, vlen + 1);
    186  1.15  christos 	return name;
    187  1.15  christos }
    188  1.15  christos 
    189   1.1       roy size_t
    190  1.15  christos _ti_store_extra(TIC *tic, int wrn, const char *id, char type, char flag,
    191  1.15  christos     int num, const char *str, size_t strl, int flags)
    192   1.1       roy {
    193   1.1       roy 	size_t l;
    194   1.1       roy 
    195   1.1       roy 	_DIAGASSERT(tic != NULL);
    196   1.1       roy 
    197   1.1       roy 	if (strcmp(id, "use") != 0) {
    198  1.15  christos 		if (_ti_find_extra(tic, &tic->extras, id) != NULL)
    199   1.1       roy 			return 0;
    200   1.1       roy 		if (!(flags & TIC_EXTRA)) {
    201   1.1       roy 			if (wrn != 0)
    202   1.1       roy 				dowarn(flags, "%s: %s: unknown capability",
    203   1.1       roy 				    tic->name, id);
    204   1.1       roy 			return 0;
    205   1.1       roy 		}
    206   1.1       roy 	}
    207   1.9       roy 
    208   1.1       roy 	l = strlen(id) + 1;
    209   1.1       roy 	if (l > UINT16_T_MAX) {
    210   1.1       roy 		dowarn(flags, "%s: %s: cap name is too long", tic->name, id);
    211   1.1       roy 		return 0;
    212   1.1       roy 	}
    213   1.9       roy 
    214   1.1       roy 	if (!_ti_grow_tbuf(&tic->extras,
    215  1.15  christos 		l + strl + sizeof(uint16_t) + _ti_numsize(tic) + 1))
    216   1.1       roy 		return 0;
    217  1.16  christos 	_ti_encode_buf_count_str(&tic->extras, id, l);
    218   1.1       roy 	tic->extras.buf[tic->extras.bufpos++] = type;
    219   1.1       roy 	switch (type) {
    220   1.1       roy 	case 'f':
    221   1.1       roy 		tic->extras.buf[tic->extras.bufpos++] = flag;
    222   1.1       roy 		break;
    223   1.1       roy 	case 'n':
    224  1.16  christos 		_ti_encode_buf_num(&tic->extras, num, tic->rtype);
    225   1.1       roy 		break;
    226   1.1       roy 	case 's':
    227  1.16  christos 		_ti_encode_buf_count_str(&tic->extras, str, strl);
    228   1.1       roy 		break;
    229   1.1       roy 	}
    230   1.1       roy 	tic->extras.entries++;
    231   1.1       roy 	return 1;
    232   1.1       roy }
    233   1.1       roy 
    234  1.16  christos static void
    235  1.16  christos _ti_encode_buf(char **cap, const TBUF *buf)
    236  1.16  christos {
    237  1.16  christos 	if (buf->entries == 0) {
    238  1.16  christos 		_ti_encode_16(cap, 0);
    239  1.16  christos 	} else {
    240  1.16  christos 		_ti_encode_16(cap, buf->bufpos + sizeof(uint16_t));
    241  1.16  christos 		_ti_encode_16(cap, buf->entries);
    242  1.16  christos 		_ti_encode_str(cap, buf->buf, buf->bufpos);
    243  1.16  christos 	}
    244  1.16  christos }
    245  1.16  christos 
    246   1.1       roy ssize_t
    247   1.1       roy _ti_flatten(uint8_t **buf, const TIC *tic)
    248   1.1       roy {
    249   1.1       roy 	size_t buflen, len, alen, dlen;
    250  1.16  christos 	char *cap;
    251   1.1       roy 
    252   1.1       roy 	_DIAGASSERT(buf != NULL);
    253   1.1       roy 	_DIAGASSERT(tic != NULL);
    254   1.1       roy 
    255   1.1       roy 	len = strlen(tic->name) + 1;
    256   1.1       roy 	if (tic->alias == NULL)
    257   1.1       roy 		alen = 0;
    258   1.1       roy 	else
    259   1.1       roy 		alen = strlen(tic->alias) + 1;
    260   1.1       roy 	if (tic->desc == NULL)
    261   1.1       roy 		dlen = 0;
    262   1.1       roy 	else
    263   1.1       roy 		dlen = strlen(tic->desc) + 1;
    264  1.16  christos 
    265   1.1       roy 	buflen = sizeof(char) +
    266   1.1       roy 	    sizeof(uint16_t) + len +
    267   1.1       roy 	    sizeof(uint16_t) + alen +
    268   1.1       roy 	    sizeof(uint16_t) + dlen +
    269   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->flags.bufpos +
    270   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->nums.bufpos +
    271   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->strs.bufpos +
    272   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->extras.bufpos;
    273  1.16  christos 
    274   1.1       roy 	*buf = malloc(buflen);
    275   1.1       roy 	if (*buf == NULL)
    276   1.1       roy 		return -1;
    277   1.9       roy 
    278  1.16  christos 	cap = (char *)*buf;
    279  1.15  christos 	*cap++ = tic->rtype;
    280   1.9       roy 
    281  1.16  christos 	_ti_encode_count_str(&cap, tic->name, len);
    282  1.16  christos 	_ti_encode_count_str(&cap, tic->alias, alen);
    283  1.16  christos 	_ti_encode_count_str(&cap, tic->desc, dlen);
    284   1.9       roy 
    285  1.16  christos 	_ti_encode_buf(&cap, &tic->flags);
    286   1.9       roy 
    287  1.16  christos 	_ti_encode_buf(&cap, &tic->nums);
    288  1.16  christos 	_ti_encode_buf(&cap, &tic->strs);
    289  1.16  christos 	_ti_encode_buf(&cap, &tic->extras);
    290   1.1       roy 
    291  1.16  christos 	return (uint8_t *)cap - *buf;
    292   1.1       roy }
    293   1.1       roy 
    294   1.1       roy static int
    295   1.1       roy encode_string(const char *term, const char *cap, TBUF *tbuf, const char *str,
    296   1.1       roy     int flags)
    297   1.1       roy {
    298   1.1       roy 	int slash, i, num;
    299   1.1       roy 	char ch, *p, *s, last;
    300   1.9       roy 
    301   1.1       roy 	if (_ti_grow_tbuf(tbuf, strlen(str) + 1) == NULL)
    302   1.1       roy 		return -1;
    303   1.1       roy 	p = s = tbuf->buf + tbuf->bufpos;
    304   1.1       roy 	slash = 0;
    305   1.1       roy 	last = '\0';
    306   1.1       roy 	/* Convert escape codes */
    307   1.1       roy 	while ((ch = *str++) != '\0') {
    308  1.10       roy 		if (ch == '\n') {
    309  1.10       roy 			/* Following a newline, strip leading whitespace from
    310  1.10       roy 			 * capability strings. */
    311  1.10       roy 			while (isspace((unsigned char)*str))
    312  1.10       roy 				str++;
    313  1.10       roy 			continue;
    314  1.10       roy 		}
    315   1.1       roy 		if (slash == 0 && ch == '\\') {
    316   1.1       roy 			slash = 1;
    317   1.1       roy 			continue;
    318   1.1       roy 		}
    319   1.1       roy 		if (slash == 0) {
    320   1.1       roy 			if (last != '%' && ch == '^') {
    321   1.1       roy 				ch = *str++;
    322   1.1       roy 				if (((unsigned char)ch) >= 128)
    323   1.1       roy 					dowarn(flags,
    324   1.1       roy 					    "%s: %s: illegal ^ character",
    325   1.1       roy 					    term, cap);
    326   1.1       roy 				if (ch == '\0')
    327   1.1       roy 					break;
    328   1.1       roy 				if (ch == '?')
    329   1.1       roy 					ch = '\177';
    330   1.1       roy 				else if ((ch &= 037) == 0)
    331   1.5       roy 					ch = (char)128;
    332  1.11       roy 			} else if (!isprint((unsigned char)ch))
    333  1.11       roy 				dowarn(flags,
    334  1.11       roy 				    "%s: %s: unprintable character",
    335  1.11       roy 				    term, cap);
    336   1.1       roy 			*p++ = ch;
    337   1.1       roy 			last = ch;
    338   1.1       roy 			continue;
    339   1.1       roy 		}
    340   1.1       roy 		slash = 0;
    341   1.1       roy 		if (ch >= '0' && ch <= '7') {
    342   1.1       roy 			num = ch - '0';
    343   1.1       roy 			for (i = 0; i < 2; i++) {
    344   1.1       roy 				if (*str < '0' || *str > '7') {
    345   1.1       roy 					if (isdigit((unsigned char)*str))
    346   1.1       roy 						dowarn(flags,
    347   1.1       roy 						    "%s: %s: non octal"
    348   1.1       roy 						    " digit", term, cap);
    349   1.1       roy 					else
    350   1.1       roy 						break;
    351   1.1       roy 				}
    352   1.1       roy 				num = num * 8 + *str++ - '0';
    353   1.1       roy 			}
    354   1.1       roy 			if (num == 0)
    355   1.1       roy 				num = 0200;
    356   1.1       roy 			*p++ = (char)num;
    357   1.1       roy 			continue;
    358   1.1       roy 		}
    359   1.1       roy 		switch (ch) {
    360   1.1       roy 		case 'a':
    361   1.1       roy 			*p++ = '\a';
    362   1.1       roy 			break;
    363   1.1       roy 		case 'b':
    364   1.1       roy 			*p++ = '\b';
    365   1.1       roy 			break;
    366   1.1       roy 		case 'e': /* FALLTHROUGH */
    367   1.1       roy 		case 'E':
    368   1.1       roy 			*p++ = '\033';
    369   1.1       roy 			break;
    370   1.1       roy 		case 'f':
    371   1.1       roy 			*p++ = '\014';
    372   1.1       roy 			break;
    373   1.1       roy 		case 'l': /* FALLTHROUGH */
    374   1.1       roy 		case 'n':
    375   1.1       roy 			*p++ = '\n';
    376   1.1       roy 			break;
    377   1.1       roy 		case 'r':
    378   1.1       roy 			*p++ = '\r';
    379   1.1       roy 			break;
    380   1.1       roy 		case 's':
    381   1.1       roy 			*p++ = ' ';
    382   1.1       roy 			break;
    383   1.1       roy 		case 't':
    384   1.1       roy 			*p++ = '\t';
    385   1.1       roy 			break;
    386   1.1       roy 		default:
    387   1.1       roy 			/* We should warn here */
    388   1.1       roy 		case '^':
    389   1.1       roy 		case ',':
    390   1.1       roy 		case ':':
    391   1.1       roy 		case '|':
    392   1.1       roy 			*p++ = ch;
    393   1.1       roy 			break;
    394   1.1       roy 		}
    395   1.1       roy 		last = ch;
    396   1.1       roy 	}
    397   1.1       roy 	*p++ = '\0';
    398  1.12       roy 	tbuf->bufpos += (size_t)(p - s);
    399   1.1       roy 	return 0;
    400   1.1       roy }
    401   1.1       roy 
    402   1.4       roy char *
    403   1.4       roy _ti_get_token(char **cap, char sep)
    404   1.1       roy {
    405   1.4       roy 	char esc, *token;
    406   1.1       roy 
    407   1.1       roy 	while (isspace((unsigned char)**cap))
    408   1.1       roy 		(*cap)++;
    409   1.1       roy 	if (**cap == '\0')
    410   1.1       roy 		return NULL;
    411   1.1       roy 
    412   1.1       roy 	/* We can't use stresep(3) as ^ we need two escape chars */
    413   1.4       roy 	esc = '\0';
    414   1.1       roy 	for (token = *cap;
    415   1.4       roy 	     **cap != '\0' && (esc != '\0' || **cap != sep);
    416   1.1       roy 	     (*cap)++)
    417   1.1       roy 	{
    418   1.4       roy 		if (esc == '\0') {
    419   1.1       roy 			if (**cap == '\\' || **cap == '^')
    420   1.4       roy 				esc = **cap;
    421   1.4       roy 		} else {
    422   1.4       roy 			/* termcap /E/ is valid */
    423   1.4       roy 			if (sep == ':' && esc == '\\' && **cap == 'E')
    424   1.4       roy 				esc = 'x';
    425   1.4       roy 			else
    426   1.4       roy 				esc = '\0';
    427   1.4       roy 		}
    428   1.1       roy 	}
    429   1.1       roy 
    430   1.1       roy 	if (**cap != '\0')
    431   1.1       roy 		*(*cap)++ = '\0';
    432   1.1       roy 
    433   1.1       roy 	return token;
    434   1.1       roy }
    435   1.1       roy 
    436  1.15  christos static int
    437  1.15  christos _ti_find_rtype(const char *cap)
    438  1.15  christos {
    439  1.17  christos 	const char *ptr;
    440  1.17  christos 
    441  1.17  christos 	for (ptr = cap; (ptr = strchr(ptr, '#')) != NULL;) {
    442  1.15  christos 		if (strtol(++ptr, NULL, 0) > SHRT_MAX) {
    443  1.15  christos 			return TERMINFO_RTYPE;
    444  1.15  christos 		}
    445  1.15  christos 	}
    446  1.15  christos 	return TERMINFO_RTYPE_O1;
    447  1.15  christos }
    448  1.15  christos 
    449  1.16  christos int
    450  1.16  christos _ti_encode_buf_id_num(TBUF *tbuf, int ind, int num, size_t len)
    451  1.16  christos {
    452  1.16  christos 	if (!_ti_grow_tbuf(tbuf, sizeof(uint16_t) + len))
    453  1.16  christos 		return 0;
    454  1.16  christos 	_ti_encode_buf_16(tbuf, ind);
    455  1.16  christos 	if (len == sizeof(uint32_t))
    456  1.16  christos 		_ti_encode_buf_32(tbuf, num);
    457  1.16  christos 	else
    458  1.16  christos 		_ti_encode_buf_16(tbuf, num);
    459  1.16  christos 	tbuf->entries++;
    460  1.16  christos 	return 1;
    461  1.16  christos }
    462  1.16  christos 
    463  1.16  christos int
    464  1.16  christos _ti_encode_buf_id_count_str(TBUF *tbuf, int ind, const void *buf, size_t len)
    465  1.16  christos {
    466  1.16  christos 	if (!_ti_grow_tbuf(tbuf, 2 * sizeof(uint16_t) + len))
    467  1.16  christos 		return 0;
    468  1.16  christos 	_ti_encode_buf_16(tbuf, ind);
    469  1.16  christos 	_ti_encode_buf_count_str(tbuf, buf, len);
    470  1.16  christos 	tbuf->entries++;
    471  1.16  christos 	return 1;
    472  1.16  christos }
    473  1.16  christos 
    474  1.16  christos int
    475  1.16  christos _ti_encode_buf_id_flags(TBUF *tbuf, int ind, int flag)
    476  1.16  christos {
    477  1.16  christos 	if (!_ti_grow_tbuf(tbuf, sizeof(uint16_t) + 1))
    478  1.16  christos 		return 0;
    479  1.16  christos 	_ti_encode_buf_16(tbuf, ind);
    480  1.16  christos 	tbuf->buf[tbuf->bufpos++] = flag;
    481  1.16  christos 	tbuf->entries++;
    482  1.16  christos 	return 1;
    483  1.16  christos }
    484  1.16  christos 
    485   1.1       roy TIC *
    486   1.1       roy _ti_compile(char *cap, int flags)
    487   1.1       roy {
    488   1.1       roy 	char *token, *p, *e, *name, *desc, *alias;
    489   1.1       roy 	signed char flag;
    490   1.5       roy 	long cnum;
    491  1.14       roy 	short ind;
    492  1.14       roy 	int num;
    493   1.1       roy 	size_t len;
    494   1.1       roy 	TBUF buf;
    495   1.1       roy 	TIC *tic;
    496   1.1       roy 
    497   1.9       roy 	_DIAGASSERT(cap != NULL);
    498   1.1       roy 
    499   1.4       roy 	name = _ti_get_token(&cap, ',');
    500   1.1       roy 	if (name == NULL) {
    501  1.15  christos 		dowarn(flags, "no separator found: %s", cap);
    502   1.1       roy 		return NULL;
    503   1.1       roy 	}
    504   1.1       roy 	desc = strrchr(name, '|');
    505   1.1       roy 	if (desc != NULL)
    506   1.1       roy 		*desc++ = '\0';
    507   1.1       roy 	alias = strchr(name, '|');
    508   1.1       roy 	if (alias != NULL)
    509   1.1       roy 		*alias++ = '\0';
    510   1.1       roy 
    511  1.14       roy 	if (strlen(name) > UINT16_MAX - 1) {
    512  1.14       roy 		dowarn(flags, "%s: name too long", name);
    513  1.14       roy 		return NULL;
    514  1.14       roy 	}
    515  1.14       roy 	if (desc != NULL && strlen(desc) > UINT16_MAX - 1) {
    516  1.14       roy 		dowarn(flags, "%s: description too long: %s", name, desc);
    517  1.14       roy 		return NULL;
    518  1.14       roy 	}
    519  1.14       roy 	if (alias != NULL && strlen(alias) > UINT16_MAX - 1) {
    520  1.14       roy 		dowarn(flags, "%s: alias too long: %s", name, alias);
    521  1.14       roy 		return NULL;
    522  1.14       roy 	}
    523  1.14       roy 
    524   1.1       roy 	tic = calloc(sizeof(*tic), 1);
    525   1.1       roy 	if (tic == NULL)
    526   1.1       roy 		return NULL;
    527   1.1       roy 
    528  1.15  christos 	tic->rtype = (flags & TIC_COMPAT_V1) ? TERMINFO_RTYPE_O1 :
    529  1.15  christos 	    _ti_find_rtype(cap);
    530   1.1       roy 	buf.buf = NULL;
    531   1.1       roy 	buf.buflen = 0;
    532   1.1       roy 
    533  1.15  christos 	tic->name = _ti_getname(tic->rtype, name);
    534   1.1       roy 	if (tic->name == NULL)
    535   1.1       roy 		goto error;
    536   1.1       roy 	if (alias != NULL && flags & TIC_ALIAS) {
    537  1.15  christos 		tic->alias = _ti_getname(tic->rtype, alias);
    538   1.1       roy 		if (tic->alias == NULL)
    539   1.1       roy 			goto error;
    540   1.1       roy 	}
    541   1.1       roy 	if (desc != NULL && flags & TIC_DESCRIPTION) {
    542   1.1       roy 		tic->desc = strdup(desc);
    543   1.1       roy 		if (tic->desc == NULL)
    544   1.1       roy 			goto error;
    545   1.1       roy 	}
    546   1.1       roy 
    547   1.4       roy 	for (token = _ti_get_token(&cap, ',');
    548   1.1       roy 	     token != NULL && *token != '\0';
    549   1.4       roy 	     token = _ti_get_token(&cap, ','))
    550   1.1       roy 	{
    551   1.1       roy 		/* Skip commented caps */
    552   1.1       roy 		if (!(flags & TIC_COMMENT) && token[0] == '.')
    553   1.1       roy 			continue;
    554   1.1       roy 
    555   1.1       roy 		/* Obsolete entries */
    556   1.1       roy 		if (token[0] == 'O' && token[1] == 'T') {
    557   1.1       roy 			if (!(flags & TIC_EXTRA))
    558   1.1       roy 				continue;
    559   1.1       roy 			token += 2;
    560   1.1       roy 		}
    561   1.1       roy 
    562   1.1       roy 		/* str cap */
    563   1.1       roy 		p = strchr(token, '=');
    564   1.1       roy 		if (p != NULL) {
    565   1.1       roy 			*p++ = '\0';
    566   1.1       roy 			/* Don't use the string if we already have it */
    567  1.12       roy 			ind = (short)_ti_strindex(token);
    568   1.1       roy 			if (ind != -1 &&
    569  1.15  christos 			    _ti_find_cap(tic, &tic->strs, 's', ind) != NULL)
    570   1.1       roy 				continue;
    571   1.1       roy 
    572   1.1       roy 			/* Encode the string to our scratch buffer */
    573   1.1       roy 			buf.bufpos = 0;
    574   1.1       roy 			if (encode_string(tic->name, token,
    575   1.1       roy 				&buf, p, flags) == -1)
    576   1.1       roy 				goto error;
    577  1.14       roy 			if (buf.bufpos > UINT16_MAX - 1) {
    578   1.1       roy 				dowarn(flags, "%s: %s: string is too long",
    579   1.1       roy 				    tic->name, token);
    580   1.1       roy 				continue;
    581   1.1       roy 			}
    582   1.1       roy 			if (!VALID_STRING(buf.buf)) {
    583   1.1       roy 				dowarn(flags, "%s: %s: invalid string",
    584   1.1       roy 				    tic->name, token);
    585   1.1       roy 				continue;
    586   1.1       roy 			}
    587   1.1       roy 
    588  1.16  christos 			if (ind == -1) {
    589  1.16  christos 				if (!_ti_store_extra(tic, 1, token, 's', -1, -2,
    590  1.16  christos 				    buf.buf, buf.bufpos, flags))
    591  1.16  christos 					goto error;
    592  1.16  christos 			} else {
    593  1.16  christos 				if (!_ti_encode_buf_id_count_str(&tic->strs,
    594  1.16  christos 				    ind, buf.buf, buf.bufpos))
    595   1.1       roy 					goto error;
    596   1.1       roy 			}
    597   1.1       roy 			continue;
    598   1.1       roy 		}
    599   1.1       roy 
    600   1.1       roy 		/* num cap */
    601   1.1       roy 		p = strchr(token, '#');
    602   1.1       roy 		if (p != NULL) {
    603   1.1       roy 			*p++ = '\0';
    604   1.1       roy 			/* Don't use the number if we already have it */
    605  1.12       roy 			ind = (short)_ti_numindex(token);
    606   1.1       roy 			if (ind != -1 &&
    607  1.15  christos 			    _ti_find_cap(tic, &tic->nums, 'n', ind) != NULL)
    608   1.1       roy 				continue;
    609   1.1       roy 
    610   1.5       roy 			cnum = strtol(p, &e, 0);
    611   1.1       roy 			if (*e != '\0') {
    612   1.1       roy 				dowarn(flags, "%s: %s: not a number",
    613   1.1       roy 				    tic->name, token);
    614   1.1       roy 				continue;
    615   1.1       roy 			}
    616  1.14       roy 			if (!VALID_NUMERIC(cnum) || cnum > INT32_MAX) {
    617  1.14       roy 				dowarn(flags, "%s: %s: number %ld out of range",
    618  1.14       roy 				    tic->name, token, cnum);
    619   1.1       roy 				continue;
    620   1.1       roy 			}
    621  1.13       roy 
    622  1.14       roy 			num = (int)cnum;
    623  1.16  christos 			if (ind == -1) {
    624  1.16  christos 				if (!_ti_store_extra(tic, 1, token, 'n', -1,
    625  1.16  christos 				    num, NULL, 0, flags))
    626   1.1       roy 					goto error;
    627  1.16  christos 			} else {
    628  1.16  christos 				if (!_ti_encode_buf_id_num(&tic->nums,
    629  1.16  christos 				    ind, num, _ti_numsize(tic)))
    630  1.16  christos 					    goto error;
    631   1.1       roy 			}
    632   1.1       roy 			continue;
    633   1.1       roy 		}
    634   1.1       roy 
    635   1.1       roy 		flag = 1;
    636   1.1       roy 		len = strlen(token) - 1;
    637   1.1       roy 		if (token[len] == '@') {
    638   1.1       roy 			flag = CANCELLED_BOOLEAN;
    639   1.1       roy 			token[len] = '\0';
    640   1.1       roy 		}
    641  1.12       roy 		ind = (short)_ti_flagindex(token);
    642   1.1       roy 		if (ind == -1 && flag == CANCELLED_BOOLEAN) {
    643  1.12       roy 			if ((ind = (short)_ti_numindex(token)) != -1) {
    644  1.15  christos 				if (_ti_find_cap(tic, &tic->nums, 'n', ind)
    645  1.15  christos 				    != NULL)
    646   1.1       roy 					continue;
    647  1.16  christos 				if (!_ti_encode_buf_id_num(&tic->nums, ind,
    648  1.16  christos 				    CANCELLED_NUMERIC, _ti_numsize(tic)))
    649   1.1       roy 					goto error;
    650   1.1       roy 				continue;
    651  1.12       roy 			} else if ((ind = (short)_ti_strindex(token)) != -1) {
    652  1.15  christos 				if (_ti_find_cap(tic, &tic->strs, 's', ind)
    653  1.15  christos 				    != NULL)
    654   1.1       roy 					continue;
    655  1.16  christos 				if (!_ti_encode_buf_id_num(
    656  1.16  christos 				    &tic->strs, ind, 0, sizeof(uint16_t)))
    657   1.1       roy 					goto error;
    658   1.1       roy 				continue;
    659   1.1       roy 			}
    660   1.1       roy 		}
    661  1.16  christos 		if (ind == -1) {
    662  1.16  christos 			if (!_ti_store_extra(tic, 1, token, 'f', flag, 0, NULL,
    663  1.16  christos 			    0, flags))
    664  1.16  christos 				goto error;
    665  1.16  christos 		} else if (_ti_find_cap(tic, &tic->flags, 'f', ind) == NULL) {
    666  1.20  christos 			if (!_ti_encode_buf_id_flags(&tic->flags, ind, flag))
    667   1.1       roy 				goto error;
    668   1.1       roy 		}
    669   1.1       roy 	}
    670   1.1       roy 
    671   1.1       roy 	free(buf.buf);
    672   1.1       roy 	return tic;
    673   1.1       roy 
    674   1.1       roy error:
    675   1.1       roy 	free(buf.buf);
    676   1.1       roy 	_ti_freetic(tic);
    677   1.1       roy 	return NULL;
    678   1.1       roy }
    679   1.1       roy 
    680   1.1       roy void
    681   1.1       roy _ti_freetic(TIC *tic)
    682   1.1       roy {
    683   1.1       roy 
    684   1.1       roy 	if (tic != NULL) {
    685   1.1       roy 		free(tic->name);
    686   1.1       roy 		free(tic->alias);
    687   1.1       roy 		free(tic->desc);
    688   1.7     joerg 		free(tic->extras.buf);
    689   1.1       roy 		free(tic->flags.buf);
    690   1.1       roy 		free(tic->nums.buf);
    691   1.1       roy 		free(tic->strs.buf);
    692   1.1       roy 		free(tic);
    693   1.1       roy 	}
    694   1.1       roy }
    695