mklocaledb.c revision 1.2.2.3 1 1.2.2.3 snj /* $NetBSD: mklocaledb.c,v 1.2.2.3 2009/01/15 03:57:38 snj Exp $ */
2 1.2.2.2 snj
3 1.2.2.2 snj /*-
4 1.2.2.2 snj * Copyright (c)2008 Citrus Project,
5 1.2.2.2 snj * All rights reserved.
6 1.2.2.2 snj *
7 1.2.2.2 snj * Redistribution and use in source and binary forms, with or without
8 1.2.2.2 snj * modification, are permitted provided that the following conditions
9 1.2.2.2 snj * are met:
10 1.2.2.2 snj * 1. Redistributions of source code must retain the above copyright
11 1.2.2.2 snj * notice, this list of conditions and the following disclaimer.
12 1.2.2.2 snj * 2. Redistributions in binary form must reproduce the above copyright
13 1.2.2.2 snj * notice, this list of conditions and the following disclaimer in the
14 1.2.2.2 snj * documentation and/or other materials provided with the distribution.
15 1.2.2.2 snj *
16 1.2.2.2 snj * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 1.2.2.2 snj * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 1.2.2.2 snj * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 1.2.2.2 snj * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 1.2.2.2 snj * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 1.2.2.2 snj * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 1.2.2.2 snj * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 1.2.2.2 snj * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 1.2.2.2 snj * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 1.2.2.2 snj * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 1.2.2.2 snj * SUCH DAMAGE.
27 1.2.2.2 snj */
28 1.2.2.2 snj
29 1.2.2.2 snj /*
30 1.2.2.2 snj * XXX TEMPORARY IMPLEMENTATION.
31 1.2.2.2 snj * don't waste your time, all we need is localedef(1).
32 1.2.2.2 snj */
33 1.2.2.2 snj
34 1.2.2.2 snj #if HAVE_NBTOOL_CONFIG_H
35 1.2.2.2 snj #include "nbtool_config.h"
36 1.2.2.2 snj #endif
37 1.2.2.2 snj
38 1.2.2.3 snj #include <sys/cdefs.h>
39 1.2.2.3 snj #if !defined(lint)
40 1.2.2.3 snj __RCSID("$NetBSD: mklocaledb.c,v 1.2.2.3 2009/01/15 03:57:38 snj Exp $");
41 1.2.2.3 snj #endif /* not lint */
42 1.2.2.3 snj
43 1.2.2.2 snj #include <assert.h>
44 1.2.2.2 snj #include <err.h>
45 1.2.2.2 snj #include <errno.h>
46 1.2.2.2 snj #include <limits.h>
47 1.2.2.2 snj #include <stdint.h>
48 1.2.2.2 snj #include <stdio.h>
49 1.2.2.2 snj #include <stdlib.h>
50 1.2.2.2 snj #include <string.h>
51 1.2.2.2 snj #include <unistd.h>
52 1.2.2.2 snj
53 1.2.2.2 snj #include "fix_grouping.h"
54 1.2.2.2 snj
55 1.2.2.2 snj #include "citrus_namespace.h"
56 1.2.2.2 snj #include "citrus_bcs.h"
57 1.2.2.2 snj #include "citrus_types.h"
58 1.2.2.2 snj #include "citrus_region.h"
59 1.2.2.2 snj #include "citrus_db_factory.h"
60 1.2.2.2 snj #include "citrus_db_hash.h"
61 1.2.2.2 snj #include "citrus_db.h"
62 1.2.2.2 snj
63 1.2.2.2 snj #include "citrus_lc_monetary.h"
64 1.2.2.2 snj #include "citrus_lc_numeric.h"
65 1.2.2.2 snj #include "citrus_lc_time.h"
66 1.2.2.2 snj #include "citrus_lc_messages.h"
67 1.2.2.2 snj
68 1.2.2.2 snj void mklocaledb(const char *, FILE *, FILE *);
69 1.2.2.2 snj
70 1.2.2.2 snj /*
71 1.2.2.2 snj * TODO: -d debug options's output.
72 1.2.2.2 snj */
73 1.2.2.2 snj extern int debug;
74 1.2.2.2 snj extern void usage(void);
75 1.2.2.2 snj
76 1.2.2.2 snj static int
77 1.2.2.2 snj save_as_string(struct _db_factory *df,
78 1.2.2.2 snj const char *key, const char *value)
79 1.2.2.2 snj {
80 1.2.2.2 snj return _db_factory_addstr_by_s(df, key, value);
81 1.2.2.2 snj }
82 1.2.2.2 snj
83 1.2.2.2 snj static int
84 1.2.2.2 snj save_as_grouping(struct _db_factory *df,
85 1.2.2.2 snj const char *key, const char *value)
86 1.2.2.2 snj {
87 1.2.2.2 snj value = __fix_locale_grouping_str(value);
88 1.2.2.2 snj return _db_factory_addstr_by_s(df, key, value);
89 1.2.2.2 snj }
90 1.2.2.2 snj
91 1.2.2.2 snj static int
92 1.2.2.2 snj save_as_uint8(struct _db_factory *df,
93 1.2.2.2 snj const char *key, const char *head)
94 1.2.2.2 snj {
95 1.2.2.2 snj char *tail;
96 1.2.2.2 snj unsigned long int value;
97 1.2.2.2 snj uint8_t u8;
98 1.2.2.2 snj
99 1.2.2.2 snj value = _bcs_strtoul(head, &tail, 0);
100 1.2.2.2 snj if (head == tail || value > 0xFF)
101 1.2.2.2 snj return 1;
102 1.2.2.2 snj u8 = (uint8_t)(value & 0xFF);
103 1.2.2.2 snj return _db_factory_add8_by_s(df, key, u8);
104 1.2.2.2 snj }
105 1.2.2.2 snj
106 1.2.2.2 snj typedef struct {
107 1.2.2.2 snj const char *key;
108 1.2.2.2 snj int (*save)(struct _db_factory *,
109 1.2.2.2 snj const char *, const char *);
110 1.2.2.2 snj } token_t;
111 1.2.2.2 snj
112 1.2.2.2 snj typedef struct {
113 1.2.2.2 snj const char *magic, * vers_sym;
114 1.2.2.2 snj uint32_t version;
115 1.2.2.2 snj const token_t tokens[];
116 1.2.2.2 snj } category_t;
117 1.2.2.2 snj
118 1.2.2.2 snj static const category_t lc_monetary = {
119 1.2.2.2 snj _CITRUS_LC_MONETARY_MAGIC_1,
120 1.2.2.2 snj _CITRUS_LC_MONETARY_SYM_VERSION,
121 1.2.2.2 snj _CITRUS_LC_MONETARY_VERSION,
122 1.2.2.2 snj {
123 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_CURR_SYMBOL, &save_as_string },
124 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_CURRENCY_SYMBOL, &save_as_string },
125 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_MON_DECIMAL_POINT, &save_as_string },
126 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_MON_THOUSANDS_SEP, &save_as_string },
127 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_MON_GROUPING, &save_as_grouping },
128 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_POSITIVE_SIGN, &save_as_string },
129 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_NEGATIVE_SIGN, &save_as_string },
130 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_FRAC_DIGITS, &save_as_uint8 },
131 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_FRAC_DIGITS, &save_as_uint8 },
132 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_P_CS_PRECEDES, &save_as_uint8 },
133 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_P_SEP_BY_SPACE, &save_as_uint8 },
134 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_N_CS_PRECEDES, &save_as_uint8 },
135 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_N_SEP_BY_SPACE, &save_as_uint8 },
136 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_P_SIGN_POSN, &save_as_uint8 },
137 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_N_SIGN_POSN, &save_as_uint8 },
138 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_P_CS_PRECEDES, &save_as_uint8 },
139 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_N_CS_PRECEDES, &save_as_uint8 },
140 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_P_SEP_BY_SPACE, &save_as_uint8 },
141 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_N_SEP_BY_SPACE, &save_as_uint8 },
142 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_P_SIGN_POSN, &save_as_uint8 },
143 1.2.2.2 snj { _CITRUS_LC_MONETARY_SYM_INT_N_SIGN_POSN, &save_as_uint8 },
144 1.2.2.2 snj { NULL },
145 1.2.2.2 snj },
146 1.2.2.2 snj };
147 1.2.2.2 snj
148 1.2.2.2 snj static const category_t lc_numeric = {
149 1.2.2.2 snj _CITRUS_LC_NUMERIC_MAGIC_1,
150 1.2.2.2 snj _CITRUS_LC_NUMERIC_SYM_VERSION,
151 1.2.2.2 snj _CITRUS_LC_NUMERIC_VERSION,
152 1.2.2.2 snj {
153 1.2.2.2 snj { _CITRUS_LC_NUMERIC_SYM_DECIMAL_POINT, &save_as_string },
154 1.2.2.2 snj { _CITRUS_LC_NUMERIC_SYM_THOUSANDS_SEP, &save_as_string },
155 1.2.2.2 snj { _CITRUS_LC_NUMERIC_SYM_GROUPING, &save_as_grouping },
156 1.2.2.2 snj { NULL },
157 1.2.2.2 snj },
158 1.2.2.2 snj };
159 1.2.2.2 snj
160 1.2.2.2 snj static const category_t lc_time = {
161 1.2.2.2 snj _CITRUS_LC_TIME_MAGIC_1,
162 1.2.2.2 snj _CITRUS_LC_TIME_SYM_VERSION,
163 1.2.2.2 snj _CITRUS_LC_TIME_VERSION,
164 1.2.2.2 snj {
165 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_1, &save_as_string },
166 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_2, &save_as_string },
167 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_3, &save_as_string },
168 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_4, &save_as_string },
169 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_5, &save_as_string },
170 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_6, &save_as_string },
171 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABDAY_7, &save_as_string },
172 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_1, &save_as_string },
173 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_2, &save_as_string },
174 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_3, &save_as_string },
175 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_4, &save_as_string },
176 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_5, &save_as_string },
177 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_6, &save_as_string },
178 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_DAY_7, &save_as_string },
179 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_1, &save_as_string },
180 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_2, &save_as_string },
181 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_3, &save_as_string },
182 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_4, &save_as_string },
183 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_5, &save_as_string },
184 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_6, &save_as_string },
185 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_7, &save_as_string },
186 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_8, &save_as_string },
187 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_9, &save_as_string },
188 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_10, &save_as_string },
189 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_11, &save_as_string },
190 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_ABMON_12, &save_as_string },
191 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_1, &save_as_string },
192 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_2, &save_as_string },
193 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_3, &save_as_string },
194 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_4, &save_as_string },
195 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_5, &save_as_string },
196 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_6, &save_as_string },
197 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_7, &save_as_string },
198 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_8, &save_as_string },
199 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_9, &save_as_string },
200 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_10, &save_as_string },
201 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_11, &save_as_string },
202 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_MON_12, &save_as_string },
203 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_AM_STR, &save_as_string },
204 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_PM_STR, &save_as_string },
205 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_D_T_FMT, &save_as_string },
206 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_D_FMT, &save_as_string },
207 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_T_FMT, &save_as_string },
208 1.2.2.2 snj { _CITRUS_LC_TIME_SYM_T_FMT_AMPM, &save_as_string },
209 1.2.2.2 snj { NULL },
210 1.2.2.2 snj },
211 1.2.2.2 snj };
212 1.2.2.2 snj
213 1.2.2.2 snj static const category_t lc_messages = {
214 1.2.2.2 snj _CITRUS_LC_MESSAGES_MAGIC_1,
215 1.2.2.2 snj _CITRUS_LC_MESSAGES_SYM_VERSION,
216 1.2.2.2 snj _CITRUS_LC_MESSAGES_VERSION,
217 1.2.2.2 snj {
218 1.2.2.2 snj { _CITRUS_LC_MESSAGES_SYM_YESEXPR, &save_as_string },
219 1.2.2.2 snj { _CITRUS_LC_MESSAGES_SYM_NOEXPR, &save_as_string },
220 1.2.2.2 snj { _CITRUS_LC_MESSAGES_SYM_YESSTR, &save_as_string },
221 1.2.2.2 snj { _CITRUS_LC_MESSAGES_SYM_NOSTR, &save_as_string },
222 1.2.2.2 snj { NULL },
223 1.2.2.2 snj },
224 1.2.2.2 snj };
225 1.2.2.2 snj
226 1.2.2.2 snj void
227 1.2.2.2 snj mklocaledb(const char *type, FILE *reader, FILE *writer)
228 1.2.2.2 snj {
229 1.2.2.2 snj static const char delim[3] = { '\0', '\0', '#' };
230 1.2.2.2 snj const category_t *category = NULL;
231 1.2.2.2 snj struct _db_factory *df;
232 1.2.2.2 snj const token_t *token;
233 1.2.2.2 snj char *line;
234 1.2.2.2 snj size_t size;
235 1.2.2.2 snj void *serialized;
236 1.2.2.2 snj struct _region r;
237 1.2.2.2 snj
238 1.2.2.2 snj _DIAGASSERT(type != NULL);
239 1.2.2.2 snj _DIAGASSERT(reader != NULL);
240 1.2.2.2 snj _DIAGASSERT(writer != NULL);
241 1.2.2.2 snj
242 1.2.2.2 snj if (!strcasecmp(type, "MONETARY"))
243 1.2.2.2 snj category = &lc_monetary;
244 1.2.2.2 snj else if (!strcasecmp(type, "NUMERIC"))
245 1.2.2.2 snj category = &lc_numeric;
246 1.2.2.2 snj else if (!strcasecmp(type, "TIME"))
247 1.2.2.2 snj category = &lc_time;
248 1.2.2.2 snj else if (!strcasecmp(type, "MESSAGES"))
249 1.2.2.2 snj category = &lc_messages;
250 1.2.2.2 snj else {
251 1.2.2.2 snj usage();
252 1.2.2.2 snj /*NOTREACHED*/
253 1.2.2.2 snj }
254 1.2.2.2 snj if (_db_factory_create(&df, &_db_hash_std, NULL))
255 1.2.2.2 snj errx(1, "can't create db factory.\n");
256 1.2.2.2 snj if (_db_factory_add32_by_s(df, category->vers_sym, category->version))
257 1.2.2.2 snj errx(1, "can't store db.\n");
258 1.2.2.2 snj token = &category->tokens[0];
259 1.2.2.2 snj while (token->key != NULL) {
260 1.2.2.2 snj line = fparseln(reader, NULL,
261 1.2.2.2 snj NULL, delim, FPARSELN_UNESCALL);
262 1.2.2.2 snj if (line == NULL)
263 1.2.2.2 snj errx(1, "can't read line.\n");
264 1.2.2.2 snj if ((*token->save)(df, token->key, (const char *)line))
265 1.2.2.2 snj errx(1, "can't store db.\n");
266 1.2.2.2 snj free(line);
267 1.2.2.2 snj ++token;
268 1.2.2.2 snj }
269 1.2.2.2 snj size = _db_factory_calc_size(df);
270 1.2.2.2 snj _DIAGASSERT(size > 0);
271 1.2.2.2 snj serialized = malloc(size);
272 1.2.2.2 snj if (serialized == NULL)
273 1.2.2.2 snj errx(1, "can't malloc.\n");
274 1.2.2.2 snj _DIAGASSERT(serialized != NULL);
275 1.2.2.2 snj _region_init(&r, serialized, size);
276 1.2.2.2 snj if (_db_factory_serialize(df, category->magic, &r))
277 1.2.2.2 snj errx(1, "can't serialize db.\n");
278 1.2.2.2 snj fwrite(serialized, size, 1, writer);
279 1.2.2.2 snj _db_factory_free(df);
280 1.2.2.2 snj }
281