tic.c revision 1.37 1 1.37 roy /* $NetBSD: tic.c,v 1.37 2020/03/28 15:22:27 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.37 roy __RCSID("$NetBSD: tic.c,v 1.37 2020/03/28 15:22:27 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.37 roy err(EXIT_FAILURE, "_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.34 christos char *cap;
108 1.34 christos len = (ssize_t)(1 + sizeof(uint32_t) + sizeof(uint16_t) + slen);
109 1.20 joerg buf = emalloc(len);
110 1.34 christos cap = (char *)buf;
111 1.34 christos *cap++ = TERMINFO_ALIAS;
112 1.34 christos _ti_encode_32(&cap, term->base_term->id);
113 1.34 christos _ti_encode_count_str(&cap, term->name, slen);
114 1.20 joerg if (cdbw_put(db, term->name, slen, buf, len))
115 1.37 roy err(EXIT_FAILURE, "cdbw_put");
116 1.26 christos free(buf);
117 1.20 joerg return 0;
118 1.20 joerg }
119 1.5 roy
120 1.10 roy len = _ti_flatten(&buf, term->tic);
121 1.10 roy if (len == -1)
122 1.5 roy return -1;
123 1.1 roy
124 1.20 joerg if (cdbw_put_data(db, buf, len, &term->id))
125 1.37 roy err(EXIT_FAILURE, "cdbw_put_data");
126 1.20 joerg if (cdbw_put_key(db, term->name, slen, term->id))
127 1.37 roy err(EXIT_FAILURE, "cdbw_put_key");
128 1.10 roy free(buf);
129 1.1 roy return 0;
130 1.1 roy }
131 1.1 roy
132 1.1 roy static TERM *
133 1.1 roy find_term(const char *name)
134 1.1 roy {
135 1.15 joerg ENTRY elem, *elemp;
136 1.14 joerg
137 1.15 joerg elem.key = __UNCONST(name);
138 1.15 joerg elem.data = NULL;
139 1.15 joerg elemp = hsearch(elem, FIND);
140 1.15 joerg return elemp ? (TERM *)elemp->data : NULL;
141 1.1 roy }
142 1.1 roy
143 1.1 roy static TERM *
144 1.20 joerg store_term(const char *name, TERM *base_term)
145 1.1 roy {
146 1.1 roy TERM *term;
147 1.15 joerg ENTRY elem;
148 1.1 roy
149 1.16 joerg term = ecalloc(1, sizeof(*term));
150 1.16 joerg term->name = estrdup(name);
151 1.20 joerg STAILQ_INSERT_TAIL(&terms, term, next);
152 1.15 joerg elem.key = estrdup(name);
153 1.15 joerg elem.data = term;
154 1.15 joerg hsearch(elem, ENTER);
155 1.18 joerg
156 1.20 joerg term->base_term = base_term;
157 1.20 joerg if (base_term != NULL)
158 1.18 joerg nalias++;
159 1.18 joerg else
160 1.18 joerg nterm++;
161 1.18 joerg
162 1.1 roy return term;
163 1.1 roy }
164 1.1 roy
165 1.1 roy static int
166 1.10 roy process_entry(TBUF *buf, int flags)
167 1.1 roy {
168 1.10 roy char *p, *e, *alias;
169 1.1 roy TERM *term;
170 1.1 roy TIC *tic;
171 1.33 christos TBUF sbuf = *buf;
172 1.25 roy
173 1.1 roy if (buf->bufpos == 0)
174 1.1 roy return 0;
175 1.1 roy /* Terminate the string */
176 1.1 roy buf->buf[buf->bufpos - 1] = '\0';
177 1.1 roy /* First rewind the buffer for new entries */
178 1.1 roy buf->bufpos = 0;
179 1.1 roy
180 1.1 roy if (isspace((unsigned char)*buf->buf))
181 1.1 roy return 0;
182 1.1 roy
183 1.10 roy tic = _ti_compile(buf->buf, flags);
184 1.10 roy if (tic == NULL)
185 1.1 roy return 0;
186 1.10 roy
187 1.10 roy if (find_term(tic->name) != NULL) {
188 1.10 roy dowarn("%s: duplicate entry", tic->name);
189 1.10 roy _ti_freetic(tic);
190 1.1 roy return 0;
191 1.1 roy }
192 1.20 joerg term = store_term(tic->name, NULL);
193 1.10 roy term->tic = tic;
194 1.1 roy
195 1.1 roy /* Create aliased terms */
196 1.10 roy if (tic->alias != NULL) {
197 1.17 joerg alias = p = estrdup(tic->alias);
198 1.10 roy while (p != NULL && *p != '\0') {
199 1.10 roy e = strchr(p, '|');
200 1.10 roy if (e != NULL)
201 1.10 roy *e++ = '\0';
202 1.32 roy /* No need to lengthcheck the alias because the main
203 1.32 roy * terminfo description already stores all the aliases
204 1.32 roy * in the same length field as the alias. */
205 1.10 roy if (find_term(p) != NULL) {
206 1.1 roy dowarn("%s: has alias for already assigned"
207 1.10 roy " term %s", tic->name, p);
208 1.1 roy } else {
209 1.20 joerg store_term(p, term);
210 1.1 roy }
211 1.10 roy p = e;
212 1.1 roy }
213 1.19 joerg free(alias);
214 1.1 roy }
215 1.25 roy
216 1.33 christos if (tic->rtype == TERMINFO_RTYPE)
217 1.33 christos return process_entry(&sbuf, flags | TIC_COMPAT_V1);
218 1.33 christos
219 1.1 roy return 0;
220 1.1 roy }
221 1.1 roy
222 1.1 roy static void
223 1.10 roy merge(TIC *rtic, TIC *utic, int flags)
224 1.1 roy {
225 1.33 christos char flag, type;
226 1.33 christos const char *cap, *code, *str;
227 1.32 roy short ind, len;
228 1.32 roy int num;
229 1.1 roy size_t n;
230 1.1 roy
231 1.35 roy /* Promote record type if needed. */
232 1.35 roy if (rtic->rtype < utic->rtype) {
233 1.35 roy cap = rtic->nums.buf;
234 1.35 roy rtic->nums.buf = NULL;
235 1.35 roy rtic->nums.buflen = rtic->nums.bufpos = 0;
236 1.35 roy for (n = rtic->nums.entries; n > 0; n--) {
237 1.35 roy ind = _ti_decode_16(&cap);
238 1.35 roy num = _ti_decode_num(&cap, rtic->rtype);
239 1.35 roy if (VALID_NUMERIC(num) &&
240 1.35 roy !_ti_encode_buf_id_num(&rtic->nums, ind, num,
241 1.35 roy _ti_numsize(utic)))
242 1.37 roy err(EXIT_FAILURE, "encode num");
243 1.35 roy }
244 1.35 roy rtic->rtype = utic->rtype;
245 1.35 roy }
246 1.35 roy
247 1.1 roy cap = utic->flags.buf;
248 1.1 roy for (n = utic->flags.entries; n > 0; n--) {
249 1.34 christos ind = _ti_decode_16(&cap);
250 1.1 roy flag = *cap++;
251 1.1 roy if (VALID_BOOLEAN(flag) &&
252 1.33 christos _ti_find_cap(rtic, &rtic->flags, 'f', ind) == NULL)
253 1.1 roy {
254 1.34 christos if (!_ti_encode_buf_id_flags(&rtic->flags, ind, flag))
255 1.37 roy err(EXIT_FAILURE, "encode flag");
256 1.1 roy }
257 1.1 roy }
258 1.1 roy
259 1.1 roy cap = utic->nums.buf;
260 1.1 roy for (n = utic->nums.entries; n > 0; n--) {
261 1.34 christos ind = _ti_decode_16(&cap);
262 1.34 christos num = _ti_decode_num(&cap, utic->rtype);
263 1.1 roy if (VALID_NUMERIC(num) &&
264 1.33 christos _ti_find_cap(rtic, &rtic->nums, 'n', ind) == NULL)
265 1.1 roy {
266 1.34 christos if (!_ti_encode_buf_id_num(&rtic->nums, ind, num,
267 1.34 christos _ti_numsize(rtic)))
268 1.37 roy err(EXIT_FAILURE, "encode num");
269 1.1 roy }
270 1.1 roy }
271 1.1 roy
272 1.1 roy cap = utic->strs.buf;
273 1.1 roy for (n = utic->strs.entries; n > 0; n--) {
274 1.34 christos ind = _ti_decode_16(&cap);
275 1.34 christos len = _ti_decode_16(&cap);
276 1.32 roy if (len > 0 &&
277 1.33 christos _ti_find_cap(rtic, &rtic->strs, 's', ind) == NULL)
278 1.1 roy {
279 1.34 christos if (!_ti_encode_buf_id_count_str(&rtic->strs, ind, cap,
280 1.34 christos len))
281 1.37 roy err(EXIT_FAILURE, "encode str");
282 1.1 roy }
283 1.32 roy cap += len;
284 1.1 roy }
285 1.1 roy
286 1.1 roy cap = utic->extras.buf;
287 1.1 roy for (n = utic->extras.entries; n > 0; n--) {
288 1.34 christos num = _ti_decode_16(&cap);
289 1.1 roy code = cap;
290 1.1 roy cap += num;
291 1.1 roy type = *cap++;
292 1.1 roy flag = 0;
293 1.1 roy str = NULL;
294 1.1 roy switch (type) {
295 1.1 roy case 'f':
296 1.1 roy flag = *cap++;
297 1.1 roy if (!VALID_BOOLEAN(flag))
298 1.1 roy continue;
299 1.1 roy break;
300 1.1 roy case 'n':
301 1.34 christos num = _ti_decode_num(&cap, utic->rtype);
302 1.1 roy if (!VALID_NUMERIC(num))
303 1.1 roy continue;
304 1.1 roy break;
305 1.1 roy case 's':
306 1.34 christos num = _ti_decode_16(&cap);
307 1.1 roy str = cap;
308 1.1 roy cap += num;
309 1.1 roy if (num == 0)
310 1.1 roy continue;
311 1.1 roy break;
312 1.1 roy }
313 1.10 roy _ti_store_extra(rtic, 0, code, type, flag, num, str, num,
314 1.10 roy flags);
315 1.1 roy }
316 1.1 roy }
317 1.1 roy
318 1.1 roy static size_t
319 1.10 roy merge_use(int flags)
320 1.1 roy {
321 1.1 roy size_t skipped, merged, memn;
322 1.34 christos const char *cap;
323 1.1 roy uint16_t num;
324 1.1 roy TIC *rtic, *utic;
325 1.1 roy TERM *term, *uterm;;
326 1.1 roy
327 1.1 roy skipped = merged = 0;
328 1.20 joerg STAILQ_FOREACH(term, &terms, next) {
329 1.20 joerg if (term->base_term != NULL)
330 1.1 roy continue;
331 1.10 roy rtic = term->tic;
332 1.33 christos while ((cap = _ti_find_extra(rtic, &rtic->extras, "use"))
333 1.33 christos != NULL) {
334 1.1 roy if (*cap++ != 's') {
335 1.1 roy dowarn("%s: use is not string", rtic->name);
336 1.1 roy break;
337 1.1 roy }
338 1.1 roy cap += sizeof(uint16_t);
339 1.1 roy if (strcmp(rtic->name, cap) == 0) {
340 1.1 roy dowarn("%s: uses itself", rtic->name);
341 1.1 roy goto remove;
342 1.1 roy }
343 1.1 roy uterm = find_term(cap);
344 1.20 joerg if (uterm != NULL && uterm->base_term != NULL)
345 1.20 joerg uterm = uterm->base_term;
346 1.1 roy if (uterm == NULL) {
347 1.1 roy dowarn("%s: no use record for %s",
348 1.1 roy rtic->name, cap);
349 1.1 roy goto remove;
350 1.1 roy }
351 1.10 roy utic = uterm->tic;
352 1.1 roy if (strcmp(utic->name, rtic->name) == 0) {
353 1.1 roy dowarn("%s: uses itself", rtic->name);
354 1.1 roy goto remove;
355 1.1 roy }
356 1.33 christos if (_ti_find_extra(utic, &utic->extras, "use")
357 1.33 christos != NULL) {
358 1.1 roy skipped++;
359 1.1 roy break;
360 1.1 roy }
361 1.33 christos cap = _ti_find_extra(rtic, &rtic->extras, "use");
362 1.10 roy merge(rtic, utic, flags);
363 1.1 roy remove:
364 1.1 roy /* The pointers may have changed, find the use again */
365 1.33 christos cap = _ti_find_extra(rtic, &rtic->extras, "use");
366 1.1 roy if (cap == NULL)
367 1.1 roy dowarn("%s: use no longer exists - impossible",
368 1.1 roy rtic->name);
369 1.1 roy else {
370 1.34 christos char *scap = __UNCONST(
371 1.34 christos cap - (4 + sizeof(uint16_t)));
372 1.1 roy cap++;
373 1.34 christos num = _ti_decode_16(&cap);
374 1.34 christos cap += num;
375 1.1 roy memn = rtic->extras.bufpos -
376 1.1 roy (cap - rtic->extras.buf);
377 1.11 roy memmove(scap, cap, memn);
378 1.1 roy rtic->extras.bufpos -= cap - scap;
379 1.1 roy cap = scap;
380 1.1 roy rtic->extras.entries--;
381 1.1 roy merged++;
382 1.1 roy }
383 1.1 roy }
384 1.1 roy }
385 1.1 roy
386 1.1 roy if (merged == 0 && skipped != 0)
387 1.1 roy dowarn("circular use detected");
388 1.1 roy return merged;
389 1.1 roy }
390 1.1 roy
391 1.5 roy static int
392 1.5 roy print_dump(int argc, char **argv)
393 1.5 roy {
394 1.5 roy TERM *term;
395 1.10 roy uint8_t *buf;
396 1.5 roy int i, n;
397 1.5 roy size_t j, col;
398 1.10 roy ssize_t len;
399 1.5 roy
400 1.6 roy printf("struct compiled_term {\n");
401 1.6 roy printf("\tconst char *name;\n");
402 1.6 roy printf("\tconst char *cap;\n");
403 1.6 roy printf("\tsize_t caplen;\n");
404 1.6 roy printf("};\n\n");
405 1.6 roy
406 1.6 roy printf("const struct compiled_term compiled_terms[] = {\n");
407 1.6 roy
408 1.5 roy n = 0;
409 1.5 roy for (i = 0; i < argc; i++) {
410 1.5 roy term = find_term(argv[i]);
411 1.5 roy if (term == NULL) {
412 1.5 roy warnx("%s: no description for terminal", argv[i]);
413 1.5 roy continue;
414 1.5 roy }
415 1.20 joerg if (term->base_term != NULL) {
416 1.5 roy warnx("%s: cannot dump alias", argv[i]);
417 1.5 roy continue;
418 1.5 roy }
419 1.10 roy /* Don't compile the aliases in, save space */
420 1.10 roy free(term->tic->alias);
421 1.10 roy term->tic->alias = NULL;
422 1.10 roy len = _ti_flatten(&buf, term->tic);
423 1.10 roy if (len == 0 || len == -1)
424 1.5 roy continue;
425 1.5 roy
426 1.6 roy printf("\t{\n");
427 1.6 roy printf("\t\t\"%s\",\n", argv[i]);
428 1.5 roy n++;
429 1.10 roy for (j = 0, col = 0; j < (size_t)len; j++) {
430 1.5 roy if (col == 0) {
431 1.6 roy printf("\t\t\"");
432 1.6 roy col = 16;
433 1.5 roy }
434 1.25 roy
435 1.10 roy col += printf("\\%03o", (uint8_t)buf[j]);
436 1.5 roy if (col > 75) {
437 1.5 roy printf("\"%s\n",
438 1.10 roy j + 1 == (size_t)len ? "," : "");
439 1.5 roy col = 0;
440 1.5 roy }
441 1.5 roy }
442 1.5 roy if (col != 0)
443 1.6 roy printf("\",\n");
444 1.10 roy printf("\t\t%zu\n", len);
445 1.6 roy printf("\t}");
446 1.6 roy if (i + 1 < argc)
447 1.6 roy printf(",");
448 1.6 roy printf("\n");
449 1.10 roy free(buf);
450 1.5 roy }
451 1.6 roy printf("};\n");
452 1.5 roy
453 1.5 roy return n;
454 1.5 roy }
455 1.5 roy
456 1.20 joerg static void
457 1.20 joerg write_database(const char *dbname)
458 1.20 joerg {
459 1.20 joerg struct cdbw *db;
460 1.20 joerg char *tmp_dbname;
461 1.20 joerg TERM *term;
462 1.20 joerg int fd;
463 1.20 joerg
464 1.20 joerg db = cdbw_open();
465 1.20 joerg if (db == NULL)
466 1.37 roy err(EXIT_FAILURE, "cdbw_open failed");
467 1.20 joerg /* Save the terms */
468 1.20 joerg STAILQ_FOREACH(term, &terms, next)
469 1.20 joerg save_term(db, term);
470 1.20 joerg
471 1.20 joerg easprintf(&tmp_dbname, "%s.XXXXXX", dbname);
472 1.20 joerg fd = mkstemp(tmp_dbname);
473 1.20 joerg if (fd == -1)
474 1.37 roy err(EXIT_FAILURE,
475 1.37 roy "creating temporary database %s failed", tmp_dbname);
476 1.20 joerg if (cdbw_output(db, fd, "NetBSD terminfo", cdbw_stable_seeder))
477 1.37 roy err(EXIT_FAILURE,
478 1.37 roy "writing temporary database %s failed", tmp_dbname);
479 1.20 joerg if (fchmod(fd, DEFFILEMODE))
480 1.37 roy err(EXIT_FAILURE, "fchmod failed");
481 1.20 joerg if (close(fd))
482 1.37 roy err(EXIT_FAILURE,
483 1.37 roy "writing temporary database %s failed", tmp_dbname);
484 1.20 joerg if (rename(tmp_dbname, dbname))
485 1.37 roy err(EXIT_FAILURE, "renaming %s to %s failed", tmp_dbname, dbname);
486 1.20 joerg free(tmp_dbname);
487 1.20 joerg cdbw_close(db);
488 1.20 joerg }
489 1.20 joerg
490 1.1 roy int
491 1.1 roy main(int argc, char **argv)
492 1.1 roy {
493 1.10 roy int ch, cflag, sflag, flags;
494 1.20 joerg char *source, *dbname, *buf, *ofile;
495 1.1 roy FILE *f;
496 1.18 joerg size_t buflen;
497 1.12 roy ssize_t len;
498 1.1 roy TBUF tbuf;
499 1.29 roy struct term *term;
500 1.1 roy
501 1.1 roy cflag = sflag = 0;
502 1.1 roy ofile = NULL;
503 1.10 roy flags = TIC_ALIAS | TIC_DESCRIPTION | TIC_WARNING;
504 1.5 roy while ((ch = getopt(argc, argv, "Saco:sx")) != -1)
505 1.1 roy switch (ch) {
506 1.5 roy case 'S':
507 1.5 roy Sflag = 1;
508 1.10 roy /* We still compile aliases so that use= works.
509 1.10 roy * However, it's removed before we flatten to save space. */
510 1.10 roy flags &= ~TIC_DESCRIPTION;
511 1.5 roy break;
512 1.1 roy case 'a':
513 1.10 roy flags |= TIC_COMMENT;
514 1.1 roy break;
515 1.1 roy case 'c':
516 1.4 roy cflag = 1;
517 1.1 roy break;
518 1.1 roy case 'o':
519 1.1 roy ofile = optarg;
520 1.1 roy break;
521 1.1 roy case 's':
522 1.4 roy sflag = 1;
523 1.1 roy break;
524 1.1 roy case 'x':
525 1.10 roy flags |= TIC_EXTRA;
526 1.1 roy break;
527 1.1 roy case '?': /* FALLTHROUGH */
528 1.1 roy default:
529 1.6 roy fprintf(stderr, "usage: %s [-acSsx] [-o file] source\n",
530 1.1 roy getprogname());
531 1.1 roy return EXIT_FAILURE;
532 1.1 roy }
533 1.1 roy
534 1.1 roy if (optind == argc)
535 1.1 roy errx(1, "No source file given");
536 1.1 roy source = argv[optind++];
537 1.1 roy f = fopen(source, "r");
538 1.1 roy if (f == NULL)
539 1.37 roy err(EXIT_FAILURE, "fopen: %s", source);
540 1.1 roy
541 1.15 joerg hcreate(HASH_SIZE);
542 1.15 joerg
543 1.19 joerg buf = tbuf.buf = NULL;
544 1.28 roy buflen = tbuf.buflen = tbuf.bufpos = 0;
545 1.12 roy while ((len = getline(&buf, &buflen, f)) != -1) {
546 1.1 roy /* Skip comments */
547 1.1 roy if (*buf == '#')
548 1.1 roy continue;
549 1.12 roy if (buf[len - 1] != '\n') {
550 1.10 roy process_entry(&tbuf, flags);
551 1.1 roy dowarn("last line is not a comment"
552 1.1 roy " and does not end with a newline");
553 1.1 roy continue;
554 1.1 roy }
555 1.1 roy /*
556 1.28 roy * If the first char is space not a space then we have a
557 1.28 roy * new entry, so process it.
558 1.28 roy */
559 1.1 roy if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0)
560 1.10 roy process_entry(&tbuf, flags);
561 1.25 roy
562 1.1 roy /* Grow the buffer if needed */
563 1.12 roy grow_tbuf(&tbuf, len);
564 1.1 roy /* Append the string */
565 1.12 roy memcpy(tbuf.buf + tbuf.bufpos, buf, len);
566 1.12 roy tbuf.bufpos += len;
567 1.1 roy }
568 1.19 joerg free(buf);
569 1.1 roy /* Process the last entry if not done already */
570 1.10 roy process_entry(&tbuf, flags);
571 1.19 joerg free(tbuf.buf);
572 1.1 roy
573 1.1 roy /* Merge use entries until we have merged all we can */
574 1.10 roy while (merge_use(flags) != 0)
575 1.1 roy ;
576 1.1 roy
577 1.6 roy if (Sflag) {
578 1.5 roy print_dump(argc - optind, argv + optind);
579 1.5 roy return error_exit;
580 1.5 roy }
581 1.5 roy
582 1.6 roy if (cflag)
583 1.1 roy return error_exit;
584 1.18 joerg
585 1.20 joerg if (ofile == NULL)
586 1.20 joerg easprintf(&dbname, "%s.cdb", source);
587 1.20 joerg else
588 1.20 joerg dbname = ofile;
589 1.20 joerg write_database(dbname);
590 1.1 roy
591 1.1 roy if (sflag != 0)
592 1.1 roy fprintf(stderr, "%zu entries and %zu aliases written to %s\n",
593 1.20 joerg nterm, nalias, dbname);
594 1.19 joerg
595 1.20 joerg if (ofile == NULL)
596 1.20 joerg free(dbname);
597 1.20 joerg while ((term = STAILQ_FIRST(&terms)) != NULL) {
598 1.20 joerg STAILQ_REMOVE_HEAD(&terms, next);
599 1.19 joerg _ti_freetic(term->tic);
600 1.19 joerg free(term->name);
601 1.19 joerg free(term);
602 1.19 joerg }
603 1.30 christos #ifndef HAVE_NBTOOL_CONFIG_H
604 1.30 christos /*
605 1.30 christos * hdestroy1 is not standard but we don't really care if we
606 1.30 christos * leak in the tools version
607 1.30 christos */
608 1.24 christos hdestroy1(free, NULL);
609 1.30 christos #endif
610 1.1 roy
611 1.1 roy return EXIT_SUCCESS;
612 1.1 roy }
613