From 3fbef999279bb581aebeb83da7871072e93a4943 Mon Sep 17 00:00:00 2001 From: NeilBrown Date: Sun, 13 Mar 2011 14:10:31 +1100 Subject: [PATCH] Discard 'terminal' arg type. At one stage during development, that 'tag' of a positional arg was NULL so we couldn't used that to mark end-of-list, so we used a special 'type' instead. But now 'tag' is always non-NULL we can used tag==NULL to mark end of list, so get rid of the 'terminal' type. Fix some bugs too... Signed-off-by: NeilBrown --- tools/lafs.c | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/tools/lafs.c b/tools/lafs.c index b3d8367..5ffad7e 100644 --- a/tools/lafs.c +++ b/tools/lafs.c @@ -71,7 +71,7 @@ struct state { * subcommand: one of a list of subcommands. * Any unique prefix of a tag is allowed to match. */ -enum argtype { flag, opaque, choice, external, internal, subcommand, terminal }; +enum argtype { flag, opaque, choice, external, internal, subcommand}; static struct args { char *tag; enum argtype type; @@ -79,7 +79,7 @@ static struct args { union { struct cmd *subcmd; char **options; }; char *desc; } lafs_args[]; -#define TERMINAL_ARG {NULL, terminal, 0, {NULL}, NULL} +#define TERMINAL_ARG {NULL, opaque, 0, {NULL}, NULL} /* When a positional parameter is identified as 'subcommand' it is associated * with a list of 'struct cmd' identifying the possible subcommands. @@ -126,7 +126,7 @@ static int find_tag(struct args *args, const char *tag, int len) int i; int best = -1; - for (i = 0; args[i].type != terminal; i++) + for (i = 0; args[i].tag; i++) if (args[i].tag[0] == '-') { if (strncmp(tag, args[i].tag+1, len) != 0) continue; @@ -233,7 +233,7 @@ static void **parse_line(struct args **argsp, char *line, int *offsetp, if (lastp) *lastp = -1; - for (i = 0; args[i].type != terminal; i++) + for (i = 0; args[i].tag; i++) ; rv = calloc(i+offset, sizeof(char*)); size = i+offset; @@ -269,11 +269,11 @@ static void **parse_line(struct args **argsp, char *line, int *offsetp, } else { /* must be next positional */ for (i=0; - args[i].tag[0] != '-' && args[i].type != terminal; + args[i].tag && args[i].tag[0] != '-'; i++) if (rv[i+offset] == NULL) break; - if (args[i].tag[0] == '-' || args[i].type == terminal) { + if (args[i].tag == NULL || args[i].tag[0] == '-') { /* No positions left */ asprintf(error, "Extra positional parameter: %s", w); break; @@ -301,7 +301,7 @@ static void **parse_line(struct args **argsp, char *line, int *offsetp, offset += i+1; if (lastp) *lastp = -1; - for (i = 0; args[i].type != terminal; i++) + for (i = 0; args[i].tag; i++) ; rv = realloc(rv, (i+offset) * sizeof(void*)); while (size < i + offset) { @@ -463,7 +463,7 @@ static char *tag_gen(const char *prefix, int state) prefix++; len = strlen(prefix); - for ( ; gen_args[next].type != terminal; next++) { + for ( ; gen_args[next].tag; next++) { char *c; if (gen_args[next].tag[0] != '-') continue; @@ -554,10 +554,10 @@ static char **complete_in_context(const char *prefix, int start, int end) p = last; else { last = -1; - for (p = 0; args[p].tag[0] != '-' && args[p].pos != terminal; p++) + for (p = 0; args[p].tag && args[p].tag[0] != '-' ; p++) if (arglist[p+offset] == NULL) break; - if (args[p].tag[0] == '-' || args[p].pos == terminal) + if (args[p].tag == NULL || args[p].tag[0] == '-') p = -1; } /* 'p' is the arg we expect here, either first positional arg that @@ -610,6 +610,7 @@ after_message: * Here are the commands. * Each command X must define * static char help_X = "One line of description for the command"; + * static struct args args_X[] = { list of arg definitions; TERMINAL_ARG}; * static void c_X(struct state *st, void *args) { implement command ;} * and must be listed in lafs_cmds below. @@ -652,10 +653,10 @@ static void c_help(struct state *st, void **args) if (cmd) { printf("%s: %s\n", cmd->name, cmd->help); - if (cmd->args[0].type != terminal) { + if (cmd->args[0].tag) { int i; printf(" Usage: %s", cmd->name); - for (i=0; cmd->args[i].type != terminal; i++) { + for (i=0; cmd->args[i].tag; i++) { struct args *a = cmd->args+i; if (a->tag[0] == '-') { printf(" [--options...]"); @@ -665,7 +666,7 @@ static void c_help(struct state *st, void **args) } printf("\n"); printf(" Arguments:\n"); - for (i=0; cmd->args[i].type != terminal; i++) { + for (i=0; cmd->args[i].tag; i++) { struct args *a = cmd->args+i; if (a->tag[0] != '-') { printf(" %-15s: %s\n", a->tag, a->desc); -- 2.39.5