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