3 * Copyright (c) 2004-2006 Kungliga Tekniska Högskolan
4 * (Royal Institute of Technology, Stockholm, Sweden).
7 * Redistribution and use in source and binary forms, with or without
8 * 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.
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
48 extern struct assignment *assignment;
49 extern int yyparse(void);
51 /* Declarations for Bison:
53 #define YYMALLOC malloc
60 struct assignment *assignment;
63 %token <string> LITERAL
64 %token <string> STRING
65 %type <assignment> assignment assignments
77 assignments : assignment assignments
85 assignment : LITERAL '=' STRING
87 $$ = malloc(sizeof(*$$));
94 | LITERAL '=' '{' assignments '}'
96 $$ = malloc(sizeof(*$$));
98 $$->type = a_assignment;
100 $$->u.assignment = $4;
109 struct assignment *assignment;
113 ex(struct assignment *a, const char *fmt, ...)
116 fprintf(stderr, "%s:%d: ", a->name, a->lineno);
118 vfprintf(stderr, fmt, ap);
120 fprintf(stderr, "\n");
126 check_option(struct assignment *as)
128 struct assignment *a;
133 int seen_argument = 0;
135 int seen_default = 0;
138 for(a = as; a != NULL; a = a->next) {
139 if(strcmp(a->name, "long") == 0)
141 else if(strcmp(a->name, "short") == 0)
143 else if(strcmp(a->name, "name") == 0)
145 else if(strcmp(a->name, "type") == 0)
147 else if(strcmp(a->name, "argument") == 0)
149 else if(strcmp(a->name, "help") == 0)
151 else if(strcmp(a->name, "default") == 0)
154 ex(a, "unknown name %s", a->name);
158 if(seen_long == 0 && seen_short == 0) {
159 ex(as, "neither long nor short option");
162 if (seen_long == 0 && seen_name == 0) {
163 ex(as, "either of long or name option must be used");
167 ex(as, "multiple long options");
171 ex(as, "multiple short options");
175 ex(as, "multiple types");
178 if(seen_argument > 1) {
179 ex(as, "multiple arguments");
183 ex(as, "multiple help strings");
186 if(seen_default > 1) {
187 ex(as, "multiple default values");
194 check_command(struct assignment *as)
196 struct assignment *a;
198 int seen_function = 0;
200 int seen_argument = 0;
201 int seen_minargs = 0;
202 int seen_maxargs = 0;
204 for(a = as; a != NULL; a = a->next) {
205 if(strcmp(a->name, "name") == 0)
207 else if(strcmp(a->name, "function") == 0) {
209 } else if(strcmp(a->name, "option") == 0)
210 ret += check_option(a->u.assignment);
211 else if(strcmp(a->name, "help") == 0) {
213 } else if(strcmp(a->name, "argument") == 0) {
215 } else if(strcmp(a->name, "min_args") == 0) {
217 } else if(strcmp(a->name, "max_args") == 0) {
220 ex(a, "unknown name: %s", a->name);
225 ex(as, "no command name");
228 if(seen_function > 1) {
229 ex(as, "multiple function names");
233 ex(as, "multiple help strings");
236 if(seen_argument > 1) {
237 ex(as, "multiple argument strings");
240 if(seen_minargs > 1) {
241 ex(as, "multiple min_args strings");
244 if(seen_maxargs > 1) {
245 ex(as, "multiple max_args strings");
253 check(struct assignment *as)
255 struct assignment *a;
257 for(a = as; a != NULL; a = a->next) {
258 if(strcmp(a->name, "command")) {
259 fprintf(stderr, "unknown type %s line %d\n", a->name, a->lineno);
263 if(a->type != a_assignment) {
264 fprintf(stderr, "bad command definition %s line %d\n", a->name, a->lineno);
268 ret += check_command(a->u.assignment);
273 static struct assignment *
274 find_next(struct assignment *as, const char *name)
276 for(as = as->next; as != NULL; as = as->next) {
277 if(strcmp(as->name, name) == 0)
283 static struct assignment *
284 find(struct assignment *as, const char *name)
286 for(; as != NULL; as = as->next) {
287 if(strcmp(as->name, name) == 0)
294 space(FILE *f, int level)
296 fprintf(f, "%*.*s", level * 4, level * 4, " ");
300 cprint(int level, const char *fmt, ...)
305 vfprintf(cfile, fmt, ap);
310 hprint(int level, const char *fmt, ...)
315 vfprintf(hfile, fmt, ap);
319 static void gen_name(char *str);
322 gen_command(struct assignment *as)
324 struct assignment *a, *b;
326 a = find(as, "name");
327 f = strdup(a->u.value);
330 fprintf(cfile, "\"%s\", ", a->u.value);
331 fprintf(cfile, "%s_wrap, ", f);
332 b = find(as, "argument");
334 fprintf(cfile, "\"%s %s\", ", a->u.value, b->u.value);
336 fprintf(cfile, "\"%s\", ", a->u.value);
337 b = find(as, "help");
339 fprintf(cfile, "\"%s\"", b->u.value);
341 fprintf(cfile, "NULL");
342 fprintf(cfile, " },\n");
343 for(a = a->next; a != NULL; a = a->next)
344 if(strcmp(a->name, "name") == 0)
345 cprint(1, " { \"%s\" },\n", a->u.value);
353 for(p = str; *p != '\0'; p++)
354 if(!isalnum((unsigned char)*p))
359 make_name(struct assignment *as)
361 struct assignment *lopt;
362 struct assignment *type;
365 lopt = find(as, "long");
367 lopt = find(as, "name");
371 type = find(as, "type");
372 if(strcmp(type->u.value, "-flag") == 0)
373 asprintf(&s, "%s_flag", lopt->u.value);
375 asprintf(&s, "%s_%s", lopt->u.value, type->u.value);
381 static void defval_int(const char *name, struct assignment *defval)
384 cprint(1, "opt.%s = %s;\n", name, defval->u.value);
386 cprint(1, "opt.%s = 0;\n", name);
388 static void defval_neg_flag(const char *name, struct assignment *defval)
391 cprint(1, "opt.%s = %s;\n", name, defval->u.value);
393 cprint(1, "opt.%s = 1;\n", name);
395 static void defval_string(const char *name, struct assignment *defval)
398 cprint(1, "opt.%s = (char *)(unsigned long)\"%s\";\n", name, defval->u.value);
400 cprint(1, "opt.%s = NULL;\n", name);
402 static void defval_strings(const char *name, struct assignment *defval)
404 cprint(1, "opt.%s.num_strings = 0;\n", name);
405 cprint(1, "opt.%s.strings = NULL;\n", name);
408 static void free_strings(const char *name)
410 cprint(1, "free_getarg_strings (&opt.%s);\n", name);
413 struct type_handler {
414 const char *typename;
416 const char *getarg_type;
417 void (*defval)(const char*, struct assignment*);
418 void (*free)(const char*);
419 } type_handlers[] = {
433 "struct getarg_strings",
453 static struct type_handler *find_handler(struct assignment *type)
455 struct type_handler *th;
456 for(th = type_handlers; th->typename != NULL; th++)
457 if(strcmp(type->u.value, th->typename) == 0)
459 ex(type, "unknown type \"%s\"", type->u.value);
464 gen_options(struct assignment *opt1, const char *name)
466 struct assignment *tmp;
468 hprint(0, "struct %s_options {\n", name);
472 tmp = find_next(tmp, "option")) {
473 struct assignment *type;
474 struct type_handler *th;
477 s = make_name(tmp->u.assignment);
478 type = find(tmp->u.assignment, "type");
479 th = find_handler(type);
480 hprint(1, "%s %s;\n", th->c_type, s);
487 gen_wrapper(struct assignment *as)
489 struct assignment *name;
490 struct assignment *arg;
491 struct assignment *opt1;
492 struct assignment *function;
493 struct assignment *tmp;
498 name = find(as, "name");
499 n = strdup(name->u.value);
501 arg = find(as, "argument");
504 opt1 = find(as, "option");
505 function = find(as, "function");
507 f = function->u.value;
513 gen_options(opt1, n);
514 hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n);
516 hprint(0, "int %s(void*, int, char **);\n", f);
519 fprintf(cfile, "static int\n");
520 fprintf(cfile, "%s_wrap(int argc, char **argv)\n", n);
521 fprintf(cfile, "{\n");
523 cprint(1, "struct %s_options opt;\n", n);
524 cprint(1, "int ret;\n");
525 cprint(1, "int optidx = 0;\n");
526 cprint(1, "struct getargs args[] = {\n");
527 for(tmp = find(as, "option");
529 tmp = find_next(tmp, "option")) {
530 struct assignment *type = find(tmp->u.assignment, "type");
531 struct assignment *lopt = find(tmp->u.assignment, "long");
532 struct assignment *sopt = find(tmp->u.assignment, "short");
533 struct assignment *aarg = find(tmp->u.assignment, "argument");
534 struct assignment *help = find(tmp->u.assignment, "help");
536 struct type_handler *th;
540 fprintf(cfile, "\"%s\", ", lopt->u.value);
542 fprintf(cfile, "NULL, ");
544 fprintf(cfile, "'%c', ", *sopt->u.value);
546 fprintf(cfile, "0, ");
547 th = find_handler(type);
548 fprintf(cfile, "%s, ", th->getarg_type);
549 fprintf(cfile, "NULL, ");
551 fprintf(cfile, "\"%s\", ", help->u.value);
553 fprintf(cfile, "NULL, ");
555 fprintf(cfile, "\"%s\"", aarg->u.value);
558 fprintf(cfile, "NULL");
559 fprintf(cfile, " },\n");
561 cprint(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
563 cprint(1, "int help_flag = 0;\n");
565 for(tmp = find(as, "option");
567 tmp = find_next(tmp, "option")) {
569 struct assignment *type = find(tmp->u.assignment, "type");
571 struct assignment *defval = find(tmp->u.assignment, "default");
573 struct type_handler *th;
575 s = make_name(tmp->u.assignment);
576 th = find_handler(type);
577 (*th->defval)(s, defval);
581 for(tmp = find(as, "option");
583 tmp = find_next(tmp, "option")) {
585 s = make_name(tmp->u.assignment);
586 cprint(1, "args[%d].value = &opt.%s;\n", nargs++, s);
589 cprint(1, "args[%d].value = &help_flag;\n", nargs++);
590 cprint(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs);
591 cprint(2, "goto usage;\n");
597 if(narguments == 0) {
600 if((tmp = find(as, "min_args")) != NULL) {
601 min_args = strtol(tmp->u.value, &end, 0);
603 ex(tmp, "min_args is not numeric");
607 ex(tmp, "min_args must be non-negative");
611 if((tmp = find(as, "max_args")) != NULL) {
612 max_args = strtol(tmp->u.value, &end, 0);
614 ex(tmp, "max_args is not numeric");
618 ex(tmp, "max_args must be non-negative");
623 if(min_args != -1 || max_args != -1) {
624 if(min_args == max_args) {
625 cprint(1, "if(argc - optidx != %d) {\n",
627 cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args);
628 cprint(2, "goto usage;\n");
632 cprint(1, "if(argc - optidx > %d) {\n", max_args);
633 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args);
634 cprint(2, "goto usage;\n");
638 cprint(1, "if(argc - optidx < %d) {\n", min_args);
639 cprint(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args);
640 cprint(2, "goto usage;\n");
647 cprint(1, "if(help_flag)\n");
648 cprint(2, "goto usage;\n");
650 cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
651 f, opt1 ? "&opt": "NULL");
653 /* free allocated data */
654 for(tmp = find(as, "option");
656 tmp = find_next(tmp, "option")) {
658 struct assignment *type = find(tmp->u.assignment, "type");
659 struct type_handler *th;
660 th = find_handler(type);
663 s = make_name(tmp->u.assignment);
667 cprint(1, "return ret;\n");
669 cprint(0, "usage:\n");
670 cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs,
671 name->u.value, arg ? arg->u.value : "");
672 /* free allocated data */
673 for(tmp = find(as, "option");
675 tmp = find_next(tmp, "option")) {
677 struct assignment *type = find(tmp->u.assignment, "type");
678 struct type_handler *th;
679 th = find_handler(type);
682 s = make_name(tmp->u.assignment);
686 cprint(1, "return 0;\n");
691 char cname[PATH_MAX];
692 char hname[PATH_MAX];
695 gen(struct assignment *as)
697 struct assignment *a;
698 cprint(0, "#include <stdio.h>\n");
699 cprint(0, "#include <getarg.h>\n");
700 cprint(0, "#include <sl.h>\n");
701 cprint(0, "#include \"%s\"\n\n", hname);
703 hprint(0, "#include <stdio.h>\n");
704 hprint(0, "#include <sl.h>\n");
708 for(a = as; a != NULL; a = a->next)
709 gen_wrapper(a->u.assignment);
711 cprint(0, "SL_cmd commands[] = {\n");
712 for(a = as; a != NULL; a = a->next)
713 gen_command(a->u.assignment);
714 cprint(1, "{ NULL }\n");
717 hprint(0, "extern SL_cmd commands[];\n");
722 struct getargs args[] = {
723 { "version", 0, arg_flag, &version_flag },
724 { "help", 0, arg_flag, &help_flag }
726 int num_args = sizeof(args) / sizeof(args[0]);
731 arg_printusage(args, num_args, NULL, "command-table");
736 main(int argc, char **argv)
742 setprogname(argv[0]);
743 if(getarg(args, num_args, argc, argv, &optidx))
755 filename = argv[optidx];
756 yyin = fopen(filename, "r");
758 err(1, "%s", filename);
759 p = strrchr(filename, '/');
761 strlcpy(cname, p + 1, sizeof(cname));
763 strlcpy(cname, filename, sizeof(cname));
764 p = strrchr(cname, '.');
767 strlcpy(hname, cname, sizeof(hname));
768 strlcat(cname, ".c", sizeof(cname));
769 strlcat(hname, ".h", sizeof(hname));
773 if(check(assignment) == 0) {
774 cfile = fopen(cname, "w");
777 hfile = fopen(hname, "w");