2024-03-26 15:20:25

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ v1 1/2] shared/shell: Add script command

From: Luiz Augusto von Dentz <[email protected]>

This adds script command to main menu which can be used to execute
scripts at any point rather than just at the init.
---
src/shared/shell.c | 186 +++++++++++++++++++++++++++------------------
1 file changed, 110 insertions(+), 76 deletions(-)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index 0e4cbb7b12cb..d68d6798f117 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -258,6 +258,115 @@ static void cmd_export(int argc, char *argv[])
}
}

+static int bt_shell_queue_exec(char *line)
+{
+ int err;
+
+ /* Queue if already executing */
+ if (data.line) {
+ /* Check if prompt is being held then release using the line */
+ if (!bt_shell_release_prompt(line))
+ return 0;
+ queue_push_tail(data.queue, strdup(line));
+ return 0;
+ }
+
+ bt_shell_printf("%s\n", line);
+
+ err = bt_shell_exec(line);
+ if (!err)
+ data.line = strdup(line);
+
+ return err;
+}
+
+static bool input_read(struct io *io, void *user_data)
+{
+ int fd;
+ char *line = NULL;
+ size_t len = 0;
+ ssize_t nread;
+
+ fd = io_get_fd(io);
+
+ if (fd == STDIN_FILENO) {
+ rl_callback_read_char();
+ return true;
+ }
+
+ if (!data.f) {
+ data.f = fdopen(fd, "r");
+ if (!data.f) {
+ printf("fdopen: %s (%d)\n", strerror(errno), errno);
+ return false;
+ }
+ }
+
+ nread = getline(&line, &len, data.f);
+ if (nread > 0) {
+ int err;
+
+ if (line[nread - 1] == '\n')
+ line[nread - 1] = '\0';
+
+ err = bt_shell_queue_exec(line);
+ if (err < 0)
+ printf("%s: %s (%d)\n", line, strerror(-err), -err);
+ } else {
+ fclose(data.f);
+ data.f = NULL;
+ }
+
+ free(line);
+
+ return true;
+}
+
+static bool io_hup(struct io *io, void *user_data)
+{
+ if (queue_remove(data.inputs, io)) {
+ if (!queue_isempty(data.inputs))
+ return false;
+ }
+
+ mainloop_quit();
+
+ return false;
+}
+
+static bool bt_shell_script_attach(int fd)
+{
+ struct io *io;
+
+ io = io_new(fd);
+ if (!io)
+ return false;
+
+ io_set_read_handler(io, input_read, NULL, NULL);
+ io_set_disconnect_handler(io, io_hup, NULL, NULL);
+
+ queue_push_tail(data.inputs, io);
+
+ return true;
+}
+
+static void cmd_script(int argc, char *argv[])
+{
+ int fd;
+
+ fd = open(argv[1], O_RDONLY);
+ if (fd < 0) {
+ printf("Unable to open %s: %s (%d)\n", argv[1],
+ strerror(errno), errno);
+ bt_shell_noninteractive_quit(EXIT_FAILURE);
+ return;
+ }
+
+ printf("Running script %s...\n", argv[1]);
+
+ bt_shell_script_attach(fd);
+}
+
static const struct bt_shell_menu_entry default_menu[] = {
{ "back", NULL, cmd_back, "Return to main menu", NULL,
NULL, cmd_back_exists },
@@ -271,6 +380,7 @@ static const struct bt_shell_menu_entry default_menu[] = {
"Display help about this program" },
{ "export", NULL, cmd_export,
"Print environment variables" },
+ { "script", "<filename>", cmd_script, "Run script" },
{ }
};

@@ -1033,18 +1143,6 @@ static char **shell_completion(const char *text, int start, int end)
return matches;
}

-static bool io_hup(struct io *io, void *user_data)
-{
- if (queue_remove(data.inputs, io)) {
- if (!queue_isempty(data.inputs))
- return false;
- }
-
- mainloop_quit();
-
- return false;
-}
-
static void signal_callback(int signum, void *user_data)
{
static bool terminated = false;
@@ -1304,28 +1402,6 @@ int bt_shell_run(void)
return status;
}

-static int bt_shell_queue_exec(char *line)
-{
- int err;
-
- /* Queue if already executing */
- if (data.line) {
- /* Check if prompt is being held then release using the line */
- if (!bt_shell_release_prompt(line))
- return 0;
- queue_push_tail(data.queue, strdup(line));
- return 0;
- }
-
- bt_shell_printf("%s\n", line);
-
- err = bt_shell_exec(line);
- if (!err)
- data.line = strdup(line);
-
- return err;
-}
-
int bt_shell_exec(const char *input)
{
wordexp_t w;
@@ -1451,48 +1527,6 @@ void bt_shell_set_prompt(const char *string)
rl_redisplay();
}

-static bool input_read(struct io *io, void *user_data)
-{
- int fd;
- char *line = NULL;
- size_t len = 0;
- ssize_t nread;
-
- fd = io_get_fd(io);
-
- if (fd == STDIN_FILENO) {
- rl_callback_read_char();
- return true;
- }
-
- if (!data.f) {
- data.f = fdopen(fd, "r");
- if (!data.f) {
- printf("fdopen: %s (%d)\n", strerror(errno), errno);
- return false;
- }
- }
-
- nread = getline(&line, &len, data.f);
- if (nread > 0) {
- int err;
-
- if (line[nread - 1] == '\n')
- line[nread - 1] = '\0';
-
- err = bt_shell_queue_exec(line);
- if (err < 0)
- printf("%s: %s (%d)\n", line, strerror(-err), -err);
- } else {
- fclose(data.f);
- data.f = NULL;
- }
-
- free(line);
-
- return true;
-}
-
static bool shell_quit(void *data)
{
mainloop_quit();
--
2.44.0



2024-03-26 15:20:28

by Luiz Augusto von Dentz

[permalink] [raw]
Subject: [PATCH BlueZ v1 2/2] shared/shell: Add commands from scripts to history

From: Luiz Augusto von Dentz <[email protected]>

This enables saving the commands run from a script to also be visible in
the history.
---
src/shared/shell.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)

diff --git a/src/shared/shell.c b/src/shared/shell.c
index d68d6798f117..f3f7bab9a616 100644
--- a/src/shared/shell.c
+++ b/src/shared/shell.c
@@ -863,8 +863,6 @@ int bt_shell_release_prompt(const char *input)

static void rl_handler(char *input)
{
- HIST_ENTRY *last;
-
if (!input) {
rl_insert_text("quit");
rl_redisplay();
@@ -879,14 +877,6 @@ static void rl_handler(char *input)
if (!bt_shell_release_prompt(input))
goto done;

- last = history_get(history_length + history_base - 1);
- /* append only if input is different from previous command */
- if (!last || strcmp(input, last->line))
- add_history(input);
-
- if (data.monitor)
- bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input);
-
bt_shell_exec(input);

done:
@@ -1404,12 +1394,21 @@ int bt_shell_run(void)

int bt_shell_exec(const char *input)
{
+ HIST_ENTRY *last;
wordexp_t w;
int err;

if (!input)
return 0;

+ last = history_get(history_length + history_base - 1);
+ /* append only if input is different from previous command */
+ if (!last || strcmp(input, last->line))
+ add_history(input);
+
+ if (data.monitor)
+ bt_log_printf(0xffff, data.name, LOG_INFO, "%s", input);
+
err = wordexp(input, &w, WRDE_NOCMD);
switch (err) {
case WRDE_BADCHAR:
--
2.44.0


2024-03-26 17:52:40

by bluez.test.bot

[permalink] [raw]
Subject: RE: [BlueZ,v1,1/2] shared/shell: Add script command

This is automated email and please do not reply to this email!

Dear submitter,

Thank you for submitting the patches to the linux bluetooth mailing list.
This is a CI test results with your patch series:
PW Link:https://patchwork.kernel.org/project/bluetooth/list/?series=838446

---Test result---

Test Summary:
CheckPatch PASS 1.02 seconds
GitLint PASS 0.67 seconds
BuildEll PASS 24.25 seconds
BluezMake PASS 1633.53 seconds
MakeCheck PASS 13.72 seconds
MakeDistcheck PASS 182.99 seconds
CheckValgrind PASS 252.68 seconds
CheckSmatch WARNING 356.81 seconds
bluezmakeextell PASS 121.18 seconds
IncrementalBuild PASS 3010.22 seconds
ScanBuild WARNING 977.78 seconds

Details
##############################
Test: CheckSmatch - WARNING
Desc: Run smatch tool with source
Output:
src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):src/shared/shell.c: note: in included file (through /usr/include/readline/readline.h):
##############################
Test: ScanBuild - WARNING
Desc: Run Scan Build
Output:
src/shared/shell.c:1331:13: warning: Access to field 'options' results in a dereference of a null pointer (loaded from variable 'opt')
if (c != opt->options[index - offset].val) {
^~~~~~~~~~~~
1 warning generated.



---
Regards,
Linux Bluetooth

2024-03-28 14:41:39

by patchwork-bot+bluetooth

[permalink] [raw]
Subject: Re: [PATCH BlueZ v1 1/2] shared/shell: Add script command

Hello:

This series was applied to bluetooth/bluez.git (master)
by Luiz Augusto von Dentz <[email protected]>:

On Tue, 26 Mar 2024 11:20:11 -0400 you wrote:
> From: Luiz Augusto von Dentz <[email protected]>
>
> This adds script command to main menu which can be used to execute
> scripts at any point rather than just at the init.
> ---
> src/shared/shell.c | 186 +++++++++++++++++++++++++++------------------
> 1 file changed, 110 insertions(+), 76 deletions(-)

Here is the summary with links:
- [BlueZ,v1,1/2] shared/shell: Add script command
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=868d8fc5dcf6
- [BlueZ,v1,2/2] shared/shell: Add commands from scripts to history
https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=e6d849f38be3

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html