shared/shell: Allow script command to be used within scripts
authorLuiz Augusto von Dentz <luiz.von.dentz@intel.com>
Wed, 25 Sep 2024 18:22:53 +0000 (14:22 -0400)
committerWootak Jung <wootak.jung@samsung.com>
Thu, 20 Feb 2025 07:43:23 +0000 (16:43 +0900)
This makes script command to allow the usage of script within the
script file by saving existing execute queue and then replacing the
line with script command with the lines of the input file.

Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
src/shared/shell.c

index ab1738a3bcdfee2218920dbb14769464ff821f30..f1505a84e2cf7715cf2d5bb86acd596d52f2417d 100644 (file)
@@ -290,15 +290,14 @@ static int bt_shell_queue_exec(char *line)
        return err;
 }
 
-static bool input_read(struct io *io, void *user_data)
+static bool bt_shell_input_line(struct input *input)
 {
-       struct input *input = user_data;
        int fd;
        char *line = NULL;
        size_t len = 0;
        ssize_t nread;
 
-       fd = io_get_fd(io);
+       fd = io_get_fd(input->io);
 
        if (fd < 0) {
                printf("io_get_fd() returned %d\n", fd);
@@ -335,7 +334,12 @@ static bool input_read(struct io *io, void *user_data)
 
        free(line);
 
-       return true;
+       return input->f ? true : false;
+}
+
+static bool input_read(struct io *io, void *user_data)
+{
+       return bt_shell_input_line(user_data);
 }
 
 static bool input_hup(struct io *io, void *user_data)
@@ -370,13 +374,25 @@ static struct input *input_new(int fd)
 static bool bt_shell_input_attach(int fd)
 {
        struct input *input;
+       struct queue *queue;
 
        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);
+       /* Save executing queue so input lines can be placed in the correct
+        * order.
+        */
+       queue = data.queue;
+       data.queue = queue_new();
+
+       while (bt_shell_input_line(input));
+
+       /* Push existing input lines back into the executing queue */
+       while (!queue_isempty(queue))
+               queue_push_tail(data.queue, queue_pop_head(queue));
+
+       queue_destroy(queue, free);
 
        return true;
 }
@@ -395,7 +411,10 @@ static void cmd_script(int argc, char *argv[])
 
        printf("Running script %s...\n", argv[1]);
 
-       bt_shell_input_attach(fd);
+       if (!bt_shell_input_attach(fd))
+               return bt_shell_noninteractive_quit(EXIT_FAILURE);
+
+       return bt_shell_noninteractive_quit(EXIT_SUCCESS);
 }
 
 static const struct bt_shell_menu_entry default_menu[] = {