From 463d0a37715b042aec441e022a1528eade643315 Mon Sep 17 00:00:00 2001 From: Luiz Augusto von Dentz Date: Mon, 28 Feb 2022 15:58:29 -0800 Subject: [PATCH] shared/shell: Introduce bt_shell_exec This introduces bt_shell_exec which can be used to inject commands into a bt_shell without using stdin/user input. Signed-off-by: Manika Shrivastava Signed-off-by: Ayush Garg --- src/shared/shell.c | 34 ++++++++++++++++++++++++---------- src/shared/shell.h | 1 + 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/shared/shell.c b/src/shared/shell.c index e65fee2..e92b160 100644 --- a/src/shared/shell.c +++ b/src/shared/shell.c @@ -677,7 +677,6 @@ int bt_shell_release_prompt(const char *input) static void rl_handler(char *input) { - wordexp_t w; HIST_ENTRY *last; if (!input) { @@ -702,16 +701,8 @@ static void rl_handler(char *input) if (data.monitor) bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input); - if (wordexp(input, &w, WRDE_NOCMD)) - goto done; - - if (w.we_wordc == 0) { - wordfree(&w); - goto done; - } + bt_shell_exec(input); - shell_exec(w.we_wordc, w.we_wordv); - wordfree(&w); done: free(input); } @@ -1177,6 +1168,29 @@ int bt_shell_run(void) return status; } +int bt_shell_exec(const char *input) +{ + wordexp_t w; + int err; + + if (!input) + return 0; + + if (wordexp(input, &w, WRDE_NOCMD)) + return -ENOEXEC; + + if (w.we_wordc == 0) { + wordfree(&w); + return -ENOEXEC; + } + + err = shell_exec(w.we_wordc, w.we_wordv); + + wordfree(&w); + + return err; +} + void bt_shell_cleanup(void) { bt_shell_release_prompt(""); diff --git a/src/shared/shell.h b/src/shared/shell.h index cc4f822..8baa285 100644 --- a/src/shared/shell.h +++ b/src/shared/shell.h @@ -55,6 +55,7 @@ struct bt_shell_opt { void bt_shell_init(int argc, char **argv, const struct bt_shell_opt *opt); int bt_shell_run(void); +int bt_shell_exec(const char *input); void bt_shell_quit(int status); void bt_shell_noninteractive_quit(int status); -- 2.7.4