summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorÁkos Kőrösi <korakos99@gmail.com>2026-05-18 13:19:15 +0200
committerÁkos Kőrösi <korakos99@gmail.com>2026-05-18 13:19:15 +0200
commit24ba34266cc977b22d777bceca9683a45dd73042 (patch)
treed9202b0f6d2e9d5ec9e14926e30ead18daa45ae8
parentd31d9ce64c3085bea1a094e649f62176b1a896a6 (diff)
start outlining fd-based redirecting
-rw-r--r--exec.c36
-rw-r--r--exec.h17
2 files changed, 44 insertions, 9 deletions
diff --git a/exec.c b/exec.c
index 45c97b8..318638e 100644
--- a/exec.c
+++ b/exec.c
@@ -2,6 +2,7 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
+#include <fcntl.h>
#include <signal.h>
#include <sys/wait.h>
@@ -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++) {
diff --git a/exec.h b/exec.h
index 2424801..ca0b1a5 100644
--- a/exec.h
+++ b/exec.h
@@ -5,6 +5,23 @@
typedef struct {
+ int old_fd;
+ int new_fd;
+} FdRedirect;
+
+DECLARE_VEC(FdRedirect, FdRedirectVec)
+
+typedef struct {
+ char** args;
+ FdRedirectVec redirs;
+ int stdin_fd;
+ int stdout_fd;
+ IntVec child_close_fds;
+ IntVec parent_close_fds;
+} ExecutableCommand;
+
+
+typedef struct {
KeyValMap aliases;
KeyValMap shell_vars;
} ShellState;