From 863b0978293c78098c0119d7baabd6dcb9ee3343 Mon Sep 17 00:00:00 2001 From: BanceDev Date: Mon, 23 Sep 2024 08:49:11 -0400 Subject: implemented piping into new chaining method --- src/lush.c | 40 ++++++++++++++++++++++++++++++---------- 1 file changed, 30 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/lush.c b/src/lush.c index 9b43324..1cdb50d 100644 --- a/src/lush.c +++ b/src/lush.c @@ -1153,28 +1153,52 @@ int lush_execute_chain(lua_State *L, char ***commands, int num_commands) { } int num_actions = (num_commands + 1) / 2; - int last_result = 0; + for (int i = 0; i < num_actions; i++) { // Determine the operator type between commands if (i < num_actions - 1) { int op_type = is_operator(commands[1][0]); + + // Handle '&&' operator if (op_type == OP_AND && last_result != 0) { + commands += 2; + continue; + } + + // Handle '|', build pipe array + if (op_type == OP_PIPE) { + char ***pipe_commands = + malloc(sizeof(char **) * (num_actions - i)); + int pipe_count = 0; + + while (i < num_actions - 1 && op_type == OP_PIPE) { + pipe_commands[pipe_count++] = commands[0]; + commands += 2; + i++; + if (i < num_actions - 1) { + op_type = is_operator(commands[1][0]); + } else { + break; + } + } + + pipe_commands[pipe_count++] = commands[0]; + last_result = lush_execute_pipeline(pipe_commands, pipe_count); + + free(pipe_commands); + commands += 2; continue; } } - // Execute the current command if it's not an operator if (!is_operator(commands[0][0])) { last_result = run_command(L, commands); commands += 2; } } - commands -= num_actions * 2; - printf("sanity check, commands[0]: %s", commands[0][0]); - - return 1; + return last_result; } int lush_execute_pipeline(char ***commands, int num_commands) { @@ -1355,10 +1379,6 @@ int main(int argc, char *argv[]) { exit(1); } - for (int i = 0; commands[i] != NULL; i++) { - printf("Command %d: '%s'\n", i, commands[i]); - } - for (int i = 0; args[i]; i++) { free(args[i]); } -- cgit v1.2.3-59-g8ed1b