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
51 extern
struct assignment
*assignment
;
56 struct assignment
*assignment
;
59 %token
<string> LITERAL
60 %token
<string> STRING
61 %type
<assignment
> assignment assignments
73 assignments
: assignment assignments
81 assignment
: LITERAL
'=' STRING
83 $$
= malloc
(sizeof
(*$$
));
90 | LITERAL
'=' '{' assignments
'}'
92 $$
= malloc
(sizeof
(*$$
));
94 $$
->type
= a_assignment
;
96 $$
->u.assignment
= $4;
105 struct assignment
*assignment
;
109 ex
(struct assignment
*a
, const char *fmt
, ...
)
112 fprintf
(stderr
, "%s:%d: ", a
->name
, a
->lineno
);
114 vfprintf
(stderr
, fmt
, ap
);
116 fprintf
(stderr
, "\n");
122 check_option
(struct assignment
*as
)
124 struct assignment
*a
;
128 int seen_argument
= 0;
130 int seen_default
= 0;
133 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
134 if
(strcmp
(a
->name
, "long") == 0)
136 else if
(strcmp
(a
->name
, "short") == 0)
138 else if
(strcmp
(a
->name
, "type") == 0)
140 else if
(strcmp
(a
->name
, "argument") == 0)
142 else if
(strcmp
(a
->name
, "help") == 0)
144 else if
(strcmp
(a
->name
, "default") == 0)
147 ex
(a
, "unknown name");
151 if
(seen_long
== 0 && seen_short
== 0) {
152 ex
(as
, "neither long nor short option");
156 ex
(as
, "multiple long options");
160 ex
(as
, "multiple short options");
164 ex
(as
, "multiple types");
167 if
(seen_argument
> 1) {
168 ex
(as
, "multiple arguments");
172 ex
(as
, "multiple help strings");
175 if
(seen_default
> 1) {
176 ex
(as
, "multiple default values");
183 check_command
(struct assignment
*as
)
185 struct assignment
*a
;
187 int seen_function
= 0;
189 int seen_argument
= 0;
190 int seen_minargs
= 0;
191 int seen_maxargs
= 0;
193 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
194 if
(strcmp
(a
->name
, "name") == 0)
196 else if
(strcmp
(a
->name
, "function") == 0) {
198 } else if
(strcmp
(a
->name
, "option") == 0)
199 ret
+= check_option
(a
->u.assignment
);
200 else if
(strcmp
(a
->name
, "help") == 0) {
202 } else if
(strcmp
(a
->name
, "argument") == 0) {
204 } else if
(strcmp
(a
->name
, "min_args") == 0) {
206 } else if
(strcmp
(a
->name
, "max_args") == 0) {
209 ex
(a
, "unknown name");
214 ex
(as
, "no command name");
217 if
(seen_function
> 1) {
218 ex
(as
, "multiple function names");
222 ex
(as
, "multiple help strings");
225 if
(seen_argument
> 1) {
226 ex
(as
, "multiple argument strings");
229 if
(seen_minargs
> 1) {
230 ex
(as
, "multiple min_args strings");
233 if
(seen_maxargs
> 1) {
234 ex
(as
, "multiple max_args strings");
242 check
(struct assignment
*as
)
244 struct assignment
*a
;
246 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
247 if
(strcmp
(a
->name
, "command")) {
248 fprintf
(stderr
, "unknown type %s line %d\n", a
->name
, a
->lineno
);
252 if
(a
->type
!= a_assignment
) {
253 fprintf
(stderr
, "bad command definition %s line %d\n", a
->name
, a
->lineno
);
257 ret
+= check_command
(a
->u.assignment
);
262 static struct assignment
*
263 find_next
(struct assignment
*as
, const char *name
)
265 for
(as
= as
->next
; as
!= NULL
; as
= as
->next
) {
266 if
(strcmp
(as
->name
, name
) == 0)
272 static struct assignment
*
273 find
(struct assignment
*as
, const char *name
)
275 for
(; as
!= NULL
; as
= as
->next
) {
276 if
(strcmp
(as
->name
, name
) == 0)
283 space
(FILE *f
, int level
)
285 fprintf
(f
, "%*.*s", level
* 4, level
* 4, " ");
289 cprint
(int level
, const char *fmt
, ...
)
294 vfprintf
(cfile
, fmt
, ap
);
299 hprint
(int level
, const char *fmt
, ...
)
304 vfprintf
(hfile
, fmt
, ap
);
308 static void gen_name
(char *str
);
311 gen_command
(struct assignment
*as
)
313 struct assignment
*a
, *b
;
315 a
= find
(as
, "name");
316 f
= strdup
(a
->u.value
);
319 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
320 fprintf
(cfile
, "%s_wrap, ", f
);
321 b
= find
(as
, "argument");
323 fprintf
(cfile
, "\"%s %s\", ", a
->u.value
, b
->u.value
);
325 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
326 b
= find
(as
, "help");
328 fprintf
(cfile
, "\"%s\"", b
->u.value
);
330 fprintf
(cfile
, "NULL");
331 fprintf
(cfile
, " },\n");
332 for
(a
= a
->next
; a
!= NULL
; a
= a
->next
)
333 if
(strcmp
(a
->name
, "name") == 0)
334 cprint
(1, " { \"%s\" },\n", a
->u.value
);
342 for
(p
= str
; *p
!= '\0'; p
++)
343 if
(!isalnum
((unsigned char)*p
))
348 make_name
(struct assignment
*as
)
350 struct assignment
*lopt
;
351 struct assignment
*type
;
354 lopt
= find
(as
, "long");
356 lopt
= find
(as
, "name");
360 type
= find
(as
, "type");
361 if
(strcmp
(type
->u.value
, "-flag") == 0)
362 asprintf
(&s
, "%s_flag", lopt
->u.value
);
364 asprintf
(&s
, "%s_%s", lopt
->u.value
, type
->u.value
);
370 static void defval_int
(const char *name
, struct assignment
*defval
)
373 cprint
(1, "opt.%s = %s;\n", name
, defval
->u.value
);
375 cprint
(1, "opt.%s = 0;\n", name
);
377 static void defval_string
(const char *name
, struct assignment
*defval
)
380 cprint
(1, "opt.%s = \"%s\";\n", name
, defval
->u.value
);
382 cprint
(1, "opt.%s = NULL;\n", name
);
384 static void defval_strings
(const char *name
, struct assignment
*defval
)
386 cprint
(1, "opt.%s.num_strings = 0;\n", name
);
387 cprint
(1, "opt.%s.strings = NULL;\n", name
);
390 static void free_strings
(const char *name
)
392 cprint
(1, "free_getarg_strings (&opt.%s);\n", name
);
395 struct type_handler
{
396 const char *typename
;
398 const char *getarg_type
;
399 void (*defval
)(const char*, struct assignment
*);
400 void (*free
)(const char*);
401 } type_handlers
[] = {
415 "struct getarg_strings",
435 static struct type_handler
*find_handler
(struct assignment
*type
)
437 struct type_handler
*th
;
438 for
(th
= type_handlers
; th
->typename
!= NULL
; th
++)
439 if
(strcmp
(type
->u.value
, th
->typename
) == 0)
441 ex
(type
, "unknown type \"%s\"", type
->u.value
);
446 gen_options
(struct assignment
*opt1
, const char *name
)
448 struct assignment
*tmp
;
450 hprint
(0, "struct %s_options {\n", name
);
454 tmp
= find_next
(tmp
, "option")) {
455 struct assignment
*type
;
456 struct type_handler
*th
;
459 s
= make_name
(tmp
->u.assignment
);
460 type
= find
(tmp
->u.assignment
, "type");
461 th
= find_handler
(type
);
462 hprint
(1, "%s %s;\n", th
->c_type
, s
);
469 gen_wrapper
(struct assignment
*as
)
471 struct assignment
*name
;
472 struct assignment
*arg
;
473 struct assignment
*opt1
;
474 struct assignment
*function
;
475 struct assignment
*tmp
;
479 name
= find
(as
, "name");
480 n
= strdup
(name
->u.value
);
482 arg
= find
(as
, "argument");
483 opt1
= find
(as
, "option");
484 function
= find
(as
, "function");
486 f
= function
->u.value
;
492 gen_options
(opt1
, n
);
493 hprint
(0, "int %s(struct %s_options*, int, char **);\n", f
, n
);
495 hprint
(0, "int %s(void*, int, char **);\n", f
);
498 fprintf
(cfile
, "static int\n");
499 fprintf
(cfile
, "%s_wrap(int argc, char **argv)\n", n
);
500 fprintf
(cfile
, "{\n");
502 cprint
(1, "struct %s_options opt;\n", n
);
503 cprint
(1, "int ret;\n");
504 cprint
(1, "int optidx = 0;\n");
505 cprint
(1, "struct getargs args[] = {\n");
506 for
(tmp
= find
(as
, "option");
508 tmp
= find_next
(tmp
, "option")) {
509 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
510 struct assignment
*lopt
= find
(tmp
->u.assignment
, "long");
511 struct assignment
*sopt
= find
(tmp
->u.assignment
, "short");
512 struct assignment
*aarg
= find
(tmp
->u.assignment
, "argument");
513 struct assignment
*help
= find
(tmp
->u.assignment
, "help");
515 struct type_handler
*th
;
519 fprintf
(cfile
, "\"%s\", ", lopt
->u.value
);
521 fprintf
(cfile
, "NULL, ");
523 fprintf
(cfile
, "'%c', ", *sopt
->u.value
);
525 fprintf
(cfile
, "0, ");
526 th
= find_handler
(type
);
527 fprintf
(cfile
, "%s, ", th
->getarg_type
);
528 fprintf
(cfile
, "NULL, ");
530 fprintf
(cfile
, "\"%s\", ", help
->u.value
);
532 fprintf
(cfile
, "NULL, ");
534 fprintf
(cfile
, "\"%s\"", aarg
->u.value
);
536 fprintf
(cfile
, "NULL");
537 fprintf
(cfile
, " },\n");
539 cprint
(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
541 cprint
(1, "int help_flag = 0;\n");
543 for
(tmp
= find
(as
, "option");
545 tmp
= find_next
(tmp
, "option")) {
547 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
549 struct assignment
*defval
= find
(tmp
->u.assignment
, "default");
551 struct type_handler
*th
;
553 s
= make_name
(tmp
->u.assignment
);
554 th
= find_handler
(type
);
555 (*th
->defval
)(s
, defval
);
559 for
(tmp
= find
(as
, "option");
561 tmp
= find_next
(tmp
, "option")) {
563 s
= make_name
(tmp
->u.assignment
);
564 cprint
(1, "args[%d].value = &opt.%s;\n", nargs
++, s
);
567 cprint
(1, "args[%d].value = &help_flag;\n", nargs
++);
568 cprint
(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs
);
569 cprint
(2, "goto usage;\n");
578 if
((tmp
= find
(as
, "min_args")) != NULL
) {
579 min_args
= strtol
(tmp
->u.value
, &end
, 0);
581 ex
(tmp
, "min_args is not numeric");
585 ex
(tmp
, "min_args must be non-negative");
589 if
((tmp
= find
(as
, "max_args")) != NULL
) {
590 max_args
= strtol
(tmp
->u.value
, &end
, 0);
592 ex
(tmp
, "max_args is not numeric");
596 ex
(tmp
, "max_args must be non-negative");
601 if
(min_args
!= -1 || max_args
!= -1) {
602 if
(min_args
== max_args
) {
603 cprint
(1, "if(argc - optidx != %d) {\n",
605 cprint
(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args
);
606 cprint
(2, "goto usage;\n");
610 cprint
(1, "if(argc - optidx > %d) {\n", max_args
);
611 cprint
(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args
);
612 cprint
(2, "goto usage;\n");
616 cprint
(1, "if(argc - optidx < %d) {\n", min_args
);
617 cprint
(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args
);
618 cprint
(2, "goto usage;\n");
625 cprint
(1, "if(help_flag)\n");
626 cprint
(2, "goto usage;\n");
628 cprint
(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
629 f
, opt1 ?
"&opt": "NULL");
631 /* free allocated data */
632 for
(tmp
= find
(as
, "option");
634 tmp
= find_next
(tmp
, "option")) {
636 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
637 struct type_handler
*th
;
638 th
= find_handler
(type
);
641 s
= make_name
(tmp
->u.assignment
);
645 cprint
(1, "return ret;\n");
647 cprint
(0, "usage:\n");
648 cprint
(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs
,
649 name
->u.value
, arg ? arg
->u.value
: "");
650 /* free allocated data */
651 for
(tmp
= find
(as
, "option");
653 tmp
= find_next
(tmp
, "option")) {
655 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
656 struct type_handler
*th
;
657 th
= find_handler
(type
);
660 s
= make_name
(tmp
->u.assignment
);
664 cprint
(1, "return 0;\n");
669 char cname
[PATH_MAX
];
670 char hname
[PATH_MAX
];
673 gen
(struct assignment
*as
)
675 struct assignment
*a
;
676 cprint
(0, "#include <stdio.h>\n");
677 cprint
(0, "#include <getarg.h>\n");
678 cprint
(0, "#include <sl.h>\n");
679 cprint
(0, "#include \"%s\"\n\n", hname
);
681 hprint
(0, "#include <stdio.h>\n");
682 hprint
(0, "#include <sl.h>\n");
686 for
(a
= as
; a
!= NULL
; a
= a
->next
)
687 gen_wrapper
(a
->u.assignment
);
689 cprint
(0, "SL_cmd commands[] = {\n");
690 for
(a
= as
; a
!= NULL
; a
= a
->next
)
691 gen_command
(a
->u.assignment
);
692 cprint
(1, "{ NULL }\n");
695 hprint
(0, "extern SL_cmd commands[];\n");
700 struct getargs args
[] = {
701 { "version", 0, arg_flag
, &version_flag
},
702 { "help", 0, arg_flag
, &help_flag
}
704 int num_args
= sizeof
(args
) / sizeof
(args
[0]);
709 arg_printusage
(args
, num_args
, NULL
, "command-table");
714 main
(int argc
, char **argv
)
720 setprogname
(argv
[0]);
721 if
(getarg
(args
, num_args
, argc
, argv
, &optidx
))
733 filename
= argv
[optidx
];
734 yyin
= fopen
(filename
, "r");
736 err
(1, "%s", filename
);
737 p
= strrchr
(filename
, '/');
739 strlcpy
(cname
, p
+ 1, sizeof
(cname
));
741 strlcpy
(cname
, filename
, sizeof
(cname
));
742 p
= strrchr
(cname
, '.');
745 strlcpy
(hname
, cname
, sizeof
(hname
));
746 strlcat
(cname
, ".c", sizeof
(cname
));
747 strlcat
(hname
, ".h", sizeof
(hname
));
751 if
(check
(assignment
) == 0) {
752 cfile
= fopen
(cname
, "w");
755 hfile
= fopen
(hname
, "w");