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