diff --git a/subsys/shell/shell.c b/subsys/shell/shell.c index 7d9bde8c967..7016ef6e6dc 100644 --- a/subsys/shell/shell.c +++ b/subsys/shell/shell.c @@ -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); }