Replace legacy make with bmake
[dragonfly.git] / lib / libc / citrus / citrus_pivot_factory.c
1 /* $NetBSD: citrus_pivot_factory.c,v 1.6 2008/02/09 14:56:20 junyoung Exp $ */
2 /* $DragonFly: src/lib/libc/citrus/citrus_pivot_factory.c,v 1.2 2008/04/10 10:21:01 hasso Exp $ */
3
4 /*-
5  * Copyright (c)2003 Citrus Project,
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/endian.h>
31 #include <sys/queue.h>
32 #include <assert.h>
33 #include <ctype.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39
40 #include "citrus_namespace.h"
41 #include "citrus_region.h"
42 #include "citrus_bcs.h"
43 #include "citrus_db_factory.h"
44 #include "citrus_db_hash.h"
45 #include "citrus_pivot_file.h"
46 #include "citrus_pivot_factory.h"
47
48 struct src_entry {
49         char *se_name;
50         struct _citrus_db_factory *se_df;
51         STAILQ_ENTRY(src_entry) se_entry;
52 };
53 STAILQ_HEAD(src_head, src_entry);
54
55 static int
56 find_src(struct src_head *sh, struct src_entry **rse, const char *name)
57 {
58         int ret;
59         struct src_entry *se;
60
61         STAILQ_FOREACH(se, sh, se_entry) {
62                 if (_bcs_strcasecmp(se->se_name, name) == 0) {
63                         *rse = se;
64                         return 0;
65                 }
66         }
67         se = malloc(sizeof(*se));
68         if (se == NULL)
69                 return errno;
70         se->se_name = strdup(name);
71         if (se->se_name == NULL) {
72                 ret = errno;
73                 free(se);
74                 return ret;
75         }
76         ret = _db_factory_create(&se->se_df, &_db_hash_std, NULL);
77         if (ret) {
78                 free(se->se_name);
79                 free(se);
80                 return ret;
81         }
82         STAILQ_INSERT_TAIL(sh, se, se_entry);
83         *rse = se;
84
85         return 0;
86 }
87
88 static void
89 free_src(struct src_head *sh)
90 {
91         struct src_entry *se;
92
93         while ((se = STAILQ_FIRST(sh)) != NULL) {
94                 STAILQ_REMOVE_HEAD(sh, se_entry);
95                 _db_factory_free(se->se_df);
96                 free(se->se_name);
97                 free(se);
98         }
99 }
100
101
102 #define T_COMM '#'
103 static int
104 convert_line(struct src_head *sh, const char *line, size_t len)
105 {
106         int ret;
107         struct src_entry *se;
108         const char *p;
109         char key1[LINE_MAX], key2[LINE_MAX], data[LINE_MAX];
110         uint32_t val;
111
112         se = NULL; /* XXX gcc */
113
114         /* cut off trailing comment */
115         p = memchr(line, T_COMM, len);
116         if (p)
117                 len = p - line;
118
119         /* key1 */
120         line = _bcs_skip_ws_len(line, &len);
121         if (len == 0)
122                 return 0;
123         p = _bcs_skip_nonws_len(line, &len);
124         if (p==line)
125                 return 0;
126         snprintf(key1, sizeof(key1), "%.*s", (int)(p-line), line);
127
128         /* key2 */
129         line = _bcs_skip_ws_len(p, &len);
130         if (len == 0)
131                 return 0;
132         p = _bcs_skip_nonws_len(line, &len);
133         if (p==line)
134                 return 0;
135         snprintf(key2, sizeof(key2), "%.*s", (int)(p-line), line);
136
137         /* data */
138         line = _bcs_skip_ws_len(p, &len);
139         _bcs_trunc_rws_len(line, &len);
140         snprintf(data, sizeof(data), "%.*s", (int)len, line);
141         /* LINTED: discard const */
142         val = strtoul(data, __DECONST(char **, &p), 0);
143         if (*p != '\0')
144                 return EFTYPE;
145
146         /* insert to DB */
147         ret = find_src(sh, &se, key1);
148         if (ret)
149                 return ret;
150
151         return _db_factory_add32_by_s(se->se_df, key2, val);
152 }
153
154 static int
155 dump_db(struct src_head *sh, struct _region *r)
156 {
157         int ret;
158         struct _db_factory *df;
159         struct src_entry *se;
160         size_t size;
161         void *ptr;
162         struct _region subr;
163
164         ret = _db_factory_create(&df, &_db_hash_std, NULL);
165         if (ret)
166                 return ret;
167
168         STAILQ_FOREACH(se, sh, se_entry) {
169                 size = _db_factory_calc_size(se->se_df);
170                 ptr = malloc(size);
171                 if (ptr == NULL)
172                         goto quit;
173                 _region_init(&subr, ptr, size);
174                 ret = _db_factory_serialize(se->se_df, _CITRUS_PIVOT_SUB_MAGIC,
175                                             &subr);
176                 if (ret)
177                         goto quit;
178                 ret = _db_factory_add_by_s(df, se->se_name, &subr, 1);
179                 if (ret)
180                         goto quit;
181         }
182
183         size = _db_factory_calc_size(df);
184         ptr = malloc(size);
185         if (ptr == NULL)
186                 goto quit;
187         _region_init(r, ptr, size);
188
189         ret = _db_factory_serialize(df, _CITRUS_PIVOT_MAGIC, r);
190         ptr = NULL;
191
192 quit:
193         free(ptr);
194         _db_factory_free(df);
195         return ret;
196 }
197
198 int
199 _citrus_pivot_factory_convert(FILE *out, FILE *in)
200 {
201         struct src_head sh;
202         struct _region r;
203         char *line;
204         size_t size;
205         int ret;
206
207         STAILQ_INIT(&sh);
208
209         while ((line = fgetln(in, &size)) != NULL)
210                 if ((ret = convert_line(&sh, line, size))) {
211                         free_src(&sh);
212                         return ret;
213                 }
214
215         ret = dump_db(&sh, &r);
216         free_src(&sh);
217         if (ret)
218                 return ret;
219
220         if (fwrite(_region_head(&r), _region_size(&r), 1, out) != 1)
221                 return errno;
222
223         return 0;
224 }