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