From fc27548c447ed9bcd07493983431e0a6e72c7cef Mon Sep 17 00:00:00 2001 From: Ákos Kőrösi Date: Thu, 21 May 2026 12:44:01 +0200 Subject: restructure dir --- src/exec.c | 194 ++++++++++++++++++++++++------------------------------------- 1 file changed, 76 insertions(+), 118 deletions(-) (limited to 'src/exec.c') diff --git a/src/exec.c b/src/exec.c index fccda7a..afdf140 100644 --- a/src/exec.c +++ b/src/exec.c @@ -1,38 +1,35 @@ #include -#include -#include #include #include #include #include -#include "utils.h" -#include "exec.h" -#include "preproc.h" -#include "expand.h" +#include "syntax.h" -void execute_command(Command cmd, int* pgid) { +void execute_cmd(ExecutableCommand cmd, int* pgid) { pid_t pid = fork(); - + if (pid == 0) { - if (*pgid == -1) { + + if (*pgid == -1) { setpgid(0, 0); } else { setpgid(0, *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.new_fd, fd_rd.old_fd); + for (int i = 0; i < cmd.redirects.count; i++) { + FdRedirect fd_rd = *get_from_FdRedirectVec(&cmd.redirects, i); + dup2(fd_rd.to, fd_rd.redirected); } - for (int i = 0; i < cmd.child_close_fds.count; i++) { - close(*get_from_IntVec(&cmd.child_close_fds, i)); + for (int j = 0; j < cmd.open_fds.count; j++) { + int fd = *get_from_IntVec(&cmd.open_fds, j); + close(fd); } signal(SIGINT, SIG_DFL); @@ -53,102 +50,90 @@ void execute_command(Command cmd, int* pgid) { } } -Command make_command(ExpandedPipelineElement elem) { - Command cmd = { - .child_close_fds = make_IntVec(64), - .parent_close_fds = make_IntVec(64), - .redirs = make_FdRedirectVec(32), - .term = elem.conn, - }; - StrVec arg_strs = make_StrVec(256); - for (int i = 0; i < elem.cmd_slots.count; i++) { - Slot curr_slot = *get_from_SlotVec(&elem.cmd_slots, i); - Slot next_slot; + +ExecutableCommand process_pl_command(PlCommand pl_cmd) { + StrVec arg_vec = make_StrVec(256); + for (int i = 0; i < pl_cmd.args.count; i++) { + Arg arg = *get_from_ArgVec(&pl_cmd.args, i); + switch (arg.kind) { + case ARG_LIT: + push_to_StrVec(&arg_vec, arg.as.lit_str); + break; + default: + exit(67); + } + } + + FdRedirectVec fd_redirs = make_FdRedirectVec(256); + IntVec open_fds = make_IntVec(256); + for (int j = 0; j < pl_cmd.redirects.count; j++) { + Redirect redir = *get_from_RedirectVec(&pl_cmd.redirects, j); + FdRedirect fd_redir; int fd; - FdRedirect redir; - switch (curr_slot.kind) { - case SLOT_STR: - push_to_StrVec(&arg_strs, curr_slot.as.str); + switch (redir.kind) { + case REDIR_READ: + fd = open(redir.as.filename, O_RDONLY, 0644); + push_to_IntVec(&open_fds, fd); + fd_redir = (FdRedirect){.to = fd, .redirected = STDIN_FILENO}; + break; + case REDIR_WRITE_APPEND: + fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_APPEND, 0644); + push_to_IntVec(&open_fds, fd); + fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO}; break; - case SLOT_SPEC: - switch (curr_slot.as.spec) { - case READ_REDIR: - i++; - next_slot = *get_from_SlotVec(&elem.cmd_slots, i); - if (next_slot.kind != SLOT_STR) exit(6); - fd = open(next_slot.as.str, O_RDONLY, 0644); - redir = (FdRedirect){.old_fd = STDIN_FILENO, .new_fd = fd}; - push_to_FdRedirectVec(&cmd.redirs, redir); - push_to_IntVec(&cmd.parent_close_fds, fd); - push_to_IntVec(&cmd.child_close_fds, fd); - break; - case WRITE_REDIR_TRUNC: - i++; - next_slot = *get_from_SlotVec(&elem.cmd_slots, i); - if (next_slot.kind != SLOT_STR) exit(6); - fd = open(next_slot.as.str, O_WRONLY | O_CREAT | O_TRUNC , 0644); - redir = (FdRedirect){.old_fd = STDOUT_FILENO, .new_fd = fd}; - push_to_FdRedirectVec(&cmd.redirs, redir); - push_to_IntVec(&cmd.parent_close_fds, fd); - push_to_IntVec(&cmd.child_close_fds, fd); - break; - case WRITE_REDIR_APPEND: - i++; - next_slot = *get_from_SlotVec(&elem.cmd_slots, i); - if (next_slot.kind != SLOT_STR) exit(6); - fd = open(next_slot.as.str, O_WRONLY | O_CREAT | O_APPEND, 0644); - redir = (FdRedirect){.old_fd = STDOUT_FILENO, .new_fd = fd}; - push_to_FdRedirectVec(&cmd.redirs, redir); - push_to_IntVec(&cmd.parent_close_fds, fd); - push_to_IntVec(&cmd.child_close_fds, fd); - break; - default: - exit(7); - } + case REDIR_WRITE_TRUNC: + fd = open(redir.as.filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); + push_to_IntVec(&open_fds, fd); + fd_redir = (FdRedirect){.to = fd, .redirected = STDOUT_FILENO}; break; default: exit(67); - // TODO: other slot kinds } - } - cmd.args = malloc(sizeof(char*) * arg_strs.count); - for (int j = 0; j < arg_strs.count; j++) { - cmd.args[j] = *get_from_StrVec(&arg_strs, j); - } - return cmd; + push_to_FdRedirectVec(&fd_redirs, fd_redir); } + char** args = malloc(sizeof(char*) * (arg_vec.count + 1)); + for (int i = 0; i < arg_vec.count; i++) { + args[i] = *get_from_StrVec(&arg_vec, i); + } + args[arg_vec.count] = NULL; + + return (ExecutableCommand){.args = args, .redirects = fd_redirs, .open_fds = open_fds}; +} -void run_cmds(CommandVec cmds) { - signal(SIGTTIN, SIG_IGN); - signal(SIGTTOU, SIG_IGN); +void execute_command_pipeline(Pipeline pipeline) { int trunk_fd = STDIN_FILENO; int pgid = -1; - for (int i = 0; i < cmds.count; i++) { - Command cmd = *get_from_CommandVec(&cmds, i); - cmd.stdin_fd = trunk_fd; + signal(SIGTTIN, SIG_IGN); + signal(SIGTTOU, SIG_IGN); + + for (int i = 0; i < pipeline.count; i++) { + PlCommand cmd = *get_from_Pipeline(&pipeline, i); + ExecutableCommand exec_cmd = process_pl_command(cmd); + + exec_cmd.stdin_fd = trunk_fd; if (i > 0) { - push_to_IntVec(&cmd.child_close_fds, trunk_fd); - push_to_IntVec(&cmd.parent_close_fds, trunk_fd); + push_to_IntVec(&exec_cmd.open_fds, trunk_fd); } - cmd.stdout_fd = STDOUT_FILENO; - if (cmd.term == CONN_PIPE) { + exec_cmd.stdout_fd = STDOUT_FILENO; + + if (cmd.conn == CONN_PIPE) { int pipe_fds[2]; pipe(pipe_fds); - cmd.stdout_fd = pipe_fds[1]; - push_to_IntVec(&cmd.child_close_fds, pipe_fds[1]); - push_to_IntVec(&cmd.parent_close_fds, pipe_fds[1]); + exec_cmd.stdout_fd = pipe_fds[1]; + push_to_IntVec(&exec_cmd.open_fds, pipe_fds[1]); trunk_fd = pipe_fds[0]; } - execute_command(cmd, &pgid); - - for (int j = 0; j < cmd.parent_close_fds.count; j++) { - close(*get_from_IntVec(&cmd.parent_close_fds, j)); + execute_cmd(exec_cmd, &pgid); + + for (int j = 0; j < exec_cmd.open_fds.count; j++) { + int fd = *get_from_IntVec(&exec_cmd.open_fds, j); + close(fd); } } @@ -157,33 +142,6 @@ void run_cmds(CommandVec cmds) { tcsetpgrp(STDIN_FILENO, getpgrp()); } - -CommandVec make_cmd_vec_pl(ExpandedPipeline pl) { - CommandVec cmds = make_CommandVec(256); - for (int i = 0; i < pl.count; i++) { - ExpandedPipelineElement elem = *get_from_ExpandedPipeline(&pl, i); - Command cmd = make_command(elem); - push_to_CommandVec(&cmds, cmd); - } - return cmds; -} - - -void process_input_line(char* line, ShellState* shstate) { - PreprocArena arena = { - .tokens = make_TokenVec(256), - .chunks = make_ChunkVec(256), - .regions = make_RegionVec(256), - }; - TokenPtrVec line_tok_ptrs = lex(line, &arena); - Token* line_toks = malloc(sizeof(Token) * line_tok_ptrs.count); - for (int i = 0; i < line_tok_ptrs.count; i++) { - line_toks[i] = **get_from_TokenPtrVec(&line_tok_ptrs, i); - } - Pipeline pl = parse_tokstream(line_toks); - ExpandedPipeline exp_pl = expand_pipeline(&pl, shstate); - CommandVec cmds = make_cmd_vec_pl(exp_pl); - run_cmds(cmds); - free(line_toks); +void execute_pipeline_tree(PipelineTree pltree) { + execute_command_pipeline(*pltree.root); } - -- cgit v1.2.3