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