From 24ba34266cc977b22d777bceca9683a45dd73042 Mon Sep 17 00:00:00 2001 From: Ákos Kőrösi Date: Mon, 18 May 2026 13:19:15 +0200 Subject: start outlining fd-based redirecting --- exec.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) (limited to 'exec.c') diff --git a/exec.c b/exec.c index 45c97b8..318638e 100644 --- a/exec.c +++ b/exec.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -108,14 +109,6 @@ char** make_cmd_args(Command command, ShellState* shstate) { } -typedef struct { - char** args; - int stdin_fd; - int stdout_fd; - IntVec child_close_fds; - IntVec parent_close_fds; -} ExecutableCommand; - void execute_command(ExecutableCommand cmd, int* pgid) { pid_t pid = fork(); @@ -130,6 +123,11 @@ void execute_command(ExecutableCommand cmd, int* pgid) { dup2(cmd.stdin_fd, STDIN_FILENO); dup2(cmd.stdout_fd, STDOUT_FILENO); + for (int j = 0; j < cmd.redirs.count; j++) { + FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirs, j); + dup2(fd_rd.old_fd, fd_rd.new_fd); + } + for (int i = 0; i < cmd.child_close_fds.count; i++) { close(*get_from_IntVec(&cmd.child_close_fds, i)); } @@ -164,7 +162,11 @@ void run_pipeline(Pipeline pl, ShellState* shstate) { for (int i = 0; i < pl.count; i++) { PipelineElement elem = *get_from_Pipeline(&pl, i); - ExecutableCommand cmd = {.child_close_fds = make_IntVec(64), .parent_close_fds = make_IntVec(64)}; + ExecutableCommand cmd = { + .child_close_fds = make_IntVec(64), + .parent_close_fds = make_IntVec(64), + .redirs = make_FdRedirectVec(32) + }; cmd.args = make_cmd_args(elem.cmd, shstate); if (i == 0) { @@ -186,6 +188,22 @@ void run_pipeline(Pipeline pl, ShellState* shstate) { cmd.stdout_fd = STDOUT_FILENO; } + for (int j = 0; j < elem.cmd.redirects.count; j++) { + Redirect rd = *get_from_RedirectVec(&elem.cmd.redirects, j); + switch (rd.kind) { + case STDIN_REDIR: + /* + int in_fd = open(rd.as.std_redir_filename, O_RDONLY, 0644); + push_to_FdRedirectVec(&cmd.redirs, (FdRedirect){.old_fd = STDIN_REDIR, .new_fd = in_fd}); + push_to_IntVec(&cmd.child_close_fds, in_fd); + push_to_IntVec(&cmd.parent_close_fds, in_fd); + */ + break; + default: + exit(69); + } + } + execute_command(cmd, &pgid); for (int j = 0; j < cmd.parent_close_fds.count; j++) { -- cgit v1.2.3