diff --git a/.gitignore b/.gitignore index 293add1..2ec1726 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,5 @@ a.out *.o obj/ .minishell_history +pipetester.c +pipetest diff --git a/README.md b/README.md index d6fecff..1ac6a5b 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A lot of amazing shell stuff -[x] Set environment variable (export) -[x] Simple builtin export - Preliminary signals -- Define struct for commands, something like ( +-[x] Define struct for commands, something like ( ```c typedef struct s_command { @@ -24,9 +24,16 @@ A lot of amazing shell stuff } t_command; ``` ) -- Make the `executor`, run a command +- [x] Make the `executor`, run a command - Make a parser to create a command list - Add redirects, appends, pipe etc. File descriptor functions + a command can have multiple redirects but only the last is used for stdout + Redirects take precedence over pipes, but pipes are still created and managed. + should it close the unused pipe-end? + all redirects are opened and closed, but only last fd is used. + multiple HEREDOCs can be nested, only last is used. + + - Expand \$ vars & support \$? - CB command to change banner - __Bonus:__ Command tree for &&, ||, * diff --git a/inc/executor.h b/inc/executor.h index 9331b8c..e6c145e 100644 --- a/inc/executor.h +++ b/inc/executor.h @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:06:07 by willem #+# #+# */ -/* Updated: 2025/02/12 20:24:20 by willem ######## odam.nl */ +/* Updated: 2025/02/13 14:57:13 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -20,6 +20,8 @@ void executor_child(t_command *command); int executor_fork(t_command *command); void executor_create_pipes(t_minishell *minishell); void executor_execute_pipeline(t_minishell *minishell); +void executor_close_fds(int n_fds); + diff --git a/inc/typedef.h b/inc/typedef.h index 601c08a..9039e04 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/05 12:36:08 by whaffman #+# #+# */ -/* Updated: 2025/02/12 20:35:09 by willem ######## odam.nl */ +/* Updated: 2025/02/13 14:27:07 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -53,7 +53,8 @@ typedef struct s_command t_list *environment; int fd_in; int fd_out; - int n_pipes; + int n_fds; + int exit_status; } t_command; typedef struct s_minishell diff --git a/pipetester.c b/pipetester.c index 833d61e..35409af 100644 --- a/pipetester.c +++ b/pipetester.c @@ -6,56 +6,94 @@ void error(char *msg) { - write(2, msg, strlen(msg)); + write(2, msg, strlen(msg)); } int main(void) { - int fd_in = STDIN_FILENO; - int fd_out = STDOUT_FILENO; - struct stat statbuf_in; - struct stat statbuf_out; + int fd_in = STDIN_FILENO; + int fd_out = STDOUT_FILENO; + struct stat statbuf_in; + struct stat statbuf_out; - if (fstat(fd_in, &statbuf_in) == -1) - { - perror("fstat"); - return (1); - } - error("STDIN File type: "); + char c; - switch (statbuf_in.st_mode & S_IFMT) { - case S_IFBLK: error("block device\n"); break; - case S_IFCHR: error("character device\n"); break; - case S_IFDIR: error("directory\n"); break; - case S_IFIFO: error("FIFO/pipe\n"); break; - case S_IFLNK: error("symlink\n"); break; - case S_IFREG: error("regular file\n"); break; - case S_IFSOCK: error("socket\n"); break; - default: error("unknown?\n"); break; + if (fstat(fd_in, &statbuf_in) == -1) + { + perror("fstat"); + return (1); + } + error("STDIN File type: "); + + switch (statbuf_in.st_mode & S_IFMT) + { + case S_IFBLK: + error("block device\n"); + break; + case S_IFCHR: + error("character device\n"); + break; + case S_IFDIR: + error("directory\n"); + break; + case S_IFIFO: + error("FIFO/pipe\n"); + break; + case S_IFLNK: + error("symlink\n"); + break; + case S_IFREG: + error("regular file\n"); + break; + case S_IFSOCK: + error("socket\n"); + break; + default: + error("unknown?\n"); + break; } + error("\n"); - error("\n"); - - if (fstat(fd_out, &statbuf_out) == -1) - { - perror("fstat"); - return (1); - } - -error("STDOUT File type: "); - - switch (statbuf_out.st_mode & S_IFMT) { - case S_IFBLK: error("block device\n"); break; - case S_IFCHR: error("character device\n"); break; - case S_IFDIR: error("directory\n"); break; - case S_IFIFO: error("FIFO/pipe\n"); break; - case S_IFLNK: error("symlink\n"); break; - case S_IFREG: error("regular file\n"); break; - case S_IFSOCK: error("socket\n"); break; - default: error("unknown?\n"); break; + if (fstat(fd_out, &statbuf_out) == -1) + { + perror("fstat"); + return (1); } + error("STDOUT File type: "); - return (0); + switch (statbuf_out.st_mode & S_IFMT) + { + case S_IFBLK: + error("block device\n"); + break; + case S_IFCHR: + error("character device\n"); + break; + case S_IFDIR: + error("directory\n"); + break; + case S_IFIFO: + error("FIFO/pipe\n"); + break; + case S_IFLNK: + error("symlink\n"); + break; + case S_IFREG: + error("regular file\n"); + break; + case S_IFSOCK: + error("socket\n"); + break; + default: + error("unknown?\n"); + break; + } + + while (read(fd_in, &c, 1) > 0) + { + write(fd_out, &c, 1); + } + return (0); } diff --git a/src/executor/executor_absolute_path.c b/src/executor/executor_absolute_path.c index 396dbb2..178d72a 100644 --- a/src/executor/executor_absolute_path.c +++ b/src/executor/executor_absolute_path.c @@ -6,7 +6,7 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/08 17:00:24 by willem #+# #+# */ -/* Updated: 2025/02/11 17:20:17 by whaffman ######## odam.nl */ +/* Updated: 2025/02/13 15:04:09 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ diff --git a/src/executor/executor_child.c b/src/executor/executor_child.c index 7855161..950b337 100644 --- a/src/executor/executor_child.c +++ b/src/executor/executor_child.c @@ -6,20 +6,20 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:10 by willem #+# #+# */ -/* Updated: 2025/02/12 21:25:11 by willem ######## odam.nl */ +/* Updated: 2025/02/13 15:04:12 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void executor_child(t_command *command) +void executor_child(t_command *command) { char *path; if (command->fd_in != 0) dup2(command->fd_in, 0); if (command->fd_out != 1) dup2(command->fd_out, 1); - executor_close_pipes(command->n_pipes); + executor_close_fds(command->n_fds); path = executor_absolute_path(command->environment, command->command); execve(path, command->args, environment_get_arr(command->environment)); -} \ No newline at end of file +} diff --git a/src/executor/executor_close_fds.c b/src/executor/executor_close_fds.c new file mode 100644 index 0000000..2c38dbc --- /dev/null +++ b/src/executor/executor_close_fds.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* :::::::: */ +/* executor_close_fds.c :+: :+: */ +/* +:+ */ +/* By: whaffman +#+ */ +/* +#+ */ +/* Created: 2025/02/13 13:31:25 by whaffman #+# #+# */ +/* Updated: 2025/02/13 15:04:15 by whaffman ######## odam.nl */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void executor_close_fds(int n_fds) +{ + int i; + + i = 3; + while (i < n_fds + 3) + { + close(i); + i++; + } +} diff --git a/src/executor/executor_close_pipes.c b/src/executor/executor_close_pipes.c deleted file mode 100644 index 3b172bd..0000000 --- a/src/executor/executor_close_pipes.c +++ /dev/null @@ -1,11 +0,0 @@ -void executor_close_pipes(int n_pipes) -{ - int i; - - i = 3; - while (i < n_pipes + 3) - { - close(i); - i++; - } -} \ No newline at end of file diff --git a/src/executor/executer_create_pipes.c b/src/executor/executor_create_pipes.c similarity index 61% rename from src/executor/executer_create_pipes.c rename to src/executor/executor_create_pipes.c index c7dae9f..d2306ac 100644 --- a/src/executor/executer_create_pipes.c +++ b/src/executor/executor_create_pipes.c @@ -1,26 +1,35 @@ /* ************************************************************************** */ /* */ /* :::::::: */ -/* executer_create_pipes.c :+: :+: */ +/* executor_create_pipes.c :+: :+: */ /* +:+ */ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:22 by willem #+# #+# */ -/* Updated: 2025/02/12 21:25:23 by willem ######## odam.nl */ +/* Updated: 2025/02/13 14:58:49 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void executer_create_pipes(t_minishell *minishell) -{ - t_list *current; - t_command *command; - int fd[2]; - int fd_in; - +/** + * @brief Creates pipes for the executor in the minishell. + * + * This function sets up the necessary pipes for inter-process communication + * within the minishell. It initializes the pipes based on the requirements + * of the minishell structure. + * + * @param minishell A pointer to the t_minishell structure containing the + * necessary information for pipe creation. + */ + +void executor_create_pipes(t_minishell *minishell) +{ + t_list *current; + t_command *command; + int fd[2]; + int fd_in; - fd_in = 0; current = minishell->commands; while (current) @@ -34,8 +43,8 @@ void executer_create_pipes(t_minishell *minishell) else command->fd_out = 1; command->fd_in = fd_in; - command->n_pipes = (ft_lstsize(minishell->commands) - 1) * 2; + command->n_fds = (ft_lstsize(minishell->commands) - 1) * 2; fd_in = fd[0]; current = current->next; } -} \ No newline at end of file +} diff --git a/src/executor/executor_execute_pipeline.c b/src/executor/executor_execute_pipeline.c index bf4961a..970de1b 100644 --- a/src/executor/executor_execute_pipeline.c +++ b/src/executor/executor_execute_pipeline.c @@ -6,18 +6,18 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:25:02 by willem #+# #+# */ -/* Updated: 2025/02/12 21:25:03 by willem ######## odam.nl */ +/* Updated: 2025/02/13 15:04:18 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -void executor_execute_pipeline(t_minishell *minishell) +void executor_execute_pipeline(t_minishell *minishell) { - t_list *current; + t_list *current; t_command *command; - executer_create_pipes(minishell); + executor_create_pipes(minishell); current = minishell->commands; while (current) { @@ -26,4 +26,4 @@ void executor_execute_pipeline(t_minishell *minishell) executor_fork(command); current = current->next; } -} \ No newline at end of file +} diff --git a/src/executor/executor_fork.c b/src/executor/executor_fork.c index ad1cd69..af52654 100644 --- a/src/executor/executor_fork.c +++ b/src/executor/executor_fork.c @@ -6,23 +6,23 @@ /* By: willem +#+ */ /* +#+ */ /* Created: 2025/02/12 21:24:52 by willem #+# #+# */ -/* Updated: 2025/02/12 21:24:53 by willem ######## odam.nl */ +/* Updated: 2025/02/13 15:03:19 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ #include "minishell.h" -int executor_fork(t_command *command) +int executor_fork(t_command *command) { - pid_t pid; + pid_t pid; int status; pid = fork(); if (pid > 0) { - if(command->fd_in != 0) + if (command->fd_in != 0) close(command->fd_in); - if(command->fd_out != 1) + if (command->fd_out != 1) close(command->fd_out); waitpid(pid, &status, 0); return (((status) & 0xff00) >> 8); @@ -31,5 +31,5 @@ int executor_fork(t_command *command) executor_child(command); else perror("fork"); - return (EXIT_FAILURE); -} \ No newline at end of file + return (-1); +} diff --git a/src/main.c b/src/main.c index 44b4751..78a4813 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: whaffman +#+ */ /* +#+ */ /* Created: 2025/02/04 16:19:22 by whaffman #+# #+# */ -/* Updated: 2025/02/12 20:46:11 by willem ######## odam.nl */ +/* Updated: 2025/02/13 13:36:23 by whaffman ######## odam.nl */ /* */ /* ************************************************************************** */ @@ -14,13 +14,13 @@ #include "minishell.h" #include "utils.h" -// static void token_print(void *param) -// { -// t_token *token; +static void token_print(void *param) +{ + t_token *token; -// token = (t_token *)param; -// printf("token type %i, value %s\n", token->type, token->value); -// } + token = (t_token *)param; + printf("token type %i, value %s\n", token->type, token->value); +} int main(int argc, char **argv, char **envp) { @@ -37,7 +37,7 @@ int main(int argc, char **argv, char **envp) minishell->line = ft_prompt(minishell); minishell->lexer = ft_lexer_new(minishell->line); minishell->tokens = ft_parse_input(minishell->lexer); - //ft_lstiter(minishell->tokens, token_print); + ft_lstiter(minishell->tokens, token_print); minishell->commands = parser_get_commands(minishell->tokens); simple_builtins(minishell); free_minishell_line(minishell);