public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH BlueZ] shared/shell: Introduce bt_shell_exec
@ 2022-03-01  0:00 Luiz Augusto von Dentz
  2022-03-01  1:35 ` [BlueZ] " bluez.test.bot
  0 siblings, 1 reply; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2022-03-01  0:00 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This introduces bt_shell_exec which can be used to inject commands into
a bt_shell without using stdin/user input.
---
 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 cbb9b8b88..0d82bc282 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -678,7 +678,6 @@ int bt_shell_release_prompt(const char *input)
 
 static void rl_handler(char *input)
 {
-	wordexp_t w;
 	HIST_ENTRY *last;
 
 	if (!input) {
@@ -703,16 +702,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);
 }
@@ -1178,6 +1169,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 cc4f822fb..8baa2854a 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.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread
* [PATCH BlueZ] shared/shell: Introduce bt_shell_exec
@ 2022-03-01 23:30 Luiz Augusto von Dentz
  2022-03-02  0:44 ` [BlueZ] " bluez.test.bot
  0 siblings, 1 reply; 4+ messages in thread
From: Luiz Augusto von Dentz @ 2022-03-01 23:30 UTC (permalink / raw)
  To: linux-bluetooth

From: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

This introduces bt_shell_exec which can be used to inject commands into
a bt_shell without using stdin/user input.
---
 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 cbb9b8b88..0d82bc282 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -678,7 +678,6 @@ int bt_shell_release_prompt(const char *input)
 
 static void rl_handler(char *input)
 {
-	wordexp_t w;
 	HIST_ENTRY *last;
 
 	if (!input) {
@@ -703,16 +702,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);
 }
@@ -1178,6 +1169,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 cc4f822fb..8baa2854a 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.35.1


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-03-02 23:58 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-01  0:00 [PATCH BlueZ] shared/shell: Introduce bt_shell_exec Luiz Augusto von Dentz
2022-03-01  1:35 ` [BlueZ] " bluez.test.bot
  -- strict thread matches above, loose matches on Subject: below --
2022-03-01 23:30 [PATCH BlueZ] " Luiz Augusto von Dentz
2022-03-02  0:44 ` [BlueZ] " bluez.test.bot
2022-03-02 23:22   ` Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox