Wed Nov 27 17:38:11 2013 UTC ()
Use LIST_FOREACH, LIST_NEXT, etc., instead of direct access to
the internals of queue.h structs.


(apb)
diff -r1.35 -r1.36 src/usr.bin/gencat/gencat.c

cvs diff -r1.35 -r1.36 src/usr.bin/gencat/gencat.c (expand / switch to unified diff)

--- src/usr.bin/gencat/gencat.c 2012/03/09 18:54:28 1.35
+++ src/usr.bin/gencat/gencat.c 2013/11/27 17:38:11 1.36
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: gencat.c,v 1.35 2012/03/09 18:54:28 ginsbach Exp $ */ 1/* $NetBSD: gencat.c,v 1.36 2013/11/27 17:38:11 apb Exp $ */
2 2
3/* 3/*
4 * Copyright (c) 1996 The NetBSD Foundation, Inc. 4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * This code is derived from software contributed to The NetBSD Foundation 7 * This code is derived from software contributed to The NetBSD Foundation
8 * by J.T. Conklin. 8 * by J.T. Conklin.
9 * 9 *
10 * Redistribution and use in source and binary forms, with or without 10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions 11 * modification, are permitted provided that the following conditions
12 * are met: 12 * are met:
13 * 1. Redistributions of source code must retain the above copyright 13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer. 14 * notice, this list of conditions and the following disclaimer.
@@ -21,27 +21,27 @@ @@ -21,27 +21,27 @@
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS  22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE. 29 * POSSIBILITY OF SUCH DAMAGE.
30 */ 30 */
31 31
32#include <sys/cdefs.h> 32#include <sys/cdefs.h>
33#if defined(__RCSID) && !defined(lint) 33#if defined(__RCSID) && !defined(lint)
34__RCSID("$NetBSD: gencat.c,v 1.35 2012/03/09 18:54:28 ginsbach Exp $"); 34__RCSID("$NetBSD: gencat.c,v 1.36 2013/11/27 17:38:11 apb Exp $");
35#endif 35#endif
36 36
37/*********************************************************** 37/***********************************************************
38Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts. 38Copyright 1990, by Alfalfa Software Incorporated, Cambridge, Massachusetts.
39 39
40 All Rights Reserved 40 All Rights Reserved
41 41
42Permission to use, copy, modify, and distribute this software and its 42Permission to use, copy, modify, and distribute this software and its
43documentation for any purpose and without fee is hereby granted, 43documentation for any purpose and without fee is hereby granted,
44provided that the above copyright notice appear in all copies and that 44provided that the above copyright notice appear in all copies and that
45both that copyright notice and this permission notice appear in 45both that copyright notice and this permission notice appear in
46supporting documentation, and that Alfalfa's name not be used in 46supporting documentation, and that Alfalfa's name not be used in
47advertising or publicity pertaining to distribution of the software 47advertising or publicity pertaining to distribution of the software
@@ -98,27 +98,27 @@ up-to-date. Many thanks. @@ -98,27 +98,27 @@ up-to-date. Many thanks.
98 98
99struct _msgT { 99struct _msgT {
100 long msgId; 100 long msgId;
101 char *str; 101 char *str;
102 LIST_ENTRY(_msgT) entries; 102 LIST_ENTRY(_msgT) entries;
103}; 103};
104 104
105struct _setT { 105struct _setT {
106 long setId; 106 long setId;
107 LIST_HEAD(msghead, _msgT) msghead; 107 LIST_HEAD(msghead, _msgT) msghead;
108 LIST_ENTRY(_setT) entries; 108 LIST_ENTRY(_setT) entries;
109}; 109};
110 110
111static LIST_HEAD(sethead, _setT) sethead; 111static LIST_HEAD(sethead, _setT) sethead = LIST_HEAD_INITIALIZER(sethead);
112static struct _setT *curSet; 112static struct _setT *curSet;
113 113
114static const char *curfile; 114static const char *curfile;
115static char *curline = NULL; 115static char *curline = NULL;
116static long lineno = 0; 116static long lineno = 0;
117 117
118static char *cskip(char *); 118static char *cskip(char *);
119__dead static void error(const char *); 119__dead static void error(const char *);
120static char *get_line(int); 120static char *get_line(int);
121static char *getmsg(int, char *, char); 121static char *getmsg(int, char *, char);
122static void warning(const char *, const char *); 122static void warning(const char *, const char *);
123static char *wskip(char *); 123static char *wskip(char *);
124static char *xstrdup(const char *); 124static char *xstrdup(const char *);
@@ -460,28 +460,26 @@ getmsg(int fd, char *cptr, char quote) @@ -460,28 +460,26 @@ getmsg(int fd, char *cptr, char quote)
460 460
461 *tptr = '\0'; 461 *tptr = '\0';
462 return (msg); 462 return (msg);
463} 463}
464 464
465static void 465static void
466MCParse(int fd) 466MCParse(int fd)
467{ 467{
468 char *cptr, *str; 468 char *cptr, *str;
469 int msgid = 0; 469 int msgid = 0;
470 int setid = 0; 470 int setid = 0;
471 char quote = 0; 471 char quote = 0;
472 472
473 /* XXX: init sethead? */ 
474 
475 while ((cptr = get_line(fd))) { 473 while ((cptr = get_line(fd))) {
476 if (*cptr == '$') { 474 if (*cptr == '$') {
477 ++cptr; 475 ++cptr;
478 if (strncmp(cptr, "set", 3) == 0) { 476 if (strncmp(cptr, "set", 3) == 0) {
479 cptr += 3; 477 cptr += 3;
480 cptr = wskip(cptr); 478 cptr = wskip(cptr);
481 setid = atoi(cptr); 479 setid = atoi(cptr);
482 MCAddSet(setid); 480 MCAddSet(setid);
483 msgid = 0; 481 msgid = 0;
484 } else if (strncmp(cptr, "delset", 6) == 0) { 482 } else if (strncmp(cptr, "delset", 6) == 0) {
485 cptr += 6; 483 cptr += 6;
486 cptr = wskip(cptr); 484 cptr = wskip(cptr);
487 setid = atoi(cptr); 485 setid = atoi(cptr);
@@ -555,28 +553,26 @@ MCParse(int fd) @@ -555,28 +553,26 @@ MCParse(int fd)
555 553
556static void 554static void
557MCReadCat(int fd) 555MCReadCat(int fd)
558{ 556{
559 void *msgcat; /* message catalog data */ 557 void *msgcat; /* message catalog data */
560 struct _nls_cat_hdr cat_hdr; 558 struct _nls_cat_hdr cat_hdr;
561 struct _nls_set_hdr *set_hdr; 559 struct _nls_set_hdr *set_hdr;
562 struct _nls_msg_hdr *msg_hdr; 560 struct _nls_msg_hdr *msg_hdr;
563 char *strings; 561 char *strings;
564 ssize_t n; 562 ssize_t n;
565 int m, s; 563 int m, s;
566 int msgno, setno; 564 int msgno, setno;
567 565
568 /* XXX init sethead? */ 
569 
570 n = read(fd, &cat_hdr, sizeof(cat_hdr)); 566 n = read(fd, &cat_hdr, sizeof(cat_hdr));
571 if (n < (ssize_t)sizeof(cat_hdr)) { 567 if (n < (ssize_t)sizeof(cat_hdr)) {
572 if (n == 0) 568 if (n == 0)
573 return; /* empty file */ 569 return; /* empty file */
574 else if (n == -1) 570 else if (n == -1)
575 err(1, "header read"); 571 err(1, "header read");
576 else 572 else
577 errx(1, CORRUPT); 573 errx(1, CORRUPT);
578 } 574 }
579 if (ntohl((uint32_t)cat_hdr.__magic) != _NLS_MAGIC) 575 if (ntohl((uint32_t)cat_hdr.__magic) != _NLS_MAGIC)
580 errx(1, "%s: bad magic number (%#x)", CORRUPT, cat_hdr.__magic); 576 errx(1, "%s: bad magic number (%#x)", CORRUPT, cat_hdr.__magic);
581 577
582 cat_hdr.__mem = ntohl(cat_hdr.__mem); 578 cat_hdr.__mem = ntohl(cat_hdr.__mem);
@@ -664,32 +660,30 @@ MCWriteCat(int fd) @@ -664,32 +660,30 @@ MCWriteCat(int fd)
664 struct _nls_msg_hdr *msg_hdr; 660 struct _nls_msg_hdr *msg_hdr;
665 char *strings; 661 char *strings;
666 struct _setT *set; 662 struct _setT *set;
667 struct _msgT *msg; 663 struct _msgT *msg;
668 int msg_index; 664 int msg_index;
669 int msg_offset; 665 int msg_offset;
670 666
671 /* determine number of sets, number of messages, and size of the 667 /* determine number of sets, number of messages, and size of the
672 * string pool */ 668 * string pool */
673 nsets = 0; 669 nsets = 0;
674 nmsgs = 0; 670 nmsgs = 0;
675 string_size = 0; 671 string_size = 0;
676 672
677 for (set = sethead.lh_first; set != NULL; 673 LIST_FOREACH(set, &sethead, entries) {
678 set = set->entries.le_next) { 
679 nsets++; 674 nsets++;
680 675
681 for (msg = set->msghead.lh_first; msg != NULL; 676 LIST_FOREACH(msg, &set->msghead, entries) {
682 msg = msg->entries.le_next) { 
683 nmsgs++; 677 nmsgs++;
684 string_size += strlen(msg->str) + 1; 678 string_size += strlen(msg->str) + 1;
685 } 679 }
686 } 680 }
687 681
688#ifdef DEBUG 682#ifdef DEBUG
689 printf("number of sets: %d\n", nsets); 683 printf("number of sets: %d\n", nsets);
690 printf("number of msgs: %d\n", nmsgs); 684 printf("number of msgs: %d\n", nmsgs);
691 printf("string pool size: %d\n", string_size); 685 printf("string pool size: %d\n", string_size);
692#endif 686#endif
693 687
694 /* determine size and then allocate buffer for constructing external 688 /* determine size and then allocate buffer for constructing external
695 * message catalog representation */ 689 * message catalog representation */
@@ -715,32 +709,30 @@ MCWriteCat(int fd) @@ -715,32 +709,30 @@ MCWriteCat(int fd)
715 /* compute offsets for set & msg header tables and string pool */ 709 /* compute offsets for set & msg header tables and string pool */
716 set_hdr = (struct _nls_set_hdr *) ((char *) msgcat + 710 set_hdr = (struct _nls_set_hdr *) ((char *) msgcat +
717 sizeof(struct _nls_cat_hdr)); 711 sizeof(struct _nls_cat_hdr));
718 msg_hdr = (struct _nls_msg_hdr *) ((char *) msgcat + 712 msg_hdr = (struct _nls_msg_hdr *) ((char *) msgcat +
719 sizeof(struct _nls_cat_hdr) + 713 sizeof(struct _nls_cat_hdr) +
720 nsets * sizeof(struct _nls_set_hdr)); 714 nsets * sizeof(struct _nls_set_hdr));
721 strings = (char *) msgcat + 715 strings = (char *) msgcat +
722 sizeof(struct _nls_cat_hdr) + 716 sizeof(struct _nls_cat_hdr) +
723 nsets * sizeof(struct _nls_set_hdr) + 717 nsets * sizeof(struct _nls_set_hdr) +
724 nmsgs * sizeof(struct _nls_msg_hdr); 718 nmsgs * sizeof(struct _nls_msg_hdr);
725 719
726 msg_index = 0; 720 msg_index = 0;
727 msg_offset = 0; 721 msg_offset = 0;
728 for (set = sethead.lh_first; set != NULL; 722 LIST_FOREACH(set, &sethead, entries) {
729 set = set->entries.le_next) { 
730 723
731 nmsgs = 0; 724 nmsgs = 0;
732 for (msg = set->msghead.lh_first; msg != NULL; 725 LIST_FOREACH(msg, &set->msghead, entries) {
733 msg = msg->entries.le_next) { 
734 int32_t msg_len = strlen(msg->str) + 1; 726 int32_t msg_len = strlen(msg->str) + 1;
735 727
736 msg_hdr->__msgno = htonl(msg->msgId); 728 msg_hdr->__msgno = htonl(msg->msgId);
737 msg_hdr->__msglen = htonl(msg_len); 729 msg_hdr->__msglen = htonl(msg_len);
738 msg_hdr->__offset = htonl(msg_offset); 730 msg_hdr->__offset = htonl(msg_offset);
739 731
740 memcpy(strings, msg->str, msg_len); 732 memcpy(strings, msg->str, msg_len);
741 strings += msg_len; 733 strings += msg_len;
742 msg_offset += msg_len; 734 msg_offset += msg_len;
743 735
744 nmsgs++; 736 nmsgs++;
745 msg_hdr++; 737 msg_hdr++;
746 } 738 }
@@ -760,29 +752,30 @@ static void @@ -760,29 +752,30 @@ static void
760MCAddSet(int setId) 752MCAddSet(int setId)
761{ 753{
762 struct _setT *p, *q; 754 struct _setT *p, *q;
763 755
764 if (setId <= 0) { 756 if (setId <= 0) {
765 error("setId's must be greater than zero"); 757 error("setId's must be greater than zero");
766 /* NOTREACHED */ 758 /* NOTREACHED */
767 } 759 }
768 if (setId > NL_SETMAX) { 760 if (setId > NL_SETMAX) {
769 error("setId exceeds limit"); 761 error("setId exceeds limit");
770 /* NOTREACHED */ 762 /* NOTREACHED */
771 } 763 }
772 764
773 p = sethead.lh_first; 765 p = LIST_FIRST(&sethead);
774 q = NULL; 766 q = NULL;
775 for (; p != NULL && p->setId < setId; q = p, p = p->entries.le_next); 767 for (; p != NULL && p->setId < setId; q = p, p = LIST_NEXT(p, entries))
 768 continue;
776 769
777 if (p && p->setId == setId) { 770 if (p && p->setId == setId) {
778 ; 771 ;
779 } else { 772 } else {
780 p = xmalloc(sizeof(struct _setT)); 773 p = xmalloc(sizeof(struct _setT));
781 memset(p, '\0', sizeof(struct _setT)); 774 memset(p, '\0', sizeof(struct _setT));
782 LIST_INIT(&p->msghead); 775 LIST_INIT(&p->msghead);
783 776
784 p->setId = setId; 777 p->setId = setId;
785 778
786 if (q == NULL) { 779 if (q == NULL) {
787 LIST_INSERT_HEAD(&sethead, p, entries); 780 LIST_INSERT_HEAD(&sethead, p, entries);
788 } else { 781 } else {
@@ -800,29 +793,30 @@ MCAddMsg(int msgId, const char *str) @@ -800,29 +793,30 @@ MCAddMsg(int msgId, const char *str)
800 793
801 if (!curSet) 794 if (!curSet)
802 error("can't specify a message when no set exists"); 795 error("can't specify a message when no set exists");
803 796
804 if (msgId <= 0) { 797 if (msgId <= 0) {
805 error("msgId's must be greater than zero"); 798 error("msgId's must be greater than zero");
806 /* NOTREACHED */ 799 /* NOTREACHED */
807 } 800 }
808 if (msgId > NL_MSGMAX) { 801 if (msgId > NL_MSGMAX) {
809 error("msgID exceeds limit"); 802 error("msgID exceeds limit");
810 /* NOTREACHED */ 803 /* NOTREACHED */
811 } 804 }
812 805
813 p = curSet->msghead.lh_first; 806 p = LIST_FIRST(&curSet->msghead);
814 q = NULL; 807 q = NULL;
815 for (; p != NULL && p->msgId < msgId; q = p, p = p->entries.le_next); 808 for (; p != NULL && p->msgId < msgId; q = p, p = LIST_NEXT(p, entries))
 809 continue;
816 810
817 if (p && p->msgId == msgId) { 811 if (p && p->msgId == msgId) {
818 free(p->str); 812 free(p->str);
819 } else { 813 } else {
820 p = xmalloc(sizeof(struct _msgT)); 814 p = xmalloc(sizeof(struct _msgT));
821 memset(p, '\0', sizeof(struct _msgT)); 815 memset(p, '\0', sizeof(struct _msgT));
822 816
823 if (q == NULL) { 817 if (q == NULL) {
824 LIST_INSERT_HEAD(&curSet->msghead, p, entries); 818 LIST_INSERT_HEAD(&curSet->msghead, p, entries);
825 } else { 819 } else {
826 LIST_INSERT_AFTER(q, p, entries); 820 LIST_INSERT_AFTER(q, p, entries);
827 } 821 }
828 } 822 }
@@ -836,48 +830,50 @@ MCDelSet(int setId) @@ -836,48 +830,50 @@ MCDelSet(int setId)
836{ 830{
837 struct _setT *set; 831 struct _setT *set;
838 struct _msgT *msg; 832 struct _msgT *msg;
839 833
840 if (setId <= 0) { 834 if (setId <= 0) {
841 error("setId's must be greater than zero"); 835 error("setId's must be greater than zero");
842 /* NOTREACHED */ 836 /* NOTREACHED */
843 } 837 }
844 if (setId > NL_SETMAX) { 838 if (setId > NL_SETMAX) {
845 error("setId exceeds limit"); 839 error("setId exceeds limit");
846 /* NOTREACHED */ 840 /* NOTREACHED */
847 } 841 }
848 842
849 set = sethead.lh_first; 843 set = LIST_FIRST(&sethead);
850 for (; set != NULL && set->setId < setId; set = set->entries.le_next); 844 for (; set != NULL && set->setId < setId; set = LIST_NEXT(set, entries))
 845 continue;
851 846
852 if (set && set->setId == setId) { 847 if (set && set->setId == setId) {
853 LIST_REMOVE(set, entries); 848 LIST_REMOVE(set, entries);
854 while ((msg = set->msghead.lh_first) != NULL) { 849 while ((msg = LIST_FIRST(&set->msghead)) != NULL) {
855 LIST_REMOVE(msg, entries); 850 LIST_REMOVE(msg, entries);
856 free(msg->str); 851 free(msg->str);
857 free(msg); 852 free(msg);
858 } 853 }
859 free(set); 854 free(set);
860 return; 855 return;
861 } 856 }
862 warning(NULL, "specified set doesn't exist"); 857 warning(NULL, "specified set doesn't exist");
863} 858}
864 859
865static void 860static void
866MCDelMsg(int msgId) 861MCDelMsg(int msgId)
867{ 862{
868 struct _msgT *msg; 863 struct _msgT *msg;
869 864
870 if (!curSet) 865 if (!curSet)
871 error("you can't delete a message before defining the set"); 866 error("you can't delete a message before defining the set");
872 867
873 msg = curSet->msghead.lh_first; 868 msg = LIST_FIRST(&curSet->msghead);
874 for (; msg != NULL && msg->msgId < msgId; msg = msg->entries.le_next); 869 for (; msg != NULL && msg->msgId < msgId; msg = LIST_NEXT(msg, entries))
 870 continue;
875 871
876 if (msg && msg->msgId == msgId) { 872 if (msg && msg->msgId == msgId) {
877 LIST_REMOVE(msg, entries); 873 LIST_REMOVE(msg, entries);
878 free(msg->str); 874 free(msg->str);
879 free(msg); 875 free(msg);
880 return; 876 return;
881 } 877 }
882 warning(NULL, "specified msg doesn't exist"); 878 warning(NULL, "specified msg doesn't exist");
883} 879}