shell: Add macros for creating conditional commands

Added macros which can be used to create a command which depends
on compilation flag. Macros are a cleaner alternative to #ifdefs
around command registration and command handler.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2019-02-04 11:38:43 +01:00 committed by Anas Nashif
commit 7e26f53470
3 changed files with 172 additions and 24 deletions

View file

@ -321,6 +321,15 @@ static bool tab_prepare(const struct shell *shell,
return true;
}
/* Empty command is identified by null handler and subcommand but contrary
* to array termination null command, it has non-null syntax address.
*/
static inline bool is_empty_cmd(const struct shell_static_entry *entry)
{
return entry->syntax &&
(entry->handler == NULL) && (entry->subcmd == NULL);
}
static inline bool is_completion_candidate(const char *candidate,
const char *str, size_t len)
{
@ -342,6 +351,9 @@ static void find_completion_candidates(const struct shell_static_entry *cmd,
*cnt = 0;
while (true) {
bool is_empty;
bool is_candidate;
shell_cmd_get(cmd ? cmd->subcmd : NULL, cmd ? 1 : 0,
idx, &candidate, &dynamic_entry);
@ -349,8 +361,10 @@ static void find_completion_candidates(const struct shell_static_entry *cmd,
break;
}
if (is_completion_candidate(candidate->syntax, incompl_cmd,
incompl_cmd_len)) {
is_empty = is_empty_cmd(candidate);
is_candidate = is_completion_candidate(candidate->syntax,
incompl_cmd, incompl_cmd_len);
if (!is_empty && is_candidate) {
size_t slen = strlen(candidate->syntax);
*longest = (slen > *longest) ? slen : *longest;
@ -438,15 +452,17 @@ static void tab_options_print(const struct shell *shell,
tab_item_print(shell, SHELL_INIT_OPTION_PRINTER, longest);
while (cnt) {
bool is_empty;
/* shell->ctx->active_cmd can be safely used outside of command
* context to save stack
*/
shell_cmd_get(cmd ? cmd->subcmd : NULL, cmd ? 1 : 0,
idx, &match, &shell->ctx->active_cmd);
idx++;
if (str && match->syntax &&
!is_completion_candidate(match->syntax, str, str_len)) {
is_empty = is_empty_cmd(match);
if (is_empty || (str && match->syntax &&
!is_completion_candidate(match->syntax, str, str_len))) {
continue;
}