From a3b571e8549f7f7b5a6ca2da449cc5d3aefa9084 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 17 Oct 2024 17:19:48 +0200 Subject: Add path handling --- src/interpreter.c | 5 ++--- src/main.c | 5 +++-- 2 files changed, 5 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/interpreter.c b/src/interpreter.c index abaeb47..0fa7ac1 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: dkaiser args[0]); - printf("PATH=%s\n", env_get(env, "PATH")); + printf("%s\n", get_cmd_path(cmd->args[0], env)); return (0); } diff --git a/src/main.c b/src/main.c index 64bc312..e87bc99 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:34:02 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 17:01:45 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,8 @@ int main(int argc, char *argv[], char *envp[]) { - t_env *env; + t_env *env; + if (!argc && !argv) return (1); if (init()) -- cgit v1.2.3 From 32e454ed265fc66dc45e43a3c68fbbe416fc16f9 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 21 Oct 2024 14:32:36 +0200 Subject: Add path handling for real now --- src/get_cmd_path.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/get_cmd_path.c (limited to 'src') diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c new file mode 100644 index 0000000..1ecf393 --- /dev/null +++ b/src/get_cmd_path.c @@ -0,0 +1,82 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* get_cmd_path.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 17:01:27 by dkaiser ### ########.fr */ +/* Updated: 2024/10/21 14:57:24 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,3 +22,4 @@ typedef struct s_env void getenvlst(t_env **env, char **en); void free_envlst(t_env **env); char *env_get(t_env *env, char *name); +char **env_to_strlst(t_env *env); diff --git a/include/minishell.h b/include/minishell.h index c84fda3..6282ea6 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:58:28 by dkaiser ### ########.fr */ +/* Updated: 2024/10/21 15:07:51 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include "get_next_line.h" #include "libft.h" +#include void getenvlst(t_env **env, char **en) { @@ -51,7 +52,7 @@ void free_envlst(t_env **env) } } -char *env_get(t_env *env, char *name) +char *env_get(t_env *env, char *name) { while (env != NULL) { diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c new file mode 100644 index 0000000..3a58df9 --- /dev/null +++ b/src/env_to_strlst.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env_to_strlst.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next; + } + result = malloc(sizeof(char *) * (size + 1)); + if (result == NULL) + return (NULL); + i = 0; + cur = env; + while (i < size) + { + result[i] = get_var_assign(cur); + cur = cur->next; + i++; + } + result[i] = NULL; + return (result); +} + +static char *get_var_assign(t_env *cur) +{ + char *left_side; + char *result; + + left_side = ft_strjoin(cur->name, "="); + if (left_side == NULL) + return (NULL); + result = ft_strjoin(left_side, cur->value); + free(left_side); + return (result); +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c new file mode 100644 index 0000000..3730d4b --- /dev/null +++ b/src/execute_cmd.c @@ -0,0 +1,38 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* execute_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser +#include +#include + +int execute_cmd(t_cmd *cmd, t_env *env) +{ + int result; + pid_t pid; + + pid = fork(); + if (pid < 0) + return (EXIT_FAILURE); + if (pid == 0) + { + result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); + exit(result); + } + else + { + // only wait if cmd is on rightmost side of pipe? + // so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4 + waitpid(pid, &result, 0); + } + return (result); +} diff --git a/src/interpreter.c b/src/interpreter.c index 0fa7ac1..1082644 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,11 +6,12 @@ /* By: dkaiser static int eval_pipe(t_pipe *pipe, t_env *env); static int eval_cmd(t_cmd *cmd, t_env *env); @@ -38,6 +39,13 @@ static int eval_pipe(t_pipe *pipe, t_env *env) static int eval_cmd(t_cmd *cmd, t_env *env) { - printf("%s\n", get_cmd_path(cmd->args[0], env)); + char *cmd_path; + + cmd_path = get_cmd_path(cmd->args[0], env); + if (cmd_path == NULL) + return (1); + free(cmd->args[0]); + cmd->args[0] = cmd_path; + execute_cmd(cmd, env); return (0); } diff --git a/src/main.c b/src/main.c index e87bc99..c3e0ec7 100644 --- a/src/main.c +++ b/src/main.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 17:01:45 by dkaiser ### ########.fr */ +/* Updated: 2024/10/21 15:02:56 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ int main(int argc, char *argv[], char *envp[]) { t_env *env; + env = NULL; if (!argc && !argv) return (1); if (init()) -- cgit v1.2.3 From 0a5684e637cd6ec084575ddf9930cfde55d3cca7 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 22 Oct 2024 15:42:28 +0200 Subject: Add pipe basics TODO: Add actual piping --- src/execute_cmd.c | 21 ++++----------- src/interpreter.c | 79 +++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 58 insertions(+), 42 deletions(-) (limited to 'src') diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 3730d4b..33ba11c 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser args[0], cmd->args, env_to_strlst(env)); - exit(result); - } - else - { - // only wait if cmd is on rightmost side of pipe? - // so in cmd1 | cmd2 | cmd3 | cmd4 only wait for cmd4 - waitpid(pid, &result, 0); - } + cmd_path = get_cmd_path(cmd->args[0], env); + cmd->args[0] = cmd_path; + result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index 1082644..f6757c4 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,46 +6,73 @@ /* By: dkaiser +#include +#include #include +#include -static int eval_pipe(t_pipe *pipe, t_env *env); -static int eval_cmd(t_cmd *cmd, t_env *env); +int eval_rec(t_node *node, t_env *env); int eval(t_node *node, t_env *env) { - if (node->type == PIPE_NODE) - return (eval_pipe(&node->content.pipe, env)); - else if (node->type == CMD_NODE) - return (eval_cmd(&node->content.cmd, env)); + pid_t pid; + int result; + + result = 0; + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = eval_rec(node, env); + exit(result); + } else { - panic(UNREACHABLE); - return (-1); + waitpid(pid, &result, 0); } + return (result); } -static int eval_pipe(t_pipe *pipe, t_env *env) -{ - dbg("TODO: PIPE"); - eval_cmd(&pipe->left->content.cmd, env); - eval_cmd(&pipe->right->content.cmd, env); - return (0); -} - -static int eval_cmd(t_cmd *cmd, t_env *env) +int eval_rec(t_node *node, t_env *env) { - char *cmd_path; + pid_t pid; + int result; - cmd_path = get_cmd_path(cmd->args[0], env); - if (cmd_path == NULL) - return (1); - free(cmd->args[0]); - cmd->args[0] = cmd_path; - execute_cmd(cmd, env); - return (0); + if (node->type == PIPE_NODE) + { + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = execute_cmd(&node->content.pipe.left->content.cmd, env); + exit(result); + } + else + { + result = eval(node->content.pipe.right, env); + } + } + else if (node->type == CMD_NODE) + { + result = execute_cmd(&node->content.cmd, env); + } + else + { + panic(UNREACHABLE); + return (EXIT_FAILURE); + } + return (result); } -- cgit v1.2.3 From 07522e059d2956014b8d97f39b72a90c6dc6a7c6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 25 Oct 2024 12:51:07 +0200 Subject: Finish pipes I guess Somehow this worked at the first try. This'll have to be tested more thoroughly. --- src/interpreter.c | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/interpreter.c b/src/interpreter.c index f6757c4..e08d289 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: dkaiser #include -int eval_rec(t_node *node, t_env *env); +static int eval_rec(t_node *node, t_env *env, int in_fd); int eval(t_node *node, t_env *env) { @@ -33,7 +33,7 @@ int eval(t_node *node, t_env *env) } if (pid == 0) { - result = eval_rec(node, env); + result = eval_rec(node, env, STDIN_FILENO); exit(result); } else @@ -43,11 +43,15 @@ int eval(t_node *node, t_env *env) return (result); } -int eval_rec(t_node *node, t_env *env) +static int eval_rec(t_node *node, t_env *env, int in_fd) { pid_t pid; int result; + int p[2]; + result = pipe(p); + if (result == -1) + return (EXIT_FAILURE); if (node->type == PIPE_NODE) { pid = fork(); @@ -57,12 +61,17 @@ int eval_rec(t_node *node, t_env *env) } if (pid == 0) { + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); result = execute_cmd(&node->content.pipe.left->content.cmd, env); exit(result); } else { - result = eval(node->content.pipe.right, env); + close(p[1]); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); } } else if (node->type == CMD_NODE) -- cgit v1.2.3 From 15d8385f8ecf30e1ca74025b12fed7e45349b706 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 25 Oct 2024 13:44:13 +0200 Subject: Handle all redirections except APPEND TODO: Add APPEND handling TODO: Fix permissions --- src/execute_cmd.c | 34 +++++++++++++++++++++++++++++++++- src/interpreter.c | 4 +++- 2 files changed, 36 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 33ba11c..fa7677f 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,22 +6,54 @@ /* By: dkaiser #include +#include +#include #include +#include int execute_cmd(t_cmd *cmd, t_env *env) { int result; char *cmd_path; + int fd; cmd_path = get_cmd_path(cmd->args[0], env); cmd->args[0] = cmd_path; + + if (cmd->redirs[0].type == INPUT_FILE) + { + fd = open(cmd->redirs[0].specifier, O_RDONLY); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDIN_FILENO); + } + else if (cmd->redirs[0].type == INPUT_LIMITER) + { + dbg("INPUT_LIMITER"); + } + if (cmd->redirs[1].type == OUTPUT_APPEND) + { + dbg("OUTPUT_APPEND"); + fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + } + else if (cmd->redirs[1].type == OUTPUT_OVERRIDE) + { + fd = open(cmd->redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC); + if (fd < 0) + return (EXIT_FAILURE); + dup2(fd, STDOUT_FILENO); + dbg("OUTPUT_OVERRIDE"); + } result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } diff --git a/src/interpreter.c b/src/interpreter.c index e08d289..458f2af 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: dkaiser