2 /* $NetBSD: yacc.y,v 1.4 2005/06/02 02:09:25 lukem Exp $ */
6 * SPDX-License-Identifier: BSD-2-Clause
8 * Copyright (c)2003 Citrus Project,
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 #include <sys/types.h>
35 #include <sys/queue.h>
46 #include "citrus_namespace.h"
47 #include "citrus_types.h"
48 #include "citrus_region.h"
49 #include "citrus_esdb_file.h"
50 #include "citrus_db_hash.h"
51 #include "citrus_db_factory.h"
52 #include "citrus_lookup_factory.h"
58 static struct named_csid_list named_csids;
59 static char *encoding, *name, *output = NULL, *variable;
60 static u_int32_t invalid;
61 static int debug = 0, num_csids = 0, use_invalid = 0;
63 static void dump_file(void);
64 static void register_named_csid(char *, u_int32_t);
65 static void set_invalid(u_int32_t);
66 static void set_prop_string(const char *, char **, char **);
73 %token R_NAME R_ENCODING R_VARIABLE R_DEFCSID R_INVALID
75 %token <i_value> L_IMM
76 %token <s_value> L_STRING
83 property : /* empty */
86 | property encoding R_LN
87 | property variable R_LN
88 | property defcsid R_LN
89 | property invalid R_LN
91 name : R_NAME L_STRING
93 set_prop_string("NAME", &name, &$2);
96 encoding : R_ENCODING L_STRING
98 set_prop_string("ENCODING", &encoding, &$2);
100 variable : R_VARIABLE L_STRING
102 set_prop_string("VARIABLE", &variable, &$2);
104 defcsid : R_DEFCSID L_STRING L_IMM
106 register_named_csid($2, $3);
109 invalid : R_INVALID L_IMM
116 yyerror(const char *s)
119 fprintf(stderr, "%s in %d\n", s, linenumber);
124 #define CHKERR(ret, func, a) \
128 errx(EXIT_FAILURE, "%s: %s", #func, strerror(ret)); \
129 } while (/*CONSTCOND*/0)
133 struct _db_factory *df;
135 struct named_csid *csid;
144 fprintf(stderr, "NAME is mandatory.\n");
148 fprintf(stderr, "ENCODING is mandatory.\n");
157 CHKERR(ret, _db_factory_create, (&df, _db_hash_std, NULL));
160 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_VERSION,
161 _CITRUS_ESDB_VERSION));
164 CHKERR(ret, _db_factory_addstr_by_s, (df, _CITRUS_ESDB_SYM_ENCODING,
169 CHKERR(ret, _db_factory_addstr_by_s,
170 (df, _CITRUS_ESDB_SYM_VARIABLE, variable));
174 CHKERR(ret, _db_factory_add32_by_s, (df,
175 _CITRUS_ESDB_SYM_INVALID, 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
195 fp = output ? fopen(output, "wb") : stdout;
201 /* dump database body */
202 size = _db_factory_calc_size(df);
203 serialized = malloc(size);
204 _region_init(&data, serialized, size);
205 CHKERR(ret, _db_factory_serialize, (df, _CITRUS_ESDB_MAGIC, &data));
206 if (fwrite(serialized, size, 1, fp) != 1)
207 err(EXIT_FAILURE, "fwrite");
213 set_prop_string(const char *res, char **store, char **data)
218 snprintf(buf, sizeof(buf),
219 "%s is duplicated. ignored the one", res);
229 set_invalid(u_int32_t inv)
237 register_named_csid(char *sym, u_int32_t val)
239 struct named_csid *csid;
241 STAILQ_FOREACH(csid, &named_csids, ci_entry) {
242 if (strcmp(csid->ci_symbol, sym) == 0) {
243 yyerror("multiply defined CSID");
248 csid = malloc(sizeof(*csid));
253 csid->ci_symbol = sym;
255 STAILQ_INSERT_TAIL(&named_csids, csid, ci_entry);
265 /* dump DB to file */
266 out = output ? fopen(output, "wb") : stdout;
268 err(EXIT_FAILURE, "fopen");
270 ret = _lookup_factory_convert(out, in);
273 unlink(output); /* dump failure */
275 errx(EXIT_FAILURE, "%s\n", strerror(ret));
283 "\t%s [-d] [-o outfile] [infile]\n"
284 "\t%s -m [-d] [-o outfile] [infile]",
285 getprogname(), getprogname());
289 main(int argc, char **argv)
294 while ((ch = getopt(argc, argv, "do:m")) != EOF) {
300 output = strdup(optarg);
317 in = fopen(argv[0], "r");
319 err(EXIT_FAILURE, "%s", argv[0]);
328 STAILQ_INIT(&named_csids);