usr.sbin/makefs: Sync with sys/vfs/hammer2
[dragonfly.git] / usr.bin / mkesdb / yacc.y
blob54650ff8a88ee31fd675b53cf902c78eb46af77a
1 /* $NetBSD: src/usr.bin/mkesdb/yacc.y,v 1.3 2004/01/02 12:09:48 itojun Exp $ */
3 %{
4 /*-
5 * Copyright (c)2003 Citrus Project,
6 * All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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
27 * SUCH DAMAGE.
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <assert.h>
33 #include <err.h>
34 #include <errno.h>
35 #include <limits.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.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"
49 #include "ldef.h"
51 extern FILE *yyin;
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
66 int yylex (void);
67 #endif
69 %union {
70 uint32_t i_value;
71 char *s_value;
74 %token R_NAME R_ENCODING R_VARIABLE R_DEFCSID R_INVALID
75 %token R_LN
76 %token <i_value> L_IMM
77 %token <s_value> L_STRING
81 file : property
82 { dump_file(); }
84 property : /* empty */
85 | property R_LN
86 | property name R_LN
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);
108 $2 = NULL;
110 invalid : R_INVALID L_IMM
112 set_invalid($2);
117 yyerror(const char *s)
119 fprintf(stderr, "%s in %d\n", s, aline_number);
121 return (0);
124 #define CHKERR(ret, func, a) \
125 do { \
126 ret = func a; \
127 if (ret) \
128 errx(EXIT_FAILURE, "%s: %s", #func, strerror(ret)); \
129 } while (/*CONSTCOND*/0)
130 static void
131 dump_file(void)
133 int ret;
134 FILE *fp;
135 struct _db_factory *df;
136 struct _region data;
137 struct named_csid *csid;
138 char buf[100];
139 int i;
140 void *serialized;
141 size_t size;
143 ret = 0;
144 if (!name) {
145 fprintf(stderr, "NAME is mandatory.\n");
146 ret = 1;
148 if (!encoding) {
149 fprintf(stderr, "ENCODING is mandatory.\n");
150 ret = 1;
152 if (ret)
153 exit(1);
156 * build database
158 CHKERR(ret, _db_factory_create, (&df, _db_hash_std, NULL));
160 /* store version */
161 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_VERSION,
162 _CITRUS_ESDB_VERSION));
164 /* store encoding */
165 CHKERR(ret, _db_factory_addstr_by_s, (df, _CITRUS_ESDB_SYM_ENCODING,
166 encoding));
168 /* store variable */
169 if (variable)
170 CHKERR(ret, _db_factory_addstr_by_s,
171 (df, _CITRUS_ESDB_SYM_VARIABLE, variable));
173 /* store invalid */
174 if (use_invalid)
175 CHKERR(ret, _db_factory_add32_by_s, (df,
176 _CITRUS_ESDB_SYM_INVALID,
177 invalid));
179 /* store num of charsets */
180 CHKERR(ret, _db_factory_add32_by_s, (df, _CITRUS_ESDB_SYM_NUM_CHARSETS,
181 num_csids));
182 i=0;
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));
191 i++;
195 * dump database to file
197 if (output)
198 fp = fopen(output, "wb");
199 else
200 fp = stdout;
202 if (fp == NULL) {
203 perror("fopen");
204 exit(1);
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");
215 fclose(fp);
218 static void
219 set_prop_string(const char *res, char **store, char **data)
221 char buf[256];
223 if (*store) {
224 snprintf(buf, sizeof(buf),
225 "%s is duplicated. ignored the one", res);
226 yyerror(buf);
227 return;
230 *store = *data;
231 *data = NULL;
234 static void
235 set_invalid(uint32_t inv)
237 invalid = inv;
238 use_invalid = 1;
241 static void
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");
249 exit(1);
253 csid = malloc(sizeof(*csid));
254 if (csid == NULL) {
255 perror("malloc");
256 exit(1);
258 csid->ci_symbol = sym;
259 csid->ci_csid = val;
260 STAILQ_INSERT_TAIL(&named_csids, csid, ci_entry);
261 num_csids++;
264 static void
265 do_mkdb(FILE *in)
267 int ret;
268 FILE *out;
270 /* dump DB to file */
271 if (output)
272 out = fopen(output, "wb");
273 else
274 out = stdout;
276 if (out==NULL)
277 err(EXIT_FAILURE, "fopen");
279 ret = _lookup_factory_convert(out, in);
280 fclose(out);
281 if (ret && output)
282 unlink(output); /* dump failure */
283 if (ret)
284 errx(EXIT_FAILURE, "%s\n", strerror(ret));
287 static void
288 usage(void)
290 errx(EXIT_FAILURE,
291 "usage:\n"
292 "\t%s [-o outfile] [infile]\n"
293 "\t%s -m [-o outfile] [infile]",
294 getprogname(), getprogname());
298 main(int argc, char **argv)
300 int ch;
301 FILE *in = NULL;
302 int mkdb = 0;
304 while ((ch=getopt(argc, argv, "do:m")) != -1) {
305 switch (ch) {
306 case 'd':
307 debug = 1;
308 break;
309 case 'o':
310 output = strdup(optarg);
311 break;
312 case 'm':
313 mkdb = 1;
314 break;
315 default:
316 usage();
320 argc-=optind;
321 argv+=optind;
322 switch (argc) {
323 case 0:
324 in = stdin;
325 break;
326 case 1:
327 in = fopen(argv[0], "r");
328 if (!in)
329 err(EXIT_FAILURE, "%s", argv[0]);
330 break;
331 default:
332 usage();
335 if (mkdb)
336 do_mkdb(in);
337 else {
338 STAILQ_INIT(&named_csids);
339 yyin=in;
340 yyparse();
343 return (0);