tic.c revision 1.32 1 1.32 roy /* $NetBSD: tic.c,v 1.32 2020/03/13 15:19:25 roy Exp $ */
2 1.1 roy
3 1.1 roy /*
4 1.32 roy * Copyright (c) 2009, 2010, 2020 The NetBSD Foundation, Inc.
5 1.1 roy *
6 1.1 roy * This code is derived from software contributed to The NetBSD Foundation
7 1.1 roy * by Roy Marples.
8 1.1 roy *
9 1.1 roy * Redistribution and use in source and binary forms, with or without
10 1.1 roy * modification, are permitted provided that the following conditions
11 1.1 roy * are met:
12 1.1 roy * 1. Redistributions of source code must retain the above copyright
13 1.1 roy * notice, this list of conditions and the following disclaimer.
14 1.1 roy * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 roy * notice, this list of conditions and the following disclaimer in the
16 1.1 roy * documentation and/or other materials provided with the distribution.
17 1.1 roy *
18 1.1 roy * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 1.1 roy * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 1.1 roy * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 1.1 roy * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 1.1 roy * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 1.1 roy * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 1.1 roy * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 1.1 roy * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 1.1 roy * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 1.1 roy * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 1.1 roy */
29 1.1 roy
30 1.1 roy #if HAVE_NBTOOL_CONFIG_H
31 1.1 roy #include "nbtool_config.h"
32 1.1 roy #endif
33 1.1 roy
34 1.1 roy #include <sys/cdefs.h>
35 1.32 roy __RCSID("$NetBSD: tic.c,v 1.32 2020/03/13 15:19:25 roy Exp $");
36 1.1 roy
37 1.1 roy #include <sys/types.h>
38 1.14 joerg #include <sys/queue.h>
39 1.27 christos #include <sys/stat.h>
40 1.8 pgoyette
41 1.9 pgoyette #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H
42 1.7 pgoyette #include <sys/endian.h>
43 1.8 pgoyette #endif
44 1.1 roy
45 1.20 joerg #include <cdbw.h>
46 1.1 roy #include <ctype.h>
47 1.1 roy #include <err.h>
48 1.1 roy #include <errno.h>
49 1.1 roy #include <getopt.h>
50 1.1 roy #include <limits.h>
51 1.1 roy #include <fcntl.h>
52 1.15 joerg #include <search.h>
53 1.1 roy #include <stdarg.h>
54 1.1 roy #include <stdlib.h>
55 1.1 roy #include <stdio.h>
56 1.1 roy #include <string.h>
57 1.1 roy #include <term_private.h>
58 1.1 roy #include <term.h>
59 1.31 joerg #include <unistd.h>
60 1.23 joerg #include <util.h>
61 1.15 joerg
62 1.15 joerg #define HASH_SIZE 16384 /* 2012-06-01: 3600 entries */
63 1.1 roy
64 1.1 roy typedef struct term {
65 1.20 joerg STAILQ_ENTRY(term) next;
66 1.1 roy char *name;
67 1.10 roy TIC *tic;
68 1.20 joerg uint32_t id;
69 1.20 joerg struct term *base_term;
70 1.1 roy } TERM;
71 1.20 joerg static STAILQ_HEAD(, term) terms = STAILQ_HEAD_INITIALIZER(terms);
72 1.1 roy
73 1.1 roy static int error_exit;
74 1.10 roy static int Sflag;
75 1.18 joerg static size_t nterm, nalias;
76 1.1 roy
77 1.13 joerg static void __printflike(1, 2)
78 1.1 roy dowarn(const char *fmt, ...)
79 1.1 roy {
80 1.1 roy va_list va;
81 1.1 roy
82 1.1 roy error_exit = 1;
83 1.1 roy va_start(va, fmt);
84 1.1 roy vwarnx(fmt, va);
85 1.1 roy va_end(va);
86 1.1 roy }
87 1.1 roy
88 1.1 roy static char *
89 1.1 roy grow_tbuf(TBUF *tbuf, size_t len)
90 1.1 roy {
91 1.1 roy char *buf;
92 1.1 roy
93 1.10 roy buf = _ti_grow_tbuf(tbuf, len);
94 1.10 roy if (buf == NULL)
95 1.10 roy err(1, "_ti_grow_tbuf");
96 1.10 roy return buf;
97 1.5 roy }
98 1.5 roy
99 1.5 roy static int
100 1.20 joerg save_term(struct cdbw *db, TERM *term)
101 1.5 roy {
102 1.10 roy uint8_t *buf;
103 1.10 roy ssize_t len;
104 1.20 joerg size_t slen = strlen(term->name) + 1;
105 1.20 joerg
106 1.20 joerg if (term->base_term != NULL) {
107 1.20 joerg len = (ssize_t)slen + 7;
108 1.20 joerg buf = emalloc(len);
109 1.32 roy buf[0] = TERMINFO_ALIAS;
110 1.20 joerg le32enc(buf + 1, term->base_term->id);
111 1.20 joerg le16enc(buf + 5, slen);
112 1.20 joerg memcpy(buf + 7, term->name, slen);
113 1.20 joerg if (cdbw_put(db, term->name, slen, buf, len))
114 1.20 joerg err(1, "cdbw_put");
115 1.26 christos free(buf);
116 1.20 joerg return 0;
117 1.20 joerg }
118 1.5 roy
119 1.10 roy len = _ti_flatten(&buf, term->tic);
120 1.10 roy if (len == -1)
121 1.5 roy return -1;
122 1.1 roy
123 1.20 joerg if (cdbw_put_data(db, buf, len, &term->id))
124 1.20 joerg err(1, "cdbw_put_data");
125 1.20 joerg if (cdbw_put_key(db, term->name, slen, term->id))
126 1.20 joerg err(1, "cdbw_put_key");
127 1.10 roy free(buf);
128 1.1 roy return 0;
129 1.1 roy }
130 1.1 roy
131 1.1 roy static TERM *
132 1.1 roy find_term(const char *name)
133 1.1 roy {
134 1.15 joerg ENTRY elem, *elemp;
135 1.14 joerg
136 1.15 joerg elem.key = __UNCONST(name);
137 1.15 joerg elem.data = NULL;
138 1.15 joerg elemp = hsearch(elem, FIND);
139 1.15 joerg return elemp ? (TERM *)elemp->data : NULL;
140 1.1 roy }
141 1.1 roy
142 1.1 roy static TERM *
143 1.20 joerg store_term(const char *name, TERM *base_term)
144 1.1 roy {
145 1.1 roy TERM *term;
146 1.15 joerg ENTRY elem;
147 1.1 roy
148 1.16 joerg term = ecalloc(1, sizeof(*term));
149 1.16 joerg term->name = estrdup(name);
150 1.20 joerg STAILQ_INSERT_TAIL(&terms, term, next);
151 1.15 joerg elem.key = estrdup(name);
152 1.15 joerg elem.data = term;
153 1.15 joerg hsearch(elem, ENTER);
154 1.18 joerg
155 1.20 joerg term->base_term = base_term;
156 1.20 joerg if (base_term != NULL)
157 1.18 joerg nalias++;
158 1.18 joerg else
159 1.18 joerg nterm++;
160 1.18 joerg
161 1.1 roy return term;
162 1.1 roy }
163 1.1 roy
164 1.1 roy static int
165 1.10 roy process_entry(TBUF *buf, int flags)
166 1.1 roy {
167 1.10 roy char *p, *e, *alias;
168 1.1 roy TERM *term;
169 1.1 roy TIC *tic;
170 1.25 roy
171 1.1 roy if (buf->bufpos == 0)
172 1.1 roy return 0;
173 1.1 roy /* Terminate the string */
174 1.1 roy buf->buf[buf->bufpos - 1] = '\0';
175 1.1 roy /* First rewind the buffer for new entries */
176 1.1 roy buf->bufpos = 0;
177 1.1 roy
178 1.1 roy if (isspace((unsigned char)*buf->buf))
179 1.1 roy return 0;
180 1.1 roy
181 1.10 roy tic = _ti_compile(buf->buf, flags);
182 1.10 roy if (tic == NULL)
183 1.1 roy return 0;
184 1.10 roy
185 1.10 roy if (find_term(tic->name) != NULL) {
186 1.10 roy dowarn("%s: duplicate entry", tic->name);
187 1.10 roy _ti_freetic(tic);
188 1.1 roy return 0;
189 1.1 roy }
190 1.20 joerg term = store_term(tic->name, NULL);
191 1.10 roy term->tic = tic;
192 1.1 roy
193 1.1 roy /* Create aliased terms */
194 1.10 roy if (tic->alias != NULL) {
195 1.17 joerg alias = p = estrdup(tic->alias);
196 1.10 roy while (p != NULL && *p != '\0') {
197 1.10 roy e = strchr(p, '|');
198 1.10 roy if (e != NULL)
199 1.10 roy *e++ = '\0';
200 1.32 roy /* No need to lengthcheck the alias because the main
201 1.32 roy * terminfo description already stores all the aliases
202 1.32 roy * in the same length field as the alias. */
203 1.10 roy if (find_term(p) != NULL) {
204 1.1 roy dowarn("%s: has alias for already assigned"
205 1.10 roy " term %s", tic->name, p);
206 1.1 roy } else {
207 1.20 joerg store_term(p, term);
208 1.1 roy }
209 1.10 roy p = e;
210 1.1 roy }
211 1.19 joerg free(alias);
212 1.1 roy }
213 1.25 roy
214 1.1 roy return 0;
215 1.1 roy }
216 1.1 roy
217 1.1 roy static void
218 1.10 roy merge(TIC *rtic, TIC *utic, int flags)
219 1.1 roy {
220 1.1 roy char *cap, flag, *code, type, *str;
221 1.32 roy short ind, len;
222 1.32 roy int num;
223 1.1 roy size_t n;
224 1.1 roy
225 1.1 roy cap = utic->flags.buf;
226 1.1 roy for (n = utic->flags.entries; n > 0; n--) {
227 1.1 roy ind = le16dec(cap);
228 1.1 roy cap += sizeof(uint16_t);
229 1.1 roy flag = *cap++;
230 1.1 roy if (VALID_BOOLEAN(flag) &&
231 1.10 roy _ti_find_cap(&rtic->flags, 'f', ind) == NULL)
232 1.1 roy {
233 1.10 roy _ti_grow_tbuf(&rtic->flags, sizeof(uint16_t) + 1);
234 1.1 roy le16enc(rtic->flags.buf + rtic->flags.bufpos, ind);
235 1.1 roy rtic->flags.bufpos += sizeof(uint16_t);
236 1.1 roy rtic->flags.buf[rtic->flags.bufpos++] = flag;
237 1.1 roy rtic->flags.entries++;
238 1.1 roy }
239 1.1 roy }
240 1.1 roy
241 1.1 roy cap = utic->nums.buf;
242 1.1 roy for (n = utic->nums.entries; n > 0; n--) {
243 1.1 roy ind = le16dec(cap);
244 1.1 roy cap += sizeof(uint16_t);
245 1.32 roy num = le32dec(cap);
246 1.32 roy cap += sizeof(uint32_t);
247 1.1 roy if (VALID_NUMERIC(num) &&
248 1.10 roy _ti_find_cap(&rtic->nums, 'n', ind) == NULL)
249 1.1 roy {
250 1.32 roy grow_tbuf(&rtic->nums, sizeof(uint16_t) +
251 1.32 roy sizeof(uint32_t));
252 1.1 roy le16enc(rtic->nums.buf + rtic->nums.bufpos, ind);
253 1.1 roy rtic->nums.bufpos += sizeof(uint16_t);
254 1.32 roy le32enc(rtic->nums.buf + rtic->nums.bufpos, num);
255 1.32 roy rtic->nums.bufpos += sizeof(uint32_t);
256 1.1 roy rtic->nums.entries++;
257 1.1 roy }
258 1.1 roy }
259 1.1 roy
260 1.1 roy cap = utic->strs.buf;
261 1.1 roy for (n = utic->strs.entries; n > 0; n--) {
262 1.1 roy ind = le16dec(cap);
263 1.1 roy cap += sizeof(uint16_t);
264 1.32 roy len = le16dec(cap);
265 1.1 roy cap += sizeof(uint16_t);
266 1.32 roy if (len > 0 &&
267 1.10 roy _ti_find_cap(&rtic->strs, 's', ind) == NULL)
268 1.1 roy {
269 1.32 roy grow_tbuf(&rtic->strs, (sizeof(uint16_t) * 2) + len);
270 1.1 roy le16enc(rtic->strs.buf + rtic->strs.bufpos, ind);
271 1.1 roy rtic->strs.bufpos += sizeof(uint16_t);
272 1.32 roy le16enc(rtic->strs.buf + rtic->strs.bufpos, len);
273 1.1 roy rtic->strs.bufpos += sizeof(uint16_t);
274 1.1 roy memcpy(rtic->strs.buf + rtic->strs.bufpos,
275 1.32 roy cap, len);
276 1.32 roy rtic->strs.bufpos += len;
277 1.1 roy rtic->strs.entries++;
278 1.1 roy }
279 1.32 roy cap += len;
280 1.1 roy }
281 1.1 roy
282 1.1 roy cap = utic->extras.buf;
283 1.1 roy for (n = utic->extras.entries; n > 0; n--) {
284 1.1 roy num = le16dec(cap);
285 1.1 roy cap += sizeof(uint16_t);
286 1.1 roy code = cap;
287 1.1 roy cap += num;
288 1.1 roy type = *cap++;
289 1.1 roy flag = 0;
290 1.1 roy str = NULL;
291 1.1 roy switch (type) {
292 1.1 roy case 'f':
293 1.1 roy flag = *cap++;
294 1.1 roy if (!VALID_BOOLEAN(flag))
295 1.1 roy continue;
296 1.1 roy break;
297 1.1 roy case 'n':
298 1.32 roy num = le32dec(cap);
299 1.32 roy cap += sizeof(uint32_t);
300 1.1 roy if (!VALID_NUMERIC(num))
301 1.1 roy continue;
302 1.1 roy break;
303 1.1 roy case 's':
304 1.1 roy num = le16dec(cap);
305 1.1 roy cap += sizeof(uint16_t);
306 1.1 roy str = cap;
307 1.1 roy cap += num;
308 1.1 roy if (num == 0)
309 1.1 roy continue;
310 1.1 roy break;
311 1.1 roy }
312 1.10 roy _ti_store_extra(rtic, 0, code, type, flag, num, str, num,
313 1.10 roy flags);
314 1.1 roy }
315 1.1 roy }
316 1.1 roy
317 1.1 roy static size_t
318 1.10 roy merge_use(int flags)
319 1.1 roy {
320 1.1 roy size_t skipped, merged, memn;
321 1.1 roy char *cap, *scap;
322 1.1 roy uint16_t num;
323 1.1 roy TIC *rtic, *utic;
324 1.1 roy TERM *term, *uterm;;
325 1.1 roy
326 1.1 roy skipped = merged = 0;
327 1.20 joerg STAILQ_FOREACH(term, &terms, next) {
328 1.20 joerg if (term->base_term != NULL)
329 1.1 roy continue;
330 1.10 roy rtic = term->tic;
331 1.10 roy while ((cap = _ti_find_extra(&rtic->extras, "use")) != NULL) {
332 1.1 roy if (*cap++ != 's') {
333 1.1 roy dowarn("%s: use is not string", rtic->name);
334 1.1 roy break;
335 1.1 roy }
336 1.1 roy cap += sizeof(uint16_t);
337 1.1 roy if (strcmp(rtic->name, cap) == 0) {
338 1.1 roy dowarn("%s: uses itself", rtic->name);
339 1.1 roy goto remove;
340 1.1 roy }
341 1.1 roy uterm = find_term(cap);
342 1.20 joerg if (uterm != NULL && uterm->base_term != NULL)
343 1.20 joerg uterm = uterm->base_term;
344 1.1 roy if (uterm == NULL) {
345 1.1 roy dowarn("%s: no use record for %s",
346 1.1 roy rtic->name, cap);
347 1.1 roy goto remove;
348 1.1 roy }
349 1.10 roy utic = uterm->tic;
350 1.1 roy if (strcmp(utic->name, rtic->name) == 0) {
351 1.1 roy dowarn("%s: uses itself", rtic->name);
352 1.1 roy goto remove;
353 1.1 roy }
354 1.10 roy if (_ti_find_extra(&utic->extras, "use") != NULL) {
355 1.1 roy skipped++;
356 1.1 roy break;
357 1.1 roy }
358 1.10 roy cap = _ti_find_extra(&rtic->extras, "use");
359 1.10 roy merge(rtic, utic, flags);
360 1.1 roy remove:
361 1.1 roy /* The pointers may have changed, find the use again */
362 1.10 roy cap = _ti_find_extra(&rtic->extras, "use");
363 1.1 roy if (cap == NULL)
364 1.1 roy dowarn("%s: use no longer exists - impossible",
365 1.1 roy rtic->name);
366 1.1 roy else {
367 1.1 roy scap = cap - (4 + sizeof(uint16_t));
368 1.1 roy cap++;
369 1.1 roy num = le16dec(cap);
370 1.1 roy cap += sizeof(uint16_t) + num;
371 1.1 roy memn = rtic->extras.bufpos -
372 1.1 roy (cap - rtic->extras.buf);
373 1.11 roy memmove(scap, cap, memn);
374 1.1 roy rtic->extras.bufpos -= cap - scap;
375 1.1 roy cap = scap;
376 1.1 roy rtic->extras.entries--;
377 1.1 roy merged++;
378 1.1 roy }
379 1.1 roy }
380 1.1 roy }
381 1.1 roy
382 1.1 roy if (merged == 0 && skipped != 0)
383 1.1 roy dowarn("circular use detected");
384 1.1 roy return merged;
385 1.1 roy }
386 1.1 roy
387 1.5 roy static int
388 1.5 roy print_dump(int argc, char **argv)
389 1.5 roy {
390 1.5 roy TERM *term;
391 1.10 roy uint8_t *buf;
392 1.5 roy int i, n;
393 1.5 roy size_t j, col;
394 1.10 roy ssize_t len;
395 1.5 roy
396 1.6 roy printf("struct compiled_term {\n");
397 1.6 roy printf("\tconst char *name;\n");
398 1.6 roy printf("\tconst char *cap;\n");
399 1.6 roy printf("\tsize_t caplen;\n");
400 1.6 roy printf("};\n\n");
401 1.6 roy
402 1.6 roy printf("const struct compiled_term compiled_terms[] = {\n");
403 1.6 roy
404 1.5 roy n = 0;
405 1.5 roy for (i = 0; i < argc; i++) {
406 1.5 roy term = find_term(argv[i]);
407 1.5 roy if (term == NULL) {
408 1.5 roy warnx("%s: no description for terminal", argv[i]);
409 1.5 roy continue;
410 1.5 roy }
411 1.20 joerg if (term->base_term != NULL) {
412 1.5 roy warnx("%s: cannot dump alias", argv[i]);
413 1.5 roy continue;
414 1.5 roy }
415 1.10 roy /* Don't compile the aliases in, save space */
416 1.10 roy free(term->tic->alias);
417 1.10 roy term->tic->alias = NULL;
418 1.10 roy len = _ti_flatten(&buf, term->tic);
419 1.10 roy if (len == 0 || len == -1)
420 1.5 roy continue;
421 1.5 roy
422 1.6 roy printf("\t{\n");
423 1.6 roy printf("\t\t\"%s\",\n", argv[i]);
424 1.5 roy n++;
425 1.10 roy for (j = 0, col = 0; j < (size_t)len; j++) {
426 1.5 roy if (col == 0) {
427 1.6 roy printf("\t\t\"");
428 1.6 roy col = 16;
429 1.5 roy }
430 1.25 roy
431 1.10 roy col += printf("\\%03o", (uint8_t)buf[j]);
432 1.5 roy if (col > 75) {
433 1.5 roy printf("\"%s\n",
434 1.10 roy j + 1 == (size_t)len ? "," : "");
435 1.5 roy col = 0;
436 1.5 roy }
437 1.5 roy }
438 1.5 roy if (col != 0)
439 1.6 roy printf("\",\n");
440 1.10 roy printf("\t\t%zu\n", len);
441 1.6 roy printf("\t}");
442 1.6 roy if (i + 1 < argc)
443 1.6 roy printf(",");
444 1.6 roy printf("\n");
445 1.10 roy free(buf);
446 1.5 roy }
447 1.6 roy printf("};\n");
448 1.5 roy
449 1.5 roy return n;
450 1.5 roy }
451 1.5 roy
452 1.20 joerg static void
453 1.20 joerg write_database(const char *dbname)
454 1.20 joerg {
455 1.20 joerg struct cdbw *db;
456 1.20 joerg char *tmp_dbname;
457 1.20 joerg TERM *term;
458 1.20 joerg int fd;
459 1.20 joerg
460 1.20 joerg db = cdbw_open();
461 1.20 joerg if (db == NULL)
462 1.20 joerg err(1, "cdbw_open failed");
463 1.20 joerg /* Save the terms */
464 1.20 joerg STAILQ_FOREACH(term, &terms, next)
465 1.20 joerg save_term(db, term);
466 1.20 joerg
467 1.20 joerg easprintf(&tmp_dbname, "%s.XXXXXX", dbname);
468 1.20 joerg fd = mkstemp(tmp_dbname);
469 1.20 joerg if (fd == -1)
470 1.20 joerg err(1, "creating temporary database %s failed", tmp_dbname);
471 1.20 joerg if (cdbw_output(db, fd, "NetBSD terminfo", cdbw_stable_seeder))
472 1.20 joerg err(1, "writing temporary database %s failed", tmp_dbname);
473 1.20 joerg if (fchmod(fd, DEFFILEMODE))
474 1.20 joerg err(1, "fchmod failed");
475 1.20 joerg if (close(fd))
476 1.20 joerg err(1, "writing temporary database %s failed", tmp_dbname);
477 1.20 joerg if (rename(tmp_dbname, dbname))
478 1.20 joerg err(1, "renaming %s to %s failed", tmp_dbname, dbname);
479 1.20 joerg free(tmp_dbname);
480 1.20 joerg cdbw_close(db);
481 1.20 joerg }
482 1.20 joerg
483 1.1 roy int
484 1.1 roy main(int argc, char **argv)
485 1.1 roy {
486 1.10 roy int ch, cflag, sflag, flags;
487 1.20 joerg char *source, *dbname, *buf, *ofile;
488 1.1 roy FILE *f;
489 1.18 joerg size_t buflen;
490 1.12 roy ssize_t len;
491 1.1 roy TBUF tbuf;
492 1.29 roy struct term *term;
493 1.1 roy
494 1.1 roy cflag = sflag = 0;
495 1.1 roy ofile = NULL;
496 1.10 roy flags = TIC_ALIAS | TIC_DESCRIPTION | TIC_WARNING;
497 1.5 roy while ((ch = getopt(argc, argv, "Saco:sx")) != -1)
498 1.1 roy switch (ch) {
499 1.5 roy case 'S':
500 1.5 roy Sflag = 1;
501 1.10 roy /* We still compile aliases so that use= works.
502 1.10 roy * However, it's removed before we flatten to save space. */
503 1.10 roy flags &= ~TIC_DESCRIPTION;
504 1.5 roy break;
505 1.1 roy case 'a':
506 1.10 roy flags |= TIC_COMMENT;
507 1.1 roy break;
508 1.1 roy case 'c':
509 1.4 roy cflag = 1;
510 1.1 roy break;
511 1.1 roy case 'o':
512 1.1 roy ofile = optarg;
513 1.1 roy break;
514 1.1 roy case 's':
515 1.4 roy sflag = 1;
516 1.1 roy break;
517 1.1 roy case 'x':
518 1.10 roy flags |= TIC_EXTRA;
519 1.1 roy break;
520 1.1 roy case '?': /* FALLTHROUGH */
521 1.1 roy default:
522 1.6 roy fprintf(stderr, "usage: %s [-acSsx] [-o file] source\n",
523 1.1 roy getprogname());
524 1.1 roy return EXIT_FAILURE;
525 1.1 roy }
526 1.1 roy
527 1.1 roy if (optind == argc)
528 1.1 roy errx(1, "No source file given");
529 1.1 roy source = argv[optind++];
530 1.1 roy f = fopen(source, "r");
531 1.1 roy if (f == NULL)
532 1.1 roy err(1, "fopen: %s", source);
533 1.1 roy
534 1.15 joerg hcreate(HASH_SIZE);
535 1.15 joerg
536 1.19 joerg buf = tbuf.buf = NULL;
537 1.28 roy buflen = tbuf.buflen = tbuf.bufpos = 0;
538 1.12 roy while ((len = getline(&buf, &buflen, f)) != -1) {
539 1.1 roy /* Skip comments */
540 1.1 roy if (*buf == '#')
541 1.1 roy continue;
542 1.12 roy if (buf[len - 1] != '\n') {
543 1.10 roy process_entry(&tbuf, flags);
544 1.1 roy dowarn("last line is not a comment"
545 1.1 roy " and does not end with a newline");
546 1.1 roy continue;
547 1.1 roy }
548 1.1 roy /*
549 1.28 roy * If the first char is space not a space then we have a
550 1.28 roy * new entry, so process it.
551 1.28 roy */
552 1.1 roy if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0)
553 1.10 roy process_entry(&tbuf, flags);
554 1.25 roy
555 1.1 roy /* Grow the buffer if needed */
556 1.12 roy grow_tbuf(&tbuf, len);
557 1.1 roy /* Append the string */
558 1.12 roy memcpy(tbuf.buf + tbuf.bufpos, buf, len);
559 1.12 roy tbuf.bufpos += len;
560 1.1 roy }
561 1.19 joerg free(buf);
562 1.1 roy /* Process the last entry if not done already */
563 1.10 roy process_entry(&tbuf, flags);
564 1.19 joerg free(tbuf.buf);
565 1.1 roy
566 1.1 roy /* Merge use entries until we have merged all we can */
567 1.10 roy while (merge_use(flags) != 0)
568 1.1 roy ;
569 1.1 roy
570 1.6 roy if (Sflag) {
571 1.5 roy print_dump(argc - optind, argv + optind);
572 1.5 roy return error_exit;
573 1.5 roy }
574 1.5 roy
575 1.6 roy if (cflag)
576 1.1 roy return error_exit;
577 1.18 joerg
578 1.20 joerg if (ofile == NULL)
579 1.20 joerg easprintf(&dbname, "%s.cdb", source);
580 1.20 joerg else
581 1.20 joerg dbname = ofile;
582 1.20 joerg write_database(dbname);
583 1.1 roy
584 1.1 roy if (sflag != 0)
585 1.1 roy fprintf(stderr, "%zu entries and %zu aliases written to %s\n",
586 1.20 joerg nterm, nalias, dbname);
587 1.19 joerg
588 1.20 joerg if (ofile == NULL)
589 1.20 joerg free(dbname);
590 1.20 joerg while ((term = STAILQ_FIRST(&terms)) != NULL) {
591 1.20 joerg STAILQ_REMOVE_HEAD(&terms, next);
592 1.19 joerg _ti_freetic(term->tic);
593 1.19 joerg free(term->name);
594 1.19 joerg free(term);
595 1.19 joerg }
596 1.30 christos #ifndef HAVE_NBTOOL_CONFIG_H
597 1.30 christos /*
598 1.30 christos * hdestroy1 is not standard but we don't really care if we
599 1.30 christos * leak in the tools version
600 1.30 christos */
601 1.24 christos hdestroy1(free, NULL);
602 1.30 christos #endif
603 1.1 roy
604 1.1 roy return EXIT_SUCCESS;
605 1.1 roy }
606