shell: Refactor command getters
Refactor and simplified fetching commands from the tree of commands. Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
parent
d45bec1fb7
commit
53b5bae41b
7 changed files with 108 additions and 195 deletions
|
@ -12,7 +12,7 @@ extern const struct shell_cmd_entry __shell_root_cmds_end[0];
|
|||
|
||||
static inline const struct shell_cmd_entry *shell_root_cmd_get(u32_t id)
|
||||
{
|
||||
return &__shell_root_cmds_start[id];
|
||||
return &__shell_root_cmds_start[id];
|
||||
}
|
||||
|
||||
/* Calculates relative line number of given position in buffer */
|
||||
|
@ -225,7 +225,7 @@ static inline u32_t shell_root_cmd_count(void)
|
|||
sizeof(struct shell_cmd_entry);
|
||||
}
|
||||
|
||||
/* Function returning pointer to root command matching requested syntax. */
|
||||
/* Function returning pointer to parent command matching requested syntax. */
|
||||
const struct shell_static_entry *shell_root_cmd_find(const char *syntax)
|
||||
{
|
||||
const size_t cmd_count = shell_root_cmd_count();
|
||||
|
@ -241,46 +241,33 @@ const struct shell_static_entry *shell_root_cmd_find(const char *syntax)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
void shell_cmd_get(const struct shell *shell,
|
||||
const struct shell_cmd_entry *command, size_t lvl,
|
||||
size_t idx, const struct shell_static_entry **entry,
|
||||
struct shell_static_entry *d_entry)
|
||||
const struct shell_static_entry *shell_cmd_get(
|
||||
const struct shell_static_entry *parent,
|
||||
size_t idx,
|
||||
struct shell_static_entry *dloc)
|
||||
{
|
||||
__ASSERT_NO_MSG(entry != NULL);
|
||||
__ASSERT_NO_MSG(d_entry != NULL);
|
||||
__ASSERT_NO_MSG(dloc != NULL);
|
||||
const struct shell_static_entry *res = NULL;
|
||||
|
||||
*entry = NULL;
|
||||
if (parent == NULL) {
|
||||
return (idx < shell_root_cmd_count()) ?
|
||||
shell_root_cmd_get(idx)->u.entry : NULL;
|
||||
}
|
||||
|
||||
if (lvl == SHELL_CMD_ROOT_LVL) {
|
||||
if (shell_in_select_mode(shell)) {
|
||||
const struct shell_static_entry *ptr =
|
||||
shell->ctx->selected_cmd;
|
||||
if (ptr->subcmd->u.entry[idx].syntax != NULL) {
|
||||
*entry = &ptr->subcmd->u.entry[idx];
|
||||
if (parent->subcmd) {
|
||||
if (parent->subcmd->is_dynamic) {
|
||||
parent->subcmd->u.dynamic_get(idx, dloc);
|
||||
if (dloc->syntax != NULL) {
|
||||
res = dloc;
|
||||
}
|
||||
} else {
|
||||
if (parent->subcmd->u.entry[idx].syntax != NULL) {
|
||||
res = &parent->subcmd->u.entry[idx];
|
||||
}
|
||||
} else if (idx < shell_root_cmd_count()) {
|
||||
const struct shell_cmd_entry *cmd;
|
||||
|
||||
cmd = shell_root_cmd_get(idx);
|
||||
*entry = cmd->u.entry;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (command == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (command->is_dynamic) {
|
||||
command->u.dynamic_get(idx, d_entry);
|
||||
if (d_entry->syntax != NULL) {
|
||||
*entry = d_entry;
|
||||
}
|
||||
} else {
|
||||
if (command->u.entry[idx].syntax != NULL) {
|
||||
*entry = &command->u.entry[idx];
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Function returns pointer to a command matching given pattern.
|
||||
|
@ -288,28 +275,26 @@ void shell_cmd_get(const struct shell *shell,
|
|||
* @param shell Shell instance.
|
||||
* @param cmd Pointer to commands array that will be searched.
|
||||
* @param lvl Root command indicator. If set to 0 shell will search
|
||||
* for pattern in root commands.
|
||||
* for pattern in parent commands.
|
||||
* @param cmd_str Command pattern to be found.
|
||||
* @param d_entry Shell static command descriptor.
|
||||
* @param dloc Shell static command descriptor.
|
||||
*
|
||||
* @return Pointer to found command.
|
||||
*/
|
||||
static const struct shell_static_entry *find_cmd(
|
||||
const struct shell *shell,
|
||||
const struct shell_cmd_entry *cmd,
|
||||
size_t lvl,
|
||||
char *cmd_str,
|
||||
struct shell_static_entry *d_entry)
|
||||
const struct shell *shell,
|
||||
const struct shell_static_entry *parent,
|
||||
char *cmd_str,
|
||||
struct shell_static_entry *dloc)
|
||||
{
|
||||
const struct shell_static_entry *entry = NULL;
|
||||
const struct shell_static_entry *entry;
|
||||
size_t idx = 0;
|
||||
|
||||
do {
|
||||
shell_cmd_get(shell, cmd, lvl, idx++, &entry, d_entry);
|
||||
if (entry && (strcmp(cmd_str, entry->syntax) == 0)) {
|
||||
while ((entry = shell_cmd_get(parent, idx++, dloc)) != NULL) {
|
||||
if (strcmp(cmd_str, entry->syntax) == 0) {
|
||||
return entry;
|
||||
}
|
||||
} while (entry);
|
||||
};
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
@ -319,12 +304,11 @@ const struct shell_static_entry *shell_get_last_command(
|
|||
size_t argc,
|
||||
char *argv[],
|
||||
size_t *match_arg,
|
||||
struct shell_static_entry *d_entry,
|
||||
struct shell_static_entry *dloc,
|
||||
bool only_static)
|
||||
{
|
||||
const struct shell_static_entry *prev_entry = NULL;
|
||||
const struct shell_static_entry *entry = NULL;
|
||||
const struct shell_cmd_entry *cmd = NULL;
|
||||
const struct shell_static_entry *entry = shell->ctx->selected_cmd;
|
||||
|
||||
*match_arg = SHELL_CMD_ROOT_LVL;
|
||||
|
||||
|
@ -338,21 +322,16 @@ const struct shell_static_entry *shell_get_last_command(
|
|||
}
|
||||
}
|
||||
|
||||
entry = find_cmd(shell, cmd, *match_arg, argv[*match_arg],
|
||||
d_entry);
|
||||
prev_entry = entry;
|
||||
entry = find_cmd(shell, entry, argv[*match_arg], dloc);
|
||||
if (entry) {
|
||||
cmd = entry->subcmd;
|
||||
prev_entry = entry;
|
||||
(*match_arg)++;
|
||||
} else {
|
||||
entry = prev_entry;
|
||||
break;
|
||||
}
|
||||
|
||||
if (cmd == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if (only_static && cmd->is_dynamic) {
|
||||
if (only_static && (entry == dloc)) {
|
||||
(*match_arg)--;
|
||||
return NULL;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue