2 /* $NetBSD: yacc.y,v 1.4 2005/06/02 02:09:25 lukem Exp $ */
6 * Copyright (c)2003 Citrus Project,
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
22 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 #include <sys/cdefs.h>
32 #include <sys/types.h>
33 #include <sys/queue.h>
44 #include "citrus_namespace.h"
45 #include "citrus_types.h"
46 #include "citrus_region.h"
47 #include "citrus_esdb_file.h"
48 #include "citrus_db_hash.h"
49 #include "citrus_db_factory.h"
50 #include "citrus_lookup_factory.h"
56 static struct named_csid_list named_csids;
57 static char *encoding, *name, *output = NULL, *variable;
58 static u_int32_t invalid;
59 static int debug = 0, num_csids = 0, use_invalid = 0;
61 static void dump_file(void);
62 static void register_named_csid(char *, u_int32_t);
63 static void set_invalid(u_int32_t);
64 static void set_prop_string(const char *, char **, char **);
71 %token R_NAME R_ENCODING R_VARIABLE R_DEFCSID R_INVALID
73 %token <i_value> L_IMM
74 %token <s_value> L_STRING
81 property : /* empty */
84 | property encoding R_LN
85 | property variable R_LN
86 | property defcsid R_LN
87 | property invalid R_LN
89 name : R_NAME L_STRING
91 set_prop_string("NAME", &name, &$2);
94 encoding : R_ENCODING L_STRING
96 set_prop_string("ENCODING", &encoding, &$2);
98 variable : R_VARIABLE L_STRING
100 set_prop_string("VARIABLE", &variable, &$2);
102 defcsid : R_DEFCSID L_STRING L_IMM
104 register_named_csid($2, $3);
107 invalid : R_INVALID L_IMM
114 yyerror(const char *s)
117 fprintf(stderr, "%s in %d\n", s, linenumber);
122 #define CHKERR(ret, func, a) \
126 errx(EXIT_FAILURE, "%s: %s", #func, strerror(ret)); \
127 } while (/*CONSTCOND*/0)
131 struct _db_factory *df;
133 struct named_csid *csid;
142 fprintf(stderr, "NAME is mandatory.\n");
146 fprintf(stderr, "ENCODING is mandatory.\n");
155 CHKERR(ret, _db_factory_create, (&df, _db_hash_std, NULL));
158 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_VERSION,
159 _CITRUS_ESDB_VERSION));
162 CHKERR(ret, _db_factory_addstr_by_s, (df, _CITRUS_ESDB_SYM_ENCODING,
167 CHKERR(ret, _db_factory_addstr_by_s,
168 (df, _CITRUS_ESDB_SYM_VARIABLE, variable));
172 CHKERR(ret, _db_factory_add32_by_s, (df,
173 _CITRUS_ESDB_SYM_INVALID, invalid));
175 /* store num of charsets */
176 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_NUM_CHARSETS,
179 STAILQ_FOREACH(csid, &named_csids, ci_entry) {
180 snprintf(buf, sizeof(buf), _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d",
182 CHKERR(ret, _db_factory_addstr_by_s,
183 (df, buf, csid->ci_symbol));
184 snprintf(buf, sizeof(buf), _CITRUS_ESDB_SYM_CSID_PREFIX "%d",
186 CHKERR(ret, _db_factory_add32_by_s, (df, buf, csid->ci_csid));
191 * dump database to file
193 fp = output ? fopen(output, "wb") : stdout;
199 /* dump database body */
200 size = _db_factory_calc_size(df);
201 serialized = malloc(size);
202 _region_init(&data, serialized, size);
203 CHKERR(ret, _db_factory_serialize, (df, _CITRUS_ESDB_MAGIC, &data));
204 if (fwrite(serialized, size, 1, fp) != 1)
205 err(EXIT_FAILURE, "fwrite");
211 set_prop_string(const char *res, char **store, char **data)
216 snprintf(buf, sizeof(buf),
217 "%s is duplicated. ignored the one", res);
227 set_invalid(u_int32_t inv)
235 register_named_csid(char *sym, u_int32_t val)
237 struct named_csid *csid;
239 STAILQ_FOREACH(csid, &named_csids, ci_entry) {
240 if (strcmp(csid->ci_symbol, sym) == 0) {
241 yyerror("multiply defined CSID");
246 csid = malloc(sizeof(*csid));
251 csid->ci_symbol = sym;
253 STAILQ_INSERT_TAIL(&named_csids, csid, ci_entry);
263 /* dump DB to file */
264 out = output ? fopen(output, "wb") : stdout;
266 err(EXIT_FAILURE, "fopen");
268 ret = _lookup_factory_convert(out, in);
271 unlink(output); /* dump failure */
273 errx(EXIT_FAILURE, "%s\n", strerror(ret));
281 "\t%s [-o outfile] [infile]\n"
282 "\t%s -m [-o outfile] [infile]",
283 getprogname(), getprogname());
287 main(int argc, char **argv)
292 while ((ch = getopt(argc, argv, "do:m")) != EOF) {
298 output = strdup(optarg);
315 in = fopen(argv[0], "r");
317 err(EXIT_FAILURE, "%s", argv[0]);
326 STAILQ_INIT(&named_csids);