slc-gram.y revision 1.1.1.2 1 1.1 elric /* $NetBSD: slc-gram.y,v 1.1.1.2 2014/04/24 12:45:53 pettai Exp $ */
2 1.1 elric
3 1.1 elric %{
4 1.1 elric /*
5 1.1 elric * Copyright (c) 2004-2006 Kungliga Tekniska Hgskolan
6 1.1 elric * (Royal Institute of Technology, Stockholm, Sweden).
7 1.1 elric * All rights reserved.
8 1.1 elric *
9 1.1 elric * Redistribution and use in source and binary forms, with or without
10 1.1 elric * modification, are permitted provided that the following conditions
11 1.1 elric * are met:
12 1.1 elric *
13 1.1 elric * 1. Redistributions of source code must retain the above copyright
14 1.1 elric * notice, this list of conditions and the following disclaimer.
15 1.1 elric *
16 1.1 elric * 2. Redistributions in binary form must reproduce the above copyright
17 1.1 elric * notice, this list of conditions and the following disclaimer in the
18 1.1 elric * documentation and/or other materials provided with the distribution.
19 1.1 elric *
20 1.1 elric * 3. Neither the name of the Institute nor the names of its contributors
21 1.1 elric * may be used to endorse or promote products derived from this software
22 1.1 elric * without specific prior written permission.
23 1.1 elric *
24 1.1 elric * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
25 1.1 elric * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 1.1 elric * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 1.1 elric * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
28 1.1 elric * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 1.1 elric * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 1.1 elric * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 1.1 elric * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 1.1 elric * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 1.1 elric * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 1.1 elric * SUCH DAMAGE.
35 1.1 elric */
36 1.1 elric
37 1.1 elric #include <config.h>
38 1.1 elric
39 1.1 elric #include <stdio.h>
40 1.1 elric #include <stdlib.h>
41 1.1 elric #include <err.h>
42 1.1 elric #include <ctype.h>
43 1.1 elric #include <limits.h>
44 1.1 elric #include <getarg.h>
45 1.1 elric #include <vers.h>
46 1.1 elric #include <roken.h>
47 1.1 elric
48 1.1 elric #include "slc.h"
49 1.1 elric extern FILE *yyin;
50 1.1 elric extern struct assignment *assignment;
51 1.1 elric
52 1.1 elric /* Declarations for Bison:
53 1.1 elric */
54 1.1 elric #define YYMALLOC malloc
55 1.1 elric #define YYFREE free
56 1.1 elric
57 1.1 elric %}
58 1.1 elric
59 1.1 elric %union {
60 1.1 elric char *string;
61 1.1 elric struct assignment *assignment;
62 1.1 elric }
63 1.1 elric
64 1.1 elric %token <string> LITERAL
65 1.1 elric %token <string> STRING
66 1.1 elric %type <assignment> assignment assignments
67 1.1 elric
68 1.1 elric %start start
69 1.1 elric
70 1.1 elric %%
71 1.1 elric
72 1.1 elric start : assignments
73 1.1 elric {
74 1.1 elric assignment = $1;
75 1.1 elric }
76 1.1 elric ;
77 1.1 elric
78 1.1 elric assignments : assignment assignments
79 1.1 elric {
80 1.1 elric $1->next = $2;
81 1.1 elric $$ = $1;
82 1.1 elric }
83 1.1 elric | assignment
84 1.1 elric ;
85 1.1 elric
86 1.1 elric assignment : LITERAL '=' STRING
87 1.1 elric {
88 1.1 elric $$ = malloc(sizeof(*$$));
89 1.1 elric $$->name = $1;
90 1.1 elric $$->type = a_value;
91 1.1 elric $$->lineno = lineno;
92 1.1 elric $$->u.value = $3;
93 1.1 elric $$->next = NULL;
94 1.1 elric }
95 1.1 elric | LITERAL '=' '{' assignments '}'
96 1.1 elric {
97 1.1 elric $$ = malloc(sizeof(*$$));
98 1.1 elric $$->name = $1;
99 1.1 elric $$->type = a_assignment;
100 1.1 elric $$->lineno = lineno;
101 1.1 elric $$->u.assignment = $4;
102 1.1 elric $$->next = NULL;
103 1.1 elric }
104 1.1 elric ;
105 1.1 elric
106 1.1 elric %%
107 1.1 elric char *filename;
108 1.1 elric FILE *cfile, *hfile;
109 1.1 elric int error_flag;
110 1.1 elric struct assignment *assignment;
111 1.1 elric
112 1.1 elric
113 1.1 elric static void
114 1.1 elric ex(struct assignment *a, const char *fmt, ...)
115 1.1 elric {
116 1.1 elric va_list ap;
117 1.1 elric fprintf(stderr, "%s:%d: ", a->name, a->lineno);
118 1.1 elric va_start(ap, fmt);
119 1.1 elric vfprintf(stderr, fmt, ap);
120 1.1 elric va_end(ap);
121 1.1 elric fprintf(stderr, "\n");
122 1.1 elric }
123 1.1 elric
124 1.1 elric
125 1.1 elric
126 1.1 elric static int
127 1.1 elric check_option(struct assignment *as)
128 1.1 elric {
129 1.1 elric struct assignment *a;
130 1.1 elric int seen_long = 0;
131 1.1 elric int seen_name = 0;
132 1.1 elric int seen_short = 0;
133 1.1 elric int seen_type = 0;
134 1.1 elric int seen_argument = 0;
135 1.1 elric int seen_help = 0;
136 1.1 elric int seen_default = 0;
137 1.1 elric int ret = 0;
138 1.1 elric
139 1.1 elric for(a = as; a != NULL; a = a->next) {
140 1.1 elric if(strcmp(a->name, "long") == 0)
141 1.1 elric seen_long++;
142 1.1 elric else if(strcmp(a->name, "short") == 0)
143 1.1 elric seen_short++;
144 1.1 elric else if(strcmp(a->name, "name") == 0)
145 1.1 elric seen_name++;
146 1.1 elric else if(strcmp(a->name, "type") == 0)
147 1.1 elric seen_type++;
148 1.1 elric else if(strcmp(a->name, "argument") == 0)
149 1.1 elric seen_argument++;
150 1.1 elric else if(strcmp(a->name, "help") == 0)
151 1.1 elric seen_help++;
152 1.1 elric else if(strcmp(a->name, "default") == 0)
153 1.1 elric seen_default++;
154 1.1 elric else {
155 1.1 elric ex(a, "unknown name %s", a->name);
156 1.1 elric ret++;
157 1.1 elric }
158 1.1 elric }
159 1.1 elric if(seen_long == 0 && seen_short == 0) {
160 1.1 elric ex(as, "neither long nor short option");
161 1.1 elric ret++;
162 1.1 elric }
163 1.1 elric if (seen_long == 0 && seen_name == 0) {
164 1.1 elric ex(as, "either of long or name option must be used");
165 1.1 elric ret++;
166 1.1 elric }
167 1.1 elric if(seen_long > 1) {
168 1.1 elric ex(as, "multiple long options");
169 1.1 elric ret++;
170 1.1 elric }
171 1.1 elric if(seen_short > 1) {
172 1.1 elric ex(as, "multiple short options");
173 1.1 elric ret++;
174 1.1 elric }
175 1.1 elric if(seen_type > 1) {
176 1.1 elric ex(as, "multiple types");
177 1.1 elric ret++;
178 1.1 elric }
179 1.1 elric if(seen_argument > 1) {
180 1.1 elric ex(as, "multiple arguments");
181 1.1 elric ret++;
182 1.1 elric }
183 1.1 elric if(seen_help > 1) {
184 1.1 elric ex(as, "multiple help strings");
185 1.1 elric ret++;
186 1.1 elric }
187 1.1 elric if(seen_default > 1) {
188 1.1 elric ex(as, "multiple default values");
189 1.1 elric ret++;
190 1.1 elric }
191 1.1 elric return ret;
192 1.1 elric }
193 1.1 elric
194 1.1 elric static int
195 1.1 elric check_command(struct assignment *as)
196 1.1 elric {
197 1.1 elric struct assignment *a;
198 1.1 elric int seen_name = 0;
199 1.1 elric int seen_function = 0;
200 1.1 elric int seen_help = 0;
201 1.1 elric int seen_argument = 0;
202 1.1 elric int seen_minargs = 0;
203 1.1 elric int seen_maxargs = 0;
204 1.1 elric int ret = 0;
205 1.1 elric for(a = as; a != NULL; a = a->next) {
206 1.1 elric if(strcmp(a->name, "name") == 0)
207 1.1 elric seen_name++;
208 1.1 elric else if(strcmp(a->name, "function") == 0) {
209 1.1 elric seen_function++;
210 1.1 elric } else if(strcmp(a->name, "option") == 0)
211 1.1 elric ret += check_option(a->u.assignment);
212 1.1 elric else if(strcmp(a->name, "help") == 0) {
213 1.1 elric seen_help++;
214 1.1 elric } else if(strcmp(a->name, "argument") == 0) {
215 1.1 elric seen_argument++;
216 1.1 elric } else if(strcmp(a->name, "min_args") == 0) {
217 1.1 elric seen_minargs++;
218 1.1 elric } else if(strcmp(a->name, "max_args") == 0) {
219 1.1 elric seen_maxargs++;
220 1.1 elric } else {
221 1.1 elric ex(a, "unknown name: %s", a->name);
222 1.1 elric ret++;
223 1.1 elric }
224 1.1 elric }
225 1.1 elric if(seen_name == 0) {
226 1.1 elric ex(as, "no command name");
227 1.1 elric ret++;
228 1.1 elric }
229 1.1 elric if(seen_function > 1) {
230 1.1 elric ex(as, "multiple function names");
231 1.1 elric ret++;
232 1.1 elric }
233 1.1 elric if(seen_help > 1) {
234 1.1 elric ex(as, "multiple help strings");
235 1.1 elric ret++;
236 1.1 elric }
237 1.1 elric if(seen_argument > 1) {
238 1.1 elric ex(as, "multiple argument strings");
239 1.1 elric ret++;
240 1.1 elric }
241 1.1 elric if(seen_minargs > 1) {
242 1.1 elric ex(as, "multiple min_args strings");
243 1.1 elric ret++;
244 1.1 elric }
245 1.1 elric if(seen_maxargs > 1) {
246 1.1 elric ex(as, "multiple max_args strings");
247 1.1 elric ret++;
248 1.1 elric }
249 1.1.1.2 pettai
250 1.1 elric return ret;
251 1.1 elric }
252 1.1 elric
253 1.1 elric static int
254 1.1 elric check(struct assignment *as)
255 1.1 elric {
256 1.1 elric struct assignment *a;
257 1.1 elric int ret = 0;
258 1.1 elric for(a = as; a != NULL; a = a->next) {
259 1.1 elric if(strcmp(a->name, "command")) {
260 1.1 elric fprintf(stderr, "unknown type %s line %d\n", a->name, a->lineno);
261 1.1 elric ret++;
262 1.1 elric continue;
263 1.1 elric }
264 1.1 elric if(a->type != a_assignment) {
265 1.1 elric fprintf(stderr, "bad command definition %s line %d\n", a->name, a->lineno);
266 1.1 elric ret++;
267 1.1 elric continue;
268 1.1 elric }
269 1.1 elric ret += check_command(a->u.assignment);
270 1.1 elric }
271 1.1 elric return ret;
272 1.1 elric }
273 1.1 elric
274 1.1 elric static struct assignment *
275 1.1 elric find_next(struct assignment *as, const char *name)
276 1.1 elric {
277 1.1 elric for(as = as->next; as != NULL; as = as->next) {
278 1.1 elric if(strcmp(as->name, name) == 0)
279 1.1 elric return as;
280 1.1 elric }
281 1.1 elric return NULL;
282 1.1 elric }
283 1.1 elric
284 1.1 elric static struct assignment *
285 1.1 elric find(struct assignment *as, const char *name)
286 1.1 elric {
287 1.1 elric for(; as != NULL; as = as->next) {
288 1.1 elric if(strcmp(as->name, name) == 0)
289 1.1 elric return as;
290 1.1 elric }
291 1.1 elric return NULL;
292 1.1 elric }
293 1.1 elric
294 1.1 elric static void
295 1.1 elric space(FILE *f, int level)
296 1.1 elric {
297 1.1 elric fprintf(f, "%*.*s", level * 4, level * 4, " ");
298 1.1 elric }
299 1.1 elric
300 1.1 elric static void
301 1.1 elric cprint(int level, const char *fmt, ...)
302 1.1 elric {
303 1.1 elric va_list ap;
304 1.1 elric va_start(ap, fmt);
305 1.1 elric space(cfile, level);
306 1.1 elric vfprintf(cfile, fmt, ap);
307 1.1 elric va_end(ap);
308 1.1 elric }
309 1.1 elric
310 1.1 elric static void
311 1.1 elric hprint(int level, const char *fmt, ...)
312 1.1 elric {
313 1.1 elric va_list ap;
314 1.1 elric va_start(ap, fmt);
315 1.1 elric space(hfile, level);
316 1.1 elric vfprintf(hfile, fmt, ap);
317 1.1 elric va_end(ap);
318 1.1 elric }
319 1.1 elric
320 1.1 elric static void gen_name(char *str);
321 1.1 elric
322 1.1 elric static void
323 1.1 elric gen_command(struct assignment *as)
324 1.1 elric {
325 1.1 elric struct assignment *a, *b;
326 1.1 elric char *f;
327 1.1 elric a = find(as, "name");
328 1.1 elric f = strdup(a->u.value);
329 1.1 elric gen_name(f);
330 1.1 elric cprint(1, " { ");
331 1.1 elric fprintf(cfile, "\"%s\", ", a->u.value);
332 1.1 elric fprintf(cfile, "%s_wrap, ", f);
333 1.1 elric b = find(as, "argument");
334 1.1 elric if(b)
335 1.1 elric fprintf(cfile, "\"%s %s\", ", a->u.value, b->u.value);
336 1.1 elric else
337 1.1 elric fprintf(cfile, "\"%s\", ", a->u.value);
338 1.1 elric b = find(as, "help");
339 1.1 elric if(b)
340 1.1 elric fprintf(cfile, "\"%s\"", b->u.value);
341 1.1 elric else
342 1.1 elric fprintf(cfile, "NULL");
343 1.1 elric fprintf(cfile, " },\n");
344 1.1 elric for(a = a->next; a != NULL; a = a->next)
345 1.1 elric if(strcmp(a->name, "name") == 0)
346 1.1 elric cprint(1, " { \"%s\" },\n", a->u.value);
347 1.1 elric cprint(0, "\n");
348 1.1 elric }
349 1.1 elric
350 1.1 elric static void
351 1.1 elric gen_name(char *str)
352 1.1 elric {
353 1.1 elric char *p;
354 1.1 elric for(p = str; *p != '\0'; p++)
355 1.1 elric if(!isalnum((unsigned char)*p))
356 1.1 elric *p = '_';
357 1.1 elric }
358 1.1 elric
359 1.1 elric static char *
360 1.1 elric make_name(struct assignment *as)
361 1.1 elric {
362 1.1 elric struct assignment *lopt;
363 1.1 elric struct assignment *type;
364 1.1 elric char *s;
365 1.1 elric
366 1.1 elric lopt = find(as, "long");
367 1.1 elric if(lopt == NULL)
368 1.1 elric lopt = find(as, "name");
369 1.1 elric if(lopt == NULL)
370 1.1 elric return NULL;
371 1.1 elric
372 1.1 elric type = find(as, "type");
373 1.1 elric if(strcmp(type->u.value, "-flag") == 0)
374 1.1 elric asprintf(&s, "%s_flag", lopt->u.value);
375 1.1 elric else
376 1.1 elric asprintf(&s, "%s_%s", lopt->u.value, type->u.value);
377 1.1 elric gen_name(s);
378 1.1 elric return s;
379 1.1 elric }
380 1.1 elric
381 1.1 elric
382 1.1 elric static void defval_int(const char *name, struct assignment *defval)
383 1.1 elric {
384 1.1 elric if(defval != NULL)
385 1.1 elric cprint(1, "opt.%s = %s;\n", name, defval->u.value);
386 1.1 elric else
387 1.1 elric cprint(1, "opt.%s = 0;\n", name);
388 1.1 elric }
389 1.1 elric static void defval_neg_flag(const char *name, struct assignment *defval)
390 1.1 elric {
391 1.1 elric if(defval != NULL)
392 1.1 elric cprint(1, "opt.%s = %s;\n", name, defval->u.value);
393 1.1 elric else
394 1.1 elric cprint(1, "opt.%s = 1;\n", name);
395 1.1 elric }
396 1.1 elric static void defval_string(const char *name, struct assignment *defval)
397 1.1 elric {
398 1.1 elric if(defval != NULL)
399 1.1.1.2 pettai cprint(1, "opt.%s = (char *)(unsigned long)\"%s\";\n", name, defval->u.value);
400 1.1 elric else
401 1.1 elric cprint(1, "opt.%s = NULL;\n", name);
402 1.1 elric }
403 1.1 elric static void defval_strings(const char *name, struct assignment *defval)
404 1.1 elric {
405 1.1 elric cprint(1, "opt.%s.num_strings = 0;\n", name);
406 1.1 elric cprint(1, "opt.%s.strings = NULL;\n", name);
407 1.1 elric }
408 1.1 elric
409 1.1 elric static void free_strings(const char *name)
410 1.1 elric {
411 1.1 elric cprint(1, "free_getarg_strings (&opt.%s);\n", name);
412 1.1 elric }
413 1.1 elric
414 1.1 elric struct type_handler {
415 1.1 elric const char *typename;
416 1.1 elric const char *c_type;
417 1.1 elric const char *getarg_type;
418 1.1 elric void (*defval)(const char*, struct assignment*);
419 1.1 elric void (*free)(const char*);
420 1.1 elric } type_handlers[] = {
421 1.1 elric { "integer",
422 1.1 elric "int",
423 1.1 elric "arg_integer",
424 1.1 elric defval_int,
425 1.1 elric NULL
426 1.1 elric },
427 1.1 elric { "string",
428 1.1 elric "char*",
429 1.1 elric "arg_string",
430 1.1 elric defval_string,
431 1.1 elric NULL
432 1.1 elric },
433 1.1 elric { "strings",
434 1.1 elric "struct getarg_strings",
435 1.1 elric "arg_strings",
436 1.1 elric defval_strings,
437 1.1 elric free_strings
438 1.1 elric },
439 1.1 elric { "flag",
440 1.1 elric "int",
441 1.1 elric "arg_flag",
442 1.1 elric defval_int,
443 1.1 elric NULL
444 1.1 elric },
445 1.1 elric { "-flag",
446 1.1 elric "int",
447 1.1 elric "arg_negative_flag",
448 1.1 elric defval_neg_flag,
449 1.1 elric NULL
450 1.1 elric },
451 1.1 elric { NULL }
452 1.1 elric };
453 1.1 elric
454 1.1 elric static struct type_handler *find_handler(struct assignment *type)
455 1.1 elric {
456 1.1 elric struct type_handler *th;
457 1.1 elric for(th = type_handlers; th->typename != NULL; th++)
458 1.1 elric if(strcmp(type->u.value, th->typename) == 0)
459 1.1 elric return th;
460 1.1 elric ex(type, "unknown type \"%s\"", type->u.value);
461 1.1 elric exit(1);
462 1.1 elric }
463 1.1 elric
464 1.1 elric static void
465 1.1 elric gen_options(struct assignment *opt1, const char *name)
466 1.1 elric {
467 1.1 elric struct assignment *tmp;
468 1.1 elric
469 1.1 elric hprint(0, "struct %s_options {\n", name);
470 1.1 elric
471 1.1 elric for(tmp = opt1;
472 1.1 elric tmp != NULL;
473 1.1 elric tmp = find_next(tmp, "option")) {
474 1.1 elric struct assignment *type;
475 1.1 elric struct type_handler *th;
476 1.1 elric char *s;
477 1.1.1.2 pettai
478 1.1 elric s = make_name(tmp->u.assignment);
479 1.1 elric type = find(tmp->u.assignment, "type");
480 1.1 elric th = find_handler(type);
481 1.1 elric hprint(1, "%s %s;\n", th->c_type, s);
482 1.1 elric free(s);
483 1.1 elric }
484 1.1 elric hprint(0, "};\n");
485 1.1 elric }
486 1.1 elric
487 1.1 elric static void
488 1.1 elric gen_wrapper(struct assignment *as)
489 1.1 elric {
490 1.1 elric struct assignment *name;
491 1.1 elric struct assignment *arg;
492 1.1 elric struct assignment *opt1;
493 1.1 elric struct assignment *function;
494 1.1 elric struct assignment *tmp;
495 1.1 elric char *n, *f;
496 1.1 elric int nargs = 0;
497 1.1.1.2 pettai int narguments = 0;
498 1.1 elric
499 1.1 elric name = find(as, "name");
500 1.1 elric n = strdup(name->u.value);
501 1.1 elric gen_name(n);
502 1.1 elric arg = find(as, "argument");
503 1.1.1.2 pettai if (arg)
504 1.1.1.2 pettai narguments++;
505 1.1 elric opt1 = find(as, "option");
506 1.1 elric function = find(as, "function");
507 1.1 elric if(function)
508 1.1 elric f = function->u.value;
509 1.1 elric else
510 1.1 elric f = n;
511 1.1 elric
512 1.1 elric
513 1.1 elric if(opt1 != NULL) {
514 1.1 elric gen_options(opt1, n);
515 1.1 elric hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n);
516 1.1 elric } else {
517 1.1 elric hprint(0, "int %s(void*, int, char **);\n", f);
518 1.1 elric }
519 1.1 elric
520 1.1 elric fprintf(cfile, "static int\n");
521 1.1 elric fprintf(cfile, "%s_wrap(int argc, char **argv)\n", n);
522 1.1 elric fprintf(cfile, "{\n");
523 1.1 elric if(opt1 != NULL)
524 1.1 elric cprint(1, "struct %s_options opt;\n", n);
525 1.1 elric cprint(1, "int ret;\n");
526 1.1 elric cprint(1, "int optidx = 0;\n");
527 1.1 elric cprint(1, "struct getargs args[] = {\n");
528 1.1 elric for(tmp = find(as, "option");
529 1.1 elric tmp != NULL;
530 1.1 elric tmp = find_next(tmp, "option")) {
531 1.1 elric struct assignment *type = find(tmp->u.assignment, "type");
532 1.1 elric struct assignment *lopt = find(tmp->u.assignment, "long");
533 1.1 elric struct assignment *sopt = find(tmp->u.assignment, "short");
534 1.1 elric struct assignment *aarg = find(tmp->u.assignment, "argument");
535 1.1 elric struct assignment *help = find(tmp->u.assignment, "help");
536 1.1 elric
537 1.1 elric struct type_handler *th;
538 1.1.1.2 pettai
539 1.1 elric cprint(2, "{ ");
540 1.1 elric if(lopt)
541 1.1 elric fprintf(cfile, "\"%s\", ", lopt->u.value);
542 1.1 elric else
543 1.1 elric fprintf(cfile, "NULL, ");
544 1.1 elric if(sopt)
545 1.1 elric fprintf(cfile, "'%c', ", *sopt->u.value);
546 1.1 elric else
547 1.1 elric fprintf(cfile, "0, ");
548 1.1 elric th = find_handler(type);
549 1.1 elric fprintf(cfile, "%s, ", th->getarg_type);
550 1.1 elric fprintf(cfile, "NULL, ");
551 1.1 elric if(help)
552 1.1 elric fprintf(cfile, "\"%s\", ", help->u.value);
553 1.1 elric else
554 1.1 elric fprintf(cfile, "NULL, ");
555 1.1.1.2 pettai if(aarg) {
556 1.1 elric fprintf(cfile, "\"%s\"", aarg->u.value);
557 1.1.1.2 pettai narguments++;
558 1.1.1.2 pettai } else
559 1.1 elric fprintf(cfile, "NULL");
560 1.1 elric fprintf(cfile, " },\n");
561 1.1 elric }
562 1.1 elric cprint(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
563 1.1 elric cprint(1, "};\n");
564 1.1 elric cprint(1, "int help_flag = 0;\n");
565 1.1 elric
566 1.1 elric for(tmp = find(as, "option");
567 1.1 elric tmp != NULL;
568 1.1 elric tmp = find_next(tmp, "option")) {
569 1.1 elric char *s;
570 1.1 elric struct assignment *type = find(tmp->u.assignment, "type");
571 1.1 elric
572 1.1 elric struct assignment *defval = find(tmp->u.assignment, "default");
573 1.1 elric
574 1.1 elric struct type_handler *th;
575 1.1.1.2 pettai
576 1.1 elric s = make_name(tmp->u.assignment);
577 1.1 elric th = find_handler(type);
578 1.1 elric (*th->defval)(s, defval);
579 1.1 elric free(s);
580 1.1 elric }
581 1.1 elric
582 1.1 elric for(tmp = find(as, "option");
583 1.1 elric tmp != NULL;
584 1.1 elric tmp = find_next(tmp, "option")) {
585 1.1 elric char *s;
586 1.1 elric s = make_name(tmp->u.assignment);
587 1.1 elric cprint(1, "args[%d].value = &opt.%s;\n", nargs++, s);
588 1.1 elric free(s);
589 1.1 elric }
590 1.1 elric cprint(1, "args[%d].value = &help_flag;\n", nargs++);
591 1.1 elric cprint(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs);
592 1.1 elric cprint(2, "goto usage;\n");
593 1.1 elric
594 1.1 elric {
595 1.1 elric int min_args = -1;
596 1.1 elric int max_args = -1;
597 1.1 elric char *end;
598 1.1.1.2 pettai if(narguments == 0) {
599 1.1 elric max_args = 0;
600 1.1 elric } else {
601 1.1 elric if((tmp = find(as, "min_args")) != NULL) {
602 1.1 elric min_args = strtol(tmp->u.value, &end, 0);
603 1.1 elric if(*end != '\0') {
604 1.1 elric ex(tmp, "min_args is not numeric");
605 1.1 elric exit(1);
606 1.1 elric }
607 1.1 elric if(min_args < 0) {
608 1.1 elric ex(tmp, "min_args must be non-negative");
609 1.1 elric exit(1);
610 1.1 elric }
611 1.1 elric }
612 1.1 elric if((tmp = find(as, "max_args")) != NULL) {
613 1.1 elric max_args = strtol(tmp->u.value, &end, 0);
614 1.1 elric if(*end != '\0') {
615 1.1 elric ex(tmp, "max_args is not numeric");
616 1.1 elric exit(1);
617 1.1 elric }
618 1.1 elric if(max_args < 0) {
619 1.1 elric ex(tmp, "max_args must be non-negative");
620 1.1 elric exit(1);
621 1.1 elric }
622 1.1 elric }
623 1.1 elric }
624 1.1 elric if(min_args != -1 || max_args != -1) {
625 1.1 elric if(min_args == max_args) {
626 1.1 elric cprint(1, "if(argc - optidx != %d) {\n",
627 1.1 elric min_args);
628 1.1 elric cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args);
629 1.1 elric cprint(2, "goto usage;\n");
630 1.1 elric cprint(1, "}\n");
631 1.1 elric } else {
632 1.1 elric if(max_args != -1) {
633 1.1 elric cprint(1, "if(argc - optidx > %d) {\n", max_args);
634 1.1 elric cprint(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args);
635 1.1 elric cprint(2, "goto usage;\n");
636 1.1 elric cprint(1, "}\n");
637 1.1 elric }
638 1.1 elric if(min_args != -1) {
639 1.1 elric cprint(1, "if(argc - optidx < %d) {\n", min_args);
640 1.1 elric cprint(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args);
641 1.1 elric cprint(2, "goto usage;\n");
642 1.1 elric cprint(1, "}\n");
643 1.1 elric }
644 1.1 elric }
645 1.1 elric }
646 1.1 elric }
647 1.1 elric
648 1.1 elric cprint(1, "if(help_flag)\n");
649 1.1 elric cprint(2, "goto usage;\n");
650 1.1 elric
651 1.1 elric cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
652 1.1 elric f, opt1 ? "&opt": "NULL");
653 1.1 elric
654 1.1 elric /* free allocated data */
655 1.1 elric for(tmp = find(as, "option");
656 1.1 elric tmp != NULL;
657 1.1 elric tmp = find_next(tmp, "option")) {
658 1.1 elric char *s;
659 1.1 elric struct assignment *type = find(tmp->u.assignment, "type");
660 1.1 elric struct type_handler *th;
661 1.1 elric th = find_handler(type);
662 1.1 elric if(th->free == NULL)
663 1.1 elric continue;
664 1.1 elric s = make_name(tmp->u.assignment);
665 1.1 elric (*th->free)(s);
666 1.1 elric free(s);
667 1.1 elric }
668 1.1 elric cprint(1, "return ret;\n");
669 1.1 elric
670 1.1 elric cprint(0, "usage:\n");
671 1.1 elric cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs,
672 1.1 elric name->u.value, arg ? arg->u.value : "");
673 1.1 elric /* free allocated data */
674 1.1 elric for(tmp = find(as, "option");
675 1.1 elric tmp != NULL;
676 1.1 elric tmp = find_next(tmp, "option")) {
677 1.1 elric char *s;
678 1.1 elric struct assignment *type = find(tmp->u.assignment, "type");
679 1.1 elric struct type_handler *th;
680 1.1 elric th = find_handler(type);
681 1.1 elric if(th->free == NULL)
682 1.1 elric continue;
683 1.1 elric s = make_name(tmp->u.assignment);
684 1.1 elric (*th->free)(s);
685 1.1 elric free(s);
686 1.1 elric }
687 1.1 elric cprint(1, "return 0;\n");
688 1.1 elric cprint(0, "}\n");
689 1.1 elric cprint(0, "\n");
690 1.1 elric }
691 1.1 elric
692 1.1 elric char cname[PATH_MAX];
693 1.1 elric char hname[PATH_MAX];
694 1.1 elric
695 1.1 elric static void
696 1.1 elric gen(struct assignment *as)
697 1.1 elric {
698 1.1 elric struct assignment *a;
699 1.1 elric cprint(0, "#include <stdio.h>\n");
700 1.1 elric cprint(0, "#include <krb5/getarg.h>\n");
701 1.1 elric cprint(0, "#include <krb5/sl.h>\n");
702 1.1 elric cprint(0, "#include \"%s\"\n\n", hname);
703 1.1 elric
704 1.1 elric hprint(0, "#include <stdio.h>\n");
705 1.1 elric hprint(0, "#include <krb5/sl.h>\n");
706 1.1 elric hprint(0, "\n");
707 1.1 elric
708 1.1 elric
709 1.1 elric for(a = as; a != NULL; a = a->next)
710 1.1 elric gen_wrapper(a->u.assignment);
711 1.1 elric
712 1.1 elric cprint(0, "SL_cmd commands[] = {\n");
713 1.1 elric for(a = as; a != NULL; a = a->next)
714 1.1 elric gen_command(a->u.assignment);
715 1.1 elric cprint(1, "{ NULL }\n");
716 1.1 elric cprint(0, "};\n");
717 1.1 elric
718 1.1 elric hprint(0, "extern SL_cmd commands[];\n");
719 1.1 elric }
720 1.1 elric
721 1.1 elric int version_flag;
722 1.1 elric int help_flag;
723 1.1 elric struct getargs args[] = {
724 1.1 elric { "version", 0, arg_flag, &version_flag },
725 1.1 elric { "help", 0, arg_flag, &help_flag }
726 1.1 elric };
727 1.1 elric int num_args = sizeof(args) / sizeof(args[0]);
728 1.1 elric
729 1.1 elric static void
730 1.1 elric usage(int code)
731 1.1 elric {
732 1.1 elric arg_printusage(args, num_args, NULL, "command-table");
733 1.1 elric exit(code);
734 1.1 elric }
735 1.1 elric
736 1.1 elric int
737 1.1 elric main(int argc, char **argv)
738 1.1 elric {
739 1.1 elric char *p;
740 1.1 elric
741 1.1 elric int optidx = 0;
742 1.1 elric
743 1.1 elric setprogname(argv[0]);
744 1.1 elric if(getarg(args, num_args, argc, argv, &optidx))
745 1.1 elric usage(1);
746 1.1 elric if(help_flag)
747 1.1 elric usage(0);
748 1.1 elric if(version_flag) {
749 1.1 elric print_version(NULL);
750 1.1 elric exit(0);
751 1.1 elric }
752 1.1 elric
753 1.1 elric if(argc == optidx)
754 1.1 elric usage(1);
755 1.1 elric
756 1.1 elric filename = argv[optidx];
757 1.1 elric yyin = fopen(filename, "r");
758 1.1 elric if(yyin == NULL)
759 1.1 elric err(1, "%s", filename);
760 1.1 elric p = strrchr(filename, '/');
761 1.1 elric if(p)
762 1.1 elric strlcpy(cname, p + 1, sizeof(cname));
763 1.1 elric else
764 1.1 elric strlcpy(cname, filename, sizeof(cname));
765 1.1 elric p = strrchr(cname, '.');
766 1.1 elric if(p)
767 1.1 elric *p = '\0';
768 1.1 elric strlcpy(hname, cname, sizeof(hname));
769 1.1 elric strlcat(cname, ".c", sizeof(cname));
770 1.1 elric strlcat(hname, ".h", sizeof(hname));
771 1.1 elric yyparse();
772 1.1 elric if(error_flag)
773 1.1 elric exit(1);
774 1.1 elric if(check(assignment) == 0) {
775 1.1 elric cfile = fopen(cname, "w");
776 1.1 elric if(cfile == NULL)
777 1.1 elric err(1, "%s", cname);
778 1.1 elric hfile = fopen(hname, "w");
779 1.1 elric if(hfile == NULL)
780 1.1 elric err(1, "%s", hname);
781 1.1 elric gen(assignment);
782 1.1 elric fclose(cfile);
783 1.1 elric fclose(hfile);
784 1.1 elric }
785 1.1 elric fclose(yyin);
786 1.1 elric return 0;
787 1.1 elric }
788