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);
65 #if YYPATCH < 20180510
74 %token R_NAME R_ENCODING R_VARIABLE R_DEFCSID R_INVALID
76 %token
<i_value
> L_IMM
77 %token
<s_value
> L_STRING
84 property
: /* empty */
87 | property encoding R_LN
88 | property variable R_LN
89 | property defcsid R_LN
90 | property invalid R_LN
92 name
: R_NAME L_STRING
94 set_prop_string
("NAME", &name
, &$2);
97 encoding
: R_ENCODING L_STRING
99 set_prop_string
("ENCODING", &encoding
, &$2);
101 variable
: R_VARIABLE L_STRING
103 set_prop_string
("VARIABLE", &variable
, &$2);
105 defcsid
: R_DEFCSID L_STRING L_IMM
107 register_named_csid
($2, $3);
110 invalid
: R_INVALID L_IMM
117 yyerror(const char *s
)
119 fprintf
(stderr
, "%s in %d\n", s
, aline_number
);
124 #define CHKERR(ret, func, a) \
128 errx
(EXIT_FAILURE
, "%s: %s", #func, strerror(ret)); \
129 } while
(/*CONSTCOND*/0)
135 struct _db_factory
*df
;
137 struct named_csid
*csid
;
145 fprintf
(stderr
, "NAME is mandatory.\n");
149 fprintf
(stderr
, "ENCODING is mandatory.\n");
158 CHKERR
(ret
, _db_factory_create
, (&df
, _db_hash_std
, NULL
));
161 CHKERR
(ret
, _db_factory_add32_by_s
, (df
, _CITRUS_ESDB_SYM_VERSION
,
162 _CITRUS_ESDB_VERSION
));
165 CHKERR
(ret
, _db_factory_addstr_by_s
, (df
, _CITRUS_ESDB_SYM_ENCODING
,
170 CHKERR
(ret
, _db_factory_addstr_by_s
,
171 (df
, _CITRUS_ESDB_SYM_VARIABLE
, variable
));
175 CHKERR
(ret
, _db_factory_add32_by_s
, (df
,
176 _CITRUS_ESDB_SYM_INVALID
,
179 /* store num of charsets */
180 CHKERR
(ret
, _db_factory_add32_by_s
, (df
, _CITRUS_ESDB_SYM_NUM_CHARSETS
,
183 STAILQ_FOREACH
(csid
, &named_csids
, ci_entry
) {
184 snprintf
(buf
, sizeof
(buf
), _CITRUS_ESDB_SYM_CSNAME_PREFIX
"%d",
186 CHKERR
(ret
, _db_factory_addstr_by_s
,
187 (df
, buf
, csid
->ci_symbol
));
188 snprintf
(buf
, sizeof
(buf
), _CITRUS_ESDB_SYM_CSID_PREFIX
"%d",
190 CHKERR
(ret
, _db_factory_add32_by_s
, (df
, buf
, csid
->ci_csid
));
195 * dump database to file
198 fp
= fopen
(output
, "wb");
207 /* dump database body */
208 size
= _db_factory_calc_size
(df
);
209 serialized
= malloc
(size
);
210 _region_init
(&data
, serialized
, size
);
211 CHKERR
(ret
, _db_factory_serialize
, (df
, _CITRUS_ESDB_MAGIC
, &data
));
212 if
(fwrite
(serialized
, size
, 1, fp
) != 1)
213 err
(EXIT_FAILURE
, "fwrite");
219 set_prop_string
(const char *res
, char **store
, char **data
)
224 snprintf
(buf
, sizeof
(buf
),
225 "%s is duplicated. ignored the one", res
);
235 set_invalid
(uint32_t inv
)
242 register_named_csid
(char *sym
, uint32_t val
)
244 struct named_csid
*csid
;
246 STAILQ_FOREACH
(csid
, &named_csids
, ci_entry
) {
247 if
(strcmp
(csid
->ci_symbol
, sym
) == 0) {
248 yyerror("multiply defined CSID");
253 csid
= malloc
(sizeof
(*csid
));
258 csid
->ci_symbol
= sym
;
260 STAILQ_INSERT_TAIL
(&named_csids
, csid
, ci_entry
);
270 /* dump DB to file */
272 out
= fopen
(output
, "wb");
277 err
(EXIT_FAILURE
, "fopen");
279 ret
= _lookup_factory_convert
(out
, in
);
282 unlink
(output
); /* dump failure */
284 errx
(EXIT_FAILURE
, "%s\n", strerror
(ret
));
292 "\t%s [-o outfile] [infile]\n"
293 "\t%s -m [-o outfile] [infile]",
294 getprogname
(), getprogname
());
298 main
(int argc
, char **argv
)
304 while
((ch
=getopt
(argc
, argv
, "do:m")) != -1) {
310 output
= strdup
(optarg
);
327 in
= fopen
(argv
[0], "r");
329 err
(EXIT_FAILURE
, "%s", argv
[0]);
338 STAILQ_INIT
(&named_csids
);