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
;
53 struct assignment
*assignment
;
56 %token
<string> LITERAL
57 %token
<string> STRING
58 %type
<assignment
> assignment assignments
70 assignments
: assignment assignments
78 assignment
: LITERAL
'=' STRING
80 $$
= malloc
(sizeof
(*$$
));
87 | LITERAL
'=' '{' assignments
'}'
89 $$
= malloc
(sizeof
(*$$
));
91 $$
->type
= a_assignment
;
93 $$
->u.assignment
= $4;
102 struct assignment
*assignment
;
106 ex
(struct assignment
*a
, const char *fmt
, ...
)
109 fprintf
(stderr
, "%s:%d: ", a
->name
, a
->lineno
);
111 vfprintf
(stderr
, fmt
, ap
);
113 fprintf
(stderr
, "\n");
119 check_option
(struct assignment
*as
)
121 struct assignment
*a
;
125 int seen_argument
= 0;
127 int seen_default
= 0;
130 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
131 if
(strcmp
(a
->name
, "long") == 0)
133 else if
(strcmp
(a
->name
, "short") == 0)
135 else if
(strcmp
(a
->name
, "type") == 0)
137 else if
(strcmp
(a
->name
, "argument") == 0)
139 else if
(strcmp
(a
->name
, "help") == 0)
141 else if
(strcmp
(a
->name
, "default") == 0)
144 ex
(a
, "unknown name");
148 if
(seen_long
== 0 && seen_short
== 0) {
149 ex
(as
, "neither long nor short option");
153 ex
(as
, "multiple long options");
157 ex
(as
, "multiple short options");
161 ex
(as
, "multiple types");
164 if
(seen_argument
> 1) {
165 ex
(as
, "multiple arguments");
169 ex
(as
, "multiple help strings");
172 if
(seen_default
> 1) {
173 ex
(as
, "multiple default values");
180 check_command
(struct assignment
*as
)
182 struct assignment
*a
;
184 int seen_function
= 0;
186 int seen_argument
= 0;
187 int seen_minargs
= 0;
188 int seen_maxargs
= 0;
190 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
191 if
(strcmp
(a
->name
, "name") == 0)
193 else if
(strcmp
(a
->name
, "function") == 0) {
195 } else if
(strcmp
(a
->name
, "option") == 0)
196 ret
+= check_option
(a
->u.assignment
);
197 else if
(strcmp
(a
->name
, "help") == 0) {
199 } else if
(strcmp
(a
->name
, "argument") == 0) {
201 } else if
(strcmp
(a
->name
, "min_args") == 0) {
203 } else if
(strcmp
(a
->name
, "max_args") == 0) {
206 ex
(a
, "unknown name");
211 ex
(as
, "no command name");
214 if
(seen_function
> 1) {
215 ex
(as
, "multiple function names");
219 ex
(as
, "multiple help strings");
222 if
(seen_argument
> 1) {
223 ex
(as
, "multiple argument strings");
226 if
(seen_minargs
> 1) {
227 ex
(as
, "multiple min_args strings");
230 if
(seen_maxargs
> 1) {
231 ex
(as
, "multiple max_args strings");
239 check
(struct assignment
*as
)
241 struct assignment
*a
;
243 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
244 if
(strcmp
(a
->name
, "command")) {
245 fprintf
(stderr
, "unknown type %s line %d\n", a
->name
, a
->lineno
);
249 if
(a
->type
!= a_assignment
) {
250 fprintf
(stderr
, "bad command definition %s line %d\n", a
->name
, a
->lineno
);
254 ret
+= check_command
(a
->u.assignment
);
259 static struct assignment
*
260 find_next
(struct assignment
*as
, const char *name
)
262 for
(as
= as
->next
; as
!= NULL
; as
= as
->next
) {
263 if
(strcmp
(as
->name
, name
) == 0)
269 static struct assignment
*
270 find
(struct assignment
*as
, const char *name
)
272 for
(; as
!= NULL
; as
= as
->next
) {
273 if
(strcmp
(as
->name
, name
) == 0)
280 space
(FILE *f
, int level
)
282 fprintf
(f
, "%*.*s", level
* 4, level
* 4, " ");
286 cprint
(int level
, const char *fmt
, ...
)
291 vfprintf
(cfile
, fmt
, ap
);
296 hprint
(int level
, const char *fmt
, ...
)
301 vfprintf
(hfile
, fmt
, ap
);
305 static void gen_name
(char *str
);
308 gen_command
(struct assignment
*as
)
310 struct assignment
*a
, *b
;
312 a
= find
(as
, "name");
313 f
= strdup
(a
->u.value
);
316 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
317 fprintf
(cfile
, "%s_wrap, ", f
);
318 b
= find
(as
, "argument");
320 fprintf
(cfile
, "\"%s %s\", ", a
->u.value
, b
->u.value
);
322 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
323 b
= find
(as
, "help");
325 fprintf
(cfile
, "\"%s\"", b
->u.value
);
327 fprintf
(cfile
, "NULL");
328 fprintf
(cfile
, " },\n");
329 for
(a
= a
->next
; a
!= NULL
; a
= a
->next
)
330 if
(strcmp
(a
->name
, "name") == 0)
331 cprint
(1, " { \"%s\" },\n", a
->u.value
);
339 for
(p
= str
; *p
!= '\0'; p
++)
340 if
(!isalnum
((unsigned char)*p
))
345 make_name
(struct assignment
*as
)
347 struct assignment
*lopt
;
348 struct assignment
*type
;
351 lopt
= find
(as
, "long");
353 lopt
= find
(as
, "name");
357 type
= find
(as
, "type");
358 if
(strcmp
(type
->u.value
, "-flag") == 0)
359 asprintf
(&s
, "%s_flag", lopt
->u.value
);
361 asprintf
(&s
, "%s_%s", lopt
->u.value
, type
->u.value
);
367 static void defval_int
(const char *name
, struct assignment
*defval
)
370 cprint
(1, "opt.%s = %s;\n", name
, defval
->u.value
);
372 cprint
(1, "opt.%s = 0;\n", name
);
374 static void defval_neg_flag
(const char *name
, struct assignment
*defval
)
377 cprint
(1, "opt.%s = %s;\n", name
, defval
->u.value
);
379 cprint
(1, "opt.%s = 1;\n", name
);
381 static void defval_string
(const char *name
, struct assignment
*defval
)
384 cprint
(1, "opt.%s = \"%s\";\n", name
, defval
->u.value
);
386 cprint
(1, "opt.%s = NULL;\n", name
);
388 static void defval_strings
(const char *name
, struct assignment
*defval
)
390 cprint
(1, "opt.%s.num_strings = 0;\n", name
);
391 cprint
(1, "opt.%s.strings = NULL;\n", name
);
394 static void free_strings
(const char *name
)
396 cprint
(1, "free_getarg_strings (&opt.%s);\n", name
);
399 struct type_handler
{
400 const char *typename
;
402 const char *getarg_type
;
403 void (*defval
)(const char*, struct assignment
*);
404 void (*free
)(const char*);
405 } type_handlers
[] = {
419 "struct getarg_strings",
439 static struct type_handler
*find_handler
(struct assignment
*type
)
441 struct type_handler
*th
;
442 for
(th
= type_handlers
; th
->typename
!= NULL
; th
++)
443 if
(strcmp
(type
->u.value
, th
->typename
) == 0)
445 ex
(type
, "unknown type \"%s\"", type
->u.value
);
450 gen_options
(struct assignment
*opt1
, const char *name
)
452 struct assignment
*tmp
;
454 hprint
(0, "struct %s_options {\n", name
);
458 tmp
= find_next
(tmp
, "option")) {
459 struct assignment
*type
;
460 struct type_handler
*th
;
463 s
= make_name
(tmp
->u.assignment
);
464 type
= find
(tmp
->u.assignment
, "type");
465 th
= find_handler
(type
);
466 hprint
(1, "%s %s;\n", th
->c_type
, s
);
473 gen_wrapper
(struct assignment
*as
)
475 struct assignment
*name
;
476 struct assignment
*arg
;
477 struct assignment
*opt1
;
478 struct assignment
*function
;
479 struct assignment
*tmp
;
483 name
= find
(as
, "name");
484 n
= strdup
(name
->u.value
);
486 arg
= find
(as
, "argument");
487 opt1
= find
(as
, "option");
488 function
= find
(as
, "function");
490 f
= function
->u.value
;
496 gen_options
(opt1
, n
);
497 hprint
(0, "int %s(struct %s_options*, int, char **);\n", f
, n
);
499 hprint
(0, "int %s(void*, int, char **);\n", f
);
502 fprintf
(cfile
, "static int\n");
503 fprintf
(cfile
, "%s_wrap(int argc, char **argv)\n", n
);
504 fprintf
(cfile
, "{\n");
506 cprint
(1, "struct %s_options opt;\n", n
);
507 cprint
(1, "int ret;\n");
508 cprint
(1, "int optidx = 0;\n");
509 cprint
(1, "struct getargs args[] = {\n");
510 for
(tmp
= find
(as
, "option");
512 tmp
= find_next
(tmp
, "option")) {
513 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
514 struct assignment
*lopt
= find
(tmp
->u.assignment
, "long");
515 struct assignment
*sopt
= find
(tmp
->u.assignment
, "short");
516 struct assignment
*aarg
= find
(tmp
->u.assignment
, "argument");
517 struct assignment
*help
= find
(tmp
->u.assignment
, "help");
519 struct type_handler
*th
;
523 fprintf
(cfile
, "\"%s\", ", lopt
->u.value
);
525 fprintf
(cfile
, "NULL, ");
527 fprintf
(cfile
, "'%c', ", *sopt
->u.value
);
529 fprintf
(cfile
, "0, ");
530 th
= find_handler
(type
);
531 fprintf
(cfile
, "%s, ", th
->getarg_type
);
532 fprintf
(cfile
, "NULL, ");
534 fprintf
(cfile
, "\"%s\", ", help
->u.value
);
536 fprintf
(cfile
, "NULL, ");
538 fprintf
(cfile
, "\"%s\"", aarg
->u.value
);
540 fprintf
(cfile
, "NULL");
541 fprintf
(cfile
, " },\n");
543 cprint
(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
545 cprint
(1, "int help_flag = 0;\n");
547 for
(tmp
= find
(as
, "option");
549 tmp
= find_next
(tmp
, "option")) {
551 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
553 struct assignment
*defval
= find
(tmp
->u.assignment
, "default");
555 struct type_handler
*th
;
557 s
= make_name
(tmp
->u.assignment
);
558 th
= find_handler
(type
);
559 (*th
->defval
)(s
, defval
);
563 for
(tmp
= find
(as
, "option");
565 tmp
= find_next
(tmp
, "option")) {
567 s
= make_name
(tmp
->u.assignment
);
568 cprint
(1, "args[%d].value = &opt.%s;\n", nargs
++, s
);
571 cprint
(1, "args[%d].value = &help_flag;\n", nargs
++);
572 cprint
(1, "if(getarg(args, %d, argc, argv, &optidx))\n", nargs
);
573 cprint
(2, "goto usage;\n");
582 if
((tmp
= find
(as
, "min_args")) != NULL
) {
583 min_args
= strtol
(tmp
->u.value
, &end
, 0);
585 ex
(tmp
, "min_args is not numeric");
589 ex
(tmp
, "min_args must be non-negative");
593 if
((tmp
= find
(as
, "max_args")) != NULL
) {
594 max_args
= strtol
(tmp
->u.value
, &end
, 0);
596 ex
(tmp
, "max_args is not numeric");
600 ex
(tmp
, "max_args must be non-negative");
605 if
(min_args
!= -1 || max_args
!= -1) {
606 if
(min_args
== max_args
) {
607 cprint
(1, "if(argc - optidx != %d) {\n",
609 cprint
(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args
);
610 cprint
(2, "goto usage;\n");
614 cprint
(1, "if(argc - optidx > %d) {\n", max_args
);
615 cprint
(2, "fprintf(stderr, \"Arguments given (%%u) are more than expected (%u).\\n\\n\", argc - optidx);\n", max_args
);
616 cprint
(2, "goto usage;\n");
620 cprint
(1, "if(argc - optidx < %d) {\n", min_args
);
621 cprint
(2, "fprintf(stderr, \"Arguments given (%%u) are less than expected (%u).\\n\\n\", argc - optidx);\n", min_args
);
622 cprint
(2, "goto usage;\n");
629 cprint
(1, "if(help_flag)\n");
630 cprint
(2, "goto usage;\n");
632 cprint
(1, "ret = %s(%s, argc - optidx, argv + optidx);\n",
633 f
, opt1 ?
"&opt": "NULL");
635 /* free allocated data */
636 for
(tmp
= find
(as
, "option");
638 tmp
= find_next
(tmp
, "option")) {
640 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
641 struct type_handler
*th
;
642 th
= find_handler
(type
);
645 s
= make_name
(tmp
->u.assignment
);
649 cprint
(1, "return ret;\n");
651 cprint
(0, "usage:\n");
652 cprint
(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs
,
653 name
->u.value
, arg ? arg
->u.value
: "");
654 /* free allocated data */
655 for
(tmp
= find
(as
, "option");
657 tmp
= find_next
(tmp
, "option")) {
659 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
660 struct type_handler
*th
;
661 th
= find_handler
(type
);
664 s
= make_name
(tmp
->u.assignment
);
668 cprint
(1, "return 0;\n");
673 char cname
[PATH_MAX
];
674 char hname
[PATH_MAX
];
677 gen
(struct assignment
*as
)
679 struct assignment
*a
;
680 cprint
(0, "#include <stdio.h>\n");
681 cprint
(0, "#include <getarg.h>\n");
682 cprint
(0, "#include <sl.h>\n");
683 cprint
(0, "#include \"%s\"\n\n", hname
);
685 hprint
(0, "#include <stdio.h>\n");
686 hprint
(0, "#include <sl.h>\n");
690 for
(a
= as
; a
!= NULL
; a
= a
->next
)
691 gen_wrapper
(a
->u.assignment
);
693 cprint
(0, "SL_cmd commands[] = {\n");
694 for
(a
= as
; a
!= NULL
; a
= a
->next
)
695 gen_command
(a
->u.assignment
);
696 cprint
(1, "{ NULL }\n");
699 hprint
(0, "extern SL_cmd commands[];\n");
704 struct getargs args
[] = {
705 { "version", 0, arg_flag
, &version_flag
},
706 { "help", 0, arg_flag
, &help_flag
}
708 int num_args
= sizeof
(args
) / sizeof
(args
[0]);
713 arg_printusage
(args
, num_args
, NULL
, "command-table");
718 main
(int argc
, char **argv
)
724 setprogname
(argv
[0]);
725 if
(getarg
(args
, num_args
, argc
, argv
, &optidx
))
737 filename
= argv
[optidx
];
738 yyin
= fopen
(filename
, "r");
740 err
(1, "%s", filename
);
741 p
= strrchr
(filename
, '/');
743 strlcpy
(cname
, p
+ 1, sizeof
(cname
));
745 strlcpy
(cname
, filename
, sizeof
(cname
));
746 p
= strrchr
(cname
, '.');
749 strlcpy
(hname
, cname
, sizeof
(hname
));
750 strlcat
(cname
, ".c", sizeof
(cname
));
751 strlcat
(hname
, ".h", sizeof
(hname
));
755 if
(check
(assignment
) == 0) {
756 cfile
= fopen
(cname
, "w");
759 hfile
= fopen
(hname
, "w");