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