1 /* $NetBSD: src/usr.bin/mkesdb/yacc.y,v 1.3 2004/01/02 12:09:48 itojun Exp $ */
5 * Copyright (c)2003 Citrus Project,
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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.
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
30 #include <sys/types.h>
31 #include <sys/queue.h>
41 #include "citrus_namespace.h"
42 #include "citrus_types.h"
43 #include "citrus_region.h"
44 #include "citrus_esdb_file.h"
45 #include "citrus_db_hash.h"
46 #include "citrus_db_factory.h"
47 #include "citrus_lookup_factory.h"
53 static int debug = 0, num_csids = 0;
54 static char *output = NULL;
55 static char *name, *encoding, *variable;
56 static uint32_t invalid;
57 static int use_invalid = 0;
58 static struct named_csid_list named_csids;
60 static void dump_file(void);
61 static void register_named_csid(char *, uint32_t);
62 static void set_prop_string(const char *, char **, char **);
63 static void set_invalid(uint32_t);
72 %token R_NAME R_ENCODING R_VARIABLE R_DEFCSID R_INVALID
74 %token <i_value> L_IMM
75 %token <s_value> L_STRING
82 property : /* empty */
85 | property encoding R_LN
86 | property variable R_LN
87 | property defcsid R_LN
88 | property invalid R_LN
90 name : R_NAME L_STRING
92 set_prop_string("NAME", &name, &$2);
95 encoding : R_ENCODING L_STRING
97 set_prop_string("ENCODING", &encoding, &$2);
99 variable : R_VARIABLE L_STRING
101 set_prop_string("VARIABLE", &variable, &$2);
103 defcsid : R_DEFCSID L_STRING L_IMM
105 register_named_csid($2, $3);
108 invalid : R_INVALID L_IMM
115 yyerror(const char *s)
117 fprintf(stderr, "%s in %d\n", s, aline_number);
122 #define CHKERR(ret, func, a) \
126 errx(EXIT_FAILURE, "%s: %s", #func, strerror(ret)); \
127 } while (/*CONSTCOND*/0)
133 struct _db_factory *df;
135 struct named_csid *csid;
143 fprintf(stderr, "NAME is mandatory.\n");
147 fprintf(stderr, "ENCODING is mandatory.\n");
156 CHKERR(ret, _db_factory_create, (&df, _db_hash_std, NULL));
159 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_VERSION,
160 _CITRUS_ESDB_VERSION));
163 CHKERR(ret, _db_factory_addstr_by_s, (df, _CITRUS_ESDB_SYM_ENCODING,
168 CHKERR(ret, _db_factory_addstr_by_s,
169 (df, _CITRUS_ESDB_SYM_VARIABLE, variable));
173 CHKERR(ret, _db_factory_add32_by_s, (df,
174 _CITRUS_ESDB_SYM_INVALID,
177 /* store num of charsets */
178 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_NUM_CHARSETS,
181 STAILQ_FOREACH(csid, &named_csids, ci_entry) {
182 snprintf(buf, sizeof(buf), _CITRUS_ESDB_SYM_CSNAME_PREFIX "%d",
184 CHKERR(ret, _db_factory_addstr_by_s,
185 (df, buf, csid->ci_symbol));
186 snprintf(buf, sizeof(buf), _CITRUS_ESDB_SYM_CSID_PREFIX "%d",
188 CHKERR(ret, _db_factory_add32_by_s, (df, buf, csid->ci_csid));
193 * dump database to file
196 fp = fopen(output, "wb");
205 /* dump database body */
206 size = _db_factory_calc_size(df);
207 serialized = malloc(size);
208 _region_init(&data, serialized, size);
209 CHKERR(ret, _db_factory_serialize, (df, _CITRUS_ESDB_MAGIC, &data));
210 if (fwrite(serialized, size, 1, fp) != 1)
211 err(EXIT_FAILURE, "fwrite");
217 set_prop_string(const char *res, char **store, char **data)
222 snprintf(buf, sizeof(buf),
223 "%s is duplicated. ignored the one", res);
233 set_invalid(uint32_t inv)
240 register_named_csid(char *sym, uint32_t val)
242 struct named_csid *csid;
244 STAILQ_FOREACH(csid, &named_csids, ci_entry) {
245 if (strcmp(csid->ci_symbol, sym) == 0) {
246 yyerror("multiply defined CSID");
251 csid = malloc(sizeof(*csid));
256 csid->ci_symbol = sym;
258 STAILQ_INSERT_TAIL(&named_csids, csid, ci_entry);
268 /* dump DB to file */
270 out = fopen(output, "wb");
275 err(EXIT_FAILURE, "fopen");
277 ret = _lookup_factory_convert(out, in);
280 unlink(output); /* dump failure */
282 errx(EXIT_FAILURE, "%s\n", strerror(ret));
290 "\t%s [-o outfile] [infile]\n"
291 "\t%s -m [-o outfile] [infile]",
292 getprogname(), getprogname());
296 main(int argc, char **argv)
302 while ((ch=getopt(argc, argv, "do:m")) != -1) {
308 output = strdup(optarg);
325 in = fopen(argv[0], "r");
327 err(EXIT_FAILURE, "%s", argv[0]);
336 STAILQ_INIT(&named_csids);