No empty .Rs/.Re
[netbsd-mini2440.git] / crypto / dist / heimdal / lib / sl / slc-gram.y
blobb9441e6bd80d94bbdcc46a006d8980cf4e819a6e
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 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 __RCSID("$Heimdal: slc-gram.y 20767 2007-06-01 11:24:52Z lha $"
38 "$NetBSD$");
39 #endif
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <err.h>
44 #include <ctype.h>
45 #include <limits.h>
46 #include <getarg.h>
47 #include <vers.h>
48 #include <roken.h>
50 #include "slc.h"
51 extern FILE *yyin;
52 extern struct assignment *assignment;
55 %union {
56 char *string;
57 struct assignment *assignment;
60 %token <string> LITERAL
61 %token <string> STRING
62 %type <assignment> assignment assignments
64 %start start
68 start : assignments
70 assignment = $1;
74 assignments : assignment assignments
76 $1->next = $2;
77 $$ = $1;
79 | assignment
82 assignment : LITERAL '=' STRING
84 $$ = malloc(sizeof(*$$));
85 $$->name = $1;
86 $$->type = a_value;
87 $$->lineno = lineno;
88 $$->u.value = $3;
89 $$->next = NULL;
91 | LITERAL '=' '{' assignments '}'
93 $$ = malloc(sizeof(*$$));
94 $$->name = $1;
95 $$->type = a_assignment;
96 $$->lineno = lineno;
97 $$->u.assignment = $4;
98 $$->next = NULL;
103 char *filename;
104 FILE *cfile, *hfile;
105 int error_flag;
106 struct assignment *assignment;
109 static void
110 ex(struct assignment *a, const char *fmt, ...)
112 va_list ap;
113 fprintf(stderr, "%s:%d: ", a->name, a->lineno);
114 va_start(ap, fmt);
115 vfprintf(stderr, fmt, ap);
116 va_end(ap);
117 fprintf(stderr, "\n");
122 static int
123 check_option(struct assignment *as)
125 struct assignment *a;
126 int seen_long = 0;
127 int seen_short = 0;
128 int seen_type = 0;
129 int seen_argument = 0;
130 int seen_help = 0;
131 int seen_default = 0;
132 int ret = 0;
134 for(a = as; a != NULL; a = a->next) {
135 if(strcmp(a->name, "long") == 0)
136 seen_long++;
137 else if(strcmp(a->name, "short") == 0)
138 seen_short++;
139 else if(strcmp(a->name, "type") == 0)
140 seen_type++;
141 else if(strcmp(a->name, "argument") == 0)
142 seen_argument++;
143 else if(strcmp(a->name, "help") == 0)
144 seen_help++;
145 else if(strcmp(a->name, "default") == 0)
146 seen_default++;
147 else {
148 ex(a, "unknown name");
149 ret++;
152 if(seen_long == 0 && seen_short == 0) {
153 ex(as, "neither long nor short option");
154 ret++;
156 if(seen_long > 1) {
157 ex(as, "multiple long options");
158 ret++;
160 if(seen_short > 1) {
161 ex(as, "multiple short options");
162 ret++;
164 if(seen_type > 1) {
165 ex(as, "multiple types");
166 ret++;
168 if(seen_argument > 1) {
169 ex(as, "multiple arguments");
170 ret++;
172 if(seen_help > 1) {
173 ex(as, "multiple help strings");
174 ret++;
176 if(seen_default > 1) {
177 ex(as, "multiple default values");
178 ret++;
180 return ret;
183 static int
184 check_command(struct assignment *as)
186 struct assignment *a;
187 int seen_name = 0;
188 int seen_function = 0;
189 int seen_help = 0;
190 int seen_argument = 0;
191 int seen_minargs = 0;
192 int seen_maxargs = 0;
193 int ret = 0;
194 for(a = as; a != NULL; a = a->next) {
195 if(strcmp(a->name, "name") == 0)
196 seen_name++;
197 else if(strcmp(a->name, "function") == 0) {
198 seen_function++;
199 } else if(strcmp(a->name, "option") == 0)
200 ret += check_option(a->u.assignment);
201 else if(strcmp(a->name, "help") == 0) {
202 seen_help++;
203 } else if(strcmp(a->name, "argument") == 0) {
204 seen_argument++;
205 } else if(strcmp(a->name, "min_args") == 0) {
206 seen_minargs++;
207 } else if(strcmp(a->name, "max_args") == 0) {
208 seen_maxargs++;
209 } else {
210 ex(a, "unknown name");
211 ret++;
214 if(seen_name == 0) {
215 ex(as, "no command name");
216 ret++;
218 if(seen_function > 1) {
219 ex(as, "multiple function names");
220 ret++;
222 if(seen_help > 1) {
223 ex(as, "multiple help strings");
224 ret++;
226 if(seen_argument > 1) {
227 ex(as, "multiple argument strings");
228 ret++;
230 if(seen_minargs > 1) {
231 ex(as, "multiple min_args strings");
232 ret++;
234 if(seen_maxargs > 1) {
235 ex(as, "multiple max_args strings");
236 ret++;
239 return ret;
242 static int
243 check(struct assignment *as)
245 struct assignment *a;
246 int ret = 0;
247 for(a = as; a != NULL; a = a->next) {
248 if(strcmp(a->name, "command")) {
249 fprintf(stderr, "unknown type %s line %d\n", a->name, a->lineno);
250 ret++;
251 continue;
253 if(a->type != a_assignment) {
254 fprintf(stderr, "bad command definition %s line %d\n", a->name, a->lineno);
255 ret++;
256 continue;
258 ret += check_command(a->u.assignment);
260 return ret;
263 static struct assignment *
264 find_next(struct assignment *as, const char *name)
266 for(as = as->next; as != NULL; as = as->next) {
267 if(strcmp(as->name, name) == 0)
268 return as;
270 return NULL;
273 static struct assignment *
274 find(struct assignment *as, const char *name)
276 for(; as != NULL; as = as->next) {
277 if(strcmp(as->name, name) == 0)
278 return as;
280 return NULL;
283 static void
284 space(FILE *f, int level)
286 fprintf(f, "%*.*s", level * 4, level * 4, " ");
289 static void
290 cprint(int level, const char *fmt, ...)
292 va_list ap;
293 va_start(ap, fmt);
294 space(cfile, level);
295 vfprintf(cfile, fmt, ap);
296 va_end(ap);
299 static void
300 hprint(int level, const char *fmt, ...)
302 va_list ap;
303 va_start(ap, fmt);
304 space(hfile, level);
305 vfprintf(hfile, fmt, ap);
306 va_end(ap);
309 static void gen_name(char *str);
311 static void
312 gen_command(struct assignment *as)
314 struct assignment *a, *b;
315 char *f;
316 a = find(as, "name");
317 f = strdup(a->u.value);
318 gen_name(f);
319 cprint(1, " { ");
320 fprintf(cfile, "\"%s\", ", a->u.value);
321 fprintf(cfile, "%s_wrap, ", f);
322 b = find(as, "argument");
323 if(b)
324 fprintf(cfile, "\"%s %s\", ", a->u.value, b->u.value);
325 else
326 fprintf(cfile, "\"%s\", ", a->u.value);
327 b = find(as, "help");
328 if(b)
329 fprintf(cfile, "\"%s\"", b->u.value);
330 else
331 fprintf(cfile, "NULL");
332 fprintf(cfile, " },\n");
333 for(a = a->next; a != NULL; a = a->next)
334 if(strcmp(a->name, "name") == 0)
335 cprint(1, " { \"%s\" },\n", a->u.value);
336 cprint(0, "\n");
339 static void
340 gen_name(char *str)
342 char *p;
343 for(p = str; *p != '\0'; p++)
344 if(!isalnum((unsigned char)*p))
345 *p = '_';
348 static char *
349 make_name(struct assignment *as)
351 struct assignment *lopt;
352 struct assignment *type;
353 char *s;
355 lopt = find(as, "long");
356 if(lopt == NULL)
357 lopt = find(as, "name");
358 if(lopt == NULL)
359 return NULL;
361 type = find(as, "type");
362 if(strcmp(type->u.value, "-flag") == 0)
363 asprintf(&s, "%s_flag", lopt->u.value);
364 else
365 asprintf(&s, "%s_%s", lopt->u.value, type->u.value);
366 gen_name(s);
367 return s;
371 static void defval_int(const char *name, struct assignment *defval)
373 if(defval != NULL)
374 cprint(1, "opt.%s = %s;\n", name, defval->u.value);
375 else
376 cprint(1, "opt.%s = 0;\n", name);
378 static void defval_string(const char *name, struct assignment *defval)
380 if(defval != NULL)
381 cprint(1, "opt.%s = \"%s\";\n", name, defval->u.value);
382 else
383 cprint(1, "opt.%s = NULL;\n", name);
385 static void defval_strings(const char *name, struct assignment *defval)
387 cprint(1, "opt.%s.num_strings = 0;\n", name);
388 cprint(1, "opt.%s.strings = NULL;\n", name);
391 static void free_strings(const char *name)
393 cprint(1, "free_getarg_strings (&opt.%s);\n", name);
396 struct type_handler {
397 const char *typename;
398 const char *c_type;
399 const char *getarg_type;
400 void (*defval)(const char*, struct assignment*);
401 void (*free)(const char*);
402 } type_handlers[] = {
403 { "integer",
404 "int",
405 "arg_integer",
406 defval_int,
407 NULL
409 { "string",
410 "char*",
411 "arg_string",
412 defval_string,
413 NULL
415 { "strings",
416 "struct getarg_strings",
417 "arg_strings",
418 defval_strings,
419 free_strings
421 { "flag",
422 "int",
423 "arg_flag",
424 defval_int,
425 NULL
427 { "-flag",
428 "int",
429 "arg_negative_flag",
430 defval_int,
431 NULL
433 { NULL }
436 static struct type_handler *find_handler(struct assignment *type)
438 struct type_handler *th;
439 for(th = type_handlers; th->typename != NULL; th++)
440 if(strcmp(type->u.value, th->typename) == 0)
441 return th;
442 ex(type, "unknown type \"%s\"", type->u.value);
443 exit(1);
446 static void
447 gen_options(struct assignment *opt1, const char *name)
449 struct assignment *tmp;
451 hprint(0, "struct %s_options {\n", name);
453 for(tmp = opt1;
454 tmp != NULL;
455 tmp = find_next(tmp, "option")) {
456 struct assignment *type;
457 struct type_handler *th;
458 char *s;
460 s = make_name(tmp->u.assignment);
461 type = find(tmp->u.assignment, "type");
462 th = find_handler(type);
463 hprint(1, "%s %s;\n", th->c_type, s);
464 free(s);
466 hprint(0, "};\n");
469 static void
470 gen_wrapper(struct assignment *as)
472 struct assignment *name;
473 struct assignment *arg;
474 struct assignment *opt1;
475 struct assignment *function;
476 struct assignment *tmp;
477 char *n, *f;
478 int nargs = 0;
480 name = find(as, "name");
481 n = strdup(name->u.value);
482 gen_name(n);
483 arg = find(as, "argument");
484 opt1 = find(as, "option");
485 function = find(as, "function");
486 if(function)
487 f = function->u.value;
488 else
489 f = n;
492 if(opt1 != NULL) {
493 gen_options(opt1, n);
494 hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n);
495 } else {
496 hprint(0, "int %s(void*, int, char **);\n", f);
499 fprintf(cfile, "static int\n");
500 fprintf(cfile, "%s_wrap(int argc, char **argv)\n", n);
501 fprintf(cfile, "{\n");
502 if(opt1 != NULL)
503 cprint(1, "struct %s_options opt;\n", n);
504 cprint(1, "int ret;\n");
505 cprint(1, "int optidx = 0;\n");
506 cprint(1, "struct getargs args[] = {\n");
507 for(tmp = find(as, "option");
508 tmp != NULL;
509 tmp = find_next(tmp, "option")) {
510 struct assignment *type = find(tmp->u.assignment, "type");
511 struct assignment *lopt = find(tmp->u.assignment, "long");
512 struct assignment *sopt = find(tmp->u.assignment, "short");
513 struct assignment *aarg = find(tmp->u.assignment, "argument");
514 struct assignment *help = find(tmp->u.assignment, "help");
516 struct type_handler *th;
518 cprint(2, "{ ");
519 if(lopt)
520 fprintf(cfile, "\"%s\", ", lopt->u.value);
521 else
522 fprintf(cfile, "NULL, ");
523 if(sopt)
524 fprintf(cfile, "'%c', ", *sopt->u.value);
525 else
526 fprintf(cfile, "0, ");
527 th = find_handler(type);
528 fprintf(cfile, "%s, ", th->getarg_type);
529 fprintf(cfile, "NULL, ");
530 if(help)
531 fprintf(cfile, "\"%s\", ", help->u.value);
532 else
533 fprintf(cfile, "NULL, ");
534 if(aarg)
535 fprintf(cfile, "\"%s\"", aarg->u.value);
536 else
537 fprintf(cfile, "NULL");
538 fprintf(cfile, " },\n");
540 cprint(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
541 cprint(1, "};\n");
542 cprint(1, "int help_flag = 0;\n");
544 for(tmp = find(as, "option");
545 tmp != NULL;
546 tmp = find_next(tmp, "option")) {
547 char *s;
548 struct assignment *type = find(tmp->u.assignment, "type");
550 struct assignment *defval = find(tmp->u.assignment, "default");
552 struct type_handler *th;
554 s = make_name(tmp->u.assignment);
555 th = find_handler(type);
556 (*th->defval)(s, defval);
557 free(s);
560 for(tmp = find(as, "option");
561 tmp != NULL;
562 tmp = find_next(tmp, "option")) {
563 char *s;
564 s = make_name(tmp->u.assignment);
565 cprint(1, "args[%d].value = &opt.%s;\n", nargs++, s);
566 free(s);
568 cprint(1, "args[%d].value = &help_flag;\n", nargs++);
569 cprint(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs);
570 cprint(2, "goto usage;\n");
573 int min_args = -1;
574 int max_args = -1;
575 char *end;
576 if(arg == NULL) {
577 max_args = 0;
578 } else {
579 if((tmp = find(as, "min_args")) != NULL) {
580 min_args = strtol(tmp->u.value, &end, 0);
581 if(*end != '\0') {
582 ex(tmp, "min_args is not numeric");
583 exit(1);
585 if(min_args < 0) {
586 ex(tmp, "min_args must be non-negative");
587 exit(1);
590 if((tmp = find(as, "max_args")) != NULL) {
591 max_args = strtol(tmp->u.value, &end, 0);
592 if(*end != '\0') {
593 ex(tmp, "max_args is not numeric");
594 exit(1);
596 if(max_args < 0) {
597 ex(tmp, "max_args must be non-negative");
598 exit(1);
602 if(min_args != -1 || max_args != -1) {
603 if(min_args == max_args) {
604 cprint(1, "if(argc - optidx != %d) {\n",
605 min_args);
606 cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args);
607 cprint(2, "goto usage;\n");
608 cprint(1, "}\n");
609 } else {
610 if(max_args != -1) {
611 cprint(1, "if(argc - optidx > %d) {\n", max_args);
612 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args);
613 cprint(2, "goto usage;\n");
614 cprint(1, "}\n");
616 if(min_args != -1) {
617 cprint(1, "if(argc - optidx < %d) {\n", min_args);
618 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args);
619 cprint(2, "goto usage;\n");
620 cprint(1, "}\n");
626 cprint(1, "if(help_flag)\n");
627 cprint(2, "goto usage;\n");
629 cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
630 f, opt1 ? "&opt": "NULL");
632 /* free allocated data */
633 for(tmp = find(as, "option");
634 tmp != NULL;
635 tmp = find_next(tmp, "option")) {
636 char *s;
637 struct assignment *type = find(tmp->u.assignment, "type");
638 struct type_handler *th;
639 th = find_handler(type);
640 if(th->free == NULL)
641 continue;
642 s = make_name(tmp->u.assignment);
643 (*th->free)(s);
644 free(s);
646 cprint(1, "return ret;\n");
648 cprint(0, "usage:\n");
649 cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs,
650 name->u.value, arg ? arg->u.value : "");
651 /* free allocated data */
652 for(tmp = find(as, "option");
653 tmp != NULL;
654 tmp = find_next(tmp, "option")) {
655 char *s;
656 struct assignment *type = find(tmp->u.assignment, "type");
657 struct type_handler *th;
658 th = find_handler(type);
659 if(th->free == NULL)
660 continue;
661 s = make_name(tmp->u.assignment);
662 (*th->free)(s);
663 free(s);
665 cprint(1, "return 0;\n");
666 cprint(0, "}\n");
667 cprint(0, "\n");
670 char cname[PATH_MAX];
671 char hname[PATH_MAX];
673 static void
674 gen(struct assignment *as)
676 struct assignment *a;
677 cprint(0, "#include <stdio.h>\n");
678 cprint(0, "#include <getarg.h>\n");
679 cprint(0, "#include <sl.h>\n");
680 cprint(0, "#include \"%s\"\n\n", hname);
682 hprint(0, "#include <stdio.h>\n");
683 hprint(0, "#include <sl.h>\n");
684 hprint(0, "\n");
687 for(a = as; a != NULL; a = a->next)
688 gen_wrapper(a->u.assignment);
690 cprint(0, "SL_cmd commands[] = {\n");
691 for(a = as; a != NULL; a = a->next)
692 gen_command(a->u.assignment);
693 cprint(1, "{ NULL }\n");
694 cprint(0, "};\n");
696 hprint(0, "extern SL_cmd commands[];\n");
699 int version_flag;
700 int help_flag;
701 struct getargs args[] = {
702 { "version", 0, arg_flag, &version_flag },
703 { "help", 0, arg_flag, &help_flag }
705 int num_args = sizeof(args) / sizeof(args[0]);
707 static void
708 usage(int code)
710 arg_printusage(args, num_args, NULL, "command-table");
711 exit(code);
715 main(int argc, char **argv)
717 char *p;
719 int optidx = 0;
721 setprogname(argv[0]);
722 if(getarg(args, num_args, argc, argv, &optidx))
723 usage(1);
724 if(help_flag)
725 usage(0);
726 if(version_flag) {
727 print_version(NULL);
728 exit(0);
731 if(argc == optidx)
732 usage(1);
734 filename = argv[optidx];
735 yyin = fopen(filename, "r");
736 if(yyin == NULL)
737 err(1, "%s", filename);
738 p = strrchr(filename, '/');
739 if(p)
740 strlcpy(cname, p + 1, sizeof(cname));
741 else
742 strlcpy(cname, filename, sizeof(cname));
743 p = strrchr(cname, '.');
744 if(p)
745 *p = '\0';
746 strlcpy(hname, cname, sizeof(hname));
747 strlcat(cname, ".c", sizeof(cname));
748 strlcat(hname, ".h", sizeof(hname));
749 yyparse();
750 if(error_flag)
751 exit(1);
752 if(check(assignment) == 0) {
753 cfile = fopen(cname, "w");
754 if(cfile == NULL)
755 err(1, "%s", cname);
756 hfile = fopen(hname, "w");
757 if(hfile == NULL)
758 err(1, "%s", hname);
759 gen(assignment);
760 fclose(cfile);
761 fclose(hfile);
763 fclose(yyin);
764 return 0;