From 06f7c2548f68ec2786eec97219b55cd095de450d Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 8 Aug 2024 17:10:25 +0200 Subject: Add data structure and prototypes for env --- include/env.h | 23 +++++++++++++++++++++++ include/minishell.h | 3 ++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 include/env.h diff --git a/include/env.h b/include/env.h new file mode 100644 index 0000000..1ea6f2e --- /dev/null +++ b/include/env.h @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser # include -- cgit v1.2.3 From a0016dffa6c5412d7c53b3d9040b837299c50159 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 14:32:28 +0200 Subject: added env linked lis c file --- src/env.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/env.c diff --git a/src/env.c b/src/env.c new file mode 100644 index 0000000..2403329 --- /dev/null +++ b/src/env.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ +/* Updated: 2024/10/17 14:31:50 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void getenvlst(t_env **env, char **en)// seperated name and value +{ + char *tmp; + int i; + t_env *current; + + i = 0; + while (en[i] != NULL) + { + tmp = ft_strchr(en[i], '='); + tmp = '\0'; + current = *env; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + i++; + } + return (0); +} + + +void free_envlst(t_env **env) +{ + t_env *cur; + t_env *new; + + cur = *env; + while (cur) + { + new = cur->next; + free(cur->name); + free(cur->value); + free(cur); + cur = new; + } + +} \ No newline at end of file -- cgit v1.2.3 From 9298e72de61af5311678c656c12fc177589671a7 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 15:32:02 +0200 Subject: fixed normitte and fixed return issue --- src/env.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/src/env.c b/src/env.c index 2403329..8105bf4 100644 --- a/src/env.c +++ b/src/env.c @@ -6,17 +6,17 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 14:31:50 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" -void getenvlst(t_env **env, char **en)// seperated name and value +void getenvlst(t_env **env, char **en) { char *tmp; int i; - t_env *current; + t_env *current; i = 0; while (en[i] != NULL) @@ -24,21 +24,19 @@ void getenvlst(t_env **env, char **en)// seperated name and value tmp = ft_strchr(en[i], '='); tmp = '\0'; current = *env; - current = malloc(sizeof(t_env)); - current->name = ft_strdup(en[i]); - current->value = ft_strdup(tmp + 1); - current->next = *env; - *env = current; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; i++; } - return (0); } - void free_envlst(t_env **env) { t_env *cur; - t_env *new; + t_env *new; cur = *env; while (cur) @@ -49,5 +47,4 @@ void free_envlst(t_env **env) free(cur); cur = new; } - } \ No newline at end of file -- cgit v1.2.3 From e6e8051fbda53a75332e09f917e74d687f467a8a Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 15:38:15 +0200 Subject: changed prototypes in header file --- include/env.h | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/include/env.h b/include/env.h index 12d8db0..5a65333 100644 --- a/include/env.h +++ b/include/env.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* env.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/09/13 16:26:16 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,9 +17,6 @@ typedef struct s_env struct s_env *next; } t_env; -char *env_get(t_env *env, char *name); -void env_export(t_env *env, char *name, char *value); -void env_unset(t_env *env, char *name); -char **env_to_strlst(t_env *env); -t_env **env_from_strlst(char **lst); +void getenvlst(t_env **env, char **en); +void free_envlst(t_env **env); -- cgit v1.2.3 From ae5512ea0d6d8be833ca3a9b39f93239109f45b4 Mon Sep 17 00:00:00 2001 From: cuhlig Date: Thu, 17 Oct 2024 15:42:11 +0200 Subject: Env (#23) * Add data structure and prototypes for env * added env linked lis c file * fixed normitte and fixed return issue * changed prototypes in header file --------- Co-authored-by: Dominik Kaiser Co-authored-by: Christopher Uhlig --- include/env.h | 12 +++++------- src/env.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/env.c diff --git a/include/env.h b/include/env.h index f3d3c75..5a65333 100644 --- a/include/env.h +++ b/include/env.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* env.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/09/13 16:26:16 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ typedef struct s_env struct s_env *next; } t_env; -char *env_get(t_env *env, char *name); -void env_export(t_env *env, char *name, char *value); -void env_unset(t_env *env, char *name); -char **env_to_strlst(t_env *env); -t_env **env_from_strlst(char **lst); +void getenvlst(t_env **env, char **en); +void free_envlst(t_env **env); + diff --git a/src/env.c b/src/env.c new file mode 100644 index 0000000..8105bf4 --- /dev/null +++ b/src/env.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ +/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void getenvlst(t_env **env, char **en) +{ + char *tmp; + int i; + t_env *current; + + i = 0; + while (en[i] != NULL) + { + tmp = ft_strchr(en[i], '='); + tmp = '\0'; + current = *env; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + i++; + } +} + +void free_envlst(t_env **env) +{ + t_env *cur; + t_env *new; + + cur = *env; + while (cur) + { + new = cur->next; + free(cur->name); + free(cur->value); + free(cur); + cur = new; + } +} \ No newline at end of file -- cgit v1.2.3 From d05140c9c6bacce28b37124fbda48ea3a39ac486 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Thu, 17 Oct 2024 15:48:50 +0200 Subject: fixed *tmp and added libft in env header --- include/env.h | 4 +++- src/env.c | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/include/env.h b/include/env.h index 5a65333..e774002 100644 --- a/include/env.h +++ b/include/env.h @@ -6,10 +6,12 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:47:30 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + typedef struct s_env { char *name; diff --git a/src/env.c b/src/env.c index 8105bf4..c71b2cb 100644 --- a/src/env.c +++ b/src/env.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:47:11 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,7 +22,7 @@ void getenvlst(t_env **env, char **en) while (en[i] != NULL) { tmp = ft_strchr(en[i], '='); - tmp = '\0'; + *tmp = '\0'; current = *env; current = malloc(sizeof(t_env)); current->name = ft_strdup(en[i]); -- cgit v1.2.3 From e7faa6525a007e463c06a20e438a2253287011f1 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 17 Oct 2024 16:06:53 +0200 Subject: yes --- Makefile | 2 +- include/env.h | 6 ++++-- include/minishell.h | 6 ++++-- src/env.c | 19 ++++++++++++++++--- src/interpreter.c | 21 +++++++++++++-------- src/main.c | 10 +++++++--- src/repl.c | 7 +++++-- 7 files changed, 50 insertions(+), 21 deletions(-) diff --git a/Makefile b/Makefile index 827c317..283db1a 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ free_token.c new_node.c free_node.c tokenizer.c parser.c \ - parse_cmd.c collect_redirs.c print_ast.c + parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/env.h b/include/env.h index 5a65333..ee01181 100644 --- a/include/env.h +++ b/include/env.h @@ -6,10 +6,12 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:59:59 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "libft.h" + typedef struct s_env { char *name; @@ -19,4 +21,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); diff --git a/include/minishell.h b/include/minishell.h index 6997b15..2319e8b 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:18:44 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:58:28 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" +#include "get_next_line.h" +#include "libft.h" void getenvlst(t_env **env, char **en) { @@ -22,7 +24,7 @@ void getenvlst(t_env **env, char **en) while (en[i] != NULL) { tmp = ft_strchr(en[i], '='); - tmp = '\0'; + *tmp = '\0'; current = *env; current = malloc(sizeof(t_env)); current->name = ft_strdup(en[i]); @@ -47,4 +49,15 @@ void free_envlst(t_env **env) free(cur); cur = new; } -} \ No newline at end of file +} + +char *env_get(t_env *env, char *name) +{ + while (env != NULL) + { + if (!ft_strncmp(env->name, name, ft_strlen(name))) + return (env->value); + env = env->next; + } + return (NULL); +} diff --git a/src/interpreter.c b/src/interpreter.c index 2a09e6d..abaeb47 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,21 +6,21 @@ /* By: dkaiser type == PIPE_NODE) - return (eval_pipe(&node->content.pipe)); + return (eval_pipe(&node->content.pipe, env)); else if (node->type == CMD_NODE) - return (eval_cmd(&node->content.cmd)); + return (eval_cmd(&node->content.cmd, env)); else { panic(UNREACHABLE); @@ -28,12 +28,17 @@ int eval(t_node *node) } } -static int eval_pipe(t_pipe *pipe) +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) +static int eval_cmd(t_cmd *cmd, t_env *env) { + printf("%s\n", cmd->args[0]); + printf("PATH=%s\n", env_get(env, "PATH")); return (0); } diff --git a/src/main.c b/src/main.c index 8523b9e..64bc312 100644 --- a/src/main.c +++ b/src/main.c @@ -6,15 +6,19 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:03 by dkaiser #+# #+# */ -/* Updated: 2024/07/18 16:44:14 by chuhlig ### ########.fr */ +/* Updated: 2024/10/17 15:34:02 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" -int main(void) +int main(int argc, char *argv[], char *envp[]) { + t_env *env; + if (!argc && !argv) + return (1); if (init()) return (1); - repl("Minishell $ "); + getenvlst(&env, envp); + repl("Minishell $ ", env); } diff --git a/src/repl.c b/src/repl.c index d590fec..931e25e 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,14 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/09/13 16:26:35 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 15:25:48 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" #include "token.h" -void repl(const char *prompt) +void repl(const char *prompt, t_env *env) { char *input; t_token *token_list; @@ -31,7 +31,10 @@ void repl(const char *prompt) tokenizer(input, &token_list, '\0'); lines = parse(token_list); if (lines) + { print_ast(lines->content); + eval(lines->content, env); + } free(input); } } -- cgit v1.2.3 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 --- Makefile | 3 ++- include/env.h | 8 ++++---- include/minishell.h | 5 +++-- src/interpreter.c | 5 ++--- src/main.c | 5 +++-- 5 files changed, 14 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 283db1a..3ae1359 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,8 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ free_token.c new_node.c free_node.c tokenizer.c parser.c \ - parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c + parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ + get_cmd_path.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/env.h b/include/env.h index ee01181..f22ebdd 100644 --- a/include/env.h +++ b/include/env.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:59:59 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 17:01:27 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,6 +19,6 @@ typedef struct s_env struct s_env *next; } t_env; -void getenvlst(t_env **env, char **en); -void free_envlst(t_env **env); -char *env_get(t_env *env, char *name); +void getenvlst(t_env **env, char **en); +void free_envlst(t_env **env); +char *env_get(t_env *env, char *name); diff --git a/include/minishell.h b/include/minishell.h index 2319e8b..c84fda3 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -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 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(-) 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(-) 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(-) 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 +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/10/17 15:58:28 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 15:48:56 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -- cgit v1.2.3 From 44378a79d5c6b742e8dbb03e5025b51a45d0e881 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:50:31 +0200 Subject: norm --- include/env.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/env.h b/include/env.h index ee01181..3bf6bd6 100644 --- a/include/env.h +++ b/include/env.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:59:59 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 17:08:01 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,4 +21,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_get(t_env *env, char *name); -- cgit v1.2.3 From cecb3e3560c395f2355a068624a7f6ca3d6b5655 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:51:12 +0200 Subject: adjusted header for env --- include/minishell.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/include/minishell.h b/include/minishell.h index 2319e8b..7b6226e 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* minishell.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:25:58 by dkaiser ### ########.fr */ +/* Updated: 2024/10/22 16:52:26 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,7 +29,7 @@ int init(void); int init_signal_handling(void); -void repl(const char *prompt, t_env *env); +void repl(const char *prompt, t_env **env); t_list *parse(t_token *tokens); t_node *parse_cmd(t_token *tokens); @@ -37,5 +37,5 @@ t_redirection *collect_redirs(t_token **tokens); void print_ast(t_node *ast); -int eval(t_node *node, t_env *env); +int eval(t_node *node, t_env **env); #endif -- cgit v1.2.3 From 46003a3dcff9f8cdbc89f0c371785fe3715b0769 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:53:59 +0200 Subject: assigned null to en struct against segfault --- src/main.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main.c b/src/main.c index 64bc312..3672fdd 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/25 15:53:10 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,13 @@ int main(int argc, char *argv[], char *envp[]) { - t_env *env; + t_env *env; + + env = NULL; if (!argc && !argv) return (1); if (init()) return (1); getenvlst(&env, envp); - repl("Minishell $ ", env); + repl("Minishell $ ", &env); } -- cgit v1.2.3 From 32cba376c211abe3c96f0d39b4d4eff65c104c0a Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:54:49 +0200 Subject: adjusted function prototype --- src/repl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/repl.c b/src/repl.c index 931e25e..9fae849 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,14 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 15:25:48 by dkaiser ### ########.fr */ +/* Updated: 2024/10/22 17:05:14 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" #include "token.h" -void repl(const char *prompt, t_env *env) +void repl(const char *prompt, t_env **env) { char *input; t_token *token_list; -- cgit v1.2.3 From ca4acea03cda19c2a0f0fd168d3c8fd418d71e04 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 15:56:05 +0200 Subject: adjuste input parameter for builtins --- src/interpreter.c | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/src/interpreter.c b/src/interpreter.c index abaeb47..13f10ed 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -3,22 +3,23 @@ /* ::: :::::::: */ /* interpreter.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ -/* Updated: 2024/10/17 16:01:23 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 15:46:53 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include static int eval_pipe(t_pipe *pipe, t_env *env); -static int eval_cmd(t_cmd *cmd, t_env *env); +static int eval_cmd(t_cmd *cmd, t_env **env); -int eval(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)); + return (eval_pipe(&node->content.pipe, *env)); else if (node->type == CMD_NODE) return (eval_cmd(&node->content.cmd, env)); else @@ -31,14 +32,24 @@ int eval(t_node *node, t_env *env) 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); + 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) +static int eval_cmd(t_cmd *cmd, t_env **env) { - printf("%s\n", cmd->args[0]); - printf("PATH=%s\n", env_get(env, "PATH")); + if (ft_strncmp(cmd->args[0], "pwd", 4) == 0) + pwd(*env); + else if (ft_strncmp(cmd->args[0], "echo", 5) == 0) + echo(cmd->args); + else if (ft_strncmp(cmd->args[0], "env", 4) == 0) + ft_env(*env); + else if (ft_strncmp(cmd->args[0], "cd", 3) == 0) + cd(env, cmd->args); + else if (ft_strncmp(cmd->args[0], "unset", 6) == 0) + unset(cmd->args, env); + else if (ft_strncmp(cmd->args[0], "export", 7) == 0) + export(cmd->args, env); return (0); } -- cgit v1.2.3 From 36b693a56d4f0d2841d7323ca7ae8df900c611dc Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Fri, 25 Oct 2024 21:00:39 +0200 Subject: restored merge conflict error" " --- src/execute_cmd.c | 21 +++++++++++-- src/interpreter.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 2 deletions(-) diff --git a/src/execute_cmd.c b/src/execute_cmd.c index fa7677f..6386fc0 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* execute_cmd.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/21 13:58:56 by dkaiser #+# #+# */ -/* Updated: 2024/10/25 13:31:16 by dkaiser ### ########.fr */ +/* Updated: 2024/10/25 20:59:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,3 +57,20 @@ int execute_cmd(t_cmd *cmd, t_env *env) result = execve(cmd->args[0], cmd->args, env_to_strlst(env)); return (result); } + +static int eval_cmd(t_cmd *cmd, t_env **env) +{ + if (ft_strncmp(cmd->args[0], "pwd", 4) == 0) + pwd(*env); + else if (ft_strncmp(cmd->args[0], "echo", 5) == 0) + echo(cmd->args); + else if (ft_strncmp(cmd->args[0], "env", 4) == 0) + ft_env(*env); + else if (ft_strncmp(cmd->args[0], "cd", 3) == 0) + cd(env, cmd->args); + else if (ft_strncmp(cmd->args[0], "unset", 6) == 0) + unset(cmd->args, env); + else if (ft_strncmp(cmd->args[0], "export", 7) == 0) + export(cmd->args, env); + return (0); +} \ No newline at end of file diff --git a/src/interpreter.c b/src/interpreter.c index e69de29..c6a4a00 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -0,0 +1,90 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* interpreter.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/05 13:15:24 by dkaiser #+# #+# */ +/* Updated: 2024/10/25 20:59:16 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "debug_tools.h" +#include "minishell.h" +#include +#include +#include +#include +#include + +static int eval_rec(t_node *node, t_env *env, int in_fd); + +int eval(t_node *node, t_env *env) +{ + pid_t pid; + int result; + + if (node == NULL) + return (EXIT_FAILURE); + result = 0; + pid = fork(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + if (pid == 0) + { + result = eval_rec(node, env, STDIN_FILENO); + exit(result); + } + else + { + waitpid(pid, &result, 0); + } + return (result); +} + +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(); + if (pid < 0) + { + return (EXIT_FAILURE); + } + 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 + { + close(p[1]); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); + } + } + 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