tic.c revision 1.2 1 /* $NetBSD: tic.c,v 1.2 2010/02/05 12:31:56 roy Exp $ */
2
3 /*
4 * Copyright (c) 2009 The NetBSD Foundation, Inc.
5 *
6 * This code is derived from software contributed to The NetBSD Foundation
7 * by Roy Marples.
8 *
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.
17 *
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.
28 */
29
30 #if HAVE_NBTOOL_CONFIG_H
31 #include "nbtool_config.h"
32 #endif
33
34 #include <sys/cdefs.h>
35 __RCSID("$NetBSD: tic.c,v 1.2 2010/02/05 12:31:56 roy Exp $");
36
37 #include <sys/types.h>
38
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <getopt.h>
43 #include <limits.h>
44 #include <fcntl.h>
45 #include <ndbm.h>
46 #include <stdarg.h>
47 #include <stdlib.h>
48 #include <stdio.h>
49 #include <string.h>
50 #include <term_private.h>
51 #include <term.h>
52
53 #define UINT16_T_MAX 0xffff
54
55 typedef struct tbuf {
56 char *buf;
57 size_t buflen;
58 size_t bufpos;
59 size_t entries;
60 } TBUF;
61
62 typedef struct tic {
63 char *name;
64 char *alias;
65 char *desc;
66 TBUF flags;
67 TBUF nums;
68 TBUF strs;
69 TBUF extras;
70 } TIC;
71
72 /* We store the full list of terminals we have instead of iterating
73 through the database as the sequential iterator doesn't work
74 the the data size stored changes N amount which ours will. */
75 typedef struct term {
76 struct term *next;
77 char *name;
78 char type;
79 TIC tic;
80 } TERM;
81 static TERM *terms;
82
83 static int error_exit;
84 static int aflag, xflag;
85 static char *dbname;
86
87 static TBUF scratch;
88
89 static void
90 do_unlink(void)
91 {
92
93 if (dbname != NULL)
94 unlink(dbname);
95 }
96
97 static void __attribute__((__format__(__printf__, 1, 2)))
98 dowarn(const char *fmt, ...)
99 {
100 va_list va;
101
102 error_exit = 1;
103 va_start(va, fmt);
104 vwarnx(fmt, va);
105 va_end(va);
106 }
107
108 static char *
109 grow_tbuf(TBUF *tbuf, size_t len)
110 {
111 char *buf;
112 size_t l;
113
114 l = tbuf->bufpos + len;
115 if (l > tbuf->buflen) {
116 if (tbuf->bufpos == 0) {
117 buf = malloc(l);
118 if (buf == NULL)
119 err(1, "malloc (%zu bytes)", l);
120 } else {
121 buf = realloc(tbuf->buf, l);
122 if (buf == NULL)
123 err(1, "realloc (%zu bytes)", l);
124 }
125 tbuf->buf = buf;
126 tbuf->buflen = l;
127 }
128 return tbuf->buf;
129 }
130
131 static char *
132 find_cap(TBUF *tbuf, char type, short ind)
133 {
134 size_t n;
135 short num;
136 char *cap;
137
138 cap = tbuf->buf;
139 for (n = tbuf->entries; n > 0; n--) {
140 num = le16dec(cap);
141 cap += sizeof(uint16_t);
142 if (num == ind)
143 return cap;
144 switch (type) {
145 case 'f':
146 cap++;
147 break;
148 case 'n':
149 cap += sizeof(uint16_t);
150 break;
151 case 's':
152 num = le16dec(cap);
153 cap += sizeof(uint16_t);
154 cap += num;
155 break;
156 }
157 }
158 return NULL;
159 }
160
161 static char *
162 find_extra(TBUF *tbuf, const char *code)
163 {
164 size_t n;
165 short num;
166 char *cap;
167
168 cap = tbuf->buf;
169 for (n = tbuf->entries; n > 0; n--) {
170 num = le16dec(cap);
171 cap += sizeof(uint16_t);
172 if (strcmp(cap, code) == 0)
173 return cap + num;
174 cap += num;
175 switch (*cap++) {
176 case 'f':
177 cap++;
178 break;
179 case 'n':
180 cap += sizeof(uint16_t);
181 break;
182 case 's':
183 num = le16dec(cap);
184 cap += sizeof(uint16_t);
185 cap += num;
186 break;
187 }
188 }
189 return NULL;
190 }
191
192 static size_t
193 store_extra(TIC *tic, int wrn, char *id, char type, char flag, short num,
194 char *str, size_t strl)
195 {
196 size_t l;
197
198 if (strcmp(id, "use") != 0) {
199 if (find_extra(&tic->extras, id) != NULL)
200 return 0;
201 if (xflag == 0) {
202 if (wrn != 0)
203 dowarn("%s: %s: unknown capability",
204 tic->name, id);
205 return 0;
206 }
207 }
208
209 l = strlen(id) + 1;
210 if (l > UINT16_T_MAX) {
211 dowarn("%s: %s: cap name is too long", tic->name, id);
212 return 0;
213 }
214
215 grow_tbuf(&tic->extras, l + strl + (sizeof(uint16_t) * 2) + 1);
216 le16enc(tic->extras.buf + tic->extras.bufpos, l);
217 tic->extras.bufpos += sizeof(uint16_t);
218 memcpy(tic->extras.buf + tic->extras.bufpos, id, l);
219 tic->extras.bufpos += l;
220 tic->extras.buf[tic->extras.bufpos++] = type;
221 switch (type) {
222 case 'f':
223 tic->extras.buf[tic->extras.bufpos++] = flag;
224 break;
225 case 'n':
226 le16enc(tic->extras.buf + tic->extras.bufpos, num);
227 tic->extras.bufpos += sizeof(uint16_t);
228 break;
229 case 's':
230 le16enc(tic->extras.buf + tic->extras.bufpos, strl);
231 tic->extras.bufpos += sizeof(uint16_t);
232 memcpy(tic->extras.buf + tic->extras.bufpos, str, strl);
233 tic->extras.bufpos += strl;
234 break;
235 }
236 tic->extras.entries++;
237 return 1;
238 }
239
240 static int
241 save_term(DBM *db, TERM *term)
242 {
243 size_t buflen, len, alen, dlen;
244 char *cap;
245 datum key, value;
246 TIC *tic;
247
248 scratch.bufpos = 0;
249 tic = &term->tic;
250 len = strlen(tic->name) + 1;
251 if (tic->alias == NULL)
252 alen = 0;
253 else
254 alen = strlen(tic->alias) + 1;
255 if (tic->desc == NULL)
256 dlen = 0;
257 else
258 dlen = strlen(tic->desc) + 1;
259 buflen = sizeof(char) +
260 sizeof(uint16_t) + len +
261 //sizeof(uint16_t) + alen +
262 sizeof(uint16_t) + dlen +
263 (sizeof(uint16_t) * 2) + tic->flags.bufpos +
264 (sizeof(uint16_t) * 2) + tic->nums.bufpos +
265 (sizeof(uint16_t) * 2) + tic->strs.bufpos +
266 (sizeof(uint16_t) * 2) + tic->extras.bufpos;
267 grow_tbuf(&scratch, buflen);
268 cap = scratch.buf;
269 if (term->type == 'a')
270 *cap++ = 0;
271 else
272 *cap++ = 2; /* version */
273 le16enc(cap, len);
274 cap += sizeof(uint16_t);
275 memcpy(cap, tic->name, len);
276 cap += len;
277 if (term->type != 'a') {
278 le16enc(cap, alen);
279 cap += sizeof(uint16_t);
280 if (tic->alias != NULL) {
281 memcpy(cap, tic->alias, alen);
282 cap += alen;
283 }
284 le16enc(cap, dlen);
285 cap += sizeof(uint16_t);
286 if (tic->desc != NULL) {
287 memcpy(cap, tic->desc, dlen);
288 cap += dlen;
289 }
290
291 if (tic->flags.entries == 0) {
292 le16enc(cap, 0);
293 cap += sizeof(uint16_t);
294 } else {
295 le16enc(cap, (tic->flags.bufpos + sizeof(uint16_t)));
296 cap += sizeof(uint16_t);
297 le16enc(cap, tic->flags.entries);
298 cap += sizeof(uint16_t);
299 memcpy(cap, tic->flags.buf, tic->flags.bufpos);
300 cap += tic->flags.bufpos;
301 }
302
303 if (tic->nums.entries == 0) {
304 le16enc(cap, 0);
305 cap += sizeof(uint16_t);
306 } else {
307 le16enc(cap, (tic->nums.bufpos + sizeof(uint16_t)));
308 cap += sizeof(uint16_t);
309 le16enc(cap, tic->nums.entries);
310 cap += sizeof(uint16_t);
311 memcpy(cap, tic->nums.buf, tic->nums.bufpos);
312 cap += tic->nums.bufpos;
313 }
314
315 if (tic->strs.entries == 0) {
316 le16enc(cap, 0);
317 cap += sizeof(uint16_t);
318 } else {
319 le16enc(cap, (tic->strs.bufpos + sizeof(uint16_t)));
320 cap += sizeof(uint16_t);
321 le16enc(cap, tic->strs.entries);
322 cap += sizeof(uint16_t);
323 memcpy(cap, tic->strs.buf, tic->strs.bufpos);
324 cap += tic->strs.bufpos;
325 }
326
327 if (tic->extras.entries == 0) {
328 le16enc(cap, 0);
329 cap += sizeof(uint16_t);
330 } else {
331 le16enc(cap, (tic->extras.bufpos + sizeof(uint16_t)));
332 cap += sizeof(uint16_t);
333 le16enc(cap, tic->extras.entries);
334 cap += sizeof(uint16_t);
335 memcpy(cap, tic->extras.buf, tic->extras.bufpos);
336 cap += tic->extras.bufpos;
337 }
338 }
339
340 key.dptr = term->name;
341 key.dsize = strlen(term->name);
342 value.dptr = scratch.buf;
343 value.dsize = cap - scratch.buf;
344 if (dbm_store(db, key, value, DBM_REPLACE) == -1)
345 err(1, "dbm_store");
346 return 0;
347 }
348
349 static TERM *
350 find_term(const char *name)
351 {
352 TERM *term;
353
354 for (term = terms; term != NULL; term = term->next)
355 if (strcmp(term->name, name) == 0)
356 return term;
357 return NULL;
358 }
359
360 static TERM *
361 store_term(const char *name, char type)
362 {
363 TERM *term;
364
365 term = calloc(1, sizeof(*term));
366 if (term == NULL)
367 errx(1, "malloc");
368 term->name = strdup(name);
369 term->type = type;
370 if (term->name == NULL)
371 errx(1, "malloc");
372 term->next = terms;
373 terms = term;
374 return term;
375 }
376
377 static void
378 encode_string(const char *term, const char *cap, TBUF *tbuf, const char *str)
379 {
380 int slash, i, num;
381 char ch, *p, *s, last;
382
383 grow_tbuf(tbuf, strlen(str) + 1);
384 p = s = tbuf->buf + tbuf->bufpos;
385 slash = 0;
386 last = '\0';
387 /* Convert escape codes */
388 while ((ch = *str++) != '\0') {
389 if (slash == 0 && ch == '\\') {
390 slash = 1;
391 continue;
392 }
393 if (slash == 0) {
394 if (last != '%' && ch == '^') {
395 ch = *str++;
396 if (((unsigned char)ch) >= 128)
397 dowarn("%s: %s: illegal ^ character",
398 term, cap);
399 if (ch == '\0')
400 break;
401 if (ch == '?')
402 ch = '\177';
403 else if ((ch &= 037) == 0)
404 ch = 128;
405 }
406 *p++ = ch;
407 last = ch;
408 continue;
409 }
410 slash = 0;
411 if (ch >= '0' && ch <= '7') {
412 num = ch - '0';
413 for (i = 0; i < 2; i++) {
414 if (*str < '0' || *str > '7') {
415 if (isdigit((unsigned char)*str))
416 dowarn("%s: %s: non octal"
417 " digit", term, cap);
418 else
419 break;
420 }
421 num = num * 8 + *str++ - '0';
422 }
423 if (num == 0)
424 num = 0200;
425 *p++ = (char)num;
426 continue;
427 }
428 switch (ch) {
429 case 'a':
430 *p++ = '\a';
431 break;
432 case 'b':
433 *p++ = '\b';
434 break;
435 case 'e': /* FALLTHROUGH */
436 case 'E':
437 *p++ = '\033';
438 break;
439 case 'f':
440 *p++ = '\014';
441 break;
442 case 'l': /* FALLTHROUGH */
443 case 'n':
444 *p++ = '\n';
445 break;
446 case 'r':
447 *p++ = '\r';
448 break;
449 case 's':
450 *p++ = ' ';
451 break;
452 case 't':
453 *p++ = '\t';
454 break;
455 default:
456
457 /* We should warn here */
458 case '^':
459 case ',':
460 case ':':
461 case '|':
462 *p++ = ch;
463 break;
464 }
465 last = ch;
466 }
467 *p++ = '\0';
468 tbuf->bufpos += p - s;
469 }
470
471 static int
472 process_entry(TBUF *buf)
473 {
474 char *cap, *capstart, *p, *e, *name, *desc, *alias, flag;
475 long num;
476 int slash;
477 ssize_t ind;
478 size_t len;
479 TERM *term;
480 TIC *tic;
481
482 if (buf->bufpos == 0)
483 return 0;
484 /* Terminate the string */
485 buf->buf[buf->bufpos - 1] = '\0';
486 /* First rewind the buffer for new entries */
487 buf->bufpos = 0;
488
489 if (isspace((unsigned char)*buf->buf))
490 return 0;
491
492 cap = strchr(buf->buf, '\n');
493 if (cap == NULL)
494 return 0;
495 e = cap - 1;
496 if (*e == ',')
497 *e = '\0';
498 *cap++ = '\0';
499
500 name = buf->buf;
501 desc = strrchr(buf->buf, '|');
502 if (desc != NULL)
503 *desc++ = '\0';
504 alias = strchr(buf->buf, '|');
505 if (alias != NULL)
506 *alias++ = '\0';
507
508 if (*e != '\0')
509 dowarn("%s: description missing separator", buf->buf);
510
511 /* If we already have this term, abort */
512 if (find_term(name) != NULL) {
513 dowarn("%s: duplicate entry", name);
514 return 0;
515 }
516 term = store_term(name, 't');
517 tic = &term->tic;
518 tic->name = strdup(name);
519 if (tic->name == NULL)
520 err(1, "malloc");
521 if (alias != NULL) {
522 tic->alias = strdup(alias);
523 if (tic->alias == NULL)
524 err(1, "malloc");
525 }
526 if (desc != NULL) {
527 tic->desc = strdup(desc);
528 if (tic->desc == NULL)
529 err(1, "malloc");
530 }
531
532 do {
533 while (isspace((unsigned char)*cap))
534 cap++;
535 if (*cap == '\0')
536 break;
537 slash = 0;
538 for (capstart = cap;
539 *cap != '\0' && (slash == 1 || *cap != ',');
540 cap++)
541 {
542 if (slash == 0) {
543 if (*cap == '\\')
544 slash = 1;
545 } else
546 slash = 0;
547 continue;
548 }
549 *cap++ = '\0';
550
551 /* Skip commented caps */
552 if (aflag == 0 && capstart[0] == '.')
553 continue;
554
555 /* Obsolete entries */
556 if (capstart[0] == 'O' && capstart[1] == 'T') {
557 if (xflag == 0)
558 continue;
559 capstart += 2;
560 }
561
562 /* str cap */
563 p = strchr(capstart, '=');
564 if (p != NULL) {
565 *p++ = '\0';
566 /* Don't use the string if we already have it */
567 ind = _ti_strindex(capstart);
568 if (ind != -1 &&
569 find_cap(&tic->strs, 's', ind) != NULL)
570 continue;
571
572 /* Encode the string to our scratch buffer */
573 scratch.bufpos = 0;
574 encode_string(tic->name, capstart, &scratch, p);
575 if (scratch.bufpos > UINT16_T_MAX) {
576 dowarn("%s: %s: string is too long",
577 tic->name, capstart);
578 continue;
579 }
580 if (!VALID_STRING(scratch.buf)) {
581 dowarn("%s: %s: invalid string",
582 tic->name, capstart);
583 continue;
584 }
585
586 if (ind == -1)
587 store_extra(tic, 1, capstart, 's', -1, -2,
588 scratch.buf, scratch.bufpos);
589 else {
590 grow_tbuf(&tic->strs, (sizeof(uint16_t) * 2) +
591 scratch.bufpos);
592 le16enc(tic->strs.buf + tic->strs.bufpos, ind);
593 tic->strs.bufpos += sizeof(uint16_t);
594 le16enc(tic->strs.buf + tic->strs.bufpos,
595 scratch.bufpos);
596 tic->strs.bufpos += sizeof(uint16_t);
597 memcpy(tic->strs.buf + tic->strs.bufpos,
598 scratch.buf, scratch.bufpos);
599 tic->strs.bufpos += scratch.bufpos;
600 tic->strs.entries++;
601 }
602 continue;
603 }
604
605 /* num cap */
606 p = strchr(capstart, '#');
607 if (p != NULL) {
608 *p++ = '\0';
609 /* Don't use the number if we already have it */
610 ind = _ti_numindex(capstart);
611 if (ind != -1 &&
612 find_cap(&tic->nums, 'n', ind) != NULL)
613 continue;
614
615 num = strtol(p, &e, 0);
616 if (*e != '\0') {
617 dowarn("%s: %s: not a number",
618 tic->name, capstart);
619 continue;
620 }
621 if (!VALID_NUMERIC(num)) {
622 dowarn("%s: %s: number out of range",
623 tic->name, capstart);
624 continue;
625 }
626 if (ind == -1)
627 store_extra(tic, 1, capstart, 'n', -1,
628 num, NULL, 0);
629 else {
630 grow_tbuf(&tic->nums, sizeof(uint16_t) * 2);
631 le16enc(tic->nums.buf + tic->nums.bufpos, ind);
632 tic->nums.bufpos += sizeof(uint16_t);
633 le16enc(tic->nums.buf + tic->nums.bufpos, num);
634 tic->nums.bufpos += sizeof(uint16_t);
635 tic->nums.entries++;
636 }
637 continue;
638 }
639
640 flag = 1;
641 len = strlen(capstart) - 1;
642 if (capstart[len] == '@') {
643 flag = CANCELLED_BOOLEAN;
644 capstart[len] = '\0';
645 }
646 ind = _ti_flagindex(capstart);
647 if (ind == -1 && flag == CANCELLED_BOOLEAN) {
648 if ((ind = _ti_numindex(capstart)) != -1) {
649 if (find_cap(&tic->nums, 'n', ind) != NULL)
650 continue;
651 grow_tbuf(&tic->nums, sizeof(uint16_t) * 2);
652 le16enc(tic->nums.buf + tic->nums.bufpos, ind);
653 tic->nums.bufpos += sizeof(uint16_t);
654 le16enc(tic->nums.buf + tic->nums.bufpos,
655 CANCELLED_NUMERIC);
656 tic->nums.bufpos += sizeof(uint16_t);
657 tic->nums.entries++;
658 continue;
659 } else if ((ind = _ti_strindex(capstart)) != -1) {
660 if (find_cap(&tic->strs, 's', ind) != NULL)
661 continue;
662 grow_tbuf(&tic->strs,
663 (sizeof(uint16_t) * 2) + 1);
664 le16enc(tic->strs.buf + tic->strs.bufpos, ind);
665 tic->strs.bufpos += sizeof(uint16_t);
666 le16enc(tic->strs.buf + tic->strs.bufpos, 0);
667 tic->strs.bufpos += sizeof(uint16_t);
668 tic->strs.entries++;
669 continue;
670 }
671 }
672 if (ind == -1)
673 store_extra(tic, 1, capstart, 'f', flag, 0, NULL, 0);
674 else if (find_cap(&tic->flags, 'f', ind) == NULL) {
675 grow_tbuf(&tic->flags, sizeof(uint16_t) + 1);
676 le16enc(tic->flags.buf + tic->flags.bufpos, ind);
677 tic->flags.bufpos += sizeof(uint16_t);
678 tic->flags.buf[tic->flags.bufpos++] = flag;
679 tic->flags.entries++;
680 }
681 } while (*cap == ',' || isspace((unsigned char)*cap));
682
683 /* Create aliased terms */
684 if (alias != NULL) {
685 while (alias != NULL && *alias != '\0') {
686 desc = strchr(alias, '|');
687 if (desc != NULL)
688 *desc++ = '\0';
689 if (find_term(alias) != NULL) {
690 dowarn("%s: has alias for already assigned"
691 " term %s", tic->name, alias);
692 } else {
693 term = store_term(alias, 'a');
694 term->tic.name = strdup(tic->name);
695 if (term->tic.name == NULL)
696 err(1, "malloc");
697 }
698 alias = desc;
699 }
700 }
701
702 return 0;
703 }
704
705 static void
706 merge(TIC *rtic, TIC *utic)
707 {
708 char *cap, flag, *code, type, *str;
709 short ind, num;
710 size_t n;
711
712 cap = utic->flags.buf;
713 for (n = utic->flags.entries; n > 0; n--) {
714 ind = le16dec(cap);
715 cap += sizeof(uint16_t);
716 flag = *cap++;
717 if (VALID_BOOLEAN(flag) &&
718 find_cap(&rtic->flags, 'f', ind) == NULL)
719 {
720 grow_tbuf(&rtic->flags, sizeof(uint16_t) + 1);
721 le16enc(rtic->flags.buf + rtic->flags.bufpos, ind);
722 rtic->flags.bufpos += sizeof(uint16_t);
723 rtic->flags.buf[rtic->flags.bufpos++] = flag;
724 rtic->flags.entries++;
725 }
726 }
727
728 cap = utic->nums.buf;
729 for (n = utic->nums.entries; n > 0; n--) {
730 ind = le16dec(cap);
731 cap += sizeof(uint16_t);
732 num = le16dec(cap);
733 cap += sizeof(uint16_t);
734 if (VALID_NUMERIC(num) &&
735 find_cap(&rtic->nums, 'n', ind) == NULL)
736 {
737 grow_tbuf(&rtic->nums, sizeof(uint16_t) * 2);
738 le16enc(rtic->nums.buf + rtic->nums.bufpos, ind);
739 rtic->nums.bufpos += sizeof(uint16_t);
740 le16enc(rtic->nums.buf + rtic->nums.bufpos, num);
741 rtic->nums.bufpos += sizeof(uint16_t);
742 rtic->nums.entries++;
743 }
744 }
745
746 cap = utic->strs.buf;
747 for (n = utic->strs.entries; n > 0; n--) {
748 ind = le16dec(cap);
749 cap += sizeof(uint16_t);
750 num = le16dec(cap);
751 cap += sizeof(uint16_t);
752 if (num > 0 &&
753 find_cap(&rtic->strs, 's', ind) == NULL)
754 {
755 grow_tbuf(&rtic->strs, (sizeof(uint16_t) * 2) + num);
756 le16enc(rtic->strs.buf + rtic->strs.bufpos, ind);
757 rtic->strs.bufpos += sizeof(uint16_t);
758 le16enc(rtic->strs.buf + rtic->strs.bufpos, num);
759 rtic->strs.bufpos += sizeof(uint16_t);
760 memcpy(rtic->strs.buf + rtic->strs.bufpos,
761 cap, num);
762 rtic->strs.bufpos += num;
763 rtic->strs.entries++;
764 }
765 cap += num;
766 }
767
768 cap = utic->extras.buf;
769 for (n = utic->extras.entries; n > 0; n--) {
770 num = le16dec(cap);
771 cap += sizeof(uint16_t);
772 code = cap;
773 cap += num;
774 type = *cap++;
775 flag = 0;
776 str = NULL;
777 switch (type) {
778 case 'f':
779 flag = *cap++;
780 if (!VALID_BOOLEAN(flag))
781 continue;
782 break;
783 case 'n':
784 num = le16dec(cap);
785 cap += sizeof(uint16_t);
786 if (!VALID_NUMERIC(num))
787 continue;
788 break;
789 case 's':
790 num = le16dec(cap);
791 cap += sizeof(uint16_t);
792 str = cap;
793 cap += num;
794 if (num == 0)
795 continue;
796 break;
797 }
798 store_extra(rtic, 0, code, type, flag, num, str, num);
799 }
800 }
801
802 static size_t
803 merge_use(void)
804 {
805 size_t skipped, merged, memn;
806 char *cap, *scap;
807 uint16_t num;
808 TIC *rtic, *utic;
809 TERM *term, *uterm;;
810
811 skipped = merged = 0;
812 for (term = terms; term != NULL; term = term->next) {
813 if (term->type == 'a')
814 continue;
815 rtic = &term->tic;
816 while ((cap = find_extra(&rtic->extras, "use")) != NULL) {
817 if (*cap++ != 's') {
818 dowarn("%s: use is not string", rtic->name);
819 break;
820 }
821 cap += sizeof(uint16_t);
822 if (strcmp(rtic->name, cap) == 0) {
823 dowarn("%s: uses itself", rtic->name);
824 goto remove;
825 }
826 uterm = find_term(cap);
827 if (uterm != NULL && uterm->type == 'a')
828 uterm = find_term(uterm->tic.name);
829 if (uterm == NULL) {
830 dowarn("%s: no use record for %s",
831 rtic->name, cap);
832 goto remove;
833 }
834 utic = &uterm->tic;
835 if (strcmp(utic->name, rtic->name) == 0) {
836 dowarn("%s: uses itself", rtic->name);
837 goto remove;
838 }
839 if (find_extra(&utic->extras, "use") != NULL) {
840 skipped++;
841 break;
842 }
843 cap = find_extra(&rtic->extras, "use");
844 merge(rtic, utic);
845 remove:
846 /* The pointers may have changed, find the use again */
847 cap = find_extra(&rtic->extras, "use");
848 if (cap == NULL)
849 dowarn("%s: use no longer exists - impossible",
850 rtic->name);
851 else {
852 scap = cap - (4 + sizeof(uint16_t));
853 cap++;
854 num = le16dec(cap);
855 cap += sizeof(uint16_t) + num;
856 memn = rtic->extras.bufpos -
857 (cap - rtic->extras.buf);
858 memcpy(scap, cap, memn);
859 rtic->extras.bufpos -= cap - scap;
860 cap = scap;
861 rtic->extras.entries--;
862 merged++;
863 }
864 }
865 }
866
867 if (merged == 0 && skipped != 0)
868 dowarn("circular use detected");
869 return merged;
870 }
871
872 int
873 main(int argc, char **argv)
874 {
875 int ch, cflag, sflag;
876 char *source, *p, *buf, *ofile;
877 FILE *f;
878 DBM *db;
879 size_t len, buflen, nterm, nalias;
880 TBUF tbuf;
881 TERM *term;
882
883 cflag = sflag = 0;
884 ofile = NULL;
885 while ((ch = getopt(argc, argv, "aco:sx")) != -1)
886 switch (ch) {
887 case 'a':
888 aflag++;
889 xflag++;
890 break;
891 case 'c':
892 cflag++;
893 break;
894 case 'o':
895 ofile = optarg;
896 break;
897 case 's':
898 sflag++;
899 break;
900 case 'x':
901 xflag++;
902 break;
903 case '?': /* FALLTHROUGH */
904 default:
905 fprintf(stderr, "usage: %s [-acsx] [-o file] source\n",
906 getprogname());
907 return EXIT_FAILURE;
908 }
909
910 if (optind == argc)
911 errx(1, "No source file given");
912 source = argv[optind++];
913 f = fopen(source, "r");
914 if (f == NULL)
915 err(1, "fopen: %s", source);
916 if (cflag == 0) {
917 if (ofile == NULL)
918 ofile = source;
919 len = strlen(ofile) + 9;
920 dbname = malloc(len + 4); /* For adding .db after open */
921 if (dbname == NULL)
922 err(1, "malloc");
923 snprintf(dbname, len, "%s.tmp", ofile);
924 db = dbm_open(dbname, O_CREAT | O_RDWR | O_TRUNC, DEFFILEMODE);
925 if (db == NULL)
926 err(1, "dbopen: %s", source);
927 p = dbname + strlen(dbname);
928 *p++ = '.';
929 *p++ = 'd';
930 *p++ = 'b';
931 *p++ = '\0';
932 atexit(do_unlink);
933 } else
934 db = NULL; /* satisfy gcc warning */
935
936 tbuf.buflen = tbuf.bufpos = 0;
937 while ((buf = fgetln(f, &buflen)) != NULL) {
938 /* Skip comments */
939 if (*buf == '#')
940 continue;
941 if (buf[buflen - 1] != '\n') {
942 process_entry(&tbuf);
943 dowarn("last line is not a comment"
944 " and does not end with a newline");
945 continue;
946 }
947 /*
948 If the first char is space not a space then we have a
949 new entry, so process it.
950 */
951 if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0)
952 process_entry(&tbuf);
953
954 /* Grow the buffer if needed */
955 grow_tbuf(&tbuf, buflen);
956 /* Append the string */
957 memcpy(tbuf.buf + tbuf.bufpos, buf, buflen);
958 tbuf.bufpos += buflen;
959 }
960 /* Process the last entry if not done already */
961 process_entry(&tbuf);
962
963 /* Merge use entries until we have merged all we can */
964 while (merge_use() != 0)
965 ;
966
967 if (cflag != 0)
968 return error_exit;
969
970 /* Save the terms */
971 nterm = nalias = 0;
972 for (term = terms; term != NULL; term = term->next) {
973 save_term(db, term);
974 if (term->type == 'a')
975 nalias++;
976 else
977 nterm++;
978 }
979
980 /* done! */
981 dbm_close(db);
982
983 /* Rename the tmp db to the real one now */
984 len = strlen(ofile) + 4;
985 p = malloc(len);
986 if (p == NULL)
987 err(1, "malloc");
988 snprintf(p, len, "%s.db", ofile);
989 if (rename(dbname, p) == -1)
990 err(1, "rename");
991 free(dbname);
992 dbname = NULL;
993
994 if (sflag != 0)
995 fprintf(stderr, "%zu entries and %zu aliases written to %s\n",
996 nterm, nalias, p);
997
998 return EXIT_SUCCESS;
999 }
1000