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