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