Home | History | Annotate | Line # | Download | only in libterminfo
compile.c revision 1.17
      1  1.17  christos /* $NetBSD: compile.c,v 1.17 2020/03/28 02:38:15 christos 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.17  christos __RCSID("$NetBSD: compile.c,v 1.17 2020/03/28 02:38:15 christos 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.15  christos 	char *name;
    159  1.15  christos 
    160  1.15  christos 	if (rtype == TERMINFO_RTYPE) {
    161  1.15  christos 		if (asprintf(&name, "%s@v3", orig) < 0)
    162  1.15  christos 			name = NULL;
    163  1.15  christos 	} else {
    164  1.15  christos 		name = strdup(orig);
    165  1.15  christos 	}
    166  1.15  christos 	return name;
    167  1.15  christos }
    168  1.15  christos 
    169   1.1       roy size_t
    170  1.15  christos _ti_store_extra(TIC *tic, int wrn, const char *id, char type, char flag,
    171  1.15  christos     int num, const char *str, size_t strl, int flags)
    172   1.1       roy {
    173   1.1       roy 	size_t l;
    174   1.1       roy 
    175   1.1       roy 	_DIAGASSERT(tic != NULL);
    176   1.1       roy 
    177   1.1       roy 	if (strcmp(id, "use") != 0) {
    178  1.15  christos 		if (_ti_find_extra(tic, &tic->extras, id) != NULL)
    179   1.1       roy 			return 0;
    180   1.1       roy 		if (!(flags & TIC_EXTRA)) {
    181   1.1       roy 			if (wrn != 0)
    182   1.1       roy 				dowarn(flags, "%s: %s: unknown capability",
    183   1.1       roy 				    tic->name, id);
    184   1.1       roy 			return 0;
    185   1.1       roy 		}
    186   1.1       roy 	}
    187   1.9       roy 
    188   1.1       roy 	l = strlen(id) + 1;
    189   1.1       roy 	if (l > UINT16_T_MAX) {
    190   1.1       roy 		dowarn(flags, "%s: %s: cap name is too long", tic->name, id);
    191   1.1       roy 		return 0;
    192   1.1       roy 	}
    193   1.9       roy 
    194   1.1       roy 	if (!_ti_grow_tbuf(&tic->extras,
    195  1.15  christos 		l + strl + sizeof(uint16_t) + _ti_numsize(tic) + 1))
    196   1.1       roy 		return 0;
    197  1.16  christos 	_ti_encode_buf_count_str(&tic->extras, id, l);
    198   1.1       roy 	tic->extras.buf[tic->extras.bufpos++] = type;
    199   1.1       roy 	switch (type) {
    200   1.1       roy 	case 'f':
    201   1.1       roy 		tic->extras.buf[tic->extras.bufpos++] = flag;
    202   1.1       roy 		break;
    203   1.1       roy 	case 'n':
    204  1.16  christos 		_ti_encode_buf_num(&tic->extras, num, tic->rtype);
    205   1.1       roy 		break;
    206   1.1       roy 	case 's':
    207  1.16  christos 		_ti_encode_buf_count_str(&tic->extras, str, strl);
    208   1.1       roy 		break;
    209   1.1       roy 	}
    210   1.1       roy 	tic->extras.entries++;
    211   1.1       roy 	return 1;
    212   1.1       roy }
    213   1.1       roy 
    214  1.16  christos static void
    215  1.16  christos _ti_encode_buf(char **cap, const TBUF *buf)
    216  1.16  christos {
    217  1.16  christos 	if (buf->entries == 0) {
    218  1.16  christos 		_ti_encode_16(cap, 0);
    219  1.16  christos 	} else {
    220  1.16  christos 		_ti_encode_16(cap, buf->bufpos + sizeof(uint16_t));
    221  1.16  christos 		_ti_encode_16(cap, buf->entries);
    222  1.16  christos 		_ti_encode_str(cap, buf->buf, buf->bufpos);
    223  1.16  christos 	}
    224  1.16  christos }
    225  1.16  christos 
    226   1.1       roy ssize_t
    227   1.1       roy _ti_flatten(uint8_t **buf, const TIC *tic)
    228   1.1       roy {
    229   1.1       roy 	size_t buflen, len, alen, dlen;
    230  1.16  christos 	char *cap;
    231   1.1       roy 
    232   1.1       roy 	_DIAGASSERT(buf != NULL);
    233   1.1       roy 	_DIAGASSERT(tic != NULL);
    234   1.1       roy 
    235   1.1       roy 	len = strlen(tic->name) + 1;
    236   1.1       roy 	if (tic->alias == NULL)
    237   1.1       roy 		alen = 0;
    238   1.1       roy 	else
    239   1.1       roy 		alen = strlen(tic->alias) + 1;
    240   1.1       roy 	if (tic->desc == NULL)
    241   1.1       roy 		dlen = 0;
    242   1.1       roy 	else
    243   1.1       roy 		dlen = strlen(tic->desc) + 1;
    244  1.16  christos 
    245   1.1       roy 	buflen = sizeof(char) +
    246   1.1       roy 	    sizeof(uint16_t) + len +
    247   1.1       roy 	    sizeof(uint16_t) + alen +
    248   1.1       roy 	    sizeof(uint16_t) + dlen +
    249   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->flags.bufpos +
    250   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->nums.bufpos +
    251   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->strs.bufpos +
    252   1.1       roy 	    (sizeof(uint16_t) * 2) + tic->extras.bufpos;
    253  1.16  christos 
    254   1.1       roy 	*buf = malloc(buflen);
    255   1.1       roy 	if (*buf == NULL)
    256   1.1       roy 		return -1;
    257   1.9       roy 
    258  1.16  christos 	cap = (char *)*buf;
    259  1.15  christos 	*cap++ = tic->rtype;
    260   1.9       roy 
    261  1.16  christos 	_ti_encode_count_str(&cap, tic->name, len);
    262  1.16  christos 	_ti_encode_count_str(&cap, tic->alias, alen);
    263  1.16  christos 	_ti_encode_count_str(&cap, tic->desc, dlen);
    264   1.9       roy 
    265  1.16  christos 	_ti_encode_buf(&cap, &tic->flags);
    266   1.9       roy 
    267  1.16  christos 	_ti_encode_buf(&cap, &tic->nums);
    268  1.16  christos 	_ti_encode_buf(&cap, &tic->strs);
    269  1.16  christos 	_ti_encode_buf(&cap, &tic->extras);
    270   1.1       roy 
    271  1.16  christos 	return (uint8_t *)cap - *buf;
    272   1.1       roy }
    273   1.1       roy 
    274   1.1       roy static int
    275   1.1       roy encode_string(const char *term, const char *cap, TBUF *tbuf, const char *str,
    276   1.1       roy     int flags)
    277   1.1       roy {
    278   1.1       roy 	int slash, i, num;
    279   1.1       roy 	char ch, *p, *s, last;
    280   1.9       roy 
    281   1.1       roy 	if (_ti_grow_tbuf(tbuf, strlen(str) + 1) == NULL)
    282   1.1       roy 		return -1;
    283   1.1       roy 	p = s = tbuf->buf + tbuf->bufpos;
    284   1.1       roy 	slash = 0;
    285   1.1       roy 	last = '\0';
    286   1.1       roy 	/* Convert escape codes */
    287   1.1       roy 	while ((ch = *str++) != '\0') {
    288  1.10       roy 		if (ch == '\n') {
    289  1.10       roy 			/* Following a newline, strip leading whitespace from
    290  1.10       roy 			 * capability strings. */
    291  1.10       roy 			while (isspace((unsigned char)*str))
    292  1.10       roy 				str++;
    293  1.10       roy 			continue;
    294  1.10       roy 		}
    295   1.1       roy 		if (slash == 0 && ch == '\\') {
    296   1.1       roy 			slash = 1;
    297   1.1       roy 			continue;
    298   1.1       roy 		}
    299   1.1       roy 		if (slash == 0) {
    300   1.1       roy 			if (last != '%' && ch == '^') {
    301   1.1       roy 				ch = *str++;
    302   1.1       roy 				if (((unsigned char)ch) >= 128)
    303   1.1       roy 					dowarn(flags,
    304   1.1       roy 					    "%s: %s: illegal ^ character",
    305   1.1       roy 					    term, cap);
    306   1.1       roy 				if (ch == '\0')
    307   1.1       roy 					break;
    308   1.1       roy 				if (ch == '?')
    309   1.1       roy 					ch = '\177';
    310   1.1       roy 				else if ((ch &= 037) == 0)
    311   1.5       roy 					ch = (char)128;
    312  1.11       roy 			} else if (!isprint((unsigned char)ch))
    313  1.11       roy 				dowarn(flags,
    314  1.11       roy 				    "%s: %s: unprintable character",
    315  1.11       roy 				    term, cap);
    316   1.1       roy 			*p++ = ch;
    317   1.1       roy 			last = ch;
    318   1.1       roy 			continue;
    319   1.1       roy 		}
    320   1.1       roy 		slash = 0;
    321   1.1       roy 		if (ch >= '0' && ch <= '7') {
    322   1.1       roy 			num = ch - '0';
    323   1.1       roy 			for (i = 0; i < 2; i++) {
    324   1.1       roy 				if (*str < '0' || *str > '7') {
    325   1.1       roy 					if (isdigit((unsigned char)*str))
    326   1.1       roy 						dowarn(flags,
    327   1.1       roy 						    "%s: %s: non octal"
    328   1.1       roy 						    " digit", term, cap);
    329   1.1       roy 					else
    330   1.1       roy 						break;
    331   1.1       roy 				}
    332   1.1       roy 				num = num * 8 + *str++ - '0';
    333   1.1       roy 			}
    334   1.1       roy 			if (num == 0)
    335   1.1       roy 				num = 0200;
    336   1.1       roy 			*p++ = (char)num;
    337   1.1       roy 			continue;
    338   1.1       roy 		}
    339   1.1       roy 		switch (ch) {
    340   1.1       roy 		case 'a':
    341   1.1       roy 			*p++ = '\a';
    342   1.1       roy 			break;
    343   1.1       roy 		case 'b':
    344   1.1       roy 			*p++ = '\b';
    345   1.1       roy 			break;
    346   1.1       roy 		case 'e': /* FALLTHROUGH */
    347   1.1       roy 		case 'E':
    348   1.1       roy 			*p++ = '\033';
    349   1.1       roy 			break;
    350   1.1       roy 		case 'f':
    351   1.1       roy 			*p++ = '\014';
    352   1.1       roy 			break;
    353   1.1       roy 		case 'l': /* FALLTHROUGH */
    354   1.1       roy 		case 'n':
    355   1.1       roy 			*p++ = '\n';
    356   1.1       roy 			break;
    357   1.1       roy 		case 'r':
    358   1.1       roy 			*p++ = '\r';
    359   1.1       roy 			break;
    360   1.1       roy 		case 's':
    361   1.1       roy 			*p++ = ' ';
    362   1.1       roy 			break;
    363   1.1       roy 		case 't':
    364   1.1       roy 			*p++ = '\t';
    365   1.1       roy 			break;
    366   1.1       roy 		default:
    367   1.1       roy 			/* We should warn here */
    368   1.1       roy 		case '^':
    369   1.1       roy 		case ',':
    370   1.1       roy 		case ':':
    371   1.1       roy 		case '|':
    372   1.1       roy 			*p++ = ch;
    373   1.1       roy 			break;
    374   1.1       roy 		}
    375   1.1       roy 		last = ch;
    376   1.1       roy 	}
    377   1.1       roy 	*p++ = '\0';
    378  1.12       roy 	tbuf->bufpos += (size_t)(p - s);
    379   1.1       roy 	return 0;
    380   1.1       roy }
    381   1.1       roy 
    382   1.4       roy char *
    383   1.4       roy _ti_get_token(char **cap, char sep)
    384   1.1       roy {
    385   1.4       roy 	char esc, *token;
    386   1.1       roy 
    387   1.1       roy 	while (isspace((unsigned char)**cap))
    388   1.1       roy 		(*cap)++;
    389   1.1       roy 	if (**cap == '\0')
    390   1.1       roy 		return NULL;
    391   1.1       roy 
    392   1.1       roy 	/* We can't use stresep(3) as ^ we need two escape chars */
    393   1.4       roy 	esc = '\0';
    394   1.1       roy 	for (token = *cap;
    395   1.4       roy 	     **cap != '\0' && (esc != '\0' || **cap != sep);
    396   1.1       roy 	     (*cap)++)
    397   1.1       roy 	{
    398   1.4       roy 		if (esc == '\0') {
    399   1.1       roy 			if (**cap == '\\' || **cap == '^')
    400   1.4       roy 				esc = **cap;
    401   1.4       roy 		} else {
    402   1.4       roy 			/* termcap /E/ is valid */
    403   1.4       roy 			if (sep == ':' && esc == '\\' && **cap == 'E')
    404   1.4       roy 				esc = 'x';
    405   1.4       roy 			else
    406   1.4       roy 				esc = '\0';
    407   1.4       roy 		}
    408   1.1       roy 	}
    409   1.1       roy 
    410   1.1       roy 	if (**cap != '\0')
    411   1.1       roy 		*(*cap)++ = '\0';
    412   1.1       roy 
    413   1.1       roy 	return token;
    414   1.1       roy }
    415   1.1       roy 
    416  1.15  christos static int
    417  1.15  christos _ti_find_rtype(const char *cap)
    418  1.15  christos {
    419  1.17  christos 	const char *ptr;
    420  1.17  christos 
    421  1.17  christos 	for (ptr = cap; (ptr = strchr(ptr, '#')) != NULL;) {
    422  1.15  christos 		if (strtol(++ptr, NULL, 0) > SHRT_MAX) {
    423  1.15  christos 			return TERMINFO_RTYPE;
    424  1.15  christos 		}
    425  1.15  christos 	}
    426  1.15  christos 	return TERMINFO_RTYPE_O1;
    427  1.15  christos }
    428  1.15  christos 
    429  1.16  christos int
    430  1.16  christos _ti_encode_buf_id_num(TBUF *tbuf, int ind, int num, size_t len)
    431  1.16  christos {
    432  1.16  christos 	if (!_ti_grow_tbuf(tbuf, sizeof(uint16_t) + len))
    433  1.16  christos 		return 0;
    434  1.16  christos 	_ti_encode_buf_16(tbuf, ind);
    435  1.16  christos 	if (len == sizeof(uint32_t))
    436  1.16  christos 		_ti_encode_buf_32(tbuf, num);
    437  1.16  christos 	else
    438  1.16  christos 		_ti_encode_buf_16(tbuf, num);
    439  1.16  christos 	tbuf->entries++;
    440  1.16  christos 	return 1;
    441  1.16  christos }
    442  1.16  christos 
    443  1.16  christos int
    444  1.16  christos _ti_encode_buf_id_count_str(TBUF *tbuf, int ind, const void *buf, size_t len)
    445  1.16  christos {
    446  1.16  christos 	if (!_ti_grow_tbuf(tbuf, 2 * sizeof(uint16_t) + len))
    447  1.16  christos 		return 0;
    448  1.16  christos 	_ti_encode_buf_16(tbuf, ind);
    449  1.16  christos 	_ti_encode_buf_count_str(tbuf, buf, len);
    450  1.16  christos 	tbuf->entries++;
    451  1.16  christos 	return 1;
    452  1.16  christos }
    453  1.16  christos 
    454  1.16  christos int
    455  1.16  christos _ti_encode_buf_id_flags(TBUF *tbuf, int ind, int flag)
    456  1.16  christos {
    457  1.16  christos 	if (!_ti_grow_tbuf(tbuf, sizeof(uint16_t) + 1))
    458  1.16  christos 		return 0;
    459  1.16  christos 	_ti_encode_buf_16(tbuf, ind);
    460  1.16  christos 	tbuf->buf[tbuf->bufpos++] = flag;
    461  1.16  christos 	tbuf->entries++;
    462  1.16  christos 	return 1;
    463  1.16  christos }
    464  1.16  christos 
    465   1.1       roy TIC *
    466   1.1       roy _ti_compile(char *cap, int flags)
    467   1.1       roy {
    468   1.1       roy 	char *token, *p, *e, *name, *desc, *alias;
    469   1.1       roy 	signed char flag;
    470   1.5       roy 	long cnum;
    471  1.14       roy 	short ind;
    472  1.14       roy 	int num;
    473   1.1       roy 	size_t len;
    474   1.1       roy 	TBUF buf;
    475   1.1       roy 	TIC *tic;
    476   1.1       roy 
    477   1.9       roy 	_DIAGASSERT(cap != NULL);
    478   1.1       roy 
    479   1.4       roy 	name = _ti_get_token(&cap, ',');
    480   1.1       roy 	if (name == NULL) {
    481  1.15  christos 		dowarn(flags, "no separator found: %s", cap);
    482   1.1       roy 		return NULL;
    483   1.1       roy 	}
    484   1.1       roy 	desc = strrchr(name, '|');
    485   1.1       roy 	if (desc != NULL)
    486   1.1       roy 		*desc++ = '\0';
    487   1.1       roy 	alias = strchr(name, '|');
    488   1.1       roy 	if (alias != NULL)
    489   1.1       roy 		*alias++ = '\0';
    490   1.1       roy 
    491  1.14       roy 	if (strlen(name) > UINT16_MAX - 1) {
    492  1.14       roy 		dowarn(flags, "%s: name too long", name);
    493  1.14       roy 		return NULL;
    494  1.14       roy 	}
    495  1.14       roy 	if (desc != NULL && strlen(desc) > UINT16_MAX - 1) {
    496  1.14       roy 		dowarn(flags, "%s: description too long: %s", name, desc);
    497  1.14       roy 		return NULL;
    498  1.14       roy 	}
    499  1.14       roy 	if (alias != NULL && strlen(alias) > UINT16_MAX - 1) {
    500  1.14       roy 		dowarn(flags, "%s: alias too long: %s", name, alias);
    501  1.14       roy 		return NULL;
    502  1.14       roy 	}
    503  1.14       roy 
    504   1.1       roy 	tic = calloc(sizeof(*tic), 1);
    505   1.1       roy 	if (tic == NULL)
    506   1.1       roy 		return NULL;
    507   1.1       roy 
    508  1.15  christos 	tic->rtype = (flags & TIC_COMPAT_V1) ? TERMINFO_RTYPE_O1 :
    509  1.15  christos 	    _ti_find_rtype(cap);
    510   1.1       roy 	buf.buf = NULL;
    511   1.1       roy 	buf.buflen = 0;
    512   1.1       roy 
    513  1.15  christos 	tic->name = _ti_getname(tic->rtype, name);
    514   1.1       roy 	if (tic->name == NULL)
    515   1.1       roy 		goto error;
    516   1.1       roy 	if (alias != NULL && flags & TIC_ALIAS) {
    517  1.15  christos 		tic->alias = _ti_getname(tic->rtype, alias);
    518   1.1       roy 		if (tic->alias == NULL)
    519   1.1       roy 			goto error;
    520   1.1       roy 	}
    521   1.1       roy 	if (desc != NULL && flags & TIC_DESCRIPTION) {
    522   1.1       roy 		tic->desc = strdup(desc);
    523   1.1       roy 		if (tic->desc == NULL)
    524   1.1       roy 			goto error;
    525   1.1       roy 	}
    526   1.1       roy 
    527   1.4       roy 	for (token = _ti_get_token(&cap, ',');
    528   1.1       roy 	     token != NULL && *token != '\0';
    529   1.4       roy 	     token = _ti_get_token(&cap, ','))
    530   1.1       roy 	{
    531   1.1       roy 		/* Skip commented caps */
    532   1.1       roy 		if (!(flags & TIC_COMMENT) && token[0] == '.')
    533   1.1       roy 			continue;
    534   1.1       roy 
    535   1.1       roy 		/* Obsolete entries */
    536   1.1       roy 		if (token[0] == 'O' && token[1] == 'T') {
    537   1.1       roy 			if (!(flags & TIC_EXTRA))
    538   1.1       roy 				continue;
    539   1.1       roy 			token += 2;
    540   1.1       roy 		}
    541   1.1       roy 
    542   1.1       roy 		/* str cap */
    543   1.1       roy 		p = strchr(token, '=');
    544   1.1       roy 		if (p != NULL) {
    545   1.1       roy 			*p++ = '\0';
    546   1.1       roy 			/* Don't use the string if we already have it */
    547  1.12       roy 			ind = (short)_ti_strindex(token);
    548   1.1       roy 			if (ind != -1 &&
    549  1.15  christos 			    _ti_find_cap(tic, &tic->strs, 's', ind) != NULL)
    550   1.1       roy 				continue;
    551   1.1       roy 
    552   1.1       roy 			/* Encode the string to our scratch buffer */
    553   1.1       roy 			buf.bufpos = 0;
    554   1.1       roy 			if (encode_string(tic->name, token,
    555   1.1       roy 				&buf, p, flags) == -1)
    556   1.1       roy 				goto error;
    557  1.14       roy 			if (buf.bufpos > UINT16_MAX - 1) {
    558   1.1       roy 				dowarn(flags, "%s: %s: string is too long",
    559   1.1       roy 				    tic->name, token);
    560   1.1       roy 				continue;
    561   1.1       roy 			}
    562   1.1       roy 			if (!VALID_STRING(buf.buf)) {
    563   1.1       roy 				dowarn(flags, "%s: %s: invalid string",
    564   1.1       roy 				    tic->name, token);
    565   1.1       roy 				continue;
    566   1.1       roy 			}
    567   1.1       roy 
    568  1.16  christos 			if (ind == -1) {
    569  1.16  christos 				if (!_ti_store_extra(tic, 1, token, 's', -1, -2,
    570  1.16  christos 				    buf.buf, buf.bufpos, flags))
    571  1.16  christos 					goto error;
    572  1.16  christos 			} else {
    573  1.16  christos 				if (!_ti_encode_buf_id_count_str(&tic->strs,
    574  1.16  christos 				    ind, buf.buf, buf.bufpos))
    575   1.1       roy 					goto error;
    576   1.1       roy 			}
    577   1.1       roy 			continue;
    578   1.1       roy 		}
    579   1.1       roy 
    580   1.1       roy 		/* num cap */
    581   1.1       roy 		p = strchr(token, '#');
    582   1.1       roy 		if (p != NULL) {
    583   1.1       roy 			*p++ = '\0';
    584   1.1       roy 			/* Don't use the number if we already have it */
    585  1.12       roy 			ind = (short)_ti_numindex(token);
    586   1.1       roy 			if (ind != -1 &&
    587  1.15  christos 			    _ti_find_cap(tic, &tic->nums, 'n', ind) != NULL)
    588   1.1       roy 				continue;
    589   1.1       roy 
    590   1.5       roy 			cnum = strtol(p, &e, 0);
    591   1.1       roy 			if (*e != '\0') {
    592   1.1       roy 				dowarn(flags, "%s: %s: not a number",
    593   1.1       roy 				    tic->name, token);
    594   1.1       roy 				continue;
    595   1.1       roy 			}
    596  1.14       roy 			if (!VALID_NUMERIC(cnum) || cnum > INT32_MAX) {
    597  1.14       roy 				dowarn(flags, "%s: %s: number %ld out of range",
    598  1.14       roy 				    tic->name, token, cnum);
    599   1.1       roy 				continue;
    600   1.1       roy 			}
    601  1.13       roy 
    602  1.14       roy 			num = (int)cnum;
    603  1.16  christos 			if (ind == -1) {
    604  1.16  christos 				if (!_ti_store_extra(tic, 1, token, 'n', -1,
    605  1.16  christos 				    num, NULL, 0, flags))
    606   1.1       roy 					goto error;
    607  1.16  christos 			} else {
    608  1.16  christos 				if (!_ti_encode_buf_id_num(&tic->nums,
    609  1.16  christos 				    ind, num, _ti_numsize(tic)))
    610  1.16  christos 					    goto error;
    611   1.1       roy 			}
    612   1.1       roy 			continue;
    613   1.1       roy 		}
    614   1.1       roy 
    615   1.1       roy 		flag = 1;
    616   1.1       roy 		len = strlen(token) - 1;
    617   1.1       roy 		if (token[len] == '@') {
    618   1.1       roy 			flag = CANCELLED_BOOLEAN;
    619   1.1       roy 			token[len] = '\0';
    620   1.1       roy 		}
    621  1.12       roy 		ind = (short)_ti_flagindex(token);
    622   1.1       roy 		if (ind == -1 && flag == CANCELLED_BOOLEAN) {
    623  1.12       roy 			if ((ind = (short)_ti_numindex(token)) != -1) {
    624  1.15  christos 				if (_ti_find_cap(tic, &tic->nums, 'n', ind)
    625  1.15  christos 				    != NULL)
    626   1.1       roy 					continue;
    627  1.16  christos 				if (!_ti_encode_buf_id_num(&tic->nums, ind,
    628  1.16  christos 				    CANCELLED_NUMERIC, _ti_numsize(tic)))
    629   1.1       roy 					goto error;
    630   1.1       roy 				continue;
    631  1.12       roy 			} else if ((ind = (short)_ti_strindex(token)) != -1) {
    632  1.15  christos 				if (_ti_find_cap(tic, &tic->strs, 's', ind)
    633  1.15  christos 				    != NULL)
    634   1.1       roy 					continue;
    635  1.16  christos 				if (!_ti_encode_buf_id_num(
    636  1.16  christos 				    &tic->strs, ind, 0, sizeof(uint16_t)))
    637   1.1       roy 					goto error;
    638   1.1       roy 				continue;
    639   1.1       roy 			}
    640   1.1       roy 		}
    641  1.16  christos 		if (ind == -1) {
    642  1.16  christos 			if (!_ti_store_extra(tic, 1, token, 'f', flag, 0, NULL,
    643  1.16  christos 			    0, flags))
    644  1.16  christos 				goto error;
    645  1.16  christos 		} else if (_ti_find_cap(tic, &tic->flags, 'f', ind) == NULL) {
    646  1.16  christos 			if (!_ti_encode_buf_id_flags(&tic->flags, ind, flags))
    647   1.1       roy 				goto error;
    648   1.1       roy 		}
    649   1.1       roy 	}
    650   1.1       roy 
    651   1.1       roy 	free(buf.buf);
    652   1.1       roy 	return tic;
    653   1.1       roy 
    654   1.1       roy error:
    655   1.1       roy 	free(buf.buf);
    656   1.1       roy 	_ti_freetic(tic);
    657   1.1       roy 	return NULL;
    658   1.1       roy }
    659   1.1       roy 
    660   1.1       roy void
    661   1.1       roy _ti_freetic(TIC *tic)
    662   1.1       roy {
    663   1.1       roy 
    664   1.1       roy 	if (tic != NULL) {
    665   1.1       roy 		free(tic->name);
    666   1.1       roy 		free(tic->alias);
    667   1.1       roy 		free(tic->desc);
    668   1.7     joerg 		free(tic->extras.buf);
    669   1.1       roy 		free(tic->flags.buf);
    670   1.1       roy 		free(tic->nums.buf);
    671   1.1       roy 		free(tic->strs.buf);
    672   1.1       roy 		free(tic);
    673   1.1       roy 	}
    674   1.1       roy }
    675