summaryrefslogtreecommitdiff
path: root/src/exec.c
diff options
context:
space:
mode:
authorÁkos Kőrösi <korakos99@gmail.com>2026-05-21 20:30:04 +0200
committerÁkos Kőrösi <korakos99@gmail.com>2026-05-21 20:30:04 +0200
commit3c283f1e2f4ef6b89481480c43fbc4408cd71a51 (patch)
tree0e66eef811fadeb85cfbb03820f8cd49fd0dd943 /src/exec.c
parentb56361b4687359f21c581367b05c8b74d0ff769e (diff)
start builtins
Diffstat (limited to 'src/exec.c')
-rw-r--r--src/exec.c32
1 files changed, 22 insertions, 10 deletions
diff --git a/src/exec.c b/src/exec.c
index f3dc454..2bcffb9 100644
--- a/src/exec.c
+++ b/src/exec.c
@@ -7,11 +7,21 @@
#include "syntax.h"
#include "utils.h"
-void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid);
+void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid, ShellState* shstate);
-void execute_cmd(ExecutableCommand cmd, int* pgid) {
+bool handle_builtin(ExecutableCommand cmd, ShellState* shstate) {
+
+ // TODO: handle id=value assignments
+
+ if (strcmp(cmd.args[0], "exit") == 0) {
+ exit(0);
+ }
+ return false;
+}
+void execute_cmd(ExecutableCommand cmd, int* pgid) {
+
pid_t pid = fork();
if (pid == 0) {
@@ -54,7 +64,7 @@ void execute_cmd(ExecutableCommand cmd, int* pgid) {
}
-ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid) {
+ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid, ShellState* shstate) {
StrVec arg_vec = make_StrVec(256);
FdRedirectVec fd_redirs = make_FdRedirectVec(256);
IntVec parent_open_fds = make_IntVec(256);
@@ -70,7 +80,7 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid) {
break;
case ARG_PS_IN:
pipe(ps_pipe_fds);
- execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1], false, pgid);
+ execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, STDIN_FILENO, ps_pipe_fds[1], false, pgid, shstate);
close(ps_pipe_fds[1]);
char pname[64];
snprintf(pname, sizeof(pname), "/dev/fd/%d", ps_pipe_fds[0]);
@@ -83,7 +93,7 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid) {
// 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, false, pgid);
+ execute_command_pipeline(*(Pipeline*)arg.as.sub_pipeline, ps_pipe_fds[0], STDOUT_FILENO, false, pgid, shstate);
fcntl(ps_pipe_fds[1], F_SETFD, 0);
close(ps_pipe_fds[0]);
char name[64];
@@ -135,12 +145,12 @@ ExecutableCommand process_pl_command(PlCommand pl_cmd, int* pgid) {
}
-void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid) {
+void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bool wait_here, int* pgid, ShellState* shstate) {
int trunk_fd = stdin_fd;
for (int i = 0; i < pipeline.count; i++) {
PlCommand cmd = *get_from_Pipeline(&pipeline, i);
- ExecutableCommand exec_cmd = process_pl_command(cmd, pgid);
+ ExecutableCommand exec_cmd = process_pl_command(cmd, pgid, shstate);
exec_cmd.stdin_fd = trunk_fd;
if (i > 0) {
@@ -159,7 +169,9 @@ void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bo
trunk_fd = pipe_fds[0];
}
- execute_cmd(exec_cmd, pgid);
+ if (!handle_builtin(exec_cmd, shstate)) {
+ 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);
@@ -174,9 +186,9 @@ void execute_command_pipeline(Pipeline pipeline, int stdin_fd, int stdout_fd, bo
}
}
-void execute_pipeline_tree(PipelineTree pltree) {
+void execute_pipeline_tree(PipelineTree pltree, ShellState* shstate) {
int pgid = -1;
signal(SIGTTIN, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
- execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO, true, &pgid);
+ execute_command_pipeline(*pltree.root, STDIN_FILENO, STDOUT_FILENO, true, &pgid, shstate);
}