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
37 __RCSID
("$Heimdal: slc-gram.y 20767 2007-06-01 11:24:52Z lha $"
52 extern
struct assignment
*assignment
;
57 struct assignment
*assignment
;
60 %token
<string> LITERAL
61 %token
<string> STRING
62 %type
<assignment
> assignment assignments
74 assignments
: assignment assignments
82 assignment
: LITERAL
'=' STRING
84 $$
= malloc
(sizeof
(*$$
));
91 | LITERAL
'=' '{' assignments
'}'
93 $$
= malloc
(sizeof
(*$$
));
95 $$
->type
= a_assignment
;
97 $$
->u.assignment
= $4;
106 struct assignment
*assignment
;
110 ex
(struct assignment
*a
, const char *fmt
, ...
)
113 fprintf
(stderr
, "%s:%d: ", a
->name
, a
->lineno
);
115 vfprintf
(stderr
, fmt
, ap
);
117 fprintf
(stderr
, "\n");
123 check_option
(struct assignment
*as
)
125 struct assignment
*a
;
129 int seen_argument
= 0;
131 int seen_default
= 0;
134 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
135 if
(strcmp
(a
->name
, "long") == 0)
137 else if
(strcmp
(a
->name
, "short") == 0)
139 else if
(strcmp
(a
->name
, "type") == 0)
141 else if
(strcmp
(a
->name
, "argument") == 0)
143 else if
(strcmp
(a
->name
, "help") == 0)
145 else if
(strcmp
(a
->name
, "default") == 0)
148 ex
(a
, "unknown name");
152 if
(seen_long
== 0 && seen_short
== 0) {
153 ex
(as
, "neither long nor short option");
157 ex
(as
, "multiple long options");
161 ex
(as
, "multiple short options");
165 ex
(as
, "multiple types");
168 if
(seen_argument
> 1) {
169 ex
(as
, "multiple arguments");
173 ex
(as
, "multiple help strings");
176 if
(seen_default
> 1) {
177 ex
(as
, "multiple default values");
184 check_command
(struct assignment
*as
)
186 struct assignment
*a
;
188 int seen_function
= 0;
190 int seen_argument
= 0;
191 int seen_minargs
= 0;
192 int seen_maxargs
= 0;
194 for
(a
= as
; a
!= NULL
; a
= a
->next
) {
195 if
(strcmp
(a
->name
, "name") == 0)
197 else if
(strcmp
(a
->name
, "function") == 0) {
199 } else if
(strcmp
(a
->name
, "option") == 0)
200 ret
+= check_option
(a
->u.assignment
);
201 else if
(strcmp
(a
->name
, "help") == 0) {
203 } else if
(strcmp
(a
->name
, "argument") == 0) {
205 } else if
(strcmp
(a
->name
, "min_args") == 0) {
207 } else if
(strcmp
(a
->name
, "max_args") == 0) {
210 ex
(a
, "unknown name");
215 ex
(as
, "no command name");
218 if
(seen_function
> 1) {
219 ex
(as
, "multiple function names");
223 ex
(as
, "multiple help strings");
226 if
(seen_argument
> 1) {
227 ex
(as
, "multiple argument strings");
230 if
(seen_minargs
> 1) {
231 ex
(as
, "multiple min_args strings");
234 if
(seen_maxargs
> 1) {
235 ex
(as
, "multiple max_args strings");
243 check
(struct assignment
*as
)
245 struct assignment
*a
;
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
);
253 if
(a
->type
!= a_assignment
) {
254 fprintf
(stderr
, "bad command definition %s line %d\n", a
->name
, a
->lineno
);
258 ret
+= check_command
(a
->u.assignment
);
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)
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)
284 space
(FILE *f
, int level
)
286 fprintf
(f
, "%*.*s", level
* 4, level
* 4, " ");
290 cprint
(int level
, const char *fmt
, ...
)
295 vfprintf
(cfile
, fmt
, ap
);
300 hprint
(int level
, const char *fmt
, ...
)
305 vfprintf
(hfile
, fmt
, ap
);
309 static void gen_name
(char *str
);
312 gen_command
(struct assignment
*as
)
314 struct assignment
*a
, *b
;
316 a
= find
(as
, "name");
317 f
= strdup
(a
->u.value
);
320 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
321 fprintf
(cfile
, "%s_wrap, ", f
);
322 b
= find
(as
, "argument");
324 fprintf
(cfile
, "\"%s %s\", ", a
->u.value
, b
->u.value
);
326 fprintf
(cfile
, "\"%s\", ", a
->u.value
);
327 b
= find
(as
, "help");
329 fprintf
(cfile
, "\"%s\"", b
->u.value
);
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
);
343 for
(p
= str
; *p
!= '\0'; p
++)
344 if
(!isalnum
((unsigned char)*p
))
349 make_name
(struct assignment
*as
)
351 struct assignment
*lopt
;
352 struct assignment
*type
;
355 lopt
= find
(as
, "long");
357 lopt
= find
(as
, "name");
361 type
= find
(as
, "type");
362 if
(strcmp
(type
->u.value
, "-flag") == 0)
363 asprintf
(&s
, "%s_flag", lopt
->u.value
);
365 asprintf
(&s
, "%s_%s", lopt
->u.value
, type
->u.value
);
371 static void defval_int
(const char *name
, struct assignment
*defval
)
374 cprint
(1, "opt.%s = %s;\n", name
, defval
->u.value
);
376 cprint
(1, "opt.%s = 0;\n", name
);
378 static void defval_string
(const char *name
, struct assignment
*defval
)
381 cprint
(1, "opt.%s = \"%s\";\n", name
, defval
->u.value
);
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
;
399 const char *getarg_type
;
400 void (*defval
)(const char*, struct assignment
*);
401 void (*free
)(const char*);
402 } type_handlers
[] = {
416 "struct getarg_strings",
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)
442 ex
(type
, "unknown type \"%s\"", type
->u.value
);
447 gen_options
(struct assignment
*opt1
, const char *name
)
449 struct assignment
*tmp
;
451 hprint
(0, "struct %s_options {\n", name
);
455 tmp
= find_next
(tmp
, "option")) {
456 struct assignment
*type
;
457 struct type_handler
*th
;
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
);
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
;
480 name
= find
(as
, "name");
481 n
= strdup
(name
->u.value
);
483 arg
= find
(as
, "argument");
484 opt1
= find
(as
, "option");
485 function
= find
(as
, "function");
487 f
= function
->u.value
;
493 gen_options
(opt1
, n
);
494 hprint
(0, "int %s(struct %s_options*, int, char **);\n", f
, n
);
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");
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");
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
;
520 fprintf
(cfile
, "\"%s\", ", lopt
->u.value
);
522 fprintf
(cfile
, "NULL, ");
524 fprintf
(cfile
, "'%c', ", *sopt
->u.value
);
526 fprintf
(cfile
, "0, ");
527 th
= find_handler
(type
);
528 fprintf
(cfile
, "%s, ", th
->getarg_type
);
529 fprintf
(cfile
, "NULL, ");
531 fprintf
(cfile
, "\"%s\", ", help
->u.value
);
533 fprintf
(cfile
, "NULL, ");
535 fprintf
(cfile
, "\"%s\"", aarg
->u.value
);
537 fprintf
(cfile
, "NULL");
538 fprintf
(cfile
, " },\n");
540 cprint
(2, "{ \"help\", 'h', arg_flag, NULL, NULL, NULL }\n");
542 cprint
(1, "int help_flag = 0;\n");
544 for
(tmp
= find
(as
, "option");
546 tmp
= find_next
(tmp
, "option")) {
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
);
560 for
(tmp
= find
(as
, "option");
562 tmp
= find_next
(tmp
, "option")) {
564 s
= make_name
(tmp
->u.assignment
);
565 cprint
(1, "args[%d].value = &opt.%s;\n", nargs
++, 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");
579 if
((tmp
= find
(as
, "min_args")) != NULL
) {
580 min_args
= strtol
(tmp
->u.value
, &end
, 0);
582 ex
(tmp
, "min_args is not numeric");
586 ex
(tmp
, "min_args must be non-negative");
590 if
((tmp
= find
(as
, "max_args")) != NULL
) {
591 max_args
= strtol
(tmp
->u.value
, &end
, 0);
593 ex
(tmp
, "max_args is not numeric");
597 ex
(tmp
, "max_args must be non-negative");
602 if
(min_args
!= -1 || max_args
!= -1) {
603 if
(min_args
== max_args
) {
604 cprint
(1, "if(argc - optidx != %d) {\n",
606 cprint
(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args
);
607 cprint
(2, "goto usage;\n");
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");
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");
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");
635 tmp
= find_next
(tmp
, "option")) {
637 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
638 struct type_handler
*th
;
639 th
= find_handler
(type
);
642 s
= make_name
(tmp
->u.assignment
);
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");
654 tmp
= find_next
(tmp
, "option")) {
656 struct assignment
*type
= find
(tmp
->u.assignment
, "type");
657 struct type_handler
*th
;
658 th
= find_handler
(type
);
661 s
= make_name
(tmp
->u.assignment
);
665 cprint
(1, "return 0;\n");
670 char cname
[PATH_MAX
];
671 char hname
[PATH_MAX
];
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");
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");
696 hprint
(0, "extern SL_cmd commands[];\n");
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]);
710 arg_printusage
(args
, num_args
, NULL
, "command-table");
715 main
(int argc
, char **argv
)
721 setprogname
(argv
[0]);
722 if
(getarg
(args
, num_args
, argc
, argv
, &optidx
))
734 filename
= argv
[optidx
];
735 yyin
= fopen
(filename
, "r");
737 err
(1, "%s", filename
);
738 p
= strrchr
(filename
, '/');
740 strlcpy
(cname
, p
+ 1, sizeof
(cname
));
742 strlcpy
(cname
, filename
, sizeof
(cname
));
743 p
= strrchr
(cname
, '.');
746 strlcpy
(hname
, cname
, sizeof
(hname
));
747 strlcat
(cname
, ".c", sizeof
(cname
));
748 strlcat
(hname
, ".h", sizeof
(hname
));
752 if
(check
(assignment
) == 0) {
753 cfile
= fopen
(cname
, "w");
756 hfile
= fopen
(hname
, "w");