tools/llvm: Do not build with symbols
[minix3.git] / lib / libterminfo / compile.c
blob2adb4c71b79841e1c7bac2a81cb74258c2863da5
1 /* $NetBSD: compile.c,v 1.9 2013/06/07 13:16:18 roy Exp $ */
3 /*
4 * Copyright (c) 2009, 2010, 2011 The NetBSD Foundation, Inc.
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #if HAVE_NBTOOL_CONFIG_H
31 #include "nbtool_config.h"
32 #endif
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: compile.c,v 1.9 2013/06/07 13:16:18 roy Exp $");
37 #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
38 #include <sys/endian.h>
39 #endif
41 #include <assert.h>
42 #include <ctype.h>
43 #include <err.h>
44 #include <errno.h>
45 #include <limits.h>
46 #include <stdarg.h>
47 #include <stdlib.h>
48 #include <stdint.h>
49 #include <stdio.h>
50 #include <string.h>
51 #include <term_private.h>
52 #include <term.h>
54 static void __printflike(2, 3)
55 dowarn(int flags, const char *fmt, ...)
57 va_list va;
59 errno = EINVAL;
60 if (flags & TIC_WARNING) {
61 va_start(va, fmt);
62 vwarnx(fmt, va);
63 va_end(va);
67 char *
68 _ti_grow_tbuf(TBUF *tbuf, size_t len)
70 char *buf;
71 size_t l;
73 _DIAGASSERT(tbuf != NULL);
75 l = tbuf->bufpos + len;
76 if (l > tbuf->buflen) {
77 if (tbuf->buflen == 0)
78 buf = malloc(l);
79 else
80 buf = realloc(tbuf->buf, l);
81 if (buf == NULL)
82 return NULL;
83 tbuf->buf = buf;
84 tbuf->buflen = l;
86 return tbuf->buf;
89 char *
90 _ti_find_cap(TBUF *tbuf, char type, short ind)
92 size_t n;
93 short num;
94 char *cap;
96 _DIAGASSERT(tbuf != NULL);
98 cap = tbuf->buf;
99 for (n = tbuf->entries; n > 0; n--) {
100 num = le16dec(cap);
101 cap += sizeof(uint16_t);
102 if (num == ind)
103 return cap;
104 switch (type) {
105 case 'f':
106 cap++;
107 break;
108 case 'n':
109 cap += sizeof(uint16_t);
110 break;
111 case 's':
112 num = le16dec(cap);
113 cap += sizeof(uint16_t);
114 cap += num;
115 break;
119 errno = ESRCH;
120 return NULL;
123 char *
124 _ti_find_extra(TBUF *tbuf, const char *code)
126 size_t n;
127 short num;
128 char *cap;
130 _DIAGASSERT(tbuf != NULL);
131 _DIAGASSERT(code != NULL);
133 cap = tbuf->buf;
134 for (n = tbuf->entries; n > 0; n--) {
135 num = le16dec(cap);
136 cap += sizeof(uint16_t);
137 if (strcmp(cap, code) == 0)
138 return cap + num;
139 cap += num;
140 switch (*cap++) {
141 case 'f':
142 cap++;
143 break;
144 case 'n':
145 cap += sizeof(uint16_t);
146 break;
147 case 's':
148 num = le16dec(cap);
149 cap += sizeof(uint16_t);
150 cap += num;
151 break;
155 errno = ESRCH;
156 return NULL;
159 size_t
160 _ti_store_extra(TIC *tic, int wrn, char *id, char type, char flag, short num,
161 char *str, size_t strl, int flags)
163 size_t l;
165 _DIAGASSERT(tic != NULL);
167 if (strcmp(id, "use") != 0) {
168 if (_ti_find_extra(&tic->extras, id) != NULL)
169 return 0;
170 if (!(flags & TIC_EXTRA)) {
171 if (wrn != 0)
172 dowarn(flags, "%s: %s: unknown capability",
173 tic->name, id);
174 return 0;
178 l = strlen(id) + 1;
179 if (l > UINT16_T_MAX) {
180 dowarn(flags, "%s: %s: cap name is too long", tic->name, id);
181 return 0;
184 if (!_ti_grow_tbuf(&tic->extras,
185 l + strl + (sizeof(uint16_t) * 2) + 1))
186 return 0;
187 le16enc(tic->extras.buf + tic->extras.bufpos, l);
188 tic->extras.bufpos += sizeof(uint16_t);
189 memcpy(tic->extras.buf + tic->extras.bufpos, id, l);
190 tic->extras.bufpos += l;
191 tic->extras.buf[tic->extras.bufpos++] = type;
192 switch (type) {
193 case 'f':
194 tic->extras.buf[tic->extras.bufpos++] = flag;
195 break;
196 case 'n':
197 le16enc(tic->extras.buf + tic->extras.bufpos, num);
198 tic->extras.bufpos += sizeof(uint16_t);
199 break;
200 case 's':
201 le16enc(tic->extras.buf + tic->extras.bufpos, strl);
202 tic->extras.bufpos += sizeof(uint16_t);
203 memcpy(tic->extras.buf + tic->extras.bufpos, str, strl);
204 tic->extras.bufpos += strl;
205 break;
207 tic->extras.entries++;
208 return 1;
211 ssize_t
212 _ti_flatten(uint8_t **buf, const TIC *tic)
214 size_t buflen, len, alen, dlen;
215 uint8_t *cap;
217 _DIAGASSERT(buf != NULL);
218 _DIAGASSERT(tic != NULL);
220 len = strlen(tic->name) + 1;
221 if (tic->alias == NULL)
222 alen = 0;
223 else
224 alen = strlen(tic->alias) + 1;
225 if (tic->desc == NULL)
226 dlen = 0;
227 else
228 dlen = strlen(tic->desc) + 1;
229 buflen = sizeof(char) +
230 sizeof(uint16_t) + len +
231 sizeof(uint16_t) + alen +
232 sizeof(uint16_t) + dlen +
233 (sizeof(uint16_t) * 2) + tic->flags.bufpos +
234 (sizeof(uint16_t) * 2) + tic->nums.bufpos +
235 (sizeof(uint16_t) * 2) + tic->strs.bufpos +
236 (sizeof(uint16_t) * 2) + tic->extras.bufpos;
237 *buf = malloc(buflen);
238 if (*buf == NULL)
239 return -1;
241 cap = *buf;
242 *cap++ = 1;
243 le16enc(cap, len);
244 cap += sizeof(uint16_t);
245 memcpy(cap, tic->name, len);
246 cap += len;
248 le16enc(cap, alen);
249 cap += sizeof(uint16_t);
250 if (tic->alias != NULL) {
251 memcpy(cap, tic->alias, alen);
252 cap += alen;
254 le16enc(cap, dlen);
255 cap += sizeof(uint16_t);
256 if (tic->desc != NULL) {
257 memcpy(cap, tic->desc, dlen);
258 cap += dlen;
261 if (tic->flags.entries == 0) {
262 le16enc(cap, 0);
263 cap += sizeof(uint16_t);
264 } else {
265 le16enc(cap, (tic->flags.bufpos + sizeof(uint16_t)));
266 cap += sizeof(uint16_t);
267 le16enc(cap, tic->flags.entries);
268 cap += sizeof(uint16_t);
269 memcpy(cap, tic->flags.buf, tic->flags.bufpos);
270 cap += tic->flags.bufpos;
273 if (tic->nums.entries == 0) {
274 le16enc(cap, 0);
275 cap += sizeof(uint16_t);
276 } else {
277 le16enc(cap, (tic->nums.bufpos + sizeof(uint16_t)));
278 cap += sizeof(uint16_t);
279 le16enc(cap, tic->nums.entries);
280 cap += sizeof(uint16_t);
281 memcpy(cap, tic->nums.buf, tic->nums.bufpos);
282 cap += tic->nums.bufpos;
285 if (tic->strs.entries == 0) {
286 le16enc(cap, 0);
287 cap += sizeof(uint16_t);
288 } else {
289 le16enc(cap, (tic->strs.bufpos + sizeof(uint16_t)));
290 cap += sizeof(uint16_t);
291 le16enc(cap, tic->strs.entries);
292 cap += sizeof(uint16_t);
293 memcpy(cap, tic->strs.buf, tic->strs.bufpos);
294 cap += tic->strs.bufpos;
297 if (tic->extras.entries == 0) {
298 le16enc(cap, 0);
299 cap += sizeof(uint16_t);
300 } else {
301 le16enc(cap, (tic->extras.bufpos + sizeof(uint16_t)));
302 cap += sizeof(uint16_t);
303 le16enc(cap, tic->extras.entries);
304 cap += sizeof(uint16_t);
305 memcpy(cap, tic->extras.buf, tic->extras.bufpos);
306 cap += tic->extras.bufpos;
309 return cap - *buf;
312 static int
313 encode_string(const char *term, const char *cap, TBUF *tbuf, const char *str,
314 int flags)
316 int slash, i, num;
317 char ch, *p, *s, last;
319 if (_ti_grow_tbuf(tbuf, strlen(str) + 1) == NULL)
320 return -1;
321 p = s = tbuf->buf + tbuf->bufpos;
322 slash = 0;
323 last = '\0';
324 /* Convert escape codes */
325 while ((ch = *str++) != '\0') {
326 if (slash == 0 && ch == '\\') {
327 slash = 1;
328 continue;
330 if (slash == 0) {
331 if (last != '%' && ch == '^') {
332 ch = *str++;
333 if (((unsigned char)ch) >= 128)
334 dowarn(flags,
335 "%s: %s: illegal ^ character",
336 term, cap);
337 if (ch == '\0')
338 break;
339 if (ch == '?')
340 ch = '\177';
341 else if ((ch &= 037) == 0)
342 ch = (char)128;
344 *p++ = ch;
345 last = ch;
346 continue;
348 slash = 0;
349 if (ch >= '0' && ch <= '7') {
350 num = ch - '0';
351 for (i = 0; i < 2; i++) {
352 if (*str < '0' || *str > '7') {
353 if (isdigit((unsigned char)*str))
354 dowarn(flags,
355 "%s: %s: non octal"
356 " digit", term, cap);
357 else
358 break;
360 num = num * 8 + *str++ - '0';
362 if (num == 0)
363 num = 0200;
364 *p++ = (char)num;
365 continue;
367 switch (ch) {
368 case 'a':
369 *p++ = '\a';
370 break;
371 case 'b':
372 *p++ = '\b';
373 break;
374 case 'e': /* FALLTHROUGH */
375 case 'E':
376 *p++ = '\033';
377 break;
378 case 'f':
379 *p++ = '\014';
380 break;
381 case 'l': /* FALLTHROUGH */
382 case 'n':
383 *p++ = '\n';
384 break;
385 case 'r':
386 *p++ = '\r';
387 break;
388 case 's':
389 *p++ = ' ';
390 break;
391 case 't':
392 *p++ = '\t';
393 break;
394 default:
395 /* We should warn here */
396 case '^':
397 case ',':
398 case ':':
399 case '|':
400 *p++ = ch;
401 break;
403 last = ch;
405 *p++ = '\0';
406 tbuf->bufpos += p - s;
407 return 0;
410 char *
411 _ti_get_token(char **cap, char sep)
413 char esc, *token;
415 while (isspace((unsigned char)**cap))
416 (*cap)++;
417 if (**cap == '\0')
418 return NULL;
420 /* We can't use stresep(3) as ^ we need two escape chars */
421 esc = '\0';
422 for (token = *cap;
423 **cap != '\0' && (esc != '\0' || **cap != sep);
424 (*cap)++)
426 if (esc == '\0') {
427 if (**cap == '\\' || **cap == '^')
428 esc = **cap;
429 } else {
430 /* termcap /E/ is valid */
431 if (sep == ':' && esc == '\\' && **cap == 'E')
432 esc = 'x';
433 else
434 esc = '\0';
438 if (**cap != '\0')
439 *(*cap)++ = '\0';
441 return token;
444 TIC *
445 _ti_compile(char *cap, int flags)
447 char *token, *p, *e, *name, *desc, *alias;
448 signed char flag;
449 long cnum;
450 short num;
451 ssize_t ind;
452 size_t len;
453 TBUF buf;
454 TIC *tic;
456 _DIAGASSERT(cap != NULL);
458 name = _ti_get_token(&cap, ',');
459 if (name == NULL) {
460 dowarn(flags, "no seperator found: %s", cap);
461 return NULL;
463 desc = strrchr(name, '|');
464 if (desc != NULL)
465 *desc++ = '\0';
466 alias = strchr(name, '|');
467 if (alias != NULL)
468 *alias++ = '\0';
470 tic = calloc(sizeof(*tic), 1);
471 if (tic == NULL)
472 return NULL;
474 buf.buf = NULL;
475 buf.buflen = 0;
477 tic->name = strdup(name);
478 if (tic->name == NULL)
479 goto error;
480 if (alias != NULL && flags & TIC_ALIAS) {
481 tic->alias = strdup(alias);
482 if (tic->alias == NULL)
483 goto error;
485 if (desc != NULL && flags & TIC_DESCRIPTION) {
486 tic->desc = strdup(desc);
487 if (tic->desc == NULL)
488 goto error;
491 for (token = _ti_get_token(&cap, ',');
492 token != NULL && *token != '\0';
493 token = _ti_get_token(&cap, ','))
495 /* Skip commented caps */
496 if (!(flags & TIC_COMMENT) && token[0] == '.')
497 continue;
499 /* Obsolete entries */
500 if (token[0] == 'O' && token[1] == 'T') {
501 if (!(flags & TIC_EXTRA))
502 continue;
503 token += 2;
506 /* str cap */
507 p = strchr(token, '=');
508 if (p != NULL) {
509 *p++ = '\0';
510 /* Don't use the string if we already have it */
511 ind = _ti_strindex(token);
512 if (ind != -1 &&
513 _ti_find_cap(&tic->strs, 's', ind) != NULL)
514 continue;
516 /* Encode the string to our scratch buffer */
517 buf.bufpos = 0;
518 if (encode_string(tic->name, token,
519 &buf, p, flags) == -1)
520 goto error;
521 if (buf.bufpos > UINT16_T_MAX) {
522 dowarn(flags, "%s: %s: string is too long",
523 tic->name, token);
524 continue;
526 if (!VALID_STRING(buf.buf)) {
527 dowarn(flags, "%s: %s: invalid string",
528 tic->name, token);
529 continue;
532 if (ind == -1)
533 _ti_store_extra(tic, 1, token, 's', -1, -2,
534 buf.buf, buf.bufpos, flags);
535 else {
536 if (!_ti_grow_tbuf(&tic->strs,
537 (sizeof(uint16_t) * 2) + buf.bufpos))
538 goto error;
539 le16enc(tic->strs.buf + tic->strs.bufpos, ind);
540 tic->strs.bufpos += sizeof(uint16_t);
541 le16enc(tic->strs.buf + tic->strs.bufpos,
542 buf.bufpos);
543 tic->strs.bufpos += sizeof(uint16_t);
544 memcpy(tic->strs.buf + tic->strs.bufpos,
545 buf.buf, buf.bufpos);
546 tic->strs.bufpos += buf.bufpos;
547 tic->strs.entries++;
549 continue;
552 /* num cap */
553 p = strchr(token, '#');
554 if (p != NULL) {
555 *p++ = '\0';
556 /* Don't use the number if we already have it */
557 ind = _ti_numindex(token);
558 if (ind != -1 &&
559 _ti_find_cap(&tic->nums, 'n', ind) != NULL)
560 continue;
562 cnum = strtol(p, &e, 0);
563 if (*e != '\0') {
564 dowarn(flags, "%s: %s: not a number",
565 tic->name, token);
566 continue;
568 if (!VALID_NUMERIC(cnum)) {
569 dowarn(flags, "%s: %s: number out of range",
570 tic->name, token);
571 continue;
573 num = (short)cnum;
574 if (ind == -1)
575 _ti_store_extra(tic, 1, token, 'n', -1,
576 num, NULL, 0, flags);
577 else {
578 if (_ti_grow_tbuf(&tic->nums,
579 sizeof(uint16_t) * 2) == NULL)
580 goto error;
581 le16enc(tic->nums.buf + tic->nums.bufpos, ind);
582 tic->nums.bufpos += sizeof(uint16_t);
583 le16enc(tic->nums.buf + tic->nums.bufpos, num);
584 tic->nums.bufpos += sizeof(uint16_t);
585 tic->nums.entries++;
587 continue;
590 flag = 1;
591 len = strlen(token) - 1;
592 if (token[len] == '@') {
593 flag = CANCELLED_BOOLEAN;
594 token[len] = '\0';
596 ind = _ti_flagindex(token);
597 if (ind == -1 && flag == CANCELLED_BOOLEAN) {
598 if ((ind = _ti_numindex(token)) != -1) {
599 if (_ti_find_cap(&tic->nums, 'n', ind) != NULL)
600 continue;
601 if (_ti_grow_tbuf(&tic->nums,
602 sizeof(uint16_t) * 2) == NULL)
603 goto error;
604 le16enc(tic->nums.buf + tic->nums.bufpos, ind);
605 tic->nums.bufpos += sizeof(uint16_t);
606 le16enc(tic->nums.buf + tic->nums.bufpos,
607 (uint16_t)CANCELLED_NUMERIC);
608 tic->nums.bufpos += sizeof(uint16_t);
609 tic->nums.entries++;
610 continue;
611 } else if ((ind = _ti_strindex(token)) != -1) {
612 if (_ti_find_cap(&tic->strs, 's', ind) != NULL)
613 continue;
614 if (_ti_grow_tbuf(&tic->strs,
615 (sizeof(uint16_t) * 2) + 1) == NULL)
616 goto error;
617 le16enc(tic->strs.buf + tic->strs.bufpos, ind);
618 tic->strs.bufpos += sizeof(uint16_t);
619 le16enc(tic->strs.buf + tic->strs.bufpos, 0);
620 tic->strs.bufpos += sizeof(uint16_t);
621 tic->strs.entries++;
622 continue;
625 if (ind == -1)
626 _ti_store_extra(tic, 1, token, 'f', flag, 0, NULL, 0,
627 flags);
628 else if (_ti_find_cap(&tic->flags, 'f', ind) == NULL) {
629 if (_ti_grow_tbuf(&tic->flags, sizeof(uint16_t) + 1)
630 == NULL)
631 goto error;
632 le16enc(tic->flags.buf + tic->flags.bufpos, ind);
633 tic->flags.bufpos += sizeof(uint16_t);
634 tic->flags.buf[tic->flags.bufpos++] = flag;
635 tic->flags.entries++;
639 free(buf.buf);
640 return tic;
642 error:
643 free(buf.buf);
644 _ti_freetic(tic);
645 return NULL;
648 void
649 _ti_freetic(TIC *tic)
652 if (tic != NULL) {
653 free(tic->name);
654 free(tic->alias);
655 free(tic->desc);
656 free(tic->extras.buf);
657 free(tic->flags.buf);
658 free(tic->nums.buf);
659 free(tic->strs.buf);
660 free(tic);