| @@ -1,14 +1,14 @@ | | | @@ -1,14 +1,14 @@ |
1 | /* $NetBSD: tic.c,v 1.18 2012/05/31 21:01:06 joerg Exp $ */ | | 1 | /* $NetBSD: tic.c,v 1.19 2012/06/01 12:08:40 joerg Exp $ */ |
2 | | | 2 | |
3 | /* | | 3 | /* |
4 | * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc. | | 4 | * Copyright (c) 2009, 2010 The NetBSD Foundation, Inc. |
5 | * | | 5 | * |
6 | * This code is derived from software contributed to The NetBSD Foundation | | 6 | * This code is derived from software contributed to The NetBSD Foundation |
7 | * by Roy Marples. | | 7 | * by Roy Marples. |
8 | * | | 8 | * |
9 | * Redistribution and use in source and binary forms, with or without | | 9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | | 10 | * modification, are permitted provided that the following conditions |
11 | * are met: | | 11 | * are met: |
12 | * 1. Redistributions of source code must retain the above copyright | | 12 | * 1. Redistributions of source code must retain the above copyright |
13 | * notice, this list of conditions and the following disclaimer. | | 13 | * notice, this list of conditions and the following disclaimer. |
14 | * 2. Redistributions in binary form must reproduce the above copyright | | 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| @@ -22,27 +22,27 @@ | | | @@ -22,27 +22,27 @@ |
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | | 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | | 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | | 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | | 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF | | 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | | 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ | | 28 | */ |
29 | | | 29 | |
30 | #if HAVE_NBTOOL_CONFIG_H | | 30 | #if HAVE_NBTOOL_CONFIG_H |
31 | #include "nbtool_config.h" | | 31 | #include "nbtool_config.h" |
32 | #endif | | 32 | #endif |
33 | | | 33 | |
34 | #include <sys/cdefs.h> | | 34 | #include <sys/cdefs.h> |
35 | __RCSID("$NetBSD: tic.c,v 1.18 2012/05/31 21:01:06 joerg Exp $"); | | 35 | __RCSID("$NetBSD: tic.c,v 1.19 2012/06/01 12:08:40 joerg Exp $"); |
36 | | | 36 | |
37 | #include <sys/types.h> | | 37 | #include <sys/types.h> |
38 | #include <sys/queue.h> | | 38 | #include <sys/queue.h> |
39 | | | 39 | |
40 | #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H | | 40 | #if !HAVE_NBTOOL_CONFIG_H || HAVE_SYS_ENDIAN_H |
41 | #include <sys/endian.h> | | 41 | #include <sys/endian.h> |
42 | #endif | | 42 | #endif |
43 | | | 43 | |
44 | #include <ctype.h> | | 44 | #include <ctype.h> |
45 | #include <err.h> | | 45 | #include <err.h> |
46 | #include <errno.h> | | 46 | #include <errno.h> |
47 | #include <getopt.h> | | 47 | #include <getopt.h> |
48 | #include <limits.h> | | 48 | #include <limits.h> |
| @@ -195,26 +195,27 @@ process_entry(TBUF *buf, int flags) | | | @@ -195,26 +195,27 @@ process_entry(TBUF *buf, int flags) |
195 | e = strchr(p, '|'); | | 195 | e = strchr(p, '|'); |
196 | if (e != NULL) | | 196 | if (e != NULL) |
197 | *e++ = '\0'; | | 197 | *e++ = '\0'; |
198 | if (find_term(p) != NULL) { | | 198 | if (find_term(p) != NULL) { |
199 | dowarn("%s: has alias for already assigned" | | 199 | dowarn("%s: has alias for already assigned" |
200 | " term %s", tic->name, p); | | 200 | " term %s", tic->name, p); |
201 | } else { | | 201 | } else { |
202 | term = store_term(p, 'a'); | | 202 | term = store_term(p, 'a'); |
203 | term->tic = ecalloc(sizeof(*term->tic), 1); | | 203 | term->tic = ecalloc(sizeof(*term->tic), 1); |
204 | term->tic->name = estrdup(tic->name); | | 204 | term->tic->name = estrdup(tic->name); |
205 | } | | 205 | } |
206 | p = e; | | 206 | p = e; |
207 | } | | 207 | } |
| | | 208 | free(alias); |
208 | } | | 209 | } |
209 | | | 210 | |
210 | return 0; | | 211 | return 0; |
211 | } | | 212 | } |
212 | | | 213 | |
213 | static void | | 214 | static void |
214 | merge(TIC *rtic, TIC *utic, int flags) | | 215 | merge(TIC *rtic, TIC *utic, int flags) |
215 | { | | 216 | { |
216 | char *cap, flag, *code, type, *str; | | 217 | char *cap, flag, *code, type, *str; |
217 | short ind, num; | | 218 | short ind, num; |
218 | size_t n; | | 219 | size_t n; |
219 | | | 220 | |
220 | cap = utic->flags.buf; | | 221 | cap = utic->flags.buf; |
| @@ -504,53 +505,55 @@ main(int argc, char **argv) | | | @@ -504,53 +505,55 @@ main(int argc, char **argv) |
504 | if (db == NULL) | | 505 | if (db == NULL) |
505 | err(1, "dbopen: %s", source); | | 506 | err(1, "dbopen: %s", source); |
506 | p = dbname + strlen(dbname); | | 507 | p = dbname + strlen(dbname); |
507 | *p++ = '.'; | | 508 | *p++ = '.'; |
508 | *p++ = 'd'; | | 509 | *p++ = 'd'; |
509 | *p++ = 'b'; | | 510 | *p++ = 'b'; |
510 | *p++ = '\0'; | | 511 | *p++ = '\0'; |
511 | atexit(do_unlink); | | 512 | atexit(do_unlink); |
512 | } else | | 513 | } else |
513 | db = NULL; /* satisfy gcc warning */ | | 514 | db = NULL; /* satisfy gcc warning */ |
514 | | | 515 | |
515 | hcreate(HASH_SIZE); | | 516 | hcreate(HASH_SIZE); |
516 | | | 517 | |
517 | buf = NULL; | | 518 | buf = tbuf.buf = NULL; |
518 | buflen = tbuf.buflen = tbuf.bufpos = 0; | | 519 | buflen = tbuf.buflen = tbuf.bufpos = 0; |
519 | while ((len = getline(&buf, &buflen, f)) != -1) { | | 520 | while ((len = getline(&buf, &buflen, f)) != -1) { |
520 | /* Skip comments */ | | 521 | /* Skip comments */ |
521 | if (*buf == '#') | | 522 | if (*buf == '#') |
522 | continue; | | 523 | continue; |
523 | if (buf[len - 1] != '\n') { | | 524 | if (buf[len - 1] != '\n') { |
524 | process_entry(&tbuf, flags); | | 525 | process_entry(&tbuf, flags); |
525 | dowarn("last line is not a comment" | | 526 | dowarn("last line is not a comment" |
526 | " and does not end with a newline"); | | 527 | " and does not end with a newline"); |
527 | continue; | | 528 | continue; |
528 | } | | 529 | } |
529 | /* | | 530 | /* |
530 | If the first char is space not a space then we have a | | 531 | If the first char is space not a space then we have a |
531 | new entry, so process it. | | 532 | new entry, so process it. |
532 | */ | | 533 | */ |
533 | if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0) | | 534 | if (!isspace((unsigned char)*buf) && tbuf.bufpos != 0) |
534 | process_entry(&tbuf, flags); | | 535 | process_entry(&tbuf, flags); |
535 | | | 536 | |
536 | /* Grow the buffer if needed */ | | 537 | /* Grow the buffer if needed */ |
537 | grow_tbuf(&tbuf, len); | | 538 | grow_tbuf(&tbuf, len); |
538 | /* Append the string */ | | 539 | /* Append the string */ |
539 | memcpy(tbuf.buf + tbuf.bufpos, buf, len); | | 540 | memcpy(tbuf.buf + tbuf.bufpos, buf, len); |
540 | tbuf.bufpos += len; | | 541 | tbuf.bufpos += len; |
541 | } | | 542 | } |
| | | 543 | free(buf); |
542 | /* Process the last entry if not done already */ | | 544 | /* Process the last entry if not done already */ |
543 | process_entry(&tbuf, flags); | | 545 | process_entry(&tbuf, flags); |
| | | 546 | free(tbuf.buf); |
544 | | | 547 | |
545 | /* Merge use entries until we have merged all we can */ | | 548 | /* Merge use entries until we have merged all we can */ |
546 | while (merge_use(flags) != 0) | | 549 | while (merge_use(flags) != 0) |
547 | ; | | 550 | ; |
548 | | | 551 | |
549 | if (Sflag) { | | 552 | if (Sflag) { |
550 | print_dump(argc - optind, argv + optind); | | 553 | print_dump(argc - optind, argv + optind); |
551 | return error_exit; | | 554 | return error_exit; |
552 | } | | 555 | } |
553 | | | 556 | |
554 | if (cflag) | | 557 | if (cflag) |
555 | return error_exit; | | 558 | return error_exit; |
556 | | | 559 | |
| @@ -561,19 +564,28 @@ main(int argc, char **argv) | | | @@ -561,19 +564,28 @@ main(int argc, char **argv) |
561 | /* done! */ | | 564 | /* done! */ |
562 | dbm_close(db); | | 565 | dbm_close(db); |
563 | | | 566 | |
564 | /* Rename the tmp db to the real one now */ | | 567 | /* Rename the tmp db to the real one now */ |
565 | easprintf(&p, "%s.db", ofile); | | 568 | easprintf(&p, "%s.db", ofile); |
566 | if (rename(dbname, p) == -1) | | 569 | if (rename(dbname, p) == -1) |
567 | err(1, "rename"); | | 570 | err(1, "rename"); |
568 | free(dbname); | | 571 | free(dbname); |
569 | dbname = NULL; | | 572 | dbname = NULL; |
570 | | | 573 | |
571 | if (sflag != 0) | | 574 | if (sflag != 0) |
572 | fprintf(stderr, "%zu entries and %zu aliases written to %s\n", | | 575 | fprintf(stderr, "%zu entries and %zu aliases written to %s\n", |
573 | nterm, nalias, p); | | 576 | nterm, nalias, p); |
574 | free(p); | | | |
575 | | | 577 | |
| | | 578 | #ifdef __VALGRIND__ |
| | | 579 | free(p); |
| | | 580 | while ((term = SLIST_FIRST(&terms)) != NULL) { |
| | | 581 | SLIST_REMOVE_HEAD(&terms, next); |
| | | 582 | _ti_freetic(term->tic); |
| | | 583 | free(term->name); |
| | | 584 | free(term); |
| | | 585 | } |
576 | hdestroy(); | | 586 | hdestroy(); |
| | | 587 | #endif |
| | | 588 | |
577 | | | 589 | |
578 | return EXIT_SUCCESS; | | 590 | return EXIT_SUCCESS; |
579 | } | | 591 | } |