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