shell: Fix command lookup for non-default modules

The code was completely broken wrt command lookups when the command is
given in the format "<module> <cmd> <args...>". It would only work if
the default module is not set, which is almost never the case (as most
apps set it explicitly).

Refactor the command handling by moving more logic up to shell_exec(),
so that get_cb() does a lookup for a single module

Signed-off-by: Johan Hedberg <johan.hedberg@intel.com>
This commit is contained in:
Johan Hedberg 2017-11-15 09:04:24 +02:00 committed by Johan Hedberg
commit f9e30ed582

View file

@ -299,35 +299,12 @@ static int exit_module(int argc, char *argv[])
return 0;
}
static shell_cmd_function_t get_cb(int argc, char *argv[], int *module)
static shell_cmd_function_t get_cb(int module, const char *command)
{
const struct shell_module *shell_module;
const char *command;
int i;
if (!strcmp(argv[0], "help")) {
return show_help;
}
if (!strcmp(argv[0], "select")) {
return select_module;
}
if (!strcmp(argv[0], "exit")) {
return exit_module;
}
if ((argc == 1) && (default_module == -1)) {
printk("Missing parameter\n");
return NULL;
}
command = get_command_and_module(argv, module);
if ((*module == -1) || (command == NULL)) {
return NULL;
}
shell_module = &__shell_cmd_start[*module];
shell_module = &__shell_cmd_start[module];
for (i = 0; shell_module->commands[i].cmd_name; i++) {
if (!strcmp(command, shell_module->commands[i].cmd_name)) {
return shell_module->commands[i].cb;
@ -343,20 +320,61 @@ static inline void print_cmd_unknown(char *argv)
printk("Type 'help' for list of available commands\n");
}
static shell_cmd_function_t get_internal(const char *command)
{
if (!strcmp(command, "help")) {
return show_help;
}
if (!strcmp(command, "select")) {
return select_module;
}
if (!strcmp(command, "exit")) {
return exit_module;
}
return NULL;
}
int shell_exec(char *line)
{
char *argv[ARGC_MAX + 1];
int argc;
int module = default_module;
int err;
char *argv[ARGC_MAX + 1], **argv_start = argv;
shell_cmd_function_t cb;
int argc, err;
argc = line2argv(line, argv, ARRAY_SIZE(argv));
if (!argc) {
return -EINVAL;
}
cb = get_cb(argc, argv, &module);
cb = get_internal(argv[0]);
if (cb) {
return cb(argc, argv_start);
}
if (argc == 1 && default_module == -1) {
printk("No module selected. Use 'select' or 'help'.\n");
return -EINVAL;
}
if (default_module != -1) {
cb = get_cb(default_module, argv[0]);
}
if (!cb && argc > 1) {
int module;
module = get_destination_module(argv[0]);
if (module != -1) {
cb = get_cb(module, argv[1]);
if (cb) {
argc--;
argv_start++;
}
}
}
if (!cb) {
if (app_cmd_handler != NULL) {
cb = app_cmd_handler;
@ -366,14 +384,7 @@ int shell_exec(char *line)
}
}
/* Execute callback with arguments */
if (module != -1 && module != default_module) {
/* Ajust parameters to point to the actual command */
err = cb(argc - 1, &argv[1]);
} else {
err = cb(argc, argv);
}
err = cb(argc, argv_start);
if (err < 0) {
show_cmd_help(argv, false);
}