From 78c0e3b0efffe19c33bd0467872d65af43e5a47e Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Mon, 30 May 2011 20:02:31 +0100 Subject: [PATCH] cmd: Add support for disabling specific abbrevs Sometimes, when adding a new command parameter, it's necessary to prevent it from colliding with an existing abbreviation. This patch adds a new command flag CMD_NOABBRV which can be set on a parameter to indicate that it should not be considered when checking for ambiguous abbreviations. For example, if a command has the existing '-cell' option which is popularly abbreviated to '-c', adding a '-config' option would cause the existing abbreviation to stop working. However, if '-config' is added with the NOABBRV flag set, '-c' will continue to work. Change-Id: I3b6d718f9dd81c44fb1d10c904db6a4a0fd763b8 Reviewed-on: http://gerrit.openafs.org/4810 Tested-by: BuildBot Reviewed-by: Jeffrey Altman Reviewed-by: Derrick Brashear --- src/cmd/cmd.c | 7 +++++-- src/cmd/cmd.p.h | 1 + tests/cmd/command-t.c | 16 +++++++++++++++- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index d0e634102c..d67a816a7f 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -101,8 +101,11 @@ FindType(struct cmd_syndesc *as, char *aname) alias = alias->next; } - /* A hidden option must be a full match (no best matches) */ - if (as->parms[i].flags & CMD_HIDE || !enableAbbreviation) + /* A hidden option, or one which cannot be abbreviated, + * must be a full match (no best matches) */ + if (as->parms[i].flags & CMD_HIDE || + as->parms[i].flags & CMD_NOABBRV || + !enableAbbreviation) continue; if (strncmp(as->parms[i].name, aname, cmdlen) == 0) { diff --git a/src/cmd/cmd.p.h b/src/cmd/cmd.p.h index 3193d56deb..d7c58fe174 100644 --- a/src/cmd/cmd.p.h +++ b/src/cmd/cmd.p.h @@ -45,6 +45,7 @@ struct cmd_parmdesc { #define CMD_EXPANDS 2 /* if list, try to eat tokens through eoline, instead of just 1 */ #define CMD_HIDE 4 /* A hidden option */ #define CMD_PROCESSED 8 +#define CMD_NOABBRV 16 /* Abbreviation not supported */ struct cmd_syndesc { struct cmd_syndesc *next; /* next one in system list */ diff --git a/tests/cmd/command-t.c b/tests/cmd/command-t.c index 846a8543dd..2c9fae470e 100644 --- a/tests/cmd/command-t.c +++ b/tests/cmd/command-t.c @@ -38,6 +38,7 @@ #define FLAG_OFF 0 #define FIRST_OFF 1 #define SECOND_OFF 2 +#define SUGAR_OFF 3 #define FOURTH_OFF 4 #define FIFTH_OFF 5 #define PERHAPS_OFF 6 @@ -67,7 +68,7 @@ main(int argc, char **argv) int retval; char *retstring = NULL; - plan(79); + plan(85); initialize_CMD_error_table(); @@ -154,6 +155,19 @@ main(int argc, char **argv) is_int(CMD_UNKNOWNSWITCH, code, "and fail with cmd_Parse too"); cmd_FreeArgv(tv); + /* Check that paramaters with abbreviation disabled don't make things + * ambiguous */ + cmd_AddParmAtOffset(opts, "-sugar", CMD_SINGLE, CMD_OPTIONAL | CMD_NOABBRV, + "sugar with that", SUGAR_OFF); + code = cmd_ParseLine("-fi foo -s bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(0, code, "disabling specific abbreviations succeeds"); + code = cmd_Parse(tc, tv, &retopts); + is_int(0, code, "and works with cmd_Parse into the bargain"); + cmd_FreeOptions(&retopts); + cmd_FreeArgv(tv); + /* Disable positional commands */ cmd_DisablePositionalCommands(); code = cmd_ParseLine("foo bar -flag", tv, &tc, 100);