From 616a835ad21c54a469663b1cc8d2543849d0c646 Mon Sep 17 00:00:00 2001 From: Simon Wilkinson Date: Mon, 18 Apr 2011 08:25:55 +0100 Subject: [PATCH] cmd: Add function to disable positional commands Add a new cmd_DisablePositionalCommands function which can be used to completely disable positional commands, for functions which have no desire to make use of them. Change-Id: I5646e9976e544f06902ea6f85043a6330b1ac2fe Reviewed-on: http://gerrit.openafs.org/4539 Tested-by: BuildBot Reviewed-by: Derrick Brashear --- src/cmd/cmd.c | 9 ++++++++- src/cmd/cmd.p.h | 1 + tests/cmd/command-t.c | 14 ++++++++++---- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/cmd/cmd.c b/src/cmd/cmd.c index a85edd5..829d725 100644 --- a/src/cmd/cmd.c +++ b/src/cmd/cmd.c @@ -28,6 +28,7 @@ static struct cmd_syndesc *allSyntax = 0; static int noOpcodes = 0; static int (*beforeProc) (struct cmd_syndesc * ts, void *beforeRock) = NULL; static int (*afterProc) (struct cmd_syndesc * ts, void *afterRock) = NULL; +static int enablePositional = 1; static void *beforeRock, *afterRock; static char initcmd_opcode[] = "initcmd"; /*Name of initcmd opcode */ @@ -452,6 +453,12 @@ cmd_CreateAlias(struct cmd_syndesc *as, char *aname) return 0; /* all done */ } +void +cmd_DisablePositionalCommands(void) +{ + enablePositional = 0; +} + int cmd_IsAdministratorCommand(struct cmd_syndesc *as) { @@ -779,7 +786,7 @@ cmd_Dispatch(int argc, char **argv) * out of positional mode, and from that point on, expect a switch * before any particular token. */ - positional = 1; /* Are we still in the positional region of the cmd line? */ + positional = enablePositional; /* Accepting positional cmds ? */ i = noOpcodes ? 1 : 2; SetupExpandsFlag(ts); for (; i < argc; i++) { diff --git a/src/cmd/cmd.p.h b/src/cmd/cmd.p.h index 689d9e4..b761230 100644 --- a/src/cmd/cmd.p.h +++ b/src/cmd/cmd.p.h @@ -77,6 +77,7 @@ extern int cmd_FreeArgv(char **argv); extern int cmd_ParseLine(char *aline, char **argv, afs_int32 * an, afs_int32 amaxn); extern int cmd_IsAdministratorCommand(struct cmd_syndesc *as); +extern void cmd_DisablePositionalCommands(void); extern void PrintSyntax(struct cmd_syndesc *as); extern void PrintFlagHelp(struct cmd_syndesc *as); diff --git a/tests/cmd/command-t.c b/tests/cmd/command-t.c index bbd2997..74623d0 100644 --- a/tests/cmd/command-t.c +++ b/tests/cmd/command-t.c @@ -56,7 +56,7 @@ main(int argc, char **argv) int code; int tc; - plan(25); + plan(27); initialize_CMD_error_table(); @@ -99,9 +99,6 @@ main(int argc, char **argv) is_int(CMD_NOTLIST, code, "too many parameters fails as expected"); cmd_FreeArgv(tv); - - - /* Positional parameters */ code = cmd_ParseLine("foo bar -flag", tv, &tc, 100); is_int(0, code, "cmd_ParseLine succeeds"); @@ -123,5 +120,14 @@ main(int argc, char **argv) is_int(CMD_UNKNOWNSWITCH, code, "ambiguous abbreviations correctly fail"); cmd_FreeArgv(tv); + /* Disable positional commands */ + cmd_DisablePositionalCommands(); + code = cmd_ParseLine("foo bar -flag", tv, &tc, 100); + is_int(0, code, "cmd_ParseLine succeeds"); + code = cmd_Dispatch(tc, tv); + is_int(3359746, code, "positional parameters can be disabled"); + cmd_FreeArgv(tv); + return 0; } + -- 1.9.4