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
;
50 /* Declarations for Bison:
52 #define YYMALLOC malloc
59 struct assignment
*assignment
;
62 %token
<string> LITERAL
63 %token
<string> STRING
64 %type
<assignment
> assignment assignments
76 assignments
: assignment assignments
84 assignment
: LITERAL
'=' STRING
86 $$
= malloc
(sizeof
(*$$
));
93 | LITERAL
'=' '{' assignments
'}'
95 $$
= malloc
(sizeof
(*$$
));
97 $$
->type
= a_assignment
;
99 $$
->u.assignment
= $4;
108 struct assignment
*assignment
;
112 ex
(struct assignment
*a
, const char *fmt
, ...
)
115 fprintf
(stderr
, "%s:%d: ", a
->name
, a
->lineno
);
117 vfprintf
(stderr
, fmt
, ap
);
119 fprintf
(stderr
, "\n");
125 check_option
(struct assignment
*as
)
127 struct assignment
*a
;
132 int seen_argument
= 0;
134 int seen_default
= 0;
137 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
138 if
(strcmp
(a
->name
, "long") == 0)
140 else if
(strcmp
(a
->name
, "short") == 0)
142 else if
(strcmp
(a
->name
, "name") == 0)
144 else if
(strcmp
(a
->name
, "type") == 0)
146 else if
(strcmp
(a
->name
, "argument") == 0)
148 else if
(strcmp
(a
->name
, "help") == 0)
150 else if
(strcmp
(a
->name
, "default") == 0)
153 ex
(a
, "unknown name %s", a
->name
);
157 if
(seen_long
== 0 && seen_short
== 0) {
158 ex
(as
, "neither long nor short option");
161 if
(seen_long
== 0 && seen_name
== 0) {
162 ex
(as
, "either of long or name option must be used");
166 ex
(as
, "multiple long options");
170 ex
(as
, "multiple short options");
174 ex
(as
, "multiple types");
177 if
(seen_argument
> 1) {
178 ex
(as
, "multiple arguments");
182 ex
(as
, "multiple help strings");
185 if
(seen_default
> 1) {
186 ex
(as
, "multiple default values");
193 check_command
(struct assignment
*as
)
195 struct assignment
*a
;
197 int seen_function
= 0;
199 int seen_argument
= 0;
200 int seen_minargs
= 0;
201 int seen_maxargs
= 0;
203 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
204 if
(strcmp
(a
->name
, "name") == 0)
206 else if
(strcmp
(a
->name
, "function") == 0) {
208 } else if
(strcmp
(a
->name
, "option") == 0)
209 ret
+= check_option
(a
->u.assignment
);
210 else if
(strcmp
(a
->name
, "help") == 0) {
212 } else if
(strcmp
(a
->name
, "argument") == 0) {
214 } else if
(strcmp
(a
->name
, "min_args") == 0) {
216 } else if
(strcmp
(a
->name
, "max_args") == 0) {
219 ex
(a
, "unknown name: %s", a
->name
);
224 ex
(as
, "no command name");
227 if
(seen_function
> 1) {
228 ex
(as
, "multiple function names");
232 ex
(as
, "multiple help strings");
235 if
(seen_argument
> 1) {
236 ex
(as
, "multiple argument strings");
239 if
(seen_minargs
> 1) {
240 ex
(as
, "multiple min_args strings");
243 if
(seen_maxargs
> 1) {
244 ex
(as
, "multiple max_args strings");
252 check
(struct assignment
*as
)
254 struct assignment
*a
;
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
);
262 if
(a
->type
!= a_assignment
) {
263 fprintf
(stderr
, "bad command definition %s line %d\n", a
->name
, a
->lineno
);
267 ret
+= check_command
(a
->u.assignment
);
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)
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)
293 space
(FILE *f
, int level
)
295 fprintf
(f
, "%*.*s", level
* 4, level
* 4, " ");
299 cprint
(int level
, const char *fmt
, ...
)
304 vfprintf
(cfile
, fmt
, ap
);
309 hprint
(int level
, const char *fmt
, ...
)
314 vfprintf
(hfile
, fmt
, ap
);
318 static void gen_name
(char *str
);
321 gen_command
(struct assignment
*as
)
323 struct assignment
*a
, *b
;
325 a
= find
(as
, "name");
326 f
= strdup
(a
->u.value
);
329 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
330 fprintf
(cfile
, "%s_wrap, ", f
);
331 b
= find
(as
, "argument");
333 fprintf
(cfile
, "\"%s %s\", ", a
->u.value
, b
->u.value
);
335 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
336 b
= find
(as
, "help");
338 fprintf
(cfile
, "\"%s\"", b
->u.value
);
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\", NULL, NULL, NULL },\n", a
->u.value
);
352 for
(p
= str
; *p
!= '\0'; p
++)
353 if
(!isalnum
((unsigned char)*p
))
358 make_name
(struct assignment
*as
)
360 struct assignment
*lopt
;
361 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 ret
= asprintf
(&s
, "%s_flag", lopt
->u.value
);
375 ret
= asprintf
(&s
, "%s_%s", lopt
->u.value
, type
->u.value
);
383 static void defval_int
(const char *name
, struct assignment
*defval
)
386 cprint
(1, "opt.%s = %s;\n", name
, defval
->u.value
);
388 cprint
(1, "opt.%s = 0;\n", name
);
390 static void defval_neg_flag
(const char *name
, struct assignment
*defval
)
393 cprint
(1, "opt.%s = %s;\n", name
, defval
->u.value
);
395 cprint
(1, "opt.%s = 1;\n", name
);
397 static void defval_string
(const char *name
, struct assignment
*defval
)
400 cprint
(1, "opt.%s = (char *)(unsigned long)\"%s\";\n", name
, defval
->u.value
);
402 cprint
(1, "opt.%s = NULL;\n", name
);
404 static void defval_strings
(const char *name
, struct assignment
*defval
)
406 cprint
(1, "opt.%s.num_strings = 0;\n", name
);
407 cprint
(1, "opt.%s.strings = NULL;\n", name
);
410 static void free_strings
(const char *name
)
412 cprint
(1, "free_getarg_strings (&opt.%s);\n", name
);
415 struct type_handler
{
416 const char *typename
;
418 const char *getarg_type
;
419 void (*defval
)(const char*, struct assignment
*);
420 void (*free
)(const char*);
421 } type_handlers
[] = {
435 "struct getarg_strings",
452 { NULL
, NULL
, NULL
, NULL
, NULL
}
455 static struct type_handler
*find_handler
(struct assignment
*type
)
457 struct type_handler
*th
;
458 for
(th
= type_handlers
; th
->typename
!= NULL
; th
++)
459 if
(strcmp
(type
->u.value
, th
->typename
) == 0)
461 ex
(type
, "unknown type \"%s\"", type
->u.value
);
466 gen_options
(struct assignment
*opt1
, const char *name
)
468 struct assignment
*tmp
;
470 hprint
(0, "struct %s_options {\n", name
);
474 tmp
= find_next
(tmp
, "option")) {
475 struct assignment
*type
;
476 struct type_handler
*th
;
479 s
= make_name
(tmp
->u.assignment
);
480 type
= find
(tmp
->u.assignment
, "type");
481 th
= find_handler
(type
);
482 hprint
(1, "%s %s;\n", th
->c_type
, s
);
489 gen_wrapper
(struct assignment
*as
)
491 struct assignment
*name
;
492 struct assignment
*arg
;
493 struct assignment
*opt1
;
494 struct assignment
*function
;
495 struct assignment
*tmp
;
500 name
= find
(as
, "name");
501 n
= strdup
(name
->u.value
);
503 arg
= find
(as
, "argument");
506 opt1
= find
(as
, "option");
507 function
= find
(as
, "function");
509 f
= function
->u.value
;
515 gen_options
(opt1
, n
);
516 hprint
(0, "int %s(struct %s_options*, int, char **);\n", f
, n
);
518 hprint
(0, "int %s(void*, int, char **);\n", f
);
521 fprintf
(cfile
, "static int\n");
522 fprintf
(cfile
, "%s_wrap(int argc, char **argv)\n", n
);
523 fprintf
(cfile
, "{\n");
525 cprint
(1, "struct %s_options opt;\n", n
);
526 cprint
(1, "int ret;\n");
527 cprint
(1, "int optidx = 0;\n");
528 cprint
(1, "struct getargs args[] = {\n");
529 for
(tmp
= find
(as
, "option");
531 tmp
= find_next
(tmp
, "option")) {
532 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
533 struct assignment
*lopt
= find
(tmp
->u.assignment
, "long");
534 struct assignment
*sopt
= find
(tmp
->u.assignment
, "short");
535 struct assignment
*aarg
= find
(tmp
->u.assignment
, "argument");
536 struct assignment
*help
= find
(tmp
->u.assignment
, "help");
538 struct type_handler
*th
;
542 fprintf
(cfile
, "\"%s\", ", lopt
->u.value
);
544 fprintf
(cfile
, "NULL, ");
546 fprintf
(cfile
, "'%c', ", *sopt
->u.value
);
548 fprintf
(cfile
, "0, ");
549 th
= find_handler
(type
);
550 fprintf
(cfile
, "%s, ", th
->getarg_type
);
551 fprintf
(cfile
, "NULL, ");
553 fprintf
(cfile
, "\"%s\", ", help
->u.value
);
555 fprintf
(cfile
, "NULL, ");
557 fprintf
(cfile
, "\"%s\"", aarg
->u.value
);
560 fprintf
(cfile
, "NULL");
561 fprintf
(cfile
, " },\n");
563 cprint
(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
565 cprint
(1, "int help_flag = 0;\n");
567 for
(tmp
= find
(as
, "option");
569 tmp
= find_next
(tmp
, "option")) {
571 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
573 struct assignment
*defval
= find
(tmp
->u.assignment
, "default");
575 struct type_handler
*th
;
577 s
= make_name
(tmp
->u.assignment
);
578 th
= find_handler
(type
);
579 (*th
->defval
)(s
, defval
);
583 for
(tmp
= find
(as
, "option");
585 tmp
= find_next
(tmp
, "option")) {
587 s
= make_name
(tmp
->u.assignment
);
588 cprint
(1, "args[%d].value = &opt.%s;\n", nargs
++, s
);
591 cprint
(1, "args[%d].value = &help_flag;\n", nargs
++);
592 cprint
(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs
);
593 cprint
(2, "goto usage;\n");
599 if
(narguments
== 0) {
602 if
((tmp
= find
(as
, "min_args")) != NULL
) {
603 min_args
= strtol
(tmp
->u.value
, &end
, 0);
605 ex
(tmp
, "min_args is not numeric");
609 ex
(tmp
, "min_args must be non-negative");
613 if
((tmp
= find
(as
, "max_args")) != NULL
) {
614 max_args
= strtol
(tmp
->u.value
, &end
, 0);
616 ex
(tmp
, "max_args is not numeric");
620 ex
(tmp
, "max_args must be non-negative");
625 if
(min_args
!= -1 || max_args
!= -1) {
626 if
(min_args
== max_args
) {
627 cprint
(1, "if(argc - optidx != %d) {\n",
629 cprint
(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args
);
630 cprint
(2, "goto usage;\n");
634 cprint
(1, "if(argc - optidx > %d) {\n", max_args
);
635 cprint
(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args
);
636 cprint
(2, "goto usage;\n");
640 cprint
(1, "if(argc - optidx < %d) {\n", min_args
);
641 cprint
(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args
);
642 cprint
(2, "goto usage;\n");
649 cprint
(1, "if(help_flag)\n");
650 cprint
(2, "goto usage;\n");
652 cprint
(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
653 f
, opt1 ?
"&opt": "NULL");
655 /* free allocated data */
656 for
(tmp
= find
(as
, "option");
658 tmp
= find_next
(tmp
, "option")) {
660 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
661 struct type_handler
*th
;
662 th
= find_handler
(type
);
665 s
= make_name
(tmp
->u.assignment
);
669 cprint
(1, "return ret;\n");
671 cprint
(0, "usage:\n");
672 cprint
(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs
,
673 name
->u.value
, arg ? arg
->u.value
: "");
674 /* free allocated data */
675 for
(tmp
= find
(as
, "option");
677 tmp
= find_next
(tmp
, "option")) {
679 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
680 struct type_handler
*th
;
681 th
= find_handler
(type
);
684 s
= make_name
(tmp
->u.assignment
);
688 cprint
(1, "return 0;\n");
693 char cname
[PATH_MAX
];
694 char hname
[PATH_MAX
];
697 gen
(struct assignment
*as
)
699 struct assignment
*a
;
700 cprint
(0, "#include <stdio.h>\n");
701 cprint
(0, "#include <getarg.h>\n");
702 cprint
(0, "#include <sl.h>\n");
703 cprint
(0, "#include \"%s\"\n\n", hname
);
705 hprint
(0, "#include <stdio.h>\n");
706 hprint
(0, "#include <sl.h>\n");
710 for
(a
= as
; a
!= NULL
; a
= a
->next
)
711 gen_wrapper
(a
->u.assignment
);
713 cprint
(0, "SL_cmd commands[] = {\n");
714 for
(a
= as
; a
!= NULL
; a
= a
->next
)
715 gen_command
(a
->u.assignment
);
716 cprint
(1, "{ NULL, NULL, NULL, NULL }\n");
719 hprint
(0, "extern SL_cmd commands[];\n");
724 struct getargs args
[] = {
725 { "version", 0, arg_flag
, &version_flag
, NULL
, NULL
},
726 { "help", 0, arg_flag
, &help_flag
, NULL
, NULL
}
728 int num_args
= sizeof
(args
) / sizeof
(args
[0]);
733 arg_printusage
(args
, num_args
, NULL
, "command-table");
738 main
(int argc
, char **argv
)
744 setprogname
(argv
[0]);
745 if
(getarg
(args
, num_args
, argc
, argv
, &optidx
))
757 filename
= argv
[optidx
];
758 yyin
= fopen
(filename
, "r");
760 err
(1, "%s", filename
);
761 p
= strrchr
(filename
, '/');
763 strlcpy
(cname
, p
+ 1, sizeof
(cname
));
765 strlcpy
(cname
, filename
, sizeof
(cname
));
766 p
= strrchr
(cname
, '.');
769 strlcpy
(hname
, cname
, sizeof
(hname
));
770 strlcat
(cname
, ".c", sizeof
(cname
));
771 strlcat
(hname
, ".h", sizeof
(hname
));
775 if
(check
(assignment
) == 0) {
776 cfile
= fopen
(cname
, "w");
779 hfile
= fopen
(hname
, "w");