summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore1
-rw-r--r--src/exec.c26
2 files changed, 15 insertions, 12 deletions
diff --git a/.gitignore b/.gitignore
index 54ae6be..6fcb445 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,2 +1,3 @@
bin/
+scratch/
.clangd
diff --git a/src/exec.c b/src/exec.c
index 95ba31f..f3dc454 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -7,7 +7,7 @@
#include "syntax.h"
#include "utils.h"
-void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd);
+void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid);
void execute_cmd(ExecutableCommand cmd, int* pgid) {
@@ -54,7 +54,7 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) {
}
-ExecutableCommand process_pl_command(PlCommand pl_cmd) {
+ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid) {
StrVec arg_vec = make_StrVec(256);
FdRedirectVec fd_redirs = make_FdRedirectVec(256);
IntVec parent_open_fds = make_IntVec(256);
@@ -70,7 +70,7 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) {
break;
case ARG_PS_IN:
pipe(ps_pipe_fds);
- execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1]);
+ execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1], false, pgid);
close(ps_pipe_fds[1]);
char pname[64];
snprintf(pname, sizeof(pname), "/dev/fd/%d", ps_pipe_fds[0]);
@@ -83,7 +83,7 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) {
// TODO: don't do fcntl?
fcntl(ps_pipe_fds[1], F_SETFD, FD_CLOEXEC);
- execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, ps_pipe_fds[0], STDOUT_FILENO);
+ execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, ps_pipe_fds[0], STDOUT_FILENO, false, pgid);
fcntl(ps_pipe_fds[1], F_SETFD, 0);
close(ps_pipe_fds[0]);
char name[64];
@@ -135,13 +135,12 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd) {
}
-void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) {
+void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid) {
int trunk_fd = stdin_fd;
- int pgid = -1;
for (int i = 0; i < pipeline.count; i++) {
PlCommand cmd = *get_from_Pipeline(&pipeline, i);
- ExecutableCommand exec_cmd = process_pl_command(cmd);
+ ExecutableCommand exec_cmd = process_pl_command(cmd, pgid);
exec_cmd.stdin_fd = trunk_fd;
if (i > 0) {
@@ -160,7 +159,7 @@ void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) {
trunk_fd = pipe_fds[0];
}
- execute_cmd(exec_cmd, &pgid);
+ execute_cmd(exec_cmd, pgid);
for (int j = 0; j < exec_cmd.parent_open_fds.count; j++) {
int fd = *get_from_IntVec(&exec_cmd.parent_open_fds, j);
@@ -168,13 +167,16 @@ void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd) {
}
}
- tcsetpgrp(STDIN_FILENO, pgid);
- while (waitpid(-pgid, NULL, 0) > 0);
- tcsetpgrp(STDIN_FILENO, getpgrp());
+ if (wait_here) {
+ tcsetpgrp(STDIN_FILENO, *pgid);
+ while (waitpid(-*pgid, NULL, 0) > 0);
+ tcsetpgrp(STDIN_FILENO, getpgrp());
+ }
}
void execute_pipeline_tree(PipelineTree pltree) {
+ int pgid = -1;
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
- execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO);
+ execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO, true, &pgid);
}