From 69592d5314009d829c4adeda22c32b3a45590c68 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Mon, 23 Sep 2024 16:52:47 -0400 Subject: fixed splitting within quoted string --- src/lush.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/lush.c b/src/lush.c index 629c4b5..78e7fb7 100644 --- a/src/lush.c +++ b/src/lush.c @@ -928,11 +928,14 @@ char **lush_split_commands(char *line) { int pos = 0; char *start = line; + bool in_quote = false; while (*start) { // Skip leading spaces while (isspace((unsigned char)*start)) start++; + // Check if entering or leaving a quoted string + // Check for operators int op_len = operator_length(start); if (op_len > 0) { @@ -944,8 +947,14 @@ char **lush_split_commands(char *line) { } else { // Collect regular commands until the next operator or end of string char *next = start; - while (*next && !is_operator(next)) { + while (*next) { + if (*next == '"') { + in_quote = !in_quote; + } next++; + + if (!in_quote && is_operator(next)) + break; } // Copy the command between start and next @@ -1213,7 +1222,6 @@ int lush_execute_command(char **args, int input_fd, int output_fd) { // execute the command if (execvp(args[0], args) == -1) { perror("execvp"); - printf("the file: %s\n", args[0]); exit(EXIT_FAILURE); } } else if (pid < 0) { @@ -1324,9 +1332,6 @@ int main(int argc, char *argv[]) { } char *expanded_line = lush_resolve_aliases(line); char **commands = lush_split_commands(expanded_line); - for (int i = 0; commands[i] != NULL; i++) { - printf("Command %d: '%s'\n", i, commands[i]); - } char ***args = lush_split_args(commands, &status); if (status == -1) { fprintf(stderr, "lush: Expected end of quoted string\n"); -- cgit v1.2.3-59-g8ed1b