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