aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorGravatar BanceDev 2024-09-23 08:49:11 -0400
committerGravatar BanceDev 2024-09-23 08:49:11 -0400
commit863b0978293c78098c0119d7baabd6dcb9ee3343 (patch)
tree0d1ac4d5d39de29c1cf7d4d682c09c50446b9821
parentfixed && chaining for builtins (diff)
implemented piping into new chaining method
-rw-r--r--src/lush.c40
1 files changed, 30 insertions, 10 deletions
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]);
}