diff options
Diffstat (limited to 'lib/sl/slc-gram.y')
| -rw-r--r-- | lib/sl/slc-gram.y | 143 |
1 files changed, 82 insertions, 61 deletions
diff --git a/lib/sl/slc-gram.y b/lib/sl/slc-gram.y index 7d9fadcb55bd2..530b1a3447761 100644 --- a/lib/sl/slc-gram.y +++ b/lib/sl/slc-gram.y @@ -1,41 +1,38 @@ %{ /* - * Copyright (c) 2004-2006 Kungliga Tekniska Högskolan - * (Royal Institute of Technology, Stockholm, Sweden). - * All rights reserved. + * Copyright (c) 2004-2006 Kungliga Tekniska Högskolan + * (Royal Institute of Technology, Stockholm, Sweden). + * All rights reserved. * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * 3. Neither the name of the Institute nor the names of its contributors - * may be used to endorse or promote products derived from this software - * without specific prior written permission. + * 3. Neither the name of the Institute nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. * - * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE - * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE - * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE - * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL - * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS - * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT - * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY - * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. + * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ -#ifdef HAVE_CONFIG_H #include <config.h> -RCSID("$Id: slc-gram.y 20767 2007-06-01 11:24:52Z lha $"); -#endif #include <stdio.h> #include <stdlib.h> @@ -49,6 +46,12 @@ RCSID("$Id: slc-gram.y 20767 2007-06-01 11:24:52Z lha $"); #include "slc.h" extern FILE *yyin; extern struct assignment *assignment; + +/* Declarations for Bison: + */ +#define YYMALLOC malloc +#define YYFREE free + %} %union { @@ -123,6 +126,7 @@ check_option(struct assignment *as) { struct assignment *a; int seen_long = 0; + int seen_name = 0; int seen_short = 0; int seen_type = 0; int seen_argument = 0; @@ -135,6 +139,8 @@ check_option(struct assignment *as) seen_long++; else if(strcmp(a->name, "short") == 0) seen_short++; + else if(strcmp(a->name, "name") == 0) + seen_name++; else if(strcmp(a->name, "type") == 0) seen_type++; else if(strcmp(a->name, "argument") == 0) @@ -144,7 +150,7 @@ check_option(struct assignment *as) else if(strcmp(a->name, "default") == 0) seen_default++; else { - ex(a, "unknown name"); + ex(a, "unknown name %s", a->name); ret++; } } @@ -152,6 +158,10 @@ check_option(struct assignment *as) ex(as, "neither long nor short option"); ret++; } + if (seen_long == 0 && seen_name == 0) { + ex(as, "either of long or name option must be used"); + ret++; + } if(seen_long > 1) { ex(as, "multiple long options"); ret++; @@ -206,7 +216,7 @@ check_command(struct assignment *as) } else if(strcmp(a->name, "max_args") == 0) { seen_maxargs++; } else { - ex(a, "unknown name"); + ex(a, "unknown name: %s", a->name); ret++; } } @@ -234,7 +244,7 @@ check_command(struct assignment *as) ex(as, "multiple max_args strings"); ret++; } - + return ret; } @@ -356,7 +366,7 @@ make_name(struct assignment *as) lopt = find(as, "name"); if(lopt == NULL) return NULL; - + type = find(as, "type"); if(strcmp(type->u.value, "-flag") == 0) asprintf(&s, "%s_flag", lopt->u.value); @@ -374,10 +384,17 @@ static void defval_int(const char *name, struct assignment *defval) else cprint(1, "opt.%s = 0;\n", name); } -static void defval_string(const char *name, struct assignment *defval) +static void defval_neg_flag(const char *name, struct assignment *defval) +{ + if(defval != NULL) + cprint(1, "opt.%s = %s;\n", name, defval->u.value); + else + cprint(1, "opt.%s = 1;\n", name); +} +static void defval_string(const char *name, struct assignment *defval) { if(defval != NULL) - cprint(1, "opt.%s = \"%s\";\n", name, defval->u.value); + cprint(1, "opt.%s = (char *)(unsigned long)\"%s\";\n", name, defval->u.value); else cprint(1, "opt.%s = NULL;\n", name); } @@ -426,7 +443,7 @@ struct type_handler { { "-flag", "int", "arg_negative_flag", - defval_int, + defval_neg_flag, NULL }, { NULL } @@ -449,13 +466,13 @@ gen_options(struct assignment *opt1, const char *name) hprint(0, "struct %s_options {\n", name); - for(tmp = opt1; - tmp != NULL; + for(tmp = opt1; + tmp != NULL; tmp = find_next(tmp, "option")) { struct assignment *type; struct type_handler *th; char *s; - + s = make_name(tmp->u.assignment); type = find(tmp->u.assignment, "type"); th = find_handler(type); @@ -475,19 +492,22 @@ gen_wrapper(struct assignment *as) struct assignment *tmp; char *n, *f; int nargs = 0; + int narguments = 0; name = find(as, "name"); n = strdup(name->u.value); gen_name(n); arg = find(as, "argument"); + if (arg) + narguments++; opt1 = find(as, "option"); function = find(as, "function"); if(function) f = function->u.value; else f = n; - - + + if(opt1 != NULL) { gen_options(opt1, n); hprint(0, "int %s(struct %s_options*, int, char **);\n", f, n); @@ -503,8 +523,8 @@ gen_wrapper(struct assignment *as) cprint(1, "int ret;\n"); cprint(1, "int optidx = 0;\n"); cprint(1, "struct getargs args[] = {\n"); - for(tmp = find(as, "option"); - tmp != NULL; + for(tmp = find(as, "option"); + tmp != NULL; tmp = find_next(tmp, "option")) { struct assignment *type = find(tmp->u.assignment, "type"); struct assignment *lopt = find(tmp->u.assignment, "long"); @@ -513,7 +533,7 @@ gen_wrapper(struct assignment *as) struct assignment *help = find(tmp->u.assignment, "help"); struct type_handler *th; - + cprint(2, "{ "); if(lopt) fprintf(cfile, "\"%s\", ", lopt->u.value); @@ -530,9 +550,10 @@ gen_wrapper(struct assignment *as) fprintf(cfile, "\"%s\", ", help->u.value); else fprintf(cfile, "NULL, "); - if(aarg) + if(aarg) { fprintf(cfile, "\"%s\"", aarg->u.value); - else + narguments++; + } else fprintf(cfile, "NULL"); fprintf(cfile, " },\n"); } @@ -540,8 +561,8 @@ gen_wrapper(struct assignment *as) cprint(1, "};\n"); cprint(1, "int help_flag = 0;\n"); - for(tmp = find(as, "option"); - tmp != NULL; + for(tmp = find(as, "option"); + tmp != NULL; tmp = find_next(tmp, "option")) { char *s; struct assignment *type = find(tmp->u.assignment, "type"); @@ -549,15 +570,15 @@ gen_wrapper(struct assignment *as) struct assignment *defval = find(tmp->u.assignment, "default"); struct type_handler *th; - + s = make_name(tmp->u.assignment); th = find_handler(type); (*th->defval)(s, defval); free(s); } - for(tmp = find(as, "option"); - tmp != NULL; + for(tmp = find(as, "option"); + tmp != NULL; tmp = find_next(tmp, "option")) { char *s; s = make_name(tmp->u.assignment); @@ -572,7 +593,7 @@ gen_wrapper(struct assignment *as) int min_args = -1; int max_args = -1; char *end; - if(arg == NULL) { + if(narguments == 0) { max_args = 0; } else { if((tmp = find(as, "min_args")) != NULL) { @@ -600,7 +621,7 @@ gen_wrapper(struct assignment *as) } if(min_args != -1 || max_args != -1) { if(min_args == max_args) { - cprint(1, "if(argc - optidx != %d) {\n", + cprint(1, "if(argc - optidx != %d) {\n", min_args); cprint(2, "fprintf(stderr, \"Need exactly %u parameters (%%u given).\\n\\n\", argc - optidx);\n", min_args); cprint(2, "goto usage;\n"); @@ -621,16 +642,16 @@ gen_wrapper(struct assignment *as) } } } - + cprint(1, "if(help_flag)\n"); cprint(2, "goto usage;\n"); - cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n", + cprint(1, "ret = %s(%s, argc - optidx, argv + optidx);\n", f, opt1 ? "&opt": "NULL"); - + /* free allocated data */ - for(tmp = find(as, "option"); - tmp != NULL; + for(tmp = find(as, "option"); + tmp != NULL; tmp = find_next(tmp, "option")) { char *s; struct assignment *type = find(tmp->u.assignment, "type"); @@ -645,11 +666,11 @@ gen_wrapper(struct assignment *as) cprint(1, "return ret;\n"); cprint(0, "usage:\n"); - cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs, + cprint(1, "arg_printusage (args, %d, \"%s\", \"%s\");\n", nargs, name->u.value, arg ? arg->u.value : ""); /* free allocated data */ - for(tmp = find(as, "option"); - tmp != NULL; + for(tmp = find(as, "option"); + tmp != NULL; tmp = find_next(tmp, "option")) { char *s; struct assignment *type = find(tmp->u.assignment, "type"); @@ -726,7 +747,7 @@ main(int argc, char **argv) print_version(NULL); exit(0); } - + if(argc == optidx) usage(1); |
