Skip to contentSkip to conversation

shared/shell: Add support for comments on scripts

PATCH In review Luiz Augusto von Dentz 3 participants 4 messages Aug 7, 2024 – Aug 9, 2024 Bluetooth drivers

lore
Share & tools
lore

Luiz Augusto von Dentz

author maintainer
#0 lore raw

PATCH 1/2 shared/shell: Add support for comments on scripts

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

This adds support for entering comments onscripts by starting the line '#'.

---
 src/shared/shell.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
src/shared/shell.c +6 -1
index 88ecaa076adc..932dd7dd4a52 100644
@@ -262,6 +262,10 @@ static int bt_shell_queue_exec(char *line)
 {
 	int err;
 
+	/* Ignore comments */
+	if (line[0] == '#')
+		return 0;
+
 	/* Queue if already executing */
 	if (data.line) {
 		/* Check if prompt is being held then release using the line */
@@ -876,7 +880,8 @@ static void rl_handler(char *input)
 		return;
 	}
 
-	if (!strlen(input))
+	/* Ignore empty/comment lines */
+	if (!strlen(input) || input[0] == '#')
 		goto done;
 
 	if (!bt_shell_release_prompt(input))
Signature · 1 line
2.45.2
author maintainer
#1 lore raw

PATCH 2/2 shared/shell: Add support for running script command as part of script

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

This allows running script command as part of another script.

---
 src/shared/shell.c | 89 +++++++++++++++++++++++++++++++---------------
 1 file changed, 60 insertions(+), 29 deletions(-)
src/shared/shell.c +60 -29
index 932dd7dd4a52..b3c343277ffd 100644
@@ -60,6 +60,11 @@ struct bt_shell_prompt_input {
 	void *user_data;
 };
 
+struct input {
+	struct io *io;
+	FILE *f;
+};
+
 static struct {
 	bool init;
 	char *name;
@@ -71,7 +76,6 @@ static struct {
 	bool monitor;
 	int timeout;
 	int init_fd;
-	FILE *f;
 	struct queue *inputs;
 
 	char *line;
@@ -269,8 +273,11 @@ static int bt_shell_queue_exec(char *line)
 	/* Queue if already executing */
 	if (data.line) {
 		/* Check if prompt is being held then release using the line */
-		if (!bt_shell_release_prompt(line))
+		if (!bt_shell_release_prompt(line)) {
+			bt_shell_printf("%s\n", line);
 			return 0;
+		}
+
 		queue_push_tail(data.queue, strdup(line));
 		return 0;
 	}
@@ -286,6 +293,7 @@ static int bt_shell_queue_exec(char *line)
 
 static bool input_read(struct io *io, void *user_data)
 {
+	struct input *input = user_data;
 	int fd;
 	char *line = NULL;
 	size_t len = 0;
@@ -303,15 +311,15 @@ static bool input_read(struct io *io, void *user_data)
 		return true;
 	}
 
-	if (!data.f) {
-		data.f = fdopen(fd, "r");
-		if (!data.f) {
+	if (!input->f) {
+		input->f = fdopen(fd, "r");
+		if (!input->f) {
 			printf("fdopen: %s (%d)\n", strerror(errno), errno);
 			return false;
 		}
 	}
 
-	nread = getline(&line, &len, data.f);
+	nread = getline(&line, &len, input->f);
 	if (nread > 0) {
 		int err;
 
@@ -321,9 +329,9 @@ static bool input_read(struct io *io, void *user_data)
 		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;
+	} else if (input->f) {
+		fclose(input->f);
+		input->f = NULL;
 	}
 
 	free(line);
@@ -331,9 +339,9 @@ static bool input_read(struct io *io, void *user_data)
 	return true;
 }
 
-static bool io_hup(struct io *io, void *user_data)
+static bool input_hup(struct io *io, void *user_data)
 {
-	if (queue_remove(data.inputs, io)) {
+	if (queue_remove(data.inputs, user_data)) {
 		if (!queue_isempty(data.inputs))
 			return false;
 	}
@@ -343,18 +351,33 @@ static bool io_hup(struct io *io, void *user_data)
 	return false;
 }
 
-static bool bt_shell_script_attach(int fd)
+static struct input *input_new(int fd)
 {
+	struct input *input;
 	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);
+	input = new0(struct input, 1);
+	input->io = io;
 
-	queue_push_tail(data.inputs, io);
+	queue_push_tail(data.inputs, input);
+
+	return input;
+}
+
+static bool bt_shell_input_attach(int fd)
+{
+	struct input *input;
+
+	input = input_new(fd);
+	if (!input)
+		return false;
+
+	io_set_read_handler(input->io, input_read, input, NULL);
+	io_set_disconnect_handler(input->io, input_hup, input, NULL);
 
 	return true;
 }
@@ -373,7 +396,7 @@ static void cmd_script(int argc, char *argv[])
 
 	printf("Running script %s...\n", argv[1]);
 
-	bt_shell_script_attach(fd);
+	bt_shell_input_attach(fd);
 }
 
 static const struct bt_shell_menu_entry default_menu[] = {
@@ -1449,6 +1472,17 @@ int bt_shell_exec(const char *input)
 	return err;
 }
 
+static void input_destroy(void *data)
+{
+	struct input *input = data;
+
+	if (input->f)
+		fclose(input->f);
+
+	io_destroy(input->io);
+	free(input);
+}
+
 void bt_shell_cleanup(void)
 {
 	bt_shell_release_prompt("");
@@ -1464,7 +1498,7 @@ void bt_shell_cleanup(void)
 
 	rl_cleanup();
 
-	queue_destroy(data.inputs, NULL);
+	queue_destroy(data.inputs, input_destroy);
 	data.inputs = NULL;
 	queue_destroy(data.queue, free);
 	data.queue = NULL;
@@ -1529,14 +1563,14 @@ void bt_shell_set_prompt(const char *string)
 	if (!data.init || data.mode)
 		return;
 
-	if (asprintf(&prompt, "\001%s\002", string) < 0)
+	if (asprintf(&prompt, "\001%s\002", string) < 0) {
 		rl_set_prompt(string);
-	else {
+	} else {
 		rl_set_prompt(prompt);
 		free(prompt);
 	}
 
-	rl_redisplay();
+	rl_forced_update_display();
 }
 
 static bool shell_quit(void *data)
@@ -1548,19 +1582,17 @@ static bool shell_quit(void *data)
 
 bool bt_shell_attach(int fd)
 {
-	struct io *io;
+	struct input *input;
 
-	io = io_new(fd);
-	if (!io)
+	input = input_new(fd);
+	if (!input)
 		return false;
 
 	if (!data.mode) {
-		io_set_read_handler(io, input_read, NULL, NULL);
-		io_set_disconnect_handler(io, io_hup, NULL, NULL);
+		io_set_read_handler(input->io, input_read, input, NULL);
+		io_set_disconnect_handler(input->io, input_hup, input, NULL);
 	}
 
-	queue_push_tail(data.inputs, io);
-
 	if (data.mode) {
 		if (shell_exec(data.argc, data.argv) < 0) {
 			bt_shell_noninteractive_quit(EXIT_FAILURE);
@@ -1586,8 +1618,7 @@ bool bt_shell_detach(void)
 	if (queue_isempty(data.inputs))
 		return false;
 
-	queue_remove_all(data.inputs, NULL, NULL,
-					(queue_destroy_func_t) io_destroy);
+	queue_remove_all(data.inputs, NULL, NULL, input_destroy);
 
 	return true;
 }
Signature · 1 line
2.45.2
#2 lore raw
In reply to Luiz Augusto von Dentz

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=877581

---Test result---

Test Summary:

CheckPatch                    PASS      0.96 seconds
GitLint                       FAIL      0.81 seconds
BuildEll                      PASS      24.48 seconds
BluezMake                     PASS      1618.00 seconds
MakeCheck                     PASS      12.90 seconds
MakeDistcheck                 PASS      175.25 seconds
CheckValgrind                 PASS      249.80 seconds
CheckSmatch                   WARNING   351.45 seconds
bluezmakeextell               PASS      118.40 seconds
IncrementalBuild              PASS      2856.06 seconds
ScanBuild                     PASS      985.78 seconds

Details
##############################
Test: GitLint - FAIL
Desc: Run gitlint
Output:
[BlueZ,v1,2/2] shared/shell: Add support for running script command as part of script

WARNING: I3 - ignore-body-lines: gitlint will be switching from using Python regex 'match' (match beginning) to 'search' (match anywhere) semantics. Please review your ignore-body-lines.regex option accordingly. To remove this warning, set general.regex-style-search=True. More details: https://jorisroovers.github.io/gitlint/configuration/#regex-style-search
1: T1 Title exceeds max length (85>80): "[BlueZ,v1,2/2] shared/shell: Add support for running script command as part of script"
##############################
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):

---
Regards,
Linux Bluetooth

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

Hello:

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

Quoted 7 lines · On Wed, 7 Aug 2024 17:28:17 -0400 you wrote:

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

This adds support for entering comments onscripts by starting the line '#'.

---
 src/shared/shell.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

Here is the summary with links:
- [BlueZ,v1,1/2] shared/shell: Add support for comments on scripts

    https://git.kernel.org/pub/scm/bluetooth/bluez.git/?id=66044ecd8dd9

- [BlueZ,v1,2/2] shared/shell: Add support for running script command as part of script (no matching commit)

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html