shell: Refactor shell argument count validation

Shell arguments structure was stored as a pointer in shell structure
and NULL pointer indicated that argument count checking is skipped.
It has been reworked to hold the structure (2 bytes) in the shell
structure with mandatory=0 skipping the check. This approach is cpp
friendly, contrary to the legacy one.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2019-02-12 14:09:19 +01:00 committed by Anas Nashif
parent 138f485c1e
commit 349dd964e0
2 changed files with 8 additions and 19 deletions

View file

@ -110,7 +110,7 @@ struct shell_static_entry {
const char *help; /*!< Command help string. */
const struct shell_cmd_entry *subcmd; /*!< Pointer to subcommand. */
shell_cmd_handler handler; /*!< Command handler. */
const struct shell_static_args *args; /*!< Command arguments. */
struct shell_static_args args; /*!< Command arguments. */
};
/**
@ -213,8 +213,7 @@ struct shell_static_entry {
.subcmd = _subcmd, \
.help = (const char *)_help, \
.handler = _handler, \
.args = _mandatory ? \
(&(struct shell_static_args) SHELL_ARG(_mandatory, _optional)) : NULL \
.args = SHELL_ARG(_mandatory, _optional) \
}
/**

View file

@ -535,23 +535,13 @@ static int exec_cmd(const struct shell *shell, size_t argc, char **argv,
}
}
if (shell->ctx->active_cmd.args) {
const struct shell_static_args *args;
if (shell->ctx->active_cmd.args.mandatory) {
u8_t mand = shell->ctx->active_cmd.args.mandatory;
u8_t opt = shell->ctx->active_cmd.args.optional;
bool in_range = (argc >= mand) && (argc <= (mand + opt));
args = shell->ctx->active_cmd.args;
if (args->optional > 0) {
/* Check if argc is within allowed range */
ret_val = cmd_precheck(shell,
((argc >= args->mandatory)
&&
(argc <= args->mandatory +
args->optional)));
} else {
/* Perform exact match if there are no optional args */
ret_val = cmd_precheck(shell,
(args->mandatory == argc));
}
/* Check if argc is within allowed range */
ret_val = cmd_precheck(shell, in_range);
}
if (!ret_val) {