This is an automated email from the ASF dual-hosted git repository.
jerzy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git
The following commit(s) were added to refs/heads/master by this push:
new 1255073 sys/shell: Add full command path handling
1255073 is described below
commit 1255073bd137daecc45fb5598f31b0557e669aba
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Tue Oct 22 15:26:07 2019 +0200
sys/shell: Add full command path handling
When module is selected with 'select' command commands
from other modules can't be executed.
They can be execute when no module is selected or correct module is
selected.
To execute tasks command from compat module those lines would work:
shell> comapt tasks
compat> tasks
but once other module is selected tasks can't be executed without
leaving btshell module.
With this change commands from other modules can executed if
module name is prefixed with /
btshell> /compat tasks
Auto completion also works with /
---
sys/shell/src/shell.c | 45 ++++++++++++++++++++++++++++++++++-----------
1 file changed, 34 insertions(+), 11 deletions(-)
diff --git a/sys/shell/src/shell.c b/sys/shell/src/shell.c
index e45d009..f14e423 100644
--- a/sys/shell/src/shell.c
+++ b/sys/shell/src/shell.c
@@ -160,6 +160,8 @@ get_destination_module(const char *module_str, int len)
static const char *
get_command_and_module(char *argv[], int *module, struct streamer *streamer)
{
+ int def_module = default_module;
+ const char *first_arg = argv[0];
*module = -1;
if (!argv[0]) {
@@ -167,13 +169,18 @@ get_command_and_module(char *argv[], int *module, struct
streamer *streamer)
return NULL;
}
- if (default_module == -1) {
+ if (first_arg[0] == '/') {
+ first_arg++;
+ def_module = -1;
+ }
+
+ if (def_module == -1) {
if (!argv[1] || argv[1][0] == '\0') {
streamer_printf(streamer, "Unrecognized command: %s\n", argv[0]);
return NULL;
}
- *module = get_destination_module(argv[0], -1);
+ *module = get_destination_module(first_arg, -1);
if (*module == -1) {
streamer_printf(streamer, "Illegal module %s\n", argv[0]);
return NULL;
@@ -182,7 +189,7 @@ get_command_and_module(char *argv[], int *module, struct
streamer *streamer)
return argv[1];
}
- *module = default_module;
+ *module = def_module;
return argv[0];
}
@@ -358,6 +365,7 @@ shell_find_cmd(int argc, char *argv[], struct streamer
*streamer)
{
const char *first_string = argv[0];
int module = -1;
+ int def_module = default_module;
const struct shell_module *shell_module;
const char *command;
int i;
@@ -367,6 +375,11 @@ shell_find_cmd(int argc, char *argv[], struct streamer
*streamer)
return NULL;
}
+ if (first_string[0] == '/') {
+ first_string++;
+ def_module = -1;
+ }
+
if (!strcmp(first_string, "help")) {
return &shell_cmd_help;
}
@@ -375,7 +388,7 @@ shell_find_cmd(int argc, char *argv[], struct streamer
*streamer)
return &shell_cmd_select_module;
}
- if ((argc == 1) && (default_module == -1)) {
+ if ((argc == 1) && (def_module == -1)) {
streamer_printf(streamer, "Missing parameter\n");
return NULL;
}
@@ -401,6 +414,7 @@ shell_exec(int argc, char **argv, struct streamer *streamer)
const struct shell_cmd *cmd;
size_t argc_offset = 0;
int rc;
+ int def_module = default_module;
cmd = shell_find_cmd(argc, argv, streamer);
if (!cmd) {
@@ -415,10 +429,13 @@ shell_exec(int argc, char **argv, struct streamer
*streamer)
}
}
+ if (argv[0][0] == '/') {
+ def_module = -1;
+ }
/* Allow invoking a cmd with module name as a prefix; a command should
* not know how it was invoked (with or without prefix)
*/
- if (default_module == -1 && cmd != &shell_cmd_select_module &&
+ if (def_module == -1 && cmd != &shell_cmd_select_module &&
cmd != &shell_cmd_help) {
argc_offset = 1;
}
@@ -811,6 +828,7 @@ completion(char *line, console_append_char_cb append_char)
int tok_len;
int module, command;
int null_terminated = 0;
+ int def_module = default_module;
/*
* line to completion is not ended by '\0' as the line that gets from
@@ -822,14 +840,19 @@ completion(char *line, console_append_char_cb append_char)
cur = line;
tok_len = get_token(&cur, &null_terminated);
+ if (tok_len > 0 && cur[0] == '/') {
+ def_module = -1;
+ tok_len--;
+ cur++;
+ }
/* empty token - print options */
if (tok_len == 0) {
console_out('\n');
- if (default_module == -1) {
+ if (def_module == -1) {
print_modules(streamer_console_get());
} else {
- print_module_commands(default_module, streamer_console_get());
+ print_module_commands(def_module, streamer_console_get());
}
print_prompt(line);
return;
@@ -837,12 +860,12 @@ completion(char *line, console_append_char_cb append_char)
/* token can be completed */
if (null_terminated) {
- if (default_module == -1) {
+ if (def_module == -1) {
complete_module(line, cur, tok_len, append_char);
return;
}
complete_command(line, cur, tok_len,
- default_module, append_char);
+ def_module, append_char);
return;
}
@@ -851,8 +874,8 @@ completion(char *line, console_append_char_cb append_char)
return;
}
- if (default_module != -1) {
- module = default_module;
+ if (def_module != -1) {
+ module = def_module;
} else {
module = get_destination_module(cur, tok_len);