From 8cbba6da72ddd04e358bdb893e700702f92adacd Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Sun, 11 Aug 2024 15:15:06 +0200 Subject: fixed |> fixed norm added new function --- include/token.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/token.h b/include/token.h index d7ff9f9..80ace03 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 13:23:27 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 13:46:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next); void free_token(t_token *token); -void tokenizer(char *s, t_token **token_list); +void tokenizer(char *s, t_token **token_list, + char quote_check); #endif \ No newline at end of file -- 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(-) (limited to 'include') 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(-) (limited to 'include') 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 b23715a8077cff0241b721889f17b3c3665d38f1 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 21 Oct 2024 15:08:10 +0200 Subject: Add basic command execution --- Makefile | 2 +- include/env.h | 3 ++- include/minishell.h | 3 ++- src/env.c | 5 +++-- src/env_to_strlst.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/execute_cmd.c | 38 +++++++++++++++++++++++++++++++++++ src/interpreter.c | 12 +++++++++-- src/main.c | 3 ++- 8 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 src/env_to_strlst.c create mode 100644 src/execute_cmd.c (limited to 'include') diff --git a/Makefile b/Makefile index 3ae1359..7828678 100644 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ 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 \ - get_cmd_path.c + get_cmd_path.c env_to_strlst.c execute_cmd.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/env.h b/include/env.h index f22ebdd..1e6941d 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 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 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(-) (limited to 'include') 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(-) (limited to 'include') 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 da80405b345b58f9e1bd83039b1df3771b5ca193 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Tue, 14 Jan 2025 18:18:58 +0100 Subject: update for exit bit norm removed staic stuff added prompt flag --- .vscode/c_cpp_properties.json | 18 + .vscode/launch.json | 13 + .vscode/settings.json | 64 +++ .vscode/tasks.json | 29 + garbage | 1229 +++++++++++++++++++++++++++++++++++++++++ include/env.h | 5 +- include/minishell.h | 8 +- lib/libft/ft_strcat.c | 4 +- lib/libft/ft_strcmp.c | 4 +- lib/libft/ft_strcpy.c | 4 +- src/builtins_part_one.c | 58 +- src/builtins_part_two.c | 27 +- src/collect_redirs.c | 132 +---- src/env.c | 23 +- src/env_to_strlst.c | 4 +- src/execute_cmd.c | 155 +++--- src/format_string.c | 16 +- src/init.c | 4 +- src/interpreter.c | 473 ++-------------- src/main.c | 10 +- src/repl.c | 36 +- src/signal_handling.c | 4 +- src/tokenizer.c | 24 +- teest.txt | 1 + 24 files changed, 1635 insertions(+), 710 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json create mode 100644 garbage create mode 100644 teest.txt (limited to 'include') diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..94b2ae4 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "macos-clang-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "macos-clang-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 + } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..86fa44a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "lldb", + "request": "launch", + "args": [], + "cwd": "/Users/chuhlig/Desktop/merged_minishell/", + "program": "/Users/chuhlig/Desktop/merged_minishell/minishell" + } + ] + } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..91fa952 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,64 @@ +{ + "C_Cpp_Runner.cCompilerPath": "clang", + "C_Cpp_Runner.cppCompilerPath": "clang++", + "C_Cpp_Runner.debuggerPath": "lldb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "minishell.h": "c", + "ast.h": "c", + "env.h": "c" + } + } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..4ede37a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: clang build active file", + "command": "/usr/bin/clang", + "args": [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/garbage b/garbage new file mode 100644 index 0000000..5b5afd8 --- /dev/null +++ b/garbage @@ -0,0 +1,1229 @@ +_=/Users/chuhlig/Desktop/merged_minishell/minishell/./minishell +TERM=xterm-256color +USER_ZDOTDIR=/Users/chuhlig +ZDOTDIR=/Users/chuhlig +VSCODE_INJECTION=1 +VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-e47956ed09.sock +VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js +VSCODE_GIT_ASKPASS_EXTRA_ARGS= +VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) +GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh +COLORTERM=truecolor +LANG=en_US.UTF-8 +TERM_PROGRAM_VERSION=1.95.3 +TERM_PROGRAM=vscode +OLDPWD=/Users/chuhlig/Desktop/merged_minishell +PWD=/Users/chuhlig/Desktop/merged_minishell/minishell +SHLVL=1 +ORIGINAL_XDG_CURRENT_DESKTOP=undefined +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +__CF_USER_TEXT_ENCODING=0x1928E:0x0:0x2 +SHELL=/bin/zsh +HOME=/Users/chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ECcBYJZkxn/Listeners +LOGNAME=chuhlig +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin +MallocNanoZone=0 +USER=chuhlig +_=/Users/chuhlig/Desktop/merged_minishell/minishell/./minishell +TERM=xterm-256color +USER_ZDOTDIR=/Users/chuhlig +ZDOTDIR=/Users/chuhlig +VSCODE_INJECTION=1 +VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-e47956ed09.sock +VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js +VSCODE_GIT_ASKPASS_EXTRA_ARGS= +VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) +GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh +COLORTERM=truecolor +LANG=en_US.UTF-8 +TERM_PROGRAM_VERSION=1.95.3 +TERM_PROGRAM=vscode +OLDPWD=/Users/chuhlig/Desktop/merged_minishell +PWD=/Users/chuhlig/Desktop/merged_minishell/minishell +SHLVL=1 +ORIGINAL_XDG_CURRENT_DESKTOP=undefined +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +__CF_USER_TEXT_ENCODING=0x1928E:0x0:0x2 +SHELL=/bin/zsh +HOME=/Users/chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ECcBYJZkxn/Listeners +LOGNAME=chuhlig +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin +MallocNanoZone=0 +USER=chuhlig + + +//some test case about built in command + bash-3.2$ ls +Applications Music +Desktop Pictures +Documents francinette +Downloads getting-started +Library goinfre +Movies setup_docker_environment +bash-3.2$ pwd +/Users/chuhlig +bash-3.2$ ls | cd / | ls +Applications Music +Desktop Pictures +Documents francinette +Downloads getting-started +Library goinfre +Movies setup_docker_environment +bash-3.2$ pwd +/Users/chuhlig +bash-3.2$ env +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ export test=test | unset USER +bash-3.2$ env +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ env | grep test +bash-3.2$ ls | cd / | env +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ ls | cd / | env +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ ls | cd / | env +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ ls | cd / | env | cd / +bash-3.2$ pwd +/Users/chuhlig +bash-3.2$ ls | cd / | env | cd ~ +bash-3.2$ ls | cd / | env | cd - +bash: cd: OLDPWD not set +bash-3.2$ ls | cd - | env +bash: cd: OLDPWD not set +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ +bash-3.2$ ls | cd - | env +bash: cd: OLDPWD not set +TERM_PROGRAM=iTerm.app +TERM=xterm-256color +SHELL=/bin/zsh +TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ +TERM_PROGRAM_VERSION=3.5.10 +TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +USER=chuhlig +SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners +__CF_USER_TEXT_ENCODING=0x0:0:0 +TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF +TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo +PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities +PWD=/Users/chuhlig +LANG=en_US.UTF-8 +ITERM_PROFILE=Default +XPC_FLAGS=0x0 +XPC_SERVICE_NAME=0 +SHLVL=2 +HOME=/Users/chuhlig +COLORFGBG=7;0 +LC_TERMINAL_VERSION=3.5.10 +ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 +LOGNAME=chuhlig +LC_TERMINAL=iTerm2 +COLORTERM=truecolor +_=/usr/bin/env +bash-3.2$ + + +////////////////////////////////////////////////////////////////// + +static int handle_redirections(t_redirection *redirs) { + if (redirs[0].type == INPUT_LIMITER) { + int pipe_fds[2]; + if (pipe(pipe_fds) == -1) { + perror("pipe"); + return -1; + } + write(pipe_fds[1], redirs[0].specifier, strlen(redirs[0].specifier)); + close(pipe_fds[1]); + dup2(pipe_fds[0], STDIN_FILENO); + close(pipe_fds[0]); + } else if (redirs[0].type == INPUT_FILE) { + int fd = open(redirs[0].specifier, O_RDONLY); + if (fd == -1) { + perror(redirs[0].specifier); + return -1; + } + dup2(fd, STDIN_FILENO); + close(fd); + } + + if (redirs[1].type == OUTPUT_OVERRIDE) { + int fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd == -1) { + perror(redirs[1].specifier); + return -1; + } + dup2(fd, STDOUT_FILENO); + close(fd); + } else if (redirs[1].type == OUTPUT_APPEND) { + int fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd == -1) { + perror(redirs[1].specifier); + return -1; + } + dup2(fd, STDOUT_FILENO); + close(fd); + } + return 0; +} + +static int eval_rec(t_node *node, t_env **env, int in_fd) { + pid_t pid; + int p[2], result; + + if (node->type == PIPE_NODE) { + pipe(p); + pid = fork(); + if (pid == 0) { + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); + result = eval_rec(node->content.pipe.left, env, in_fd); + 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"); + result = EXIT_FAILURE; + } + return result; +} + +int eval(t_node *node, t_env **env) { + return eval_rec(node, env, STDIN_FILENO); +} + +int execute_cmd(t_cmd *cmd, t_env **env) { + if (handle_redirections(cmd->redirs) == -1) { + return EXIT_FAILURE; + } + + if (is_builtin(cmd->args[0])) { + return execute_builtin(cmd->args, env); + } else { + pid_t pid = fork(); + if (pid == 0) { + char *cmd_path = get_cmd_path(cmd->args[0], *env); + if (!cmd_path) { + fprintf(stderr, "%s: command not found\n", cmd->args[0]); + exit(EXIT_FAILURE); + } + execve(cmd_path, cmd->args, env_to_strlst(*env)); + perror("execve"); + exit(EXIT_FAILURE); + } else { + int status; + waitpid(pid, &status, 0); + return WEXITSTATUS(status); + } + } +} +have to update env list/tokenlist order or update the existing code to handle the order of the tokenlist/env list + +echo "Hello" | grep H this works + +also more to do on heredoc + +and for the for part of domenic removing the " ' frome the string +beside + + + +t_redirection *collect_redirs(t_token **tokens) +{ + t_redirection *result; + t_token *cur; + + cur = *tokens; + result = malloc(sizeof(t_redirection) * 2); + if (result == NULL) + return (free_tokens(*tokens), NULL); + set_redir(&result[0], 0, NULL); + set_redir(&result[1], 0, NULL); + while (cur != NULL && cur->next != NULL) + { + if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) + collect_and_check_redir(result, &cur); + else if (cur->type == REDIR_TOKEN) + return (free(result), NULL); + else + cur = cur->next; + } + if (cur && cur->type == REDIR_TOKEN) + return (free(result), NULL); + return (result); +} + +static void collect_and_check_redir(t_redirection *result, t_token **cur) +{ + char *heredoc_data; + t_token *next_token; + + heredoc_data = NULL; + if ((*cur)->content.redir_type == INPUT_LIMITER) + { + // Handle Here Document (<<) + heredoc_data = read_heredoc((*cur)->next->content.string); + if (!heredoc_data) + { + perror("Heredoc allocation failed"); + return ; + } + set_redir(&result[0], INPUT_LIMITER, heredoc_data); + } + else if ((*cur)->content.redir_type == INPUT_FILE) + { + // Handle Input File (<) + set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string)); + } + else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) + { + // Handle Output File Overwrite (>) + set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string)); + } + else if ((*cur)->content.redir_type == OUTPUT_APPEND) + { + // Handle Output File Append (>>) + set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string)); + } + else + { + // Handle unexpected cases + printf("Unknown redirection type encountered\n"); + } + // Advance the token pointer to skip the redirection token and its argument + next_token = (*cur)->next; + free_token_and_connect(*cur); // Free the current redirection token + if (next_token) + { + *cur = next_token->next; // Move to the next token after the argument + } + else + { + *cur = NULL; // No more tokens + } +} + +collect args apassen x +alsso fur arg x +muss in rpl parse x +und in parse cmd collect args and x + +format args seems to work later test x + + +Test 1: ✅ echo hello world +Test 2: ✅ echo "hello world" +Test 3: ✅ echo 'hello world' +Test 4: ✅ echo hello'world' +Test 5: ✅ echo hello""world +Test 6: ✅ echo '' +Test 7: ✅ echo "$PWD" +Test 8: ✅ echo '$PWD' + + + +Test 9: ❌ echo "aspas ->'" string formater checken +mini output = (aspas ->") +bash output = (aspas ->') +Test 10: ❌ echo "aspas -> ' " +mini output = (aspas -> ") +bash output = (aspas -> ' ) +Test 11: ✅ echo 'aspas ->"' +Test 12: ✅ echo 'aspas -> " ' + + +Test 13: ❌ echo "> >> < * ? [ ] | ; [ ] || && ( ) & # $ <<" +mini output = (> >> < README.md bash.supp bash_outfiles bonus bonus_bonus builtins extras local.supp loop.out manual_tests mini_outfiles os_specific outfiles pipes redirects syntax test_files tester wildcards ? [ ] | ; [ ] || && ( ) & # ) +bash output = (> >> < README.md bash.supp bash_outfiles bonus bonus_bonus builtins extras local.supp loop.out manual_tests mini_outfiles os_specific outfiles pipes redirects syntax test_files tester wildcards ? [ ] | ; [ ] || && ( ) & # $ <<) +Test 14: ✅ echo '> >> < * ? [ ] | ; [ ] || && ( ) & # $ <<' +Test 15: ❌ echo "exit_code ->$? user ->$USER home -> $HOME" +mini output = (exit_code ->/Users/dkaiser) +bash output = (exit_code ->0 user ->dkaiser home -> /Users/dkaiser) +Test 16: ✅ echo 'exit_code ->$? user ->$USER home -> $HOME' + +string formater checken +Test 17: ❌ echo "$" +mini output = (../minishell) +bash output = ($) +Test 18: ✅ echo '$' +Test 19: ❌ echo $ +mini output = (../minishell) +bash output = ($) +Test 20: ❌ echo $? +mini output = () +bash output = (0) +Test 21: ❌ echo $?HELLO +mini output = () +bash output = (0HELLO) +Test 22: ✅ pwd +Test 23: ✅ pwd oi +Test 24: ✅ export hello +Test 25: ✅ export HELLO=123 +Test 26: ❌ export A- +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( not a valid identifier) +Test 27: ✅ export HELLO=123 A +Test 28: ✅ export HELLO="123 A-" +Test 29: ✅ export hello world +Test 30: ❌ export HELLO-=123 +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( not a valid identifier) +Test 31: ❌ export = +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( not a valid identifier) +Test 32: ❌ export 123 +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( not a valid identifier) +Test 33: ✅ unset +Test 34: ✅ unset HELLO +Test 35: ✅ unset HELLO1 HELLO2 +Test 36: ✅ unset HOME +Test 37: ✅ unset PATH +Test 38: ✅ unset SHELL +Test 39: ❌ cd $PWD +mini exit code = 139 +bash exit code = 0 +Test 40: ❌ cd $PWD hi +mini exit code = 139 +bash exit code = 0 +Test 41: ❌ cd 123123 +mini exit code = 139 +bash exit code = 1 +mini error = () +bash error = ( No such file or directory) +Test 42: ❌ exit 123 +mini exit code = 0 +bash exit code = 123 +Test 43: ❌ exit 298 +mini exit code = 0 +bash exit code = 42 +Test 44: ❌ exit +100 +mini exit code = 0 +bash exit code = 100 +Test 45: ❌ exit "+100" +mini exit code = 0 +bash exit code = 100 +Test 46: ❌ exit +"100" +mini exit code = 0 +bash exit code = 100 +Test 47: ❌ exit -100 +mini exit code = 0 +bash exit code = 156 +Test 48: ❌ exit "-100" +mini exit code = 0 +bash exit code = 156 +Test 49: ❌ exit -"100" +mini exit code = 0 +bash exit code = 156 +Test 50: ❌ exit hello +mini exit code = 0 +bash exit code = 255 +mini error = () +bash error = ( numeric argument required) +Test 51: ❌ exit 42 world +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( too many arguments) +Test 52: ❌ +mini exit code = +bash exit code = 0 +———————————— pipes +Test 53: ✅ env | sort | grep -v SHLVL | grep -v ^_ +Test 54: ✅ cat ./test_files/infile_big | grep oi +Test 55: ✅ cat minishell.h | grep ");"$ +Test 56: ✅ export GHOST=123 | env | grep GHOST +———————————— redirects +Test 57: ✅ grep hi <./test_files/infile +Test 58: ✅ grep hi "./outfiles/outfile01 +Test 82: ✅ ls > ./outfiles/outfile01 +Test 83: ✅ echo hi > ./outfiles/outfile01 bye +Test 84: ❌ ls >./outfiles/outfile01 >./outfiles/outfile02 +Only in ./bash_outfiles: outfile01 +mini outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +bash outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +Test 85: ❌ ls >./outfiles/outfile01 >./test_files/invalid_permission +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +Test 86: ❌ ls >"./outfiles/outfile with spaces" +Only in ./bash_outfiles: outfile with spaces +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +mini exit code = 1 +bash exit code = 0 +mini error = ( No such file or directory) +bash error = () +Test 87: ❌ ls >"./outfiles/outfile""1""2""3""4""5" +Only in ./bash_outfiles: outfile12345 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +mini exit code = 1 +bash exit code = 0 +mini error = ( No such file or directory) +bash error = () +Test 88: ❌ ls >"./outfiles/outfile01" >./test_files/invalid_permission >"./outfiles/outfile02" +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +mini error = ( No such file or directory) +bash error = ( Permission denied) +Test 89: ✅ ls >./test_files/invalid_permission >"./outfiles/outfile01" >./test_files/invalid_permission +Test 90: ❌ cat <"./test_files/infile" >"./outfiles/outfile01" +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +hi +hello +world +42 +mini exit code = 1 +bash exit code = 0 +mini error = ( No such file or directory) +bash error = () +Test 91: ✅ echo hi >./outfiles/outfile01 | echo bye +Test 92: ❌ echo hi >./outfiles/outfile01 >./outfiles/outfile02 | echo bye +Only in ./bash_outfiles: outfile01 +mini outfiles: +hi +bash outfiles: +hi +Test 93: ✅ echo hi | echo >./outfiles/outfile01 bye +Test 94: ❌ echo hi | echo bye >./outfiles/outfile01 >./outfiles/outfile02 +Only in ./bash_outfiles: outfile01 +mini outfiles: +bye +bash outfiles: +bye +Test 95: ✅ echo hi >./outfiles/outfile01 | echo bye >./outfiles/outfile02 +Test 96: ❌ echo hi >./outfiles/outfile01 >./test_files/invalid_permission | echo bye +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +Test 97: ✅ echo hi >./test_files/invalid_permission | echo bye +Test 98: ❌ echo hi >./test_files/invalid_permission >./outfiles/outfile01 | echo bye +Only in ./mini_outfiles: outfile01 +mini outfiles: +hi +bash outfiles: +cat: ./bash_outfiles/*: No such file or directory +mini error = () +bash error = ( Permission denied) +Test 99: ✅ echo hi | echo bye >./test_files/invalid_permission +Test 100: ❌ echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +mini exit code = 139 +bash exit code = 1 +mini error = () +bash error = ( Permission denied) +Test 101: ❌ echo hi | echo bye >./test_files/invalid_permission >./outfiles/outfile01 +Only in ./mini_outfiles: outfile01 +mini outfiles: +bye +bash outfiles: +cat: ./bash_outfiles/*: No such file or directory +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( Permission denied) +Test 102: ✅⚠️ cat <"./test_files/infile" >./test_files/invalid_permission +mini error = ( No such file or directory) +bash error = ( Permission denied) +Test 103: ✅⚠️ cat >./test_files/invalid_permission <"./test_files/infile" +mini error = ( No such file or directory) +bash error = ( Permission denied) +Test 104: ✅ cat ./outfiles/outfile01 +Test 105: ❌ cat >./outfiles/outfile01 ./test_files/invalid_permission +Test 107: ✅⚠️ cat >./test_files/invalid_permission ./outfiles/outfile01 ./test_files/invalid_permission +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +Test 109: ✅ ls >>./outfiles/outfile01 +Test 110: ✅ ls >> ./outfiles/outfile01 +Test 111: ✅ ls >>./outfiles/outfile01 >./outfiles/outfile01 +Test 112: ✅ ls >./outfiles/outfile01 >>./outfiles/outfile01 +Test 113: ❌ ls >./outfiles/outfile01 >>./outfiles/outfile01 >./outfiles/outfile02 +Only in ./bash_outfiles: outfile01 +mini outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +bash outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +Test 114: ❌ ls >>./outfiles/outfile01 >>./outfiles/outfile02 +Only in ./bash_outfiles: outfile01 +mini outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +bash outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +Test 115: ✅ ls >>./test_files/invalid_permission +Test 116: ❌ ls >>./test_files/invalid_permission >>./outfiles/outfile01 +Only in ./mini_outfiles: outfile01 +mini outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +bash outfiles: +cat: ./bash_outfiles/*: No such file or directory +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( Permission denied) +Test 117: ❌ ls >>./outfiles/outfile01 >>./test_files/invalid_permission +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +Test 118: ❌ ls >./outfiles/outfile01 >>./test_files/invalid_permission >>./outfiles/outfile02 +Only in ./bash_outfiles: outfile01 +Only in ./mini_outfiles: outfile02 +mini outfiles: +README.md +bash.supp +bash_outfiles +bonus +bonus_bonus +builtins +extras +local.supp +loop.out +manual_tests +mini_outfiles +os_specific +outfiles +pipes +redirects +syntax +test_files +tester +wildcards +bash outfiles: +mini exit code = 0 +bash exit code = 1 +mini error = () +bash error = ( Permission denied) +Test 119: ✅ ls >./test_files/invalid_permission >>./outfiles/outfile02 +Test 120: ✅⚠️ ls >>./test_files/invalid_permission >>./outfiles/outfile02 >./outfiles/outfile01 | echo bye +Test 122: ❌ echo hi >>./outfiles/outfile01 >>./outfiles/outfile02 | echo bye +Only in ./bash_outfiles: outfile01 +mini outfiles: +hi +bash outfiles: +hi +Test 123: ✅ echo hi | echo >>./outfiles/outfile01 bye +Test 124: ❌ echo hi | echo bye >>./outfiles/outfile01 >>./outfiles/outfile02 +Only in ./bash_outfiles: outfile01 +mini outfiles: +bye +bash outfiles: +bye +Test 125: ✅ echo hi >>./outfiles/outfile01 | echo bye >>./outfiles/outfile02 +Test 126: ✅ echo hi >>./test_files/invalid_permission | echo bye +Test 127: ❌ echo hi >>./test_files/invalid_permission >./outfiles/outfile01 | echo bye +Only in ./mini_outfiles: outfile01 +mini outfiles: +hi +bash outfiles: +cat: ./bash_outfiles/*: No such file or directory +mini error = () +bash error = ( Permission denied) +Test 128: ✅ echo hi | echo bye >>./test_files/invalid_permission +Test 129: ❌ echo hi | echo >>./outfiles/outfile01 bye >./test_files/invalid_permission +Only in ./bash_outfiles: outfile01 +mini outfiles: +cat: ./mini_outfiles/*: No such file or directory +bash outfiles: +Test 130: ✅ cat ./outfiles/outfile +Test 131: ✅ cat +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/12/24 16:21:50 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 16:50:55 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,6 +22,7 @@ typedef struct s_env struct s_env *next; } t_env; +void free_env_node(t_env *node); void getenvlst(t_env **env, char **en); void free_envlst(t_env **env); char *env_get(t_env *env, char *name); @@ -34,5 +35,7 @@ int echo(char **av); int pwd(t_env *env); int cd(t_env **env, char **args); int ft_env(t_env *env); +int builtin_exit(char **args, t_env **env); +t_env *env_new(char *name); #endif \ No newline at end of file diff --git a/include/minishell.h b/include/minishell.h index 028a4fc..9d586b1 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:05:11 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 15:29:46 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -25,11 +25,13 @@ # include # include # include +# include +# include int init(void); int init_signal_handling(void); -void repl(const char *prompt, t_env **env); +void repl(const char *prompt, t_env **env, int *promptflag); t_list *parse(t_token *tokens, t_env **env); t_node *parse_cmd(t_token *tokens, t_env **env); @@ -40,6 +42,6 @@ int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); - +int handle_redirections(t_redirection *redirs); #endif diff --git a/lib/libft/ft_strcat.c b/lib/libft/ft_strcat.c index 648c184..b21235b 100644 --- a/lib/libft/ft_strcat.c +++ b/lib/libft/ft_strcat.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/09 13:06:44 by chuhlig #+# #+# */ -/* Updated: 2025/01/09 13:07:15 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:09:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,4 +26,4 @@ char *ft_strcat(char *dest, char *src) } dest[i + j] = '\0'; return (dest); -} \ No newline at end of file +} diff --git a/lib/libft/ft_strcmp.c b/lib/libft/ft_strcmp.c index af7b2a1..a319c63 100644 --- a/lib/libft/ft_strcmp.c +++ b/lib/libft/ft_strcmp.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/18 19:03:14 by chuhlig #+# #+# */ -/* Updated: 2024/12/18 19:05:01 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:09:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,4 +20,4 @@ int ft_strcmp(char *s1, char *s2) while (s1[i] && s1[i] == s2[i]) i++; return (s1[i] - s2[i]); -} \ No newline at end of file +} diff --git a/lib/libft/ft_strcpy.c b/lib/libft/ft_strcpy.c index b5c612f..94a07f7 100644 --- a/lib/libft/ft_strcpy.c +++ b/lib/libft/ft_strcpy.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/09 14:38:30 by chuhlig #+# #+# */ -/* Updated: 2025/01/09 14:38:53 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:10:06 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,4 +22,4 @@ char *ft_strcpy(char *dest, char *src) } dest[i] = '\0'; return (dest); -} \ No newline at end of file +} diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index d64bb54..d8bdaa9 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/10 14:36:55 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 16:51:05 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,43 +41,22 @@ int echo(char **av) return (0); } -int pwd(t_env *env) +void exit_shell(t_env **env, int exit_status) { - while (env) - { - if (ft_strncmp(env->name, "PWD", 4) == 0) - { - ft_printf("%s\n", env->value); - break ; - } - env = env->next; - } - return (0); + free_envlst(env); + exit(exit_status); } -int ft_env(t_env *env) +int builtin_exit(char **args, t_env **env) { - while (env != NULL) - { - printf("%s", env->name); - printf("=%s\n", env->value); - env = env->next; - } - return (0); -} - -// int exit(char *av) -// { -// freenode free toke free sequence stop repl free env; -// clear history; -// } -//// + int exit_status; -void free_env_node(t_env *node) -{ - free(node->name); - free(node->value); - free(node); + if (args[1]) + exit_status = ft_atoi(args[1]); + else + exit_status = 0; + exit_shell(env, exit_status); + return (exit_status); } int unset(char **av, t_env **env) @@ -109,17 +88,6 @@ int unset(char **av, t_env **env) return (0); } -t_env *env_new(char *name) -{ - t_env *result; - - result = malloc(sizeof(t_env)); - if (!result) - return (NULL); - result->name = name; - return (result); -} - t_env *check_existing(t_env *env, char *av) { while (env) @@ -158,4 +126,4 @@ int export(char **av, t_env **env) } } return (0); -} \ No newline at end of file +} diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c index f54e04f..5af6c68 100644 --- a/src/builtins_part_two.c +++ b/src/builtins_part_two.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ -/* Updated: 2024/12/20 18:53:03 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:26:39 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -82,3 +82,28 @@ int cd(t_env **env, char **av) } return (0); } + +int pwd(t_env *env) +{ + while (env) + { + if (ft_strncmp(env->name, "PWD", 4) == 0) + { + ft_printf("%s\n", env->value); + break ; + } + env = env->next; + } + return (0); +} + +int ft_env(t_env *env) +{ + while (env != NULL) + { + printf("%s", env->name); + printf("=%s\n", env->value); + env = env->next; + } + return (0); +} diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 4b7b955..84ebd71 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,50 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 11:43:13 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 16:55:20 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -// static t_token *collect_redir(t_token **tokens, t_redirection *result, -// t_token *cur); -// static void collect_and_check_redir(t_token **tokens, t_redirection *result, -// t_token **cur); static void collect_and_check_redir(t_redirection *result, t_token **cur); static void set_redir(t_redirection *redir, int type, char *specifier); -// static int is_output_redir(int i); - -// static char *read_heredoc(char *delimiter) -// { -// char *line; -// char *result; -// size_t len; - -// result = NULL; -// while (1) -// { -// line = readline("> "); -// if (!line || ft_strcmp(line, delimiter) == 0) -// { -// free(line); -// break ; -// } -// if (result) -// len = ft_strlen(result); -// else -// len = 0; -// result = realloc(result, len + ft_strlen(line) + 2); -// if (!result) -// return (NULL); -// ft_strcpy(result + len, line); -// strcat(result, "\n"); -// free(line); -// } -// return (result); -// } - -//v2.0 static char *read_heredoc(char *delimiter) { @@ -74,8 +38,7 @@ static char *read_heredoc(char *delimiter) if (!temp) { perror("malloc"); - free(result); - return (NULL); + return (free(result), NULL); } if (result) { @@ -83,9 +46,7 @@ static char *read_heredoc(char *delimiter) free(result); } else - { temp[0] = '\0'; - } ft_strcat(temp, line); ft_strcat(temp, "\n"); result = temp; @@ -109,7 +70,7 @@ t_redirection *collect_redirs(t_token **tokens) while (cur != NULL && cur->next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur);// her is diff + collect_and_check_redir(result, &cur); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -120,62 +81,12 @@ t_redirection *collect_redirs(t_token **tokens) return (result); } -// static void collect_and_check_redir(t_token **tokens, t_redirection *result, -// t_token **cur) -// { -// int is_redir_only; - -// is_redir_only = 0; -// if ((*cur)->previous == NULL && (*cur)->next->next == NULL) -// is_redir_only = 1; -// *cur = collect_redir(tokens, result, *cur); -// if (is_redir_only) -// *tokens = NULL; -// } - -// static t_token *collect_redir(t_token **tokens, t_redirection *result, -// t_token *cur) -// { -// set_redir(&result[is_output_redir(cur->content.redir_type)], -// cur->content.redir_type, cur->next->content.string); -// cur = cur->next; -// free_token_and_connect(cur->previous); -// if (cur->next != NULL) -// { -// if (cur->previous == NULL) -// *tokens = cur->next; -// cur = cur->next; -// free_token_and_connect(cur->previous); -// } -// else -// { -// free_token(cur); -// return (NULL); -// } -// return (cur); -// } - static void set_redir(t_redirection *redir, int type, char *specifier) { redir->type = type; redir->specifier = specifier; } -// static int is_output_redir(int i) -// { -// if (i & (INPUT_FILE | INPUT_LIMITER)) -// return (0); -// else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) -// return (1); -// else -// { -// panic(UNREACHABLE); -// return (-1); -// } -// } - -//2.0 - static void collect_and_check_redir(t_redirection *result, t_token **cur) { char *heredoc_data; @@ -184,7 +95,6 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur) heredoc_data = NULL; if ((*cur)->content.redir_type == INPUT_LIMITER) { - // Handle Here Document (<<) heredoc_data = read_heredoc((*cur)->next->content.string); if (!heredoc_data) { @@ -194,35 +104,21 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur) set_redir(&result[0], INPUT_LIMITER, heredoc_data); } else if ((*cur)->content.redir_type == INPUT_FILE) - { - // Handle Input File (<) - set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string)); - } + set_redir(&result[0], INPUT_FILE, + ft_strdup((*cur)->next->content.string)); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - { - // Handle Output File Overwrite (>) - set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string)); - } + set_redir(&result[1], OUTPUT_OVERRIDE, + ft_strdup((*cur)->next->content.string)); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - { - // Handle Output File Append (>>) - set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string)); - } - else - { - // Handle unexpected cases - printf("Unknown redirection type encountered\n"); - } - // Advance the token pointer to skip the redirection token and its argument + set_redir(&result[1], OUTPUT_APPEND, + ft_strdup((*cur)->next->content.string)); next_token = (*cur)->next; - free_token_and_connect(*cur); // Free the current redirection token + free_token_and_connect(*cur); if (next_token) { - *cur = next_token->next; // Move to the next token after the argument - free_token_and_connect(next_token); // Free the argument token + *cur = next_token->next; + free_token_and_connect(next_token); } else - { - *cur = NULL; // No more tokens - } -} \ No newline at end of file + *cur = NULL; +} diff --git a/src/env.c b/src/env.c index 0213209..1972909 100644 --- a/src/env.c +++ b/src/env.c @@ -6,11 +6,12 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:36:14 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:44:34 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" +#include "minishell.h" #include void getenvlst(t_env **env, char **en) @@ -59,4 +60,22 @@ char *env_get(t_env *env, char *name) env = env->next; } return (NULL); -} \ No newline at end of file +} + +t_env *env_new(char *name) +{ + t_env *result; + + result = malloc(sizeof(t_env)); + if (!result) + return (NULL); + result->name = name; + return (result); +} + +void free_env_node(t_env *node) +{ + free(node->name); + free(node->value); + free(node); +} diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c index c4c98c3..2031c24 100644 --- a/src/env_to_strlst.c +++ b/src/env_to_strlst.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:22:36 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:10:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,4 +55,4 @@ static char *get_var_assign(t_env *cur) result = ft_strjoin(left_side, cur->value); free(left_side); return (result); -} \ No newline at end of file +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 7f93c5a..b9faf71 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,81 +6,96 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/09 16:07:01 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 16:56:24 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -#include -#include -#include -#include -#include -#include -// static void format_args(char **args, t_env *env); +int is_builtin(char *cmd) +{ + return ((ft_strcmp(cmd, "export") == 0) + || (ft_strcmp(cmd, "unset") == 0) + || (ft_strcmp(cmd, "cd") == 0) + || (ft_strcmp(cmd, "exit") == 0) + || (ft_strcmp(cmd, "echo") == 0) + || (ft_strcmp(cmd, "pwd") == 0) + || (ft_strcmp(cmd, "env") == 0)); +} -// int execute_cmd(t_cmd *cmd, t_env **env) -// { -// int result; -// char *cmd_path; -// int fd; +int execute_builtin(char **args, t_env **env) +{ + if (ft_strcmp(args[0], "export") == 0) + return (export(args, env)); + else if (ft_strcmp(args[0], "unset") == 0) + return (unset(args, env)); + else if (ft_strcmp(args[0], "cd") == 0) + return (cd(env, args)); + else if (ft_strcmp(args[0], "echo") == 0) + return (echo(args)); + else if (ft_strcmp(args[0], "pwd") == 0) + return (pwd(*env)); + else if (ft_strcmp(args[0], "env") == 0) + return (ft_env(*env)); + else if (ft_strcmp(args[0], "exit") == 0) + return (builtin_exit(args, env)); + return (1); +} -// if (ft_strncmp(cmd->args[0], "pwd", 4) == 0) -// return (pwd(*env)); -// else if (ft_strncmp(cmd->args[0], "echo", 5) == 0) -// return (echo(cmd->args)); -// else if (ft_strncmp(cmd->args[0], "env", 4) == 0) -// return (ft_env(*env)); -// else if (ft_strncmp(cmd->args[0], "cd", 3) == 0) -// return (cd(env, cmd->args)); -// else if (ft_strncmp(cmd->args[0], "unset", 6) == 0) -// return (unset(cmd->args, env)); -// else if (ft_strncmp(cmd->args[0], "export", 7) == 0) -// return (export(cmd->args, env)); -// format_args(cmd->args, *env); -// 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, 644); -// 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, 644); -// 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); -// } +int execute_cmd(t_cmd *cmd, t_env **env) +{ + char *cmd_path; + pid_t pid; + int status; + int original_stdout; + int original_stdin; + int result; -// static void format_args(char **args, t_env *env) -// { -// char *formatted; - -// while (*args != NULL) -// { -// formatted = format_string(*args, env); -// /* free(*args); */ -// *args = formatted; -// args++; -// } -// } \ No newline at end of file + original_stdout = dup(STDOUT_FILENO); + original_stdin = dup(STDIN_FILENO); + if (handle_redirections(cmd->redirs) == -1) + { + dup2(original_stdout, STDOUT_FILENO); + dup2(original_stdin, STDIN_FILENO); + close(original_stdout); + close(original_stdin); + return (EXIT_FAILURE); + } + if (is_builtin(cmd->args[0])) + { + result = execute_builtin(cmd->args, env); + dup2(original_stdout, STDOUT_FILENO); + dup2(original_stdin, STDIN_FILENO); + close(original_stdout); + close(original_stdin); + return (result); + } + pid = fork(); + if (pid == -1) + { + perror("fork"); + dup2(original_stdout, STDOUT_FILENO); + dup2(original_stdin, STDIN_FILENO); + close(original_stdout); + close(original_stdin); + return (EXIT_FAILURE); + } + if (pid == 0) + { + cmd_path = get_cmd_path(cmd->args[0], *env); + if (!cmd_path) + { + printf("command not found\n"); + exit(EXIT_FAILURE); + } + execve(cmd_path, cmd->args, env_to_strlst(*env)); + perror("execve"); + exit(EXIT_FAILURE); + } + waitpid(pid, &status, 0); + dup2(original_stdout, STDOUT_FILENO); + dup2(original_stdin, STDIN_FILENO); + close(original_stdout); + close(original_stdin); + return (WEXITSTATUS(status)); +} diff --git a/src/format_string.c b/src/format_string.c index bd7f703..7168ea0 100644 --- a/src/format_string.c +++ b/src/format_string.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:31:54 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:21:36 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,12 +17,6 @@ static void append_slice(char **dst, char *src, int start, int end); static void append_var(char **dst, char *src, int *pos, t_env *env); -enum e_format_mode -{ - LITERAL = 1, - VARIABLE = 2, -}; - char *format_string(char *str, t_env *env) { char *result; @@ -42,14 +36,14 @@ char *format_string(char *str, t_env *env) { append_slice(&result, str, start, pos); start = pos + 1; - mode ^= LITERAL; + mode ^= 1; } - if (str[pos] == '"' && !(mode & LITERAL)) + if (str[pos] == '"' && !(mode & 1)) { append_slice(&result, str, start, pos); start = pos + 1; } - if (str[pos] == '$' && !(mode & LITERAL)) + if (str[pos] == '$' && !(mode & 1)) { append_slice(&result, str, start, pos); append_var(&result, str, &pos, env); @@ -100,7 +94,7 @@ static void append_var(char **dst, char *src, int *pos, t_env *env) i = 0; *pos += 1; while (src[*pos + i] != '\0' && src[*pos + i] != '\'' && src[*pos - + i] != '"' && src[*pos + i] != '$') + + i] != '"' && src[*pos + i] != '$') { i++; } diff --git a/src/init.c b/src/init.c index fd2c8ac..f41e64a 100644 --- a/src/init.c +++ b/src/init.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* init.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:02:24 by dkaiser #+# #+# */ -/* Updated: 2024/06/24 15:25:57 by dkaiser ### ########.fr */ +/* Updated: 2025/01/13 17:45:46 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/interpreter.c b/src/interpreter.c index 33d945c..af1eb82 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,326 +6,73 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/11 12:52:57 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 16:59:45 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" #include "minishell.h" -#include -#include -#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); -// } - - -//old interpreter -// #include "minishell.h" -// #include - -// static int eval_pipe(t_pipe *pipe, t_env *env); -// static int eval_cmd(t_cmd *cmd, 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)); -// else -// { -// panic(UNREACHABLE); -// return (-1); -// } -// } - -// 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) -// { -// 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); -// } - -//v2.0 - -int is_builtin(char *cmd) +int handle_redirections(t_redirection *redirs) { - return ((ft_strcmp(cmd, "export") == 0) - || (ft_strcmp(cmd, "unset") == 0) - || (ft_strcmp(cmd, "cd") == 0) - || (ft_strcmp(cmd, "exit") == 0) - || (ft_strcmp(cmd, "echo") == 0) - || (ft_strcmp(cmd, "pwd") == 0) - || (ft_strcmp(cmd, "env") == 0)); -} + int fd; -int execute_builtin(char **args, t_env **env) -{ - if (ft_strcmp(args[0], "export") == 0) - return (export(args, env)); - else if (ft_strcmp(args[0], "unset") == 0) - return (unset(args, env)); - else if (ft_strcmp(args[0], "cd") == 0) - return (cd(env, args)); - else if (ft_strcmp(args[0], "exit") == 0) - return (EXIT_SUCCESS); - else if (ft_strcmp(args[0], "echo") == 0) - return (echo(args)); - else if (ft_strcmp(args[0], "pwd") == 0) - return (pwd(*env)); - else if (ft_strcmp(args[0], "env") == 0) - return (ft_env(*env)); - return (1); -} -// v1.0 - -// static int handle_redirections(t_redirection *redirs) -// { -// int fd; -// int pipe_fds[2]; - -// if (redirs[0].type == INPUT_LIMITER) -// { -// if (pipe(pipe_fds) == -1) -// { -// perror("pipe"); -// return (-1); -// } -// write(pipe_fds[1], redirs[0].specifier, strlen(redirs[0].specifier)); -// close(pipe_fds[1]); -// dup2(pipe_fds[0], STDIN_FILENO); -// close(pipe_fds[0]); -// } -// else if (redirs[0].type == INPUT_FILE) -// { -// fd = open(redirs[0].specifier, O_RDONLY); -// if (fd == -1) -// { -// perror(redirs[0].specifier); -// return (-1); -// } -// dup2(fd, STDIN_FILENO); -// close(fd); -// } -// if (redirs[1].type == OUTPUT_OVERRIDE) -// { -// fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); -// if (fd == -1) -// { -// perror(redirs[1].specifier); -// return (-1); -// } -// dup2(fd, STDOUT_FILENO); -// close(fd); -// } -// else if (redirs[1].type == OUTPUT_APPEND) -// { -// fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); -// if (fd == -1) -// { -// perror(redirs[1].specifier); -// return (-1); -// } -// dup2(fd, STDOUT_FILENO); -// close(fd); -// } -// return (0); -// } -//v 3.0 -static int handle_redirections(t_redirection *redirs) -{ - int fd; - - if (redirs[0].type == INPUT_FILE) - { - fd = open(redirs[0].specifier, O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - else if (redirs[0].type == INPUT_LIMITER) - { - // Handle here document (<<) - // Assuming heredoc_data is stored in redirs[0].specifier - fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - write(fd, redirs[0].specifier, strlen(redirs[0].specifier)); - close(fd); - fd = open("/tmp/heredoc_tmp", O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - if (redirs[1].type == OUTPUT_OVERRIDE) - { - fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - else if (redirs[1].type == OUTPUT_APPEND) - { - fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - return (0); + if (redirs[0].type == INPUT_FILE) + { + fd = open(redirs[0].specifier, O_RDONLY); + if (fd < 0) + { + perror(redirs[0].specifier); + return (-1); + } + dup2(fd, STDIN_FILENO); + close(fd); + } + else if (redirs[0].type == INPUT_LIMITER) + { + fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + { + perror("open"); + return (-1); + } + write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier)); + close(fd); + fd = open("/tmp/heredoc_tmp", O_RDONLY); + if (fd < 0) + { + perror("open"); + return (-1); + } + dup2(fd, STDIN_FILENO); + close(fd); + } + if (redirs[1].type == OUTPUT_OVERRIDE) + { + fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + { + perror("open"); + return (-1); + } + dup2(fd, STDOUT_FILENO); + close(fd); + } + else if (redirs[1].type == OUTPUT_APPEND) + { + fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd < 0) + { + perror("open"); + return (-1); + } + dup2(fd, STDOUT_FILENO); + close(fd); + } + return (0); } -//v 2.0 -// static int eval_rec(t_node *node, t_env **env, int in_fd) -// { -// pid_t pid; -// int p[2]; -// int result; -// int status; - -// if (node->type == PIPE_NODE) -// { -// pipe(p); -// pid = fork(); -// if (pid == 0) -// { -// close(p[0]); -// dup2(in_fd, STDIN_FILENO); -// dup2(p[1], STDOUT_FILENO); -// result = eval_rec(node->content.pipe.left, env, in_fd); -// exit(result); -// } -// else -// { -// close(p[1]); -// dup2(p[0], STDIN_FILENO); -// result = eval_rec(node->content.pipe.right, env, p[0]); -// waitpid(pid, &status, 0); -// } -// } -// else if (node->type == CMD_NODE) -// { -// result = execute_cmd(&node->content.cmd, env); -// } -// else -// { -// panic("UNREACHABLE"); -// result = EXIT_FAILURE; -// } -// return (result); -// } - -//v 3.0 -static int eval_rec(t_node *node, t_env **env, int in_fd) +int eval_rec(t_node *node, t_env **env, int in_fd) { pid_t pid; int p[2]; @@ -357,117 +104,13 @@ static int eval_rec(t_node *node, t_env **env, int in_fd) } } else if (node->type == CMD_NODE) - { result = execute_cmd(&node->content.cmd, env); - } else - { - printf("Handling unknown node type\n"); - panic("UNREACHABLE"); result = EXIT_FAILURE; - } return (result); } -int eval(t_node *node, t_env **env) +int eval(t_node *node, t_env **env) { return (eval_rec(node, env, STDIN_FILENO)); } -//v was auch immmer - -// int execute_cmd(t_cmd *cmd, t_env **env) -// { -// char *cmd_path; -// pid_t pid; -// int status; - -// if (handle_redirections(cmd->redirs) == -1) -// { -// return (EXIT_FAILURE); -// } -// if (is_builtin(cmd->args[0])) -// { -// return (execute_builtin(cmd->args, env)); -// } -// pid = fork(); -// if (pid == -1) -// { -// perror("fork"); -// return (EXIT_FAILURE); -// } -// if (pid == 0) -// { -// cmd_path = get_cmd_path(cmd->args[0], *env); -// if (!cmd_path) -// { -// printf("%s: command not found\n", cmd->args[0]); -// return (EXIT_FAILURE); -// } -// execve(cmd_path, cmd->args, env_to_strlst(*env)); -// perror("execve"); -// exit(EXIT_FAILURE); -// } -// waitpid(pid, &status, 0); -// return (WEXITSTATUS(status)); -// } -int execute_cmd(t_cmd *cmd, t_env **env) -{ - char *cmd_path; - pid_t pid; - int status; - int original_stdout; - int original_stdin; - - original_stdout = dup(STDOUT_FILENO); - original_stdin = dup(STDIN_FILENO); - - if (handle_redirections(cmd->redirs) == -1) - { - dup2(original_stdout, STDOUT_FILENO); - dup2(original_stdin, STDIN_FILENO); - close(original_stdout); - close(original_stdin); - return (EXIT_FAILURE); - } - - if (is_builtin(cmd->args[0])) - { - int result = execute_builtin(cmd->args, env); - dup2(original_stdout, STDOUT_FILENO); - dup2(original_stdin, STDIN_FILENO); - close(original_stdout); - close(original_stdin); - return (result); - } - - pid = fork(); - if (pid == -1) - { - perror("fork"); - dup2(original_stdout, STDOUT_FILENO); - dup2(original_stdin, STDIN_FILENO); - close(original_stdout); - close(original_stdin); - return (EXIT_FAILURE); - } - if (pid == 0) - { - cmd_path = get_cmd_path(cmd->args[0], *env); - if (!cmd_path) - { - printf("%s: command not found\n", cmd->args[0]); - exit(EXIT_FAILURE); - } - execve(cmd_path, cmd->args, env_to_strlst(*env)); - perror("execve"); - exit(EXIT_FAILURE); - } - waitpid(pid, &status, 0); - - dup2(original_stdout, STDOUT_FILENO); - dup2(original_stdin, STDIN_FILENO); - close(original_stdout); - close(original_stdin); - - return (WEXITSTATUS(status)); -} \ No newline at end of file diff --git a/src/main.c b/src/main.c index a53760c..8abc4c9 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/12/17 19:26:42 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 15:29:20 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,13 +14,17 @@ int main(int argc, char *argv[], char *envp[]) { - t_env *env; + t_env *env; + static int promptflag; env = NULL; + promptflag = 0; if (!argc && !argv) return (1); if (init()) return (1); getenvlst(&env, envp); - repl("Minishell $ ", &env); + repl("Minishell $ ", &env, &promptflag); + free_envlst(&env); + return (0); } diff --git a/src/repl.c b/src/repl.c index 7ff80a8..7eb6c0d 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,56 +6,38 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:01:44 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 15:39:58 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "../include/minishell.h" #include "token.h" -t_token *reverse_token_list(t_token *head) -{ - t_token *prev; - t_token *current; - t_token *next; - - prev = NULL; - current = head; - next = NULL; - while (current != NULL) - { - next = current->previous; - current->next = prev; - current->previous = next; - prev = current; - current = next; - } - return (prev); -} - -void repl(const char *prompt, t_env **env) +void repl(const char *prompt, t_env **env, int *promptflag) { char *input; t_token *token_list; t_list *lines; + (*promptflag)++; while (1) { input = readline(prompt); if (input == NULL) - return ; + { + if (*promptflag > 1) + (*promptflag)--; + printf("exit\n"); + break ; + } if (input[0] == '\0') continue ; add_history(input); token_list = NULL; tokenizer(input, &token_list, '\0'); - token_list = reverse_token_list(token_list); lines = parse(token_list, env); if (lines) - { - print_ast(lines->content); eval(lines->content, env); - } free(input); } } diff --git a/src/signal_handling.c b/src/signal_handling.c index a19fa94..c6273d0 100644 --- a/src/signal_handling.c +++ b/src/signal_handling.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* signal_handling.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:08:43 by dkaiser #+# #+# */ -/* Updated: 2024/06/25 13:33:26 by dkaiser ### ########.fr */ +/* Updated: 2025/01/14 14:11:29 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/tokenizer.c b/src/tokenizer.c index 26edd2e..6d16f1d 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,13 +6,33 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2025/01/11 15:22:07 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 15:58:41 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include "token.h" +t_token *reverse_token_list(t_token *head) +{ + t_token *prev; + t_token *current; + t_token *next; + + prev = NULL; + current = head; + next = NULL; + while (current != NULL) + { + next = current->previous; + current->next = prev; + current->previous = next; + prev = current; + current = next; + } + return (prev); +} + void print_token(t_token *token) { if (DEBUG) @@ -77,7 +97,6 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list) *token_list = new_token(PIPE_TOKEN, *token_list, NULL); else if (s[*i] == '\n') *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); - print_token(*token_list); if (s[*i] == '<' && s[*i + 1] == '<') (*i)++; if (s[*i] == '>' && s[*i + 1] == '>') @@ -111,4 +130,5 @@ void tokenizer(char *s, t_token **token_list, char quote_check) pos = i + 1; } } + *token_list = reverse_token_list(*token_list); } diff --git a/teest.txt b/teest.txt new file mode 100644 index 0000000..9daeafb --- /dev/null +++ b/teest.txt @@ -0,0 +1 @@ +test -- cgit v1.2.3 From 3e692dac63577011bd5862a66767ef412183f26b Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 14 Jan 2025 18:20:33 +0100 Subject: Some changes --- include/minishell.h | 4 ++-- src/builtins_part_one.c | 32 +++++++++++++++++++++++++++++--- src/env_to_strlst.c | 11 +++++++++-- src/format_string.c | 18 ++++++------------ src/main.c | 3 ++- src/repl.c | 4 ++-- 6 files changed, 50 insertions(+), 22 deletions(-) (limited to 'include') diff --git a/include/minishell.h b/include/minishell.h index 028a4fc..f0f458b 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:05:11 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:52:29 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,6 @@ int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); - +int set_return_code(int return_code, t_env **env); #endif diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index d64bb54..b6e5261 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,11 +6,12 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/10 14:36:55 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 15:20:42 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" +#include int echo(char **av) { @@ -22,7 +23,7 @@ int echo(char **av) if (av[1] == NULL || av[1][0] == '\0') { write(1, "\n", 1); - return (1); + return (0); } if (ft_strncmp(av[1], "-n", 3) == 0) { @@ -59,6 +60,11 @@ int ft_env(t_env *env) { while (env != NULL) { + if (strchr(env->name, '?')) + { + env = env->next; + continue; + } printf("%s", env->name); printf("=%s\n", env->value); env = env->next; @@ -89,6 +95,8 @@ int unset(char **av, t_env **env) i = 0; while (av[++i]) { + if (ft_strchr(av[i], '?')) + return (1); current = *env; prev = NULL; while (current) @@ -145,6 +153,8 @@ int export(char **av, t_env **env) { tmp = ft_strchr(av[i], '='); *tmp = '\0'; + if (ft_strchr(av[i], '?')) + return (1); current = check_existing(*env, av[i]); if (current) free(current->value); @@ -158,4 +168,20 @@ int export(char **av, t_env **env) } } return (0); -} \ No newline at end of file +} + +void set_return_code(int return_code, t_env **env) +{ + t_env *cur; + + cur = check_existing(*env, "?"); + if (cur) + free(cur->value); + else + { + cur = env_new(ft_strdup("?")); + cur->next = *env; + *env = cur; + } + cur->value = ft_itoa(return_code); +} diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c index c4c98c3..e0536c0 100644 --- a/src/env_to_strlst.c +++ b/src/env_to_strlst.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:22:36 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:41:39 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,6 +26,11 @@ char **env_to_strlst(t_env *env) cur = env; while (cur != NULL) { + if (ft_strchr(cur->name, '?')) + { + cur = cur->next; + continue; + } size++; cur = cur->next; } @@ -36,6 +41,8 @@ char **env_to_strlst(t_env *env) cur = env; while (i < size) { + if (ft_strchr(cur->name, '?')) + cur = cur->next; result[i] = get_var_assign(cur); cur = cur->next; i++; @@ -55,4 +62,4 @@ static char *get_var_assign(t_env *cur) result = ft_strjoin(left_side, cur->value); free(left_side); return (result); -} \ No newline at end of file +} diff --git a/src/format_string.c b/src/format_string.c index bd7f703..1fe028c 100644 --- a/src/format_string.c +++ b/src/format_string.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:31:54 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 18:06:17 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,22 +17,16 @@ static void append_slice(char **dst, char *src, int start, int end); static void append_var(char **dst, char *src, int *pos, t_env *env); -enum e_format_mode -{ - LITERAL = 1, - VARIABLE = 2, -}; - char *format_string(char *str, t_env *env) { char *result; int pos; int start; - int mode; + int is_literal; pos = 0; start = 0; - mode = 0; + is_literal = 0; result = NULL; if (str == NULL) return (NULL); @@ -42,14 +36,14 @@ char *format_string(char *str, t_env *env) { append_slice(&result, str, start, pos); start = pos + 1; - mode ^= LITERAL; + is_literal = !is_literal; } - if (str[pos] == '"' && !(mode & LITERAL)) + if (str[pos] == '"' && !is_literal) { append_slice(&result, str, start, pos); start = pos + 1; } - if (str[pos] == '$' && !(mode & LITERAL)) + if (str[pos] == '$' && !is_literal) { append_slice(&result, str, start, pos); append_var(&result, str, &pos, env); diff --git a/src/main.c b/src/main.c index a53760c..9e5d4ec 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/12/17 19:26:42 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 15:22:40 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,5 +22,6 @@ int main(int argc, char *argv[], char *envp[]) if (init()) return (1); getenvlst(&env, envp); + set_return_code(0, &env); repl("Minishell $ ", &env); } diff --git a/src/repl.c b/src/repl.c index 7ff80a8..4292197 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:01:44 by chuhlig ### ########.fr */ +/* Updated: 2025/01/14 14:53:29 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,7 +54,7 @@ void repl(const char *prompt, t_env **env) if (lines) { print_ast(lines->content); - eval(lines->content, env); + set_return_code(eval(lines->content, env), env); } free(input); } -- cgit v1.2.3 From 2d2df595dcf01315d080aa1b4fcf810d28f2a3a4 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 14:43:35 +0100 Subject: Add some fixes to execute_cmd --- include/minishell.h | 4 ++-- src/execute_cmd.c | 28 +++++++++++------------- src/get_cmd_path.c | 62 +++++++++++++++++++++++++++++++++++++++++++++-------- 3 files changed, 68 insertions(+), 26 deletions(-) (limited to 'include') diff --git a/include/minishell.h b/include/minishell.h index c905ca5..505b21b 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 15:29:46 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:04:19 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -39,7 +39,7 @@ t_redirection *collect_redirs(t_token **tokens); void print_ast(t_node *ast); int eval(t_node *node, t_env **env); -char *get_cmd_path(char *cmd, t_env *env); +char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); int set_return_code(int return_code, t_env **env); diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 525f333..bad6cf5 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,20 +6,18 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:55:18 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:42:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include int is_builtin(char *cmd) { - return ((ft_strcmp(cmd, "export") == 0) - || (ft_strcmp(cmd, "unset") == 0) - || (ft_strcmp(cmd, "cd") == 0) - || (ft_strcmp(cmd, "exit") == 0) - || (ft_strcmp(cmd, "echo") == 0) - || (ft_strcmp(cmd, "pwd") == 0) + return ((ft_strcmp(cmd, "export") == 0) || (ft_strcmp(cmd, "unset") == 0) + || (ft_strcmp(cmd, "cd") == 0) || (ft_strcmp(cmd, "exit") == 0) + || (ft_strcmp(cmd, "echo") == 0) || (ft_strcmp(cmd, "pwd") == 0) || (ft_strcmp(cmd, "env") == 0)); } @@ -50,6 +48,7 @@ int execute_cmd(t_cmd *cmd, t_env **env) int original_stdout; int original_stdin; int result; + int i; original_stdout = dup(STDOUT_FILENO); original_stdin = dup(STDIN_FILENO); @@ -82,15 +81,14 @@ int execute_cmd(t_cmd *cmd, t_env **env) } if (pid == 0) { - cmd_path = get_cmd_path(cmd->args[0], *env); + i = 0; + while (cmd->args[i][0] == '\0') + i++; + cmd_path = get_cmd_path(cmd->args[i], *env, &result); if (!cmd_path) - { - perror("command not found"); - exit(EXIT_FAILURE); - } - execve(cmd_path, cmd->args, env_to_strlst(*env)); - perror("execve"); - exit(EXIT_FAILURE); + exit(result); + execve(cmd_path, &(cmd->args[i]), env_to_strlst(*env)); + exit(EXIT_SUCCESS); } waitpid(pid, &status, 0); dup2(original_stdout, STDOUT_FILENO); diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 8a27584..56d9876 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,28 +6,53 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2024/12/17 19:20:08 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 14:33:58 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" #include "minishell.h" +#include +#include +#include +#include -static char *get_absolute_cmd_path(char *cmd, t_env *env); -static char *find_in_path(char *cmd, t_env *env); +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code); +static char *find_in_path(char *cmd, t_env *env, int *return_code); char **get_split_path(t_env *env); -char *get_cmd_path(char *cmd, t_env *env) +char *get_cmd_path(char *cmd, t_env *env, int *return_code) { + char *result; + if (cmd[0] == '/') - return (ft_strdup(cmd)); + { + result = ft_strdup(cmd); + if (access(result, F_OK) == -1) + { + free(result); + errno = ENOENT; + perror(cmd); + *return_code = 127; + return (NULL); + } + else if (access(result, X_OK) == -1) + { + free(result); + errno = EACCES; + perror(cmd); + *return_code = 126; + return (NULL); + } + return (result); + } else if (ft_strchr(cmd, '/')) - return (get_absolute_cmd_path(cmd, env)); + return (get_absolute_cmd_path(cmd, env, return_code)); else - return (find_in_path(cmd, env)); + return (find_in_path(cmd, env, return_code)); } -static char *get_absolute_cmd_path(char *cmd, t_env *env) +static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code) { char *cur_dir; char *result; @@ -38,16 +63,32 @@ static char *get_absolute_cmd_path(char *cmd, t_env *env) result = ft_strjoin(cur_dir, cmd); free(cur_dir); if (!result) + { + *return_code = 127; + errno = ENOENT; + perror(cmd); return (NULL); + } + if (access(result, F_OK) == -1) + { + free(result); + errno = ENOENT; + *return_code = 127; + perror(cmd); + return (NULL); + } if (access(result, X_OK) == -1) { free(result); + errno = EACCES; + *return_code = 126; + perror(cmd); return (NULL); } return (result); } -static char *find_in_path(char *cmd, t_env *env) +static char *find_in_path(char *cmd, t_env *env, int *return_code) { char *cur_path; char *cmd_path; @@ -70,6 +111,9 @@ static char *find_in_path(char *cmd, t_env *env) return (cmd_path); path++; } + *return_code = 127; + printf("%s:", cmd); + ft_putstr_fd(" command not found", 2); return (NULL); } -- cgit v1.2.3 From 64a413a49ed7979aad9a1eede5ba25c267884839 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 16:36:45 +0100 Subject: Outsource error function --- Makefile | 2 +- include/minishell.h | 3 ++- src/error.c | 23 +++++++++++++++++++++++ src/get_cmd_path.c | 6 +++--- 4 files changed, 29 insertions(+), 5 deletions(-) create mode 100644 src/error.c (limited to 'include') diff --git a/Makefile b/Makefile index 049422d..415d924 100644 --- a/Makefile +++ b/Makefile @@ -19,7 +19,7 @@ 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 \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ - builtins_part_one.c builtins_part_two.c env_tools.c \ + builtins_part_one.c builtins_part_two.c env_tools.c error.c \ OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/minishell.h b/include/minishell.h index 505b21b..88b22d5 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 14:04:19 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 16:35:40 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -44,5 +44,6 @@ int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); int set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); +void *error(int err_code, char *err_text, int exit_code, int *ret_code); #endif diff --git a/src/error.c b/src/error.c new file mode 100644 index 0000000..628200d --- /dev/null +++ b/src/error.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* error.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser + +void *error(int err_code, char *err_text, int exit_code, int *ret_code) +{ + errno = err_code; + perror(err_text); + if (ret_code != NULL) + *ret_code = exit_code; + return (NULL); +} diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index fb45730..11100bc 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/15 16:32:30 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 16:34:56 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ static char *get_simple_cmd_path(char *cmd, int *return_code); static char *get_absolute_cmd_path(char *cmd, t_env *env, int *return_code); static char *find_in_path(char *cmd, t_env *env, int *return_code); -static char *error(int err_code, char *err_text, int exit_code, int *ret_code); +static void *error(int err_code, char *err_text, int exit_code, int *ret_code); char **get_split_path(t_env *env); static int is_directory(char *path); @@ -111,7 +111,7 @@ static char *get_simple_cmd_path(char *cmd, int *return_code) return (result); } -static char *error(int err_code, char *err_text, int exit_code, int *ret_code) +static void *error(int err_code, char *err_text, int exit_code, int *ret_code) { errno = err_code; perror(err_text); -- cgit v1.2.3 From 29932bf9401f511cfa4b2fbb183bb174ecf13c24 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 18:15:16 +0100 Subject: Fix waiting errors --- include/minishell.h | 4 ++-- src/collect_redirs.c | 35 +++++++++++++++++++++-------------- src/interpreter.c | 4 +--- src/parse_cmd.c | 4 ++-- 4 files changed, 26 insertions(+), 21 deletions(-) (limited to 'include') diff --git a/include/minishell.h b/include/minishell.h index 88b22d5..2a426cf 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 16:35:40 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 17:20:56 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ void repl(const char *prompt, t_env **env, int *promptflag); t_list *parse(t_token *tokens, t_env **env); t_node *parse_cmd(t_token *tokens, t_env **env); -t_redirection *collect_redirs(t_token **tokens); +t_redirection *collect_redirs(t_token **tokens, t_env *env); void print_ast(t_node *ast); int eval(t_node *node, t_env **env); diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 84ebd71..02c866f 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,14 +6,16 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 16:55:20 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 18:10:46 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static void collect_and_check_redir(t_redirection *result, t_token **cur); -static void set_redir(t_redirection *redir, int type, char *specifier); +static void collect_and_check_redir(t_redirection *result, t_token **cur, + t_env *env); +static void set_redir(t_redirection *redir, int type, char *specifier, + t_env *env); static char *read_heredoc(char *delimiter) { @@ -56,7 +58,7 @@ static char *read_heredoc(char *delimiter) return (result); } -t_redirection *collect_redirs(t_token **tokens) +t_redirection *collect_redirs(t_token **tokens, t_env *env) { t_redirection *result; t_token *cur; @@ -65,12 +67,12 @@ t_redirection *collect_redirs(t_token **tokens) result = malloc(sizeof(t_redirection) * 2); if (result == NULL) return (free_tokens(*tokens), NULL); - set_redir(&result[0], 0, NULL); - set_redir(&result[1], 0, NULL); + set_redir(&result[0], 0, NULL, env); + set_redir(&result[1], 0, NULL, env); while (cur != NULL && cur->next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur); + collect_and_check_redir(result, &cur, env); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -81,13 +83,18 @@ t_redirection *collect_redirs(t_token **tokens) return (result); } -static void set_redir(t_redirection *redir, int type, char *specifier) +static void set_redir(t_redirection *redir, int type, char *specifier, + t_env *env) { redir->type = type; - redir->specifier = specifier; + if (specifier != NULL) + redir->specifier = format_string(specifier, env); + else + redir->specifier = specifier; } -static void collect_and_check_redir(t_redirection *result, t_token **cur) +static void collect_and_check_redir(t_redirection *result, t_token **cur, + t_env *env) { char *heredoc_data; t_token *next_token; @@ -101,17 +108,17 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur) perror("Heredoc allocation failed"); return ; } - set_redir(&result[0], INPUT_LIMITER, heredoc_data); + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); } else if ((*cur)->content.redir_type == INPUT_FILE) set_redir(&result[0], INPUT_FILE, - ft_strdup((*cur)->next->content.string)); + ft_strdup((*cur)->next->content.string), env); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) set_redir(&result[1], OUTPUT_OVERRIDE, - ft_strdup((*cur)->next->content.string)); + ft_strdup((*cur)->next->content.string), env); else if ((*cur)->content.redir_type == OUTPUT_APPEND) set_redir(&result[1], OUTPUT_APPEND, - ft_strdup((*cur)->next->content.string)); + ft_strdup((*cur)->next->content.string), env); next_token = (*cur)->next; free_token_and_connect(*cur); if (next_token) diff --git a/src/interpreter.c b/src/interpreter.c index a1d3823..0a6d781 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:52:27 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 18:10:25 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,7 +77,6 @@ int eval_rec(t_node *node, t_env **env, int in_fd) pid_t pid; int p[2]; int result; - int status; int original_stdin; if (node->type == PIPE_NODE) @@ -98,7 +97,6 @@ int eval_rec(t_node *node, t_env **env, int in_fd) original_stdin = dup(STDIN_FILENO); dup2(p[0], STDIN_FILENO); result = eval_rec(node->content.pipe.right, env, p[0]); - waitpid(pid, &status, 0); dup2(original_stdin, STDIN_FILENO); close(original_stdin); } diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 3c4eb96..9ca741b 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:04:50 by chuhlig ### ########.fr */ +/* Updated: 2025/01/15 17:22:58 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -19,7 +19,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) char **args; t_redirection *redirs; - redirs = collect_redirs(&tokens); + redirs = collect_redirs(&tokens, *env); if (redirs == NULL) return (NULL); args = collect_args(&tokens, env); -- cgit v1.2.3 From 79aeeaa6692c1c2c8282df751ff6fda1ba445883 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Wed, 15 Jan 2025 18:35:29 +0100 Subject: Refactor collect_redirs --- Makefile | 1 + include/minishell.h | 9 ++--- src/collect_redirs.c | 94 +++++++++++++++++----------------------------------- src/read_heredoc.c | 54 ++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 67 deletions(-) create mode 100644 src/read_heredoc.c (limited to 'include') diff --git a/Makefile b/Makefile index 415d924..650f5e4 100644 --- a/Makefile +++ b/Makefile @@ -20,6 +20,7 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ + read_heredoc.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/minishell.h b/include/minishell.h index 2a426cf..3b97f6a 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 17:20:56 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 18:24:09 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,8 +42,9 @@ int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env); -int set_return_code(int return_code, t_env **env); +int set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); -void *error(int err_code, char *err_text, int exit_code, int *ret_code); - +void *error(int err_code, char *err_text, int exit_code, + int *ret_code); +char *read_heredoc(char *delimiter); #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 02c866f..2947c52 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 18:10:46 by dkaiser ### ########.fr */ +/* Updated: 2025/01/15 18:33:36 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,49 +14,9 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env); -static void set_redir(t_redirection *redir, int type, char *specifier, +static void set_redir(t_redirection *redir, int type, char *spec, t_env *env); - -static char *read_heredoc(char *delimiter) -{ - char *line; - char *result; - char *temp; - size_t total_length; - size_t line_length; - - total_length = 0; - result = NULL; - while (1) - { - line = readline("> "); - if (!line || ft_strcmp(line, delimiter) == 0) - { - free(line); - break ; - } - line_length = ft_strlen(line) + 1; - temp = malloc(total_length + line_length + 1); - if (!temp) - { - perror("malloc"); - return (free(result), NULL); - } - if (result) - { - ft_strcpy(temp, result); - free(result); - } - else - temp[0] = '\0'; - ft_strcat(temp, line); - ft_strcat(temp, "\n"); - result = temp; - total_length += line_length; - free(line); - } - return (result); -} +static char *get_heredoc_data(t_token *cur); t_redirection *collect_redirs(t_token **tokens, t_env *env) { @@ -83,42 +43,33 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env) return (result); } -static void set_redir(t_redirection *redir, int type, char *specifier, - t_env *env) +static void set_redir(t_redirection *redir, int type, char *spec, t_env *env) { redir->type = type; - if (specifier != NULL) - redir->specifier = format_string(specifier, env); + if (spec != NULL) + redir->specifier = format_string(spec, env); else - redir->specifier = specifier; + redir->specifier = spec; } static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env) { - char *heredoc_data; t_token *next_token; + char *str; - heredoc_data = NULL; + if ((*cur)->content.redir_type != INPUT_LIMITER) + str = ft_strdup((*cur)->next->content.string); if ((*cur)->content.redir_type == INPUT_LIMITER) { - heredoc_data = read_heredoc((*cur)->next->content.string); - if (!heredoc_data) - { - perror("Heredoc allocation failed"); - return ; - } - set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + set_redir(&result[0], INPUT_LIMITER, get_heredoc_data(*cur), env); } else if ((*cur)->content.redir_type == INPUT_FILE) - set_redir(&result[0], INPUT_FILE, - ft_strdup((*cur)->next->content.string), env); + set_redir(&result[0], INPUT_FILE, str, env); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - set_redir(&result[1], OUTPUT_OVERRIDE, - ft_strdup((*cur)->next->content.string), env); + set_redir(&result[1], OUTPUT_OVERRIDE, str, env); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - set_redir(&result[1], OUTPUT_APPEND, - ft_strdup((*cur)->next->content.string), env); + set_redir(&result[1], OUTPUT_APPEND, str, env); next_token = (*cur)->next; free_token_and_connect(*cur); if (next_token) @@ -129,3 +80,20 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, else *cur = NULL; } + +static char *get_heredoc_data(t_token *cur) +{ + char *heredoc_data; + + if (cur->content.redir_type == INPUT_LIMITER) + { + heredoc_data = read_heredoc(cur->next->content.string); + if (!heredoc_data) + { + perror("Heredoc allocation failed"); + return ; + } + set_redir(&result[0], INPUT_LIMITER, heredoc_data, env); + } + return (heredoc_data); +} diff --git a/src/read_heredoc.c b/src/read_heredoc.c new file mode 100644 index 0000000..78efbd7 --- /dev/null +++ b/src/read_heredoc.c @@ -0,0 +1,54 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* read_heredoc.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser "); + if (!line || ft_strcmp(line, delimiter) == 0) + { + free(line); + break ; + } + line_length = ft_strlen(line) + 1; + temp = malloc(total_length + line_length + 1); + if (!temp) + { + perror("malloc"); + return (free(result), NULL); + } + if (result) + { + ft_strcpy(temp, result); + free(result); + } + else + temp[0] = '\0'; + ft_strcat(temp, line); + ft_strcat(temp, "\n"); + result = temp; + total_length += line_length; + free(line); + } + return (result); +} -- cgit v1.2.3 From 3392f2b811269f174620832d663b09ef4f4e43f3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Thu, 16 Jan 2025 19:16:44 +0100 Subject: Create files --- Makefile | 2 +- include/ast.h | 6 ++++-- include/minishell.h | 5 +++-- src/collect_redirs.c | 42 +++++++++++++++++++++++++++++------------- src/create_files.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ src/execute_cmd.c | 3 ++- src/interpreter.c | 8 ++++---- src/new_node.c | 5 +++-- src/parse_cmd.c | 8 +++++--- src/tokenizer.c | 3 ++- 10 files changed, 98 insertions(+), 29 deletions(-) create mode 100644 src/create_files.c (limited to 'include') diff --git a/Makefile b/Makefile index 650f5e4..243e9fc 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ - read_heredoc.c + read_heredoc.c create_files.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/ast.h b/include/ast.h index cd2f9c9..0335473 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 18:24:09 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:38:44 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,7 +35,7 @@ void repl(const char *prompt, t_env **env, int *promptflag); t_list *parse(t_token *tokens, t_env **env); t_node *parse_cmd(t_token *tokens, t_env **env); -t_redirection *collect_redirs(t_token **tokens, t_env *env); +t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **cf); void print_ast(t_node *ast); int eval(t_node *node, t_env **env); @@ -47,4 +47,5 @@ int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); char *read_heredoc(char *delimiter); +void create_files(t_list *files); #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 4decda7..171dc06 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,20 +6,21 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 18:54:42 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:19:36 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include -static void collect_and_check_redir(t_redirection *result, t_token **cur, - t_env *env); -static void set_redir(t_redirection *redir, int type, char *spec, - t_env *env); -static int set_heredoc_data(t_token *cur, t_redirection *result, - t_env *env); +static void collect_and_check_redir(t_redirection *result, + t_token **cur, t_env *env, t_list **create_files); +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env); +static int set_heredoc_data(t_token *cur, t_redirection *result, + t_env *env); -t_redirection *collect_redirs(t_token **tokens, t_env *env) +t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files) { t_redirection *result; t_token *cur; @@ -33,7 +34,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env) while (cur != NULL && cur->next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur, env); + collect_and_check_redir(result, &cur, env, create_files); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -44,17 +45,30 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env) return (result); } -static void set_redir(t_redirection *redir, int type, char *spec, t_env *env) +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) { + t_redirection *result; + redir->type = type; if (spec != NULL) redir->specifier = format_string(spec, env); else redir->specifier = spec; + if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + { + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); + } + return (NULL); } static void collect_and_check_redir(t_redirection *result, t_token **cur, - t_env *env) + t_env *env, t_list **create_files) { t_token *next_token; char *str; @@ -69,9 +83,11 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, else if ((*cur)->content.redir_type == INPUT_FILE) set_redir(&result[0], INPUT_FILE, str, env); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - set_redir(&result[1], OUTPUT_OVERRIDE, str, env); + ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], + OUTPUT_OVERRIDE, str, env))); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - set_redir(&result[1], OUTPUT_APPEND, str, env); + ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], + OUTPUT_APPEND, str, env))); next_token = (*cur)->next; free_token_and_connect(*cur); if (next_token) diff --git a/src/create_files.c b/src/create_files.c new file mode 100644 index 0000000..faee27f --- /dev/null +++ b/src/create_files.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* create_files.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser + +void create_files(t_list *files) +{ + t_redirection *file; + int fd; + + while (files) + { + dbg("Test"); + if (files->content == NULL) + continue; + file = (t_redirection *)files->content; + if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) + break; + if (file->type == OUTPUT_OVERRIDE) + { + fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + close(fd); + } + else if (file->type == OUTPUT_APPEND) + { + fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + close(fd); + } + /* if (files->next == NULL) */ + /* break; */ + /* if (((t_redirection *) files->next->content)->type == 0) */ + /* break; */ + files = files->next; + } +} diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 83addd2..8f5b541 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/15 15:52:08 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:38:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ int execute_cmd(t_cmd *cmd, t_env **env) original_std[1] = dup(STDOUT_FILENO); original_std[0] = dup(STDIN_FILENO); + create_files(cmd->create_files); if (handle_redirections(cmd->redirs) == -1) { establish_pipeline(original_std[0], original_std[1]); diff --git a/src/interpreter.c b/src/interpreter.c index 0a6d781..c7fe67c 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/15 18:10:25 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ int handle_redirections(t_redirection *redirs) } else if (redirs[0].type == INPUT_LIMITER) { - fd = open("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); if (fd < 0) { perror("open"); @@ -49,7 +49,7 @@ int handle_redirections(t_redirection *redirs) } if (redirs[1].type == OUTPUT_OVERRIDE) { - fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644); if (fd < 0) { perror("open"); @@ -60,7 +60,7 @@ int handle_redirections(t_redirection *redirs) } else if (redirs[1].type == OUTPUT_APPEND) { - fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644); if (fd < 0) { perror("open"); diff --git a/src/new_node.c b/src/new_node.c index c58d291..83d9159 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.redirs[0] = redirs[0]; node->content.cmd.redirs[1] = redirs[1]; + node->content.cmd.create_files = create_files; free(redirs); return (node); } diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 9ca741b..92dfd12 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/15 17:22:58 by dkaiser ### ########.fr */ +/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,8 +18,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env) { char **args; t_redirection *redirs; + t_list *create_files; - redirs = collect_redirs(&tokens, *env); + create_files = NULL; + redirs = collect_redirs(&tokens, *env, &create_files); if (redirs == NULL) return (NULL); args = collect_args(&tokens, env); @@ -28,7 +30,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) free(redirs); return (NULL); } - return (new_cmd_node(args, redirs)); + return (new_cmd_node(args, redirs, create_files)); } static char **collect_args(t_token **tokens, t_env **env) diff --git a/src/tokenizer.c b/src/tokenizer.c index 6d16f1d..eb5d8fe 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 15:58:41 by chuhlig ### ########.fr */ +/* Updated: 2025/01/16 18:57:38 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -101,6 +101,7 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list) (*i)++; if (s[*i] == '>' && s[*i + 1] == '>') (*i)++; + print_token(*token_list); *start = *i + 1; } -- cgit v1.2.3 From 8f5abcdb257393a2e576fe382835e8e060662834 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 20 Jan 2025 13:06:34 +0100 Subject: merged --- .vscode/c_cpp_properties.json | 18 - .vscode/launch.json | 13 - .vscode/settings.json | 64 -- .vscode/tasks.json | 29 - Makefile | 6 +- garbage | 1183 -------------------------------- include/ast.h | 12 +- include/debug_tools.h | 11 +- include/env.h | 3 +- include/minishell.h | 15 +- include/token.h | 3 +- lib/libft/_obj/ft_atoi.o | Bin 0 -> 932 bytes lib/libft/_obj/ft_atol.o | Bin 0 -> 948 bytes lib/libft/_obj/ft_bzero.o | Bin 0 -> 676 bytes lib/libft/_obj/ft_calloc.o | Bin 0 -> 752 bytes lib/libft/_obj/ft_isalnum.o | Bin 0 -> 704 bytes lib/libft/_obj/ft_isalpha.o | Bin 0 -> 680 bytes lib/libft/_obj/ft_isascii.o | Bin 0 -> 664 bytes lib/libft/_obj/ft_isdigit.o | Bin 0 -> 664 bytes lib/libft/_obj/ft_isprint.o | Bin 0 -> 664 bytes lib/libft/_obj/ft_isspace.o | Bin 0 -> 672 bytes lib/libft/_obj/ft_itoa.o | Bin 0 -> 1236 bytes lib/libft/_obj/ft_lstadd_back_bonus.o | Bin 0 -> 716 bytes lib/libft/_obj/ft_lstadd_front_bonus.o | Bin 0 -> 652 bytes lib/libft/_obj/ft_lstclear_bonus.o | Bin 0 -> 780 bytes lib/libft/_obj/ft_lstdelone_bonus.o | Bin 0 -> 688 bytes lib/libft/_obj/ft_lstiter_bonus.o | Bin 0 -> 712 bytes lib/libft/_obj/ft_lstlast_bonus.o | Bin 0 -> 696 bytes lib/libft/_obj/ft_lstmap_bonus.o | Bin 0 -> 932 bytes lib/libft/_obj/ft_lstnew_bonus.o | Bin 0 -> 732 bytes lib/libft/_obj/ft_lstsize_bonus.o | Bin 0 -> 704 bytes lib/libft/_obj/ft_memchr.o | Bin 0 -> 724 bytes lib/libft/_obj/ft_memcmp.o | Bin 0 -> 740 bytes lib/libft/_obj/ft_memcpy.o | Bin 0 -> 764 bytes lib/libft/_obj/ft_memmove.o | Bin 0 -> 824 bytes lib/libft/_obj/ft_memset.o | Bin 0 -> 684 bytes lib/libft/_obj/ft_printaddr.o | Bin 0 -> 1128 bytes lib/libft/_obj/ft_printf.o | Bin 0 -> 2996 bytes lib/libft/_obj/ft_printhex.o | Bin 0 -> 1012 bytes lib/libft/_obj/ft_printnbr.o | Bin 0 -> 1704 bytes lib/libft/_obj/ft_putchar_fd.o | Bin 0 -> 680 bytes lib/libft/_obj/ft_putendl_fd.o | Bin 0 -> 860 bytes lib/libft/_obj/ft_putnbr_fd.o | Bin 0 -> 1088 bytes lib/libft/_obj/ft_putstr_fd.o | Bin 0 -> 740 bytes lib/libft/_obj/ft_split.o | Bin 0 -> 1936 bytes lib/libft/_obj/ft_strcat.o | Bin 0 -> 756 bytes lib/libft/_obj/ft_strchr.o | Bin 0 -> 756 bytes lib/libft/_obj/ft_strcmp.o | Bin 0 -> 748 bytes lib/libft/_obj/ft_strcpy.o | Bin 0 -> 700 bytes lib/libft/_obj/ft_strdup.o | Bin 0 -> 816 bytes lib/libft/_obj/ft_striteri.o | Bin 0 -> 696 bytes lib/libft/_obj/ft_strjoin.o | Bin 0 -> 1044 bytes lib/libft/_obj/ft_strlcat.o | Bin 0 -> 896 bytes lib/libft/_obj/ft_strlcpy.o | Bin 0 -> 872 bytes lib/libft/_obj/ft_strlen.o | Bin 0 -> 660 bytes lib/libft/_obj/ft_strmapi.o | Bin 0 -> 864 bytes lib/libft/_obj/ft_strncmp.o | Bin 0 -> 824 bytes lib/libft/_obj/ft_strncpy.o | Bin 0 -> 728 bytes lib/libft/_obj/ft_strnstr.o | Bin 0 -> 984 bytes lib/libft/_obj/ft_strrchr.o | Bin 0 -> 744 bytes lib/libft/_obj/ft_strtrim.o | Bin 0 -> 1232 bytes lib/libft/_obj/ft_substr.o | Bin 0 -> 912 bytes lib/libft/_obj/ft_tolower.o | Bin 0 -> 664 bytes lib/libft/_obj/ft_toupper.o | Bin 0 -> 664 bytes lib/libft/_obj/get_next_line.o | Bin 0 -> 1860 bytes lib/libft/_obj/get_next_line_utils.o | Bin 0 -> 1384 bytes lib/libft/libft.a | Bin 0 -> 55752 bytes src/builtins_part_one.c | 133 ++-- src/builtins_part_three.c | 76 ++ src/builtins_part_two.c | 5 +- src/collect_redirs.c | 53 +- src/create_files.c | 16 +- src/debug_tools.c | 6 +- src/env_to_strlst.c | 29 +- src/execute_cmd.c | 2 +- src/format_string.c | 112 +-- src/free_token.c | 7 +- src/get_cmd_path.c | 4 +- src/handle_redir.c | 103 +++ src/interpreter.c | 89 +-- src/new_node.c | 5 +- src/parse_cmd.c | 13 +- src/parser.c | 4 +- src/repl.c | 6 +- src/signal_handling.c | 2 +- teest.txt | 1 - 86 files changed, 428 insertions(+), 1595 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json delete mode 100644 garbage create mode 100644 lib/libft/_obj/ft_atoi.o create mode 100644 lib/libft/_obj/ft_atol.o create mode 100644 lib/libft/_obj/ft_bzero.o create mode 100644 lib/libft/_obj/ft_calloc.o create mode 100644 lib/libft/_obj/ft_isalnum.o create mode 100644 lib/libft/_obj/ft_isalpha.o create mode 100644 lib/libft/_obj/ft_isascii.o create mode 100644 lib/libft/_obj/ft_isdigit.o create mode 100644 lib/libft/_obj/ft_isprint.o create mode 100644 lib/libft/_obj/ft_isspace.o create mode 100644 lib/libft/_obj/ft_itoa.o create mode 100644 lib/libft/_obj/ft_lstadd_back_bonus.o create mode 100644 lib/libft/_obj/ft_lstadd_front_bonus.o create mode 100644 lib/libft/_obj/ft_lstclear_bonus.o create mode 100644 lib/libft/_obj/ft_lstdelone_bonus.o create mode 100644 lib/libft/_obj/ft_lstiter_bonus.o create mode 100644 lib/libft/_obj/ft_lstlast_bonus.o create mode 100644 lib/libft/_obj/ft_lstmap_bonus.o create mode 100644 lib/libft/_obj/ft_lstnew_bonus.o create mode 100644 lib/libft/_obj/ft_lstsize_bonus.o create mode 100644 lib/libft/_obj/ft_memchr.o create mode 100644 lib/libft/_obj/ft_memcmp.o create mode 100644 lib/libft/_obj/ft_memcpy.o create mode 100644 lib/libft/_obj/ft_memmove.o create mode 100644 lib/libft/_obj/ft_memset.o create mode 100644 lib/libft/_obj/ft_printaddr.o create mode 100644 lib/libft/_obj/ft_printf.o create mode 100644 lib/libft/_obj/ft_printhex.o create mode 100644 lib/libft/_obj/ft_printnbr.o create mode 100644 lib/libft/_obj/ft_putchar_fd.o create mode 100644 lib/libft/_obj/ft_putendl_fd.o create mode 100644 lib/libft/_obj/ft_putnbr_fd.o create mode 100644 lib/libft/_obj/ft_putstr_fd.o create mode 100644 lib/libft/_obj/ft_split.o create mode 100644 lib/libft/_obj/ft_strcat.o create mode 100644 lib/libft/_obj/ft_strchr.o create mode 100644 lib/libft/_obj/ft_strcmp.o create mode 100644 lib/libft/_obj/ft_strcpy.o create mode 100644 lib/libft/_obj/ft_strdup.o create mode 100644 lib/libft/_obj/ft_striteri.o create mode 100644 lib/libft/_obj/ft_strjoin.o create mode 100644 lib/libft/_obj/ft_strlcat.o create mode 100644 lib/libft/_obj/ft_strlcpy.o create mode 100644 lib/libft/_obj/ft_strlen.o create mode 100644 lib/libft/_obj/ft_strmapi.o create mode 100644 lib/libft/_obj/ft_strncmp.o create mode 100644 lib/libft/_obj/ft_strncpy.o create mode 100644 lib/libft/_obj/ft_strnstr.o create mode 100644 lib/libft/_obj/ft_strrchr.o create mode 100644 lib/libft/_obj/ft_strtrim.o create mode 100644 lib/libft/_obj/ft_substr.o create mode 100644 lib/libft/_obj/ft_tolower.o create mode 100644 lib/libft/_obj/ft_toupper.o create mode 100644 lib/libft/_obj/get_next_line.o create mode 100644 lib/libft/_obj/get_next_line_utils.o create mode 100644 lib/libft/libft.a create mode 100644 src/builtins_part_three.c create mode 100644 src/handle_redir.c delete mode 100644 teest.txt (limited to 'include') diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 94b2ae4..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "macos-clang-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "/usr/bin/clang", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "macos-clang-x64", - "compilerArgs": [ - "" - ] - } - ], - "version": 4 - } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 86fa44a..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "lldb", - "request": "launch", - "args": [], - "cwd": "/Users/chuhlig/Desktop/merged_minishell/", - "program": "/Users/chuhlig/Desktop/merged_minishell/minishell" - } - ] - } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 91fa952..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "clang", - "C_Cpp_Runner.cppCompilerPath": "clang++", - "C_Cpp_Runner.debuggerPath": "lldb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false, - "files.associations": { - "minishell.h": "c", - "ast.h": "c", - "env.h": "c" - } - } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 4ede37a..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: clang build active file", - "command": "/usr/bin/clang", - "args": [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file diff --git a/Makefile b/Makefile index 243e9fc..9cfa670 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,7 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ - read_heredoc.c create_files.c + read_heredoc.c create_files.c builtins_part_three.c handle_redir.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) @@ -64,7 +64,9 @@ debug: CFLAGS += -g debug: CFLAGS += -fsanitize=address -fsanitize=undefined \ -fno-sanitize-recover=all -fsanitize=float-divide-by-zero \ -fsanitize=float-cast-overflow -fno-sanitize=null \ - -fno-sanitize=alignment + -fno-sanitize=alignment -fno-sanitize=object-size \ + -fno-sanitize=vptr -fno-sanitize=return -fno-sanitize=signed-integer-overflow \ + -fno-sanitize=bounds -fno-sanitize=pointer-subtract -fno-sanitize=pointer-compare debug: CFLAGS += -DDEBUG=1 debug: clean all diff --git a/garbage b/garbage deleted file mode 100644 index 07f44dc..0000000 --- a/garbage +++ /dev/null @@ -1,1183 +0,0 @@ -_=/Users/chuhlig/Desktop/merged_minishell/minishell/./minishell -TERM=xterm-256color -USER_ZDOTDIR=/Users/chuhlig -ZDOTDIR=/Users/chuhlig -VSCODE_INJECTION=1 -VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-e47956ed09.sock -VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js -VSCODE_GIT_ASKPASS_EXTRA_ARGS= -VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) -GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh -COLORTERM=truecolor -LANG=en_US.UTF-8 -TERM_PROGRAM_VERSION=1.95.3 -TERM_PROGRAM=vscode -OLDPWD=/Users/chuhlig/Desktop/merged_minishell -PWD=/Users/chuhlig/Desktop/merged_minishell/minishell -SHLVL=1 -ORIGINAL_XDG_CURRENT_DESKTOP=undefined -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -__CF_USER_TEXT_ENCODING=0x1928E:0x0:0x2 -SHELL=/bin/zsh -HOME=/Users/chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ECcBYJZkxn/Listeners -LOGNAME=chuhlig -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin -MallocNanoZone=0 -USER=chuhlig -_=/Users/chuhlig/Desktop/merged_minishell/minishell/./minishell -TERM=xterm-256color -USER_ZDOTDIR=/Users/chuhlig -ZDOTDIR=/Users/chuhlig -VSCODE_INJECTION=1 -VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-e47956ed09.sock -VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js -VSCODE_GIT_ASKPASS_EXTRA_ARGS= -VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) -GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh -COLORTERM=truecolor -LANG=en_US.UTF-8 -TERM_PROGRAM_VERSION=1.95.3 -TERM_PROGRAM=vscode -OLDPWD=/Users/chuhlig/Desktop/merged_minishell -PWD=/Users/chuhlig/Desktop/merged_minishell/minishell -SHLVL=1 -ORIGINAL_XDG_CURRENT_DESKTOP=undefined -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -__CF_USER_TEXT_ENCODING=0x1928E:0x0:0x2 -SHELL=/bin/zsh -HOME=/Users/chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.ECcBYJZkxn/Listeners -LOGNAME=chuhlig -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin -MallocNanoZone=0 -USER=chuhlig - - -//some test case about built in command - bash-3.2$ ls -Applications Music -Desktop Pictures -Documents francinette -Downloads getting-started -Library goinfre -Movies setup_docker_environment -bash-3.2$ pwd -/Users/chuhlig -bash-3.2$ ls | cd / | ls -Applications Music -Desktop Pictures -Documents francinette -Downloads getting-started -Library goinfre -Movies setup_docker_environment -bash-3.2$ pwd -/Users/chuhlig -bash-3.2$ env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ export test=test | unset USER -bash-3.2$ env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ env | grep test -bash-3.2$ ls | cd / | env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ ls | cd / | env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ ls | cd / | env -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ ls | cd / | env | cd / -bash-3.2$ pwd -/Users/chuhlig -bash-3.2$ ls | cd / | env | cd ~ -bash-3.2$ ls | cd / | env | cd - -bash: cd: OLDPWD not set -bash-3.2$ ls | cd - | env -bash: cd: OLDPWD not set -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ -bash-3.2$ ls | cd - | env -bash: cd: OLDPWD not set -TERM_PROGRAM=iTerm.app -TERM=xterm-256color -SHELL=/bin/zsh -TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ -TERM_PROGRAM_VERSION=3.5.10 -TERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -USER=chuhlig -SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.BLODixwZXQ/Listeners -__CF_USER_TEXT_ENCODING=0x0:0:0 -TERM_FEATURES=T3LrMSc7UUw9Ts3BFGsSyHNoSxF -TERMINFO_DIRS=/Applications/iTerm.app/Contents/Resources/terminfo:/usr/share/terminfo -PATH=/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Applications/iTerm.app/Contents/Resources/utilities -PWD=/Users/chuhlig -LANG=en_US.UTF-8 -ITERM_PROFILE=Default -XPC_FLAGS=0x0 -XPC_SERVICE_NAME=0 -SHLVL=2 -HOME=/Users/chuhlig -COLORFGBG=7;0 -LC_TERMINAL_VERSION=3.5.10 -ITERM_SESSION_ID=w0t0p0:EC167FE5-607B-4E5F-A816-E8EF7FA09EC7 -LOGNAME=chuhlig -LC_TERMINAL=iTerm2 -COLORTERM=truecolor -_=/usr/bin/env -bash-3.2$ - - -////////////////////////////////////////////////////////////////// - -static int handle_redirections(t_redirection *redirs) { - if (redirs[0].type == INPUT_LIMITER) { - int pipe_fds[2]; - if (pipe(pipe_fds) == -1) { - perror("pipe"); - return -1; - } - write(pipe_fds[1], redirs[0].specifier, strlen(redirs[0].specifier)); - close(pipe_fds[1]); - dup2(pipe_fds[0], STDIN_FILENO); - close(pipe_fds[0]); - } else if (redirs[0].type == INPUT_FILE) { - int fd = open(redirs[0].specifier, O_RDONLY); - if (fd == -1) { - perror(redirs[0].specifier); - return -1; - } - dup2(fd, STDIN_FILENO); - close(fd); - } - - if (redirs[1].type == OUTPUT_OVERRIDE) { - int fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); - if (fd == -1) { - perror(redirs[1].specifier); - return -1; - } - dup2(fd, STDOUT_FILENO); - close(fd); - } else if (redirs[1].type == OUTPUT_APPEND) { - int fd = open(redirs[1].specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); - if (fd == -1) { - perror(redirs[1].specifier); - return -1; - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - return 0; -} - -static int eval_rec(t_node *node, t_env **env, int in_fd) { - pid_t pid; - int p[2], result; - - if (node->type == PIPE_NODE) { - pipe(p); - pid = fork(); - if (pid == 0) { - close(p[0]); - dup2(in_fd, STDIN_FILENO); - dup2(p[1], STDOUT_FILENO); - result = eval_rec(node->content.pipe.left, env, in_fd); - 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"); - result = EXIT_FAILURE; - } - return result; -} - -int eval(t_node *node, t_env **env) { - return eval_rec(node, env, STDIN_FILENO); -} - -int execute_cmd(t_cmd *cmd, t_env **env) { - if (handle_redirections(cmd->redirs) == -1) { - return EXIT_FAILURE; - } - - if (is_builtin(cmd->args[0])) { - return execute_builtin(cmd->args, env); - } else { - pid_t pid = fork(); - if (pid == 0) { - char *cmd_path = get_cmd_path(cmd->args[0], *env); - if (!cmd_path) { - fprintf(stderr, "%s: command not found\n", cmd->args[0]); - exit(EXIT_FAILURE); - } - execve(cmd_path, cmd->args, env_to_strlst(*env)); - perror("execve"); - exit(EXIT_FAILURE); - } else { - int status; - waitpid(pid, &status, 0); - return WEXITSTATUS(status); - } - } -} -have to update env list/tokenlist order or update the existing code to handle the order of the tokenlist/env list - -echo "Hello" | grep H this works - -also more to do on heredoc - -and for the for part of domenic removing the " ' frome the string -beside - - - -t_redirection *collect_redirs(t_token **tokens) -{ - t_redirection *result; - t_token *cur; - - cur = *tokens; - result = malloc(sizeof(t_redirection) * 2); - if (result == NULL) - return (free_tokens(*tokens), NULL); - set_redir(&result[0], 0, NULL); - set_redir(&result[1], 0, NULL); - while (cur != NULL && cur->next != NULL) - { - if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur); - else if (cur->type == REDIR_TOKEN) - return (free(result), NULL); - else - cur = cur->next; - } - if (cur && cur->type == REDIR_TOKEN) - return (free(result), NULL); - return (result); -} - -static void collect_and_check_redir(t_redirection *result, t_token **cur) -{ - char *heredoc_data; - t_token *next_token; - - heredoc_data = NULL; - if ((*cur)->content.redir_type == INPUT_LIMITER) - { - // Handle Here Document (<<) - heredoc_data = read_heredoc((*cur)->next->content.string); - if (!heredoc_data) - { - perror("Heredoc allocation failed"); - return ; - } - set_redir(&result[0], INPUT_LIMITER, heredoc_data); - } - else if ((*cur)->content.redir_type == INPUT_FILE) - { - // Handle Input File (<) - set_redir(&result[0], INPUT_FILE, ft_strdup((*cur)->next->content.string)); - } - else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - { - // Handle Output File Overwrite (>) - set_redir(&result[1], OUTPUT_OVERRIDE, ft_strdup((*cur)->next->content.string)); - } - else if ((*cur)->content.redir_type == OUTPUT_APPEND) - { - // Handle Output File Append (>>) - set_redir(&result[1], OUTPUT_APPEND, ft_strdup((*cur)->next->content.string)); - } - else - { - // Handle unexpected cases - printf("Unknown redirection type encountered\n"); - } - // Advance the token pointer to skip the redirection token and its argument - next_token = (*cur)->next; - free_token_and_connect(*cur); // Free the current redirection token - if (next_token) - { - *cur = next_token->next; // Move to the next token after the argument - } - else - { - *cur = NULL; // No more tokens - } -} - -collect args apassen x -alsso fur arg x -muss in rpl parse x -und in parse cmd collect args and x - -format args seems to work later test x - - -Test 1: ✅ echo hello world -Test 2: ✅ echo "hello world" -Test 3: ✅ echo 'hello world' -Test 4: ✅ echo hello'world' -Test 5: ✅ echo hello""world -Test 6: ✅ echo '' -Test 7: ✅ echo "$PWD" -Test 8: ✅ echo '$PWD' - -format_string problem -Test 9: ❌ echo "aspas ->'" -mini output = (aspas ->") -bash output = (aspas ->') -Test 10: ❌ echo "aspas -> ' " -mini output = (aspas -> ") -bash output = (aspas -> ' ) -Test 11: ✅ echo 'aspas ->"' -Test 12: ✅ echo 'aspas -> " ' -Test 13: ❌ echo "> >> < * ? [ ] | ; [ ] || && ( ) & # $ <<" -mini output = (> >> < README.md bash.supp bash_outfiles bonus bonus_bonus builtins extras local.supp loop.out manual_tests mini_outfiles os_specific outfiles pipes redirects syntax test_files tester wildcards ? [ ] | ; [ ] || && ( ) & # ) -bash output = (> >> < README.md bash.supp bash_outfiles bonus bonus_bonus builtins extras local.supp loop.out manual_tests mini_outfiles os_specific outfiles pipes redirects syntax test_files tester wildcards ? [ ] | ; [ ] || && ( ) & # $ <<) -Test 14: ✅ echo '> >> < * ? [ ] | ; [ ] || && ( ) & # $ <<' -Test 15: ❌ echo "exit_code ->$? user ->$USER home -> $HOME" -mini output = (exit_code ->/Users/chuhlig) -bash output = (exit_code ->0 user ->chuhlig home -> /Users/chuhlig) -Test 16: ✅ echo 'exit_code ->$? user ->$USER home -> $HOME' -Test 17: ❌ echo "$" -mini output = (0) -bash output = ($) -Test 18: ✅ echo '$' -Test 19: ❌ echo $ -mini output = (0) -bash output = ($) -Test 20: ✅ echo $? -Test 21: ❌ echo $?HELLO -mini output = () -bash output = (0HELLO) -Test 22: ✅ pwd -Test 23: ✅ pwd oi - -Test 24: ❌ export hello //stupid -mini exit code = 1 -bash exit code = 0 -Test 25: ✅ export HELLO=123 -Test 26: ✅⚠️ export A- -mini error = () -bash error = ( not a valid identifier) - fucking extra function for identifier - -Test 27: ❌ export HELLO=123 A -mini exit code = 1 -bash exit code = 0 -auch dum -Test 28: ✅ export HELLO="123 A-" -Test 29: ❌ export hello world -mini exit code = 1 -bash exit code = 0 -Test 30: ❌ export HELLO-=123 -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( not a valid identifier) -Test 31: ❌ export = -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( not a valid identifier) -Test 32: ✅⚠️ export 123 -mini error = () -bash error = ( not a valid identifier) -Test 33: ✅ unset -Test 34: ✅ unset HELLO -Test 35: ✅ unset HELLO1 HELLO2 -Test 36: ✅ unset HOME -Test 37: ✅ unset PATH -Test 38: ✅ unset SHELL - - -Test 39: ❌ cd $PWD -mini exit code = 139 -bash exit code = 0 -Test 40: ❌ cd $PWD hi -mini exit code = 139 -bash exit code = 0 -Test 41: ❌ cd 123123 -mini exit code = 139 -bash exit code = 1 -mini error = () -bash error = ( No such file or directory) - -Test 42: ✅ exit 123 -Test 43: ✅ exit 298 -Test 44: ✅ exit +100 -Test 45: ✅ exit "+100" -Test 46: ✅ exit +"100" -Test 47: ✅ exit -100 -Test 48: ✅ exit "-100" -Test 49: ✅ exit -"100" - -needs a update -Test 50: ❌ exit hello -mini exit code = 0 -bash exit code = 255 -mini error = () -bash error = ( numeric argument required) - -edge case -Test 51: ❌ exit 42 world -mini exit code = 42 -bash exit code = 1 -mini error = () -bash error = ( too many arguments) -Test 52: ✅ -———————————— pipes -Test 53: ❌ env | sort | grep -v SHLVL | grep -v ^_ -mini output = (?=0 COLORTERM=truecolor GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh HOME=/Users/chuhlig LANG=en_US.UTF-8 LOGNAME=chuhlig MallocNanoZone=0 ORIGINAL_XDG_CURRENT_DESKTOP=undefined PATH=/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/Library/Application Support/Code/User/globalStorage/github.copilot-chat/debugCommand PWD=/Users/chuhlig/Desktop/minishell/minishell_tester SHELL=/bin/zsh SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.8vcs8lnaog/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.96.2 TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ USER=chuhlig USER_ZDOTDIR=/Users/chuhlig VSCODE_GIT_ASKPASS_EXTRA_ARGS= VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-a7f217e79d.sock VSCODE_INJECTION=1 XPC_FLAGS=0x0 XPC_SERVICE_NAME=0 ZDOTDIR=/Users/chuhlig) -bash output = (COLORTERM=truecolor GIT_ASKPASS=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass.sh HOME=/Users/chuhlig LANG=en_US.UTF-8 LOGNAME=chuhlig MallocNanoZone=0 ORIGINAL_XDG_CURRENT_DESKTOP=undefined PATH=/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/goinfre/.brew/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/munki:/Library/Apple/usr/bin:/Users/chuhlig/goinfre/.brew/bin:/Users/chuhlig/Library/Application Support/Code/User/globalStorage/github.copilot-chat/debugCommand PWD=/Users/chuhlig/Desktop/minishell/minishell_tester SHELL=/bin/zsh SSH_AUTH_SOCK=/private/tmp/com.apple.launchd.8vcs8lnaog/Listeners TERM=xterm-256color TERM_PROGRAM=vscode TERM_PROGRAM_VERSION=1.96.2 TMPDIR=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/ USER=chuhlig USER_ZDOTDIR=/Users/chuhlig VSCODE_GIT_ASKPASS_EXTRA_ARGS= VSCODE_GIT_ASKPASS_MAIN=/Applications/Visual Studio Code.app/Contents/Resources/app/extensions/git/dist/askpass-main.js VSCODE_GIT_ASKPASS_NODE=/Applications/Visual Studio Code.app/Contents/Frameworks/Code Helper (Plugin).app/Contents/MacOS/Code Helper (Plugin) VSCODE_GIT_IPC_HANDLE=/var/folders/zz/zyxvpxvq6csfxvn_n000ckjr0034mf/T/vscode-git-a7f217e79d.sock VSCODE_INJECTION=1 XPC_FLAGS=0x0 XPC_SERVICE_NAME=0 ZDOTDIR=/Users/chuhlig) -Test 54: ✅ cat ./test_files/infile_big | grep oi -Test 55: ✅ cat minishell.h | grep ");"$ -Test 56: ✅ export GHOST=123 | env | grep GHOST -———————————— redirects -Test 57: ✅ grep hi <./test_files/infile -Test 58: ✅ grep hi "./outfiles/outfile01 -Test 82: ✅ ls > ./outfiles/outfile01 -Test 83: ✅ echo hi > ./outfiles/outfile01 bye - - -check this extra - -Test 84: ❌ ls >./outfiles/outfile01 >./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -Test 85: ❌ ls >./outfiles/outfile01 >./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 86: ❌ ls >"./outfiles/outfile with spaces" -Only in ./bash_outfiles: outfile with spaces -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -mini exit code = 1 -bash exit code = 0 -mini error = ( No such file or directory) -bash error = () -Test 87: ❌ ls >"./outfiles/outfile""1""2""3""4""5" -Only in ./bash_outfiles: outfile12345 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -mini exit code = 1 -bash exit code = 0 -mini error = ( No such file or directory) -bash error = () -Test 88: ❌ ls >"./outfiles/outfile01" >./test_files/invalid_permission >"./outfiles/outfile02" -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -mini error = ( No such file or directory) -bash error = ( Permission denied) -Test 89: ✅ ls >./test_files/invalid_permission >"./outfiles/outfile01" >./test_files/invalid_permission -Test 90: ❌ cat <"./test_files/infile" >"./outfiles/outfile01" -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -hi -hello -world -42 -mini exit code = 1 -bash exit code = 0 -mini error = ( No such file or directory) -bash error = () -Test 91: ✅ echo hi >./outfiles/outfile01 | echo bye -Test 92: ❌ echo hi >./outfiles/outfile01 >./outfiles/outfile02 | echo bye -Only in ./bash_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -hi -Test 93: ✅ echo hi | echo >./outfiles/outfile01 bye -Test 94: ❌ echo hi | echo bye >./outfiles/outfile01 >./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -bye -bash outfiles: -bye -Test 95: ✅ echo hi >./outfiles/outfile01 | echo bye >./outfiles/outfile02 -Test 96: ❌ echo hi >./outfiles/outfile01 >./test_files/invalid_permission | echo bye -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 97: ✅ echo hi >./test_files/invalid_permission | echo bye -Test 98: ❌ echo hi >./test_files/invalid_permission >./outfiles/outfile01 | echo bye -Only in ./mini_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini error = () -bash error = ( Permission denied) -Test 99: ✅ echo hi | echo bye >./test_files/invalid_permission -Test 100: ❌ echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -mini exit code = 139 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) - -update for this. - -Test 101: ❌ echo hi | echo bye >./test_files/invalid_permission >./outfiles/outfile01 -Only in ./mini_outfiles: outfile01 -mini outfiles: -bye -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) -Test 102: ✅⚠️ cat <"./test_files/infile" >./test_files/invalid_permission -mini error = ( No such file or directory) -bash error = ( Permission denied) -Test 103: ✅⚠️ cat >./test_files/invalid_permission <"./test_files/infile" -mini error = ( No such file or directory) -bash error = ( Permission denied) -Test 104: ✅ cat ./outfiles/outfile01 -Test 105: ❌ cat >./outfiles/outfile01 ./test_files/invalid_permission -Test 107: ✅⚠️ cat >./test_files/invalid_permission ./outfiles/outfile01 ./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 109: ✅ ls >>./outfiles/outfile01 -Test 110: ✅ ls >> ./outfiles/outfile01 -Test 111: ✅ ls >>./outfiles/outfile01 >./outfiles/outfile01 -Test 112: ✅ ls >./outfiles/outfile01 >>./outfiles/outfile01 -Test 113: ❌ ls >./outfiles/outfile01 >>./outfiles/outfile01 >./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -Test 114: ❌ ls >>./outfiles/outfile01 >>./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -Test 115: ✅ ls >>./test_files/invalid_permission -Test 116: ❌ ls >>./test_files/invalid_permission >>./outfiles/outfile01 -Only in ./mini_outfiles: outfile01 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) -Test 117: ❌ ls >>./outfiles/outfile01 >>./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 118: ❌ ls >./outfiles/outfile01 >>./test_files/invalid_permission >>./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -Only in ./mini_outfiles: outfile02 -mini outfiles: -README.md -bash.supp -bash_outfiles -bonus -bonus_bonus -builtins -extras -local.supp -loop.out -manual_tests -mini_outfiles -os_specific -outfiles -pipes -redirects -syntax -test_files -tester -wildcards -bash outfiles: -mini exit code = 0 -bash exit code = 1 -mini error = () -bash error = ( Permission denied) -Test 119: ✅ ls >./test_files/invalid_permission >>./outfiles/outfile02 -Test 120: ✅⚠️ ls >>./test_files/invalid_permission >>./outfiles/outfile02 >./outfiles/outfile01 | echo bye -Test 122: ❌ echo hi >>./outfiles/outfile01 >>./outfiles/outfile02 | echo bye -Only in ./bash_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -hi -Test 123: ✅ echo hi | echo >>./outfiles/outfile01 bye -Test 124: ❌ echo hi | echo bye >>./outfiles/outfile01 >>./outfiles/outfile02 -Only in ./bash_outfiles: outfile01 -mini outfiles: -bye -bash outfiles: -bye -Test 125: ✅ echo hi >>./outfiles/outfile01 | echo bye >>./outfiles/outfile02 -Test 126: ✅ echo hi >>./test_files/invalid_permission | echo bye -Test 127: ❌ echo hi >>./test_files/invalid_permission >./outfiles/outfile01 | echo bye -Only in ./mini_outfiles: outfile01 -mini outfiles: -hi -bash outfiles: -cat: ./bash_outfiles/*: No such file or directory -mini error = () -bash error = ( Permission denied) -Test 128: ✅ echo hi | echo bye >>./test_files/invalid_permission -Test 129: ❌ echo hi | echo >>./outfiles/outfile01 bye >./test_files/invalid_permission -Only in ./bash_outfiles: outfile01 -mini outfiles: -cat: ./mini_outfiles/*: No such file or directory -bash outfiles: -Test 130: ✅ cat ./outfiles/outfile -Test 131: ✅ cat +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:48:27 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:26:30 by dkaiser ### ########.fr */ +/* Updated: 2025/01/18 19:28:51 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -#include "debug_tools.h" -#include "stdlib.h" +#ifndef AST_H +# define AST_H +# include "debug_tools.h" +# include "stdlib.h" enum e_node_type { @@ -67,3 +69,5 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_node *new_string_node(char *string); void free_node(t_node *node); + +#endif \ No newline at end of file diff --git a/include/debug_tools.h b/include/debug_tools.h index 4a7ff10..6c4bc29 100644 --- a/include/debug_tools.h +++ b/include/debug_tools.h @@ -3,15 +3,17 @@ /* ::: :::::::: */ /* debug_tools.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 18:34:37 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 15:05:12 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 21:08:15 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DEBUG_TOOLS_H # define DEBUG_TOOLS_H +# include +# include "debug_tools.h" # include "libft.h" @@ -22,4 +24,9 @@ void dbg(char *str); void panic(char *msg); + + +void dbg2(const char *format, ...); + + #endif diff --git a/include/env.h b/include/env.h index 0c45aef..3fc83b7 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: 2025/01/14 16:50:55 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:43:07 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,5 +37,6 @@ int cd(t_env **env, char **args); int ft_env(t_env *env); int builtin_exit(char **args, t_env **env); t_env *env_new(char *name); +t_env *check_existing(t_env *env, char *av); #endif \ No newline at end of file diff --git a/include/minishell.h b/include/minishell.h index ee42c79..7a573e8 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:38:44 by dkaiser ### ########.fr */ +/* Updated: 2025/01/18 19:30:43 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,11 +41,20 @@ void print_ast(t_node *ast); int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); -char *format_string(char *str, t_env *env); +char *format_string(char *str, t_env *env, int is_literal); int set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); char *read_heredoc(char *delimiter); -void create_files(t_list *files); +int handle_input_redirection(t_redirection *redir); +int handle_output_redirection(t_redirection *redir); +int handle_redirections(t_redirection *redirs); +int handle_pipe_parent(int p[2], t_node *node, t_env **env); +int handle_pipe_child(int p[2], t_node *node, + t_env **env, int in_fd); +int open_file(char *path, int flags, int mode); +int eval_rec(t_node *node, t_env **env, int in_fd); +void create_files(t_list *files); + #endif diff --git a/include/token.h b/include/token.h index 54a65f2..993a408 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/08/29 15:26:23 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:37:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,5 +49,6 @@ void free_token_and_connect(t_token *token); void free_tokens(t_token *tokens); void tokenizer(char *s, t_token **token_list, char quote_check); +void print_token(t_token *token); #endif diff --git a/lib/libft/_obj/ft_atoi.o b/lib/libft/_obj/ft_atoi.o new file mode 100644 index 0000000..efcdc0d Binary files /dev/null and b/lib/libft/_obj/ft_atoi.o differ diff --git a/lib/libft/_obj/ft_atol.o b/lib/libft/_obj/ft_atol.o new file mode 100644 index 0000000..3126061 Binary files /dev/null and b/lib/libft/_obj/ft_atol.o differ diff --git a/lib/libft/_obj/ft_bzero.o b/lib/libft/_obj/ft_bzero.o new file mode 100644 index 0000000..64ddf3a Binary files /dev/null and b/lib/libft/_obj/ft_bzero.o differ diff --git a/lib/libft/_obj/ft_calloc.o b/lib/libft/_obj/ft_calloc.o new file mode 100644 index 0000000..39c2c05 Binary files /dev/null and b/lib/libft/_obj/ft_calloc.o differ diff --git a/lib/libft/_obj/ft_isalnum.o b/lib/libft/_obj/ft_isalnum.o new file mode 100644 index 0000000..3aecc58 Binary files /dev/null and b/lib/libft/_obj/ft_isalnum.o differ diff --git a/lib/libft/_obj/ft_isalpha.o b/lib/libft/_obj/ft_isalpha.o new file mode 100644 index 0000000..da20b01 Binary files /dev/null and b/lib/libft/_obj/ft_isalpha.o differ diff --git a/lib/libft/_obj/ft_isascii.o b/lib/libft/_obj/ft_isascii.o new file mode 100644 index 0000000..2262942 Binary files /dev/null and b/lib/libft/_obj/ft_isascii.o differ diff --git a/lib/libft/_obj/ft_isdigit.o b/lib/libft/_obj/ft_isdigit.o new file mode 100644 index 0000000..a0e0f40 Binary files /dev/null and b/lib/libft/_obj/ft_isdigit.o differ diff --git a/lib/libft/_obj/ft_isprint.o b/lib/libft/_obj/ft_isprint.o new file mode 100644 index 0000000..8cf5033 Binary files /dev/null and b/lib/libft/_obj/ft_isprint.o differ diff --git a/lib/libft/_obj/ft_isspace.o b/lib/libft/_obj/ft_isspace.o new file mode 100644 index 0000000..f6f10e8 Binary files /dev/null and b/lib/libft/_obj/ft_isspace.o differ diff --git a/lib/libft/_obj/ft_itoa.o b/lib/libft/_obj/ft_itoa.o new file mode 100644 index 0000000..447812f Binary files /dev/null and b/lib/libft/_obj/ft_itoa.o differ diff --git a/lib/libft/_obj/ft_lstadd_back_bonus.o b/lib/libft/_obj/ft_lstadd_back_bonus.o new file mode 100644 index 0000000..566f7fc Binary files /dev/null and b/lib/libft/_obj/ft_lstadd_back_bonus.o differ diff --git a/lib/libft/_obj/ft_lstadd_front_bonus.o b/lib/libft/_obj/ft_lstadd_front_bonus.o new file mode 100644 index 0000000..f456510 Binary files /dev/null and b/lib/libft/_obj/ft_lstadd_front_bonus.o differ diff --git a/lib/libft/_obj/ft_lstclear_bonus.o b/lib/libft/_obj/ft_lstclear_bonus.o new file mode 100644 index 0000000..cbf23d7 Binary files /dev/null and b/lib/libft/_obj/ft_lstclear_bonus.o differ diff --git a/lib/libft/_obj/ft_lstdelone_bonus.o b/lib/libft/_obj/ft_lstdelone_bonus.o new file mode 100644 index 0000000..c357699 Binary files /dev/null and b/lib/libft/_obj/ft_lstdelone_bonus.o differ diff --git a/lib/libft/_obj/ft_lstiter_bonus.o b/lib/libft/_obj/ft_lstiter_bonus.o new file mode 100644 index 0000000..139d20d Binary files /dev/null and b/lib/libft/_obj/ft_lstiter_bonus.o differ diff --git a/lib/libft/_obj/ft_lstlast_bonus.o b/lib/libft/_obj/ft_lstlast_bonus.o new file mode 100644 index 0000000..0f262f1 Binary files /dev/null and b/lib/libft/_obj/ft_lstlast_bonus.o differ diff --git a/lib/libft/_obj/ft_lstmap_bonus.o b/lib/libft/_obj/ft_lstmap_bonus.o new file mode 100644 index 0000000..d4ed0b6 Binary files /dev/null and b/lib/libft/_obj/ft_lstmap_bonus.o differ diff --git a/lib/libft/_obj/ft_lstnew_bonus.o b/lib/libft/_obj/ft_lstnew_bonus.o new file mode 100644 index 0000000..98420b2 Binary files /dev/null and b/lib/libft/_obj/ft_lstnew_bonus.o differ diff --git a/lib/libft/_obj/ft_lstsize_bonus.o b/lib/libft/_obj/ft_lstsize_bonus.o new file mode 100644 index 0000000..1ce926d Binary files /dev/null and b/lib/libft/_obj/ft_lstsize_bonus.o differ diff --git a/lib/libft/_obj/ft_memchr.o b/lib/libft/_obj/ft_memchr.o new file mode 100644 index 0000000..23efe3d Binary files /dev/null and b/lib/libft/_obj/ft_memchr.o differ diff --git a/lib/libft/_obj/ft_memcmp.o b/lib/libft/_obj/ft_memcmp.o new file mode 100644 index 0000000..ca68348 Binary files /dev/null and b/lib/libft/_obj/ft_memcmp.o differ diff --git a/lib/libft/_obj/ft_memcpy.o b/lib/libft/_obj/ft_memcpy.o new file mode 100644 index 0000000..253e17e Binary files /dev/null and b/lib/libft/_obj/ft_memcpy.o differ diff --git a/lib/libft/_obj/ft_memmove.o b/lib/libft/_obj/ft_memmove.o new file mode 100644 index 0000000..df18bef Binary files /dev/null and b/lib/libft/_obj/ft_memmove.o differ diff --git a/lib/libft/_obj/ft_memset.o b/lib/libft/_obj/ft_memset.o new file mode 100644 index 0000000..be0b314 Binary files /dev/null and b/lib/libft/_obj/ft_memset.o differ diff --git a/lib/libft/_obj/ft_printaddr.o b/lib/libft/_obj/ft_printaddr.o new file mode 100644 index 0000000..64e9dea Binary files /dev/null and b/lib/libft/_obj/ft_printaddr.o differ diff --git a/lib/libft/_obj/ft_printf.o b/lib/libft/_obj/ft_printf.o new file mode 100644 index 0000000..44aedad Binary files /dev/null and b/lib/libft/_obj/ft_printf.o differ diff --git a/lib/libft/_obj/ft_printhex.o b/lib/libft/_obj/ft_printhex.o new file mode 100644 index 0000000..5ef814f Binary files /dev/null and b/lib/libft/_obj/ft_printhex.o differ diff --git a/lib/libft/_obj/ft_printnbr.o b/lib/libft/_obj/ft_printnbr.o new file mode 100644 index 0000000..278895f Binary files /dev/null and b/lib/libft/_obj/ft_printnbr.o differ diff --git a/lib/libft/_obj/ft_putchar_fd.o b/lib/libft/_obj/ft_putchar_fd.o new file mode 100644 index 0000000..e3a7ad2 Binary files /dev/null and b/lib/libft/_obj/ft_putchar_fd.o differ diff --git a/lib/libft/_obj/ft_putendl_fd.o b/lib/libft/_obj/ft_putendl_fd.o new file mode 100644 index 0000000..5ff74c8 Binary files /dev/null and b/lib/libft/_obj/ft_putendl_fd.o differ diff --git a/lib/libft/_obj/ft_putnbr_fd.o b/lib/libft/_obj/ft_putnbr_fd.o new file mode 100644 index 0000000..1de6482 Binary files /dev/null and b/lib/libft/_obj/ft_putnbr_fd.o differ diff --git a/lib/libft/_obj/ft_putstr_fd.o b/lib/libft/_obj/ft_putstr_fd.o new file mode 100644 index 0000000..bc970e3 Binary files /dev/null and b/lib/libft/_obj/ft_putstr_fd.o differ diff --git a/lib/libft/_obj/ft_split.o b/lib/libft/_obj/ft_split.o new file mode 100644 index 0000000..d5df61b Binary files /dev/null and b/lib/libft/_obj/ft_split.o differ diff --git a/lib/libft/_obj/ft_strcat.o b/lib/libft/_obj/ft_strcat.o new file mode 100644 index 0000000..f749d90 Binary files /dev/null and b/lib/libft/_obj/ft_strcat.o differ diff --git a/lib/libft/_obj/ft_strchr.o b/lib/libft/_obj/ft_strchr.o new file mode 100644 index 0000000..fea0b41 Binary files /dev/null and b/lib/libft/_obj/ft_strchr.o differ diff --git a/lib/libft/_obj/ft_strcmp.o b/lib/libft/_obj/ft_strcmp.o new file mode 100644 index 0000000..5c7286b Binary files /dev/null and b/lib/libft/_obj/ft_strcmp.o differ diff --git a/lib/libft/_obj/ft_strcpy.o b/lib/libft/_obj/ft_strcpy.o new file mode 100644 index 0000000..d556c8f Binary files /dev/null and b/lib/libft/_obj/ft_strcpy.o differ diff --git a/lib/libft/_obj/ft_strdup.o b/lib/libft/_obj/ft_strdup.o new file mode 100644 index 0000000..e052b40 Binary files /dev/null and b/lib/libft/_obj/ft_strdup.o differ diff --git a/lib/libft/_obj/ft_striteri.o b/lib/libft/_obj/ft_striteri.o new file mode 100644 index 0000000..e8264a9 Binary files /dev/null and b/lib/libft/_obj/ft_striteri.o differ diff --git a/lib/libft/_obj/ft_strjoin.o b/lib/libft/_obj/ft_strjoin.o new file mode 100644 index 0000000..bb8a89e Binary files /dev/null and b/lib/libft/_obj/ft_strjoin.o differ diff --git a/lib/libft/_obj/ft_strlcat.o b/lib/libft/_obj/ft_strlcat.o new file mode 100644 index 0000000..e7aff81 Binary files /dev/null and b/lib/libft/_obj/ft_strlcat.o differ diff --git a/lib/libft/_obj/ft_strlcpy.o b/lib/libft/_obj/ft_strlcpy.o new file mode 100644 index 0000000..695a120 Binary files /dev/null and b/lib/libft/_obj/ft_strlcpy.o differ diff --git a/lib/libft/_obj/ft_strlen.o b/lib/libft/_obj/ft_strlen.o new file mode 100644 index 0000000..2c66545 Binary files /dev/null and b/lib/libft/_obj/ft_strlen.o differ diff --git a/lib/libft/_obj/ft_strmapi.o b/lib/libft/_obj/ft_strmapi.o new file mode 100644 index 0000000..345a34a Binary files /dev/null and b/lib/libft/_obj/ft_strmapi.o differ diff --git a/lib/libft/_obj/ft_strncmp.o b/lib/libft/_obj/ft_strncmp.o new file mode 100644 index 0000000..683c861 Binary files /dev/null and b/lib/libft/_obj/ft_strncmp.o differ diff --git a/lib/libft/_obj/ft_strncpy.o b/lib/libft/_obj/ft_strncpy.o new file mode 100644 index 0000000..5585d29 Binary files /dev/null and b/lib/libft/_obj/ft_strncpy.o differ diff --git a/lib/libft/_obj/ft_strnstr.o b/lib/libft/_obj/ft_strnstr.o new file mode 100644 index 0000000..4f42fd4 Binary files /dev/null and b/lib/libft/_obj/ft_strnstr.o differ diff --git a/lib/libft/_obj/ft_strrchr.o b/lib/libft/_obj/ft_strrchr.o new file mode 100644 index 0000000..c6b2a16 Binary files /dev/null and b/lib/libft/_obj/ft_strrchr.o differ diff --git a/lib/libft/_obj/ft_strtrim.o b/lib/libft/_obj/ft_strtrim.o new file mode 100644 index 0000000..b4ac15c Binary files /dev/null and b/lib/libft/_obj/ft_strtrim.o differ diff --git a/lib/libft/_obj/ft_substr.o b/lib/libft/_obj/ft_substr.o new file mode 100644 index 0000000..eebbca7 Binary files /dev/null and b/lib/libft/_obj/ft_substr.o differ diff --git a/lib/libft/_obj/ft_tolower.o b/lib/libft/_obj/ft_tolower.o new file mode 100644 index 0000000..940856f Binary files /dev/null and b/lib/libft/_obj/ft_tolower.o differ diff --git a/lib/libft/_obj/ft_toupper.o b/lib/libft/_obj/ft_toupper.o new file mode 100644 index 0000000..137ae03 Binary files /dev/null and b/lib/libft/_obj/ft_toupper.o differ diff --git a/lib/libft/_obj/get_next_line.o b/lib/libft/_obj/get_next_line.o new file mode 100644 index 0000000..d5cbdc5 Binary files /dev/null and b/lib/libft/_obj/get_next_line.o differ diff --git a/lib/libft/_obj/get_next_line_utils.o b/lib/libft/_obj/get_next_line_utils.o new file mode 100644 index 0000000..0eecbe7 Binary files /dev/null and b/lib/libft/_obj/get_next_line_utils.o differ diff --git a/lib/libft/libft.a b/lib/libft/libft.a new file mode 100644 index 0000000..1a43a20 Binary files /dev/null and b/lib/libft/libft.a differ diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index f3bcfc0..7cbcdeb 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,60 +6,13 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:51:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:33:33 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "env.h" #include -int echo(char **av) -{ - int i; - int f; - - i = 1; - f = 1; - if (av[1] == NULL || av[1][0] == '\0') - { - write(1, "\n", 1); - return (0); - } - if (ft_strncmp(av[1], "-n", 3) == 0) - { - i++; - f = 0; - } - while (av[i]) - { - write(1, av[i], ft_strlen(av[i])); - i++; - if (av[i]) - write(1, " ", 1); - } - if (f) - write(1, "\n", 1); - return (0); -} - -void exit_shell(t_env **env, int exit_status) -{ - free_envlst(env); - exit(exit_status); -} - -int builtin_exit(char **args, t_env **env) -{ - int exit_status; - - if (args[1]) - exit_status = ft_atoi(args[1]); - else - exit_status = 0; - exit_shell(env, exit_status); - return (exit_status); -} - int unset(char **av, t_env **env) { t_env *current; @@ -69,8 +22,6 @@ int unset(char **av, t_env **env) i = 0; while (av[++i]) { - if (ft_strchr(av[i], '?')) - return (1); current = *env; prev = NULL; while (current) @@ -95,6 +46,8 @@ t_env *check_existing(t_env *env, char *av) { while (env) { + if (ft_strcmp("$", av) == 0) + return (NULL); if (ft_strcmp(env->name, av) == 0) return (env); env = env->next; @@ -102,51 +55,65 @@ t_env *check_existing(t_env *env, char *av) return (NULL); } -int export(char **av, t_env **env) +void export_export(char *av, t_env **env) { char *tmp; t_env *current; - int i; current = NULL; + tmp = ft_strchr(av, '='); + *tmp = '\0'; + current = check_existing(*env, av); + if (current) + free(current->value); + else + { + current = env_new(ft_strdup(av)); + current->next = *env; + *env = current; + } + current->value = ft_strdup(tmp + 1); +} + +int is_valid_identifier(char *str) +{ + int i; + i = 0; - while (av[++i]) + if (!ft_isalpha(str[0]) && str[0] != '_') + return (0); + while (str[i] && str[i] != '=') { - if ((ft_strchr(av[i], '='))) - { - tmp = ft_strchr(av[i], '='); - *tmp = '\0'; - if (ft_strchr(av[i], '?')) - return (1); - current = check_existing(*env, av[i]); - if (current) - free(current->value); - else - { - current = env_new(ft_strdup(av[i])); - current->next = *env; - *env = current; - } - current->value = ft_strdup(tmp + 1); - } - else - return (1); + if (!ft_isalnum(str[i]) && str[i] != '_') + return (0); + i++; } - return (0); + return (1); } -void set_return_code(int return_code, t_env **env) +int export(char **av, t_env **env) { - t_env *cur; + char *equal_sign; + int i; - cur = check_existing(*env, "?"); - if (cur) - free(cur->value); - else + i = 0; + while (av[++i]) { - cur = env_new(ft_strdup("?")); - cur->next = *env; - *env = cur; + equal_sign = ft_strchr(av[i], '='); + if (equal_sign) + *equal_sign = '\0'; + if (!is_valid_identifier(av[i])) + { + write(1, "Minishell $ export: not a valid identifier\n", 43); + if (equal_sign) + *equal_sign = '='; + continue ; + } + if (equal_sign) + { + *equal_sign = '='; + export_export(av[i], env); + } } - cur->value = ft_itoa(return_code); + return (0); } diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c new file mode 100644 index 0000000..7c73e95 --- /dev/null +++ b/src/builtins_part_three.c @@ -0,0 +1,76 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* builtins_part_three.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:29:24 by chuhlig #+# #+# */ +/* Updated: 2025/01/18 18:34:29 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void exit_shell(t_env **env, int exit_status) +{ + free_envlst(env); + exit(exit_status); +} + +int builtin_exit(char **av, t_env **env) +{ + int exit_status; + + if (av[1]) + exit_status = ft_atoi(av[1]); + else + exit_status = 0; + exit_shell(env, exit_status); + return (exit_status); +} + +void set_return_code(int return_code, t_env **env) +{ + t_env *cur; + + cur = check_existing(*env, "?"); + if (cur) + free(cur->value); + else + { + cur = env_new(ft_strdup("?")); + cur->next = *env; + *env = cur; + } + cur->value = ft_itoa(return_code); +} + +int echo(char **av) +{ + int i; + int f; + + i = 1; + f = 1; + if (av[1] == NULL || av[1][0] == '\0') + { + write(1, "\n", 1); + return (0); + } + if (ft_strncmp(av[1], "-n", 3) == 0) + { + i++; + f = 0; + } + while (av[i]) + { + write(1, av[i], ft_strlen(av[i])); + i++; + if (av[i]) + write(1, " ", 1); + } + if (f) + write(1, "\n", 1); + return (0); +} diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c index 05d6943..9fabc81 100644 --- a/src/builtins_part_two.c +++ b/src/builtins_part_two.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:31:17 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:57:12 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,7 @@ int cd(t_env **env, char **av) } if (chdir(current->value) == -1) return (1); + update_pwd(env); } else { @@ -101,7 +102,7 @@ int ft_env(t_env *env) { while (env != NULL) { - if (strchr(env->name, '?')) + if (ft_strchr(env->name, '?')) { env = env->next; continue ; diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 171dc06..c170a30 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:19:36 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 13:00:04 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -20,7 +20,8 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec, static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env); -t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_files) +t_redirection *collect_redirs(t_token **tokens, t_env *env, + t_list **create_files) { t_redirection *result; t_token *cur; @@ -45,28 +46,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, t_list **create_file return (result); } -static t_redirection *set_redir(t_redirection *redir, int type, char *spec, - t_env *env) -{ - t_redirection *result; - - redir->type = type; - if (spec != NULL) - redir->specifier = format_string(spec, env); - else - redir->specifier = spec; - if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) - { - result = malloc(sizeof(t_redirection)); - if (!result) - return (NULL); - result->type = type; - result->specifier = spec; - return (result); - } - return (NULL); -} - static void collect_and_check_redir(t_redirection *result, t_token **cur, t_env *env, t_list **create_files) { @@ -89,7 +68,7 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], OUTPUT_APPEND, str, env))); next_token = (*cur)->next; - free_token_and_connect(*cur); + // free_token_and_connect(*cur); if (next_token) { *cur = next_token->next; @@ -99,6 +78,30 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, *cur = NULL; } +static t_redirection *set_redir(t_redirection *redir, int type, char *spec, + t_env *env) +{ + t_redirection *result; + + redir->type = type; + if (spec != NULL) + redir->specifier = format_string(spec, env, ft_atoi("0")); + else + redir->specifier = spec; + if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + { + result = malloc(sizeof(t_redirection)); + if (!result) + return (NULL); + result->type = type; + result->specifier = spec; + return (result); + } + return (NULL); +} + + + static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { char *heredoc_data; diff --git a/src/create_files.c b/src/create_files.c index faee27f..8689f88 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* create_files.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 19:16:33 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,10 +22,10 @@ void create_files(t_list *files) { dbg("Test"); if (files->content == NULL) - continue; + continue ; file = (t_redirection *)files->content; if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) - break; + break ; if (file->type == OUTPUT_OVERRIDE) { fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); @@ -36,10 +36,10 @@ void create_files(t_list *files) fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); close(fd); } - /* if (files->next == NULL) */ - /* break; */ - /* if (((t_redirection *) files->next->content)->type == 0) */ - /* break; */ + if (files->next == NULL) + break ; + if (((t_redirection *) files->next->content)->type == 0) + break ; files = files->next; } } diff --git a/src/debug_tools.c b/src/debug_tools.c index de59703..6bee1b0 100644 --- a/src/debug_tools.c +++ b/src/debug_tools.c @@ -3,14 +3,16 @@ /* ::: :::::::: */ /* debug_tools.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 15:04:43 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:50:36 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" +#include +#include void dbg(char *msg) { diff --git a/src/env_to_strlst.c b/src/env_to_strlst.c index a1ab7cc..5806d96 100644 --- a/src/env_to_strlst.c +++ b/src/env_to_strlst.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:22:28 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 19:34:10 by chuhlig ### ########.fr */ +/* Updated: 2025/01/18 18:50:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -15,25 +15,32 @@ static char *get_var_assign(t_env *cur); -char **env_to_strlst(t_env *env) +static int getsize(t_env *env) { int size; t_env *cur; - char **result; - int i; size = 0; cur = env; - while (cur != NULL) + while (cur) { - if (ft_strchr(cur->name, '?')) - { - cur = cur->next; - continue ; - } - size++; + if (!ft_strchr(cur->name, '?')) + size++; cur = cur->next; } + return (size); +} + +char **env_to_strlst(t_env *env) +{ + int size; + t_env *cur; + char **result; + int i; + + size = 0; + cur = env; + size = getsize(env); result = malloc(sizeof(char *) * (size + 1)); if (result == NULL) return (NULL); diff --git a/src/execute_cmd.c b/src/execute_cmd.c index 8f5b541..e2b9d66 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/16 18:38:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 19:15:46 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/format_string.c b/src/format_string.c index 5f31130..7e64039 100644 --- a/src/format_string.c +++ b/src/format_string.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */ -/* Updated: 2025/01/14 18:06:17 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 20:24:05 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,46 +14,18 @@ #include "libft.h" #include "minishell.h" -static void append_slice(char **dst, char *src, int start, int end); -static void append_var(char **dst, char *src, int *pos, t_env *env); - -char *format_string(char *str, t_env *env) +void append_var_exit_code(char **dst, t_env *env) { + char *exit_code; char *result; - int pos; - int start; - int is_literal; - pos = 0; - start = 0; - is_literal = 0; - result = NULL; - if (str == NULL) - return (NULL); - while (str[pos] != '\0') + exit_code = env_get(env, "?"); + if (exit_code) { - if (str[pos] == '\'') - { - append_slice(&result, str, start, pos); - start = pos + 1; - is_literal = !is_literal; - } - if (str[pos] == '"' && !is_literal) - { - append_slice(&result, str, start, pos); - start = pos + 1; - } - if (str[pos] == '$' && !is_literal) - { - append_slice(&result, str, start, pos); - append_var(&result, str, &pos, env); - start = pos; - continue ; - } - pos++; + result = ft_strjoin(*dst, exit_code); + free(*dst); + *dst = result; } - append_slice(&result, str, start, pos); - return (result); } static void append_slice(char **dst, char *src, int start, int end) @@ -65,9 +37,7 @@ static void append_slice(char **dst, char *src, int start, int end) if (*dst != NULL) len = ft_strlen(*dst); else - { len = 0; - } result = malloc(len + (end - start) + 1); if (!result) return ; @@ -93,27 +63,63 @@ static void append_var(char **dst, char *src, int *pos, t_env *env) i = 0; *pos += 1; - while (src[*pos + i] != '\0' && src[*pos + i] != '\'' && src[*pos - + i] != '"' && src[*pos + i] != '$') - { + while (ft_isalnum(src[*pos + i]) || src[*pos + i] == '_') i++; - } - var = malloc(i + 1); - if (var == NULL) + if (i == 0) return ; - var[i] = '\0'; - i--; - while (i >= 0) - { - var[i] = src[*pos + i]; - i--; - } + var = ft_substr(src, *pos, i); value = env_get(env, var); - if (value != NULL) + if (value) { result = ft_strjoin(*dst, value); free(*dst); *dst = result; } - *pos += ft_strlen(var); + *pos += i; + free(var); +} + +static void handle_dollar_sign(char **result, char *str, int *pos, t_env *env) +{ + if (str[*pos + 1] == '?') + { + append_var_exit_code(result, env); + *pos += 2; + } + else if (ft_isalnum(str[*pos + 1]) || str[*pos + 1] == '_') + append_var(result, str, pos, env); + else + { + append_slice(result, str, *pos, *pos + 1); + (*pos)++; + } +} + +char *format_string(char *str, t_env *env, int is_literal) +{ + char *result; + int pos; + int start; + + pos = 0; + start = 0; + result = NULL; + if (!str) + return (NULL); + while (str[pos]) + { + if (str[pos] == '\'' || (str[pos] == '\"' && !is_literal) + || (str[pos] == '$' && !is_literal)) + { + append_slice(&result, str, start, pos); + if (str[pos] == '$') + handle_dollar_sign(&result, str, &pos, env); + else + is_literal ^= (str[pos++] == '\''); + start = pos; + continue ; + } + pos++; + } + return (append_slice(&result, str, start, pos), result); } diff --git a/src/free_token.c b/src/free_token.c index 9b035ac..512ba23 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -3,14 +3,15 @@ /* ::: :::::::: */ /* free_token.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */ -/* Updated: 2024/08/02 14:23:56 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:49:48 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "token.h" +#include "debug_tools.h" void free_token(t_token *token) { @@ -19,6 +20,7 @@ void free_token(t_token *token) if (token->next != NULL) token->next->previous = NULL; free(token); + token = NULL; } void free_token_and_connect(t_token *token) @@ -28,6 +30,7 @@ void free_token_and_connect(t_token *token) if (token->next != NULL) token->next->previous = token->previous; free(token); + token = NULL; } void free_tokens(t_token *tokens) diff --git a/src/get_cmd_path.c b/src/get_cmd_path.c index 543540b..c35b3bc 100644 --- a/src/get_cmd_path.c +++ b/src/get_cmd_path.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/15 16:38:39 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 18:56:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -86,7 +86,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) } *return_code = 127; printf("%s:", cmd); - ft_putstr_fd(" command not found", 2); + ft_putstr_fd(" command not found", 2);//output is shit even if its fine for tester return (NULL); } diff --git a/src/handle_redir.c b/src/handle_redir.c new file mode 100644 index 0000000..29bba92 --- /dev/null +++ b/src/handle_redir.c @@ -0,0 +1,103 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* handle_redir.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ +/* Updated: 2025/01/18 18:47:31 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int handle_input_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == INPUT_FILE) + { + fd = open_file(redir->specifier, O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + else if (redir->type == INPUT_LIMITER) + { + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return (-1); + write(fd, redir->specifier, ft_strlen(redir->specifier)); + close(fd); + fd = open_file("/tmp/heredoc_tmp", O_RDONLY, 0); + if (fd < 0) + return (-1); + dup2(fd, STDIN_FILENO); + close(fd); + } + return (0); +} + +int handle_output_redirection(t_redirection *redir) +{ + int fd; + + if (redir->type == OUTPUT_OVERRIDE) + { + fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + else if (redir->type == OUTPUT_APPEND) + { + fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + if (fd < 0) + return (-1); + dup2(fd, STDOUT_FILENO); + close(fd); + } + return (0); +} + +int handle_redirections(t_redirection *redirs) +{ + if (redirs[0].type == INPUT_FILE || redirs[0].type == INPUT_LIMITER) + { + if (handle_input_redirection(&redirs[0]) < 0) + return (-1); + } + if (redirs[1].type == OUTPUT_OVERRIDE || redirs[1].type == OUTPUT_APPEND) + { + if (handle_output_redirection(&redirs[1]) < 0) + return (-1); + } + return (0); +} + +int handle_pipe_parent(int p[2], t_node *node, t_env **env) +{ + int original_stdin; + int result; + + close(p[1]); + original_stdin = dup(STDIN_FILENO); + dup2(p[0], STDIN_FILENO); + result = eval_rec(node->content.pipe.right, env, p[0]); + dup2(original_stdin, STDIN_FILENO); + close(original_stdin); + close(p[0]); + return (result); +} + +int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd) +{ + close(p[0]); + dup2(in_fd, STDIN_FILENO); + dup2(p[1], STDOUT_FILENO); + close(p[1]); + exit(eval_rec(node->content.pipe.left, env, in_fd)); +} diff --git a/src/interpreter.c b/src/interpreter.c index c7fe67c..f31e965 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,100 +6,41 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/16 18:44:39 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:48:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ #include "debug_tools.h" #include "minishell.h" -int handle_redirections(t_redirection *redirs) +int eval_rec(t_node *node, t_env **env, int in_fd); + +int open_file(char *path, int flags, int mode) { int fd; - if (redirs[0].type == INPUT_FILE) - { - fd = open(redirs[0].specifier, O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - else if (redirs[0].type == INPUT_LIMITER) - { - fd = open("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - write(fd, redirs[0].specifier, ft_strlen(redirs[0].specifier)); - close(fd); - fd = open("/tmp/heredoc_tmp", O_RDONLY); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDIN_FILENO); - close(fd); - } - if (redirs[1].type == OUTPUT_OVERRIDE) - { - fd = open(redirs[1].specifier, O_WRONLY | O_TRUNC, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - else if (redirs[1].type == OUTPUT_APPEND) - { - fd = open(redirs[1].specifier, O_WRONLY | O_APPEND, 0644); - if (fd < 0) - { - perror("open"); - return (-1); - } - dup2(fd, STDOUT_FILENO); - close(fd); - } - return (0); + fd = open(path, flags, mode); + if (fd < 0) + perror("open"); + return (fd); } int eval_rec(t_node *node, t_env **env, int in_fd) { - pid_t pid; int p[2]; + pid_t pid; int result; - int original_stdin; if (node->type == PIPE_NODE) { - pipe(p); + if (pipe(p) == -1) + return (perror("pipe"), EXIT_FAILURE); pid = fork(); + if (pid == -1) + return (perror("fork"), close(p[0]), close(p[1]), EXIT_FAILURE); if (pid == 0) - { - close(p[0]); - dup2(in_fd, STDIN_FILENO); - dup2(p[1], STDOUT_FILENO); - result = eval_rec(node->content.pipe.left, env, in_fd); - exit(result); - } - else - { - close(p[1]); - original_stdin = dup(STDIN_FILENO); - dup2(p[0], STDIN_FILENO); - result = eval_rec(node->content.pipe.right, env, p[0]); - dup2(original_stdin, STDIN_FILENO); - close(original_stdin); - } + handle_pipe_child(p, node, env, in_fd); + result = handle_pipe_parent(p, node, env); } else if (node->type == CMD_NODE) result = execute_cmd(&node->content.cmd, env); diff --git a/src/new_node.c b/src/new_node.c index 83d9159..bbac154 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* new_node.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 18:25:54 by dkaiser ### ########.fr */ +/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,6 +51,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) node->content.cmd.redirs[1] = redirs[1]; node->content.cmd.create_files = create_files; free(redirs); + redirs = NULL;//1 return (node); } return (NULL); diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 92dfd12..d13bf3f 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/16 19:06:03 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 12:44:44 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,7 +18,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) { char **args; t_redirection *redirs; - t_list *create_files; + t_list *create_files; create_files = NULL; redirs = collect_redirs(&tokens, *env, &create_files); @@ -36,6 +36,7 @@ t_node *parse_cmd(t_token *tokens, t_env **env) static char **collect_args(t_token **tokens, t_env **env) { t_token *cur; + t_token *next;//2 char **result; int i; @@ -50,12 +51,14 @@ static char **collect_args(t_token **tokens, t_env **env) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { + next = cur->next;//2 if (cur->previous) free_token(cur->previous); - result[i] = format_string(cur->content.string, *env); + result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; - cur = cur->next; + // cur = cur->next; + cur = next;//2 } result[i] = NULL; return (result); -} +} \ No newline at end of file diff --git a/src/parser.c b/src/parser.c index 1375954..aef8d70 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ -/* Updated: 2025/01/11 16:06:54 by chuhlig ### ########.fr */ +/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,6 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env) if (left_side_tokens == NULL) { free_tokens(tokens); + tokens = NULL;//1 return (NULL); } else if (tokens != NULL) @@ -70,6 +71,7 @@ t_token *split_at_first(t_token **tokens, int type) if (result == split) result = NULL; free_token(split); + split = NULL;//1 return (result); } diff --git a/src/repl.c b/src/repl.c index 5079ee8..15b0a80 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 15:39:58 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 12:45:00 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void repl(const char *prompt, t_env **env, int *promptflag) while (1) { input = readline(prompt); - if (input == NULL) + if (input == NULL) { if (*promptflag > 1) (*promptflag)--; @@ -41,3 +41,5 @@ void repl(const char *prompt, t_env **env, int *promptflag) free(input); } } + +//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission \ No newline at end of file diff --git a/src/signal_handling.c b/src/signal_handling.c index c6273d0..6c6ca1e 100644 --- a/src/signal_handling.c +++ b/src/signal_handling.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 15:08:43 by dkaiser #+# #+# */ -/* Updated: 2025/01/14 14:11:29 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 12:15:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/teest.txt b/teest.txt deleted file mode 100644 index 9daeafb..0000000 --- a/teest.txt +++ /dev/null @@ -1 +0,0 @@ -test -- cgit v1.2.3 From f2ffaa70eb299c5e2bb7c181d2910337de7e99d3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 20 Jan 2025 17:13:37 +0100 Subject: Fix some errors --- include/minishell.h | 4 ++-- src/collect_redirs.c | 13 +++++++------ src/create_files.c | 11 ++++++++--- src/execute_cmd.c | 8 +++++--- src/handle_redir.c | 8 ++++---- 5 files changed, 26 insertions(+), 18 deletions(-) (limited to 'include') diff --git a/include/minishell.h b/include/minishell.h index 7a573e8..df89630 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/18 19:30:43 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 14:39:38 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,6 +55,6 @@ int handle_pipe_child(int p[2], t_node *node, t_env **env, int in_fd); int open_file(char *path, int flags, int mode); int eval_rec(t_node *node, t_env **env, int in_fd); -void create_files(t_list *files); +int create_files(t_list *files); #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index aeaf177..db4ab00 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 13:17:59 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 17:12:42 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,7 +32,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, return (free_tokens(*tokens), NULL); set_redir(&result[0], 0, NULL, env); set_redir(&result[1], 0, NULL, env); - while (cur != NULL && cur->next != NULL) + while (cur != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) collect_and_check_redir(result, &cur, env, create_files); @@ -60,17 +60,18 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, return ; } else if ((*cur)->content.redir_type == INPUT_FILE) - set_redir(&result[0], INPUT_FILE, format_string(str, env), env); + ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[0], INPUT_FILE, format_string(str, env, 0), env))); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_OVERRIDE, format_string(str, env), env))); + OUTPUT_OVERRIDE, format_string(str, env, 0), env))); else if ((*cur)->content.redir_type == OUTPUT_APPEND) ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_APPEND, format_string(str, env), env))); + OUTPUT_APPEND, format_string(str, env, 0), env))); next_token = (*cur)->next; // free_token_and_connect(*cur); if (next_token) { + free_token_and_connect(*cur); *cur = next_token->next; free_token_and_connect(next_token); } @@ -88,7 +89,7 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec, redir->specifier = format_string(spec, env, ft_atoi("0")); else redir->specifier = spec; - if (redir->type == OUTPUT_APPEND || redir->type == OUTPUT_OVERRIDE) + if (redir->type != INPUT_LIMITER) { result = malloc(sizeof(t_redirection)); if (!result) diff --git a/src/create_files.c b/src/create_files.c index 8689f88..0550e57 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -6,24 +6,28 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 14:36:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:54:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" #include -void create_files(t_list *files) +int create_files(t_list *files) { t_redirection *file; int fd; while (files) { - dbg("Test"); if (files->content == NULL) + { + files = files->next; continue ; + } file = (t_redirection *)files->content; + if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1 || access(file->specifier, R_OK) == -1)) + return (EXIT_FAILURE); if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) break ; if (file->type == OUTPUT_OVERRIDE) @@ -42,4 +46,5 @@ void create_files(t_list *files) break ; files = files->next; } + return (EXIT_SUCCESS); } diff --git a/src/execute_cmd.c b/src/execute_cmd.c index e2b9d66..ac0d5b1 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/19 19:15:46 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:54:43 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -51,8 +51,8 @@ int execute_cmd(t_cmd *cmd, t_env **env) original_std[1] = dup(STDOUT_FILENO); original_std[0] = dup(STDIN_FILENO); - create_files(cmd->create_files); - if (handle_redirections(cmd->redirs) == -1) + result = create_files(cmd->create_files); + if (result != EXIT_SUCCESS || handle_redirections(cmd->redirs) == -1) { establish_pipeline(original_std[0], original_std[1]); return (EXIT_FAILURE); @@ -63,6 +63,8 @@ int execute_cmd(t_cmd *cmd, t_env **env) establish_pipeline(original_std[0], original_std[1]); return (result); } + if (result != EXIT_SUCCESS) + return (result); return (exec_cmd(cmd, env, original_std, EXIT_SUCCESS)); } diff --git a/src/handle_redir.c b/src/handle_redir.c index 29bba92..03fa453 100644 --- a/src/handle_redir.c +++ b/src/handle_redir.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ -/* Updated: 2025/01/18 18:47:31 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:24:49 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int handle_input_redirection(t_redirection *redir) } else if (redir->type == INPUT_LIMITER) { - fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); if (fd < 0) return (-1); write(fd, redir->specifier, ft_strlen(redir->specifier)); @@ -46,7 +46,7 @@ int handle_output_redirection(t_redirection *redir) if (redir->type == OUTPUT_OVERRIDE) { - fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open_file(redir->specifier, O_WRONLY | O_TRUNC, 0644); if (fd < 0) return (-1); dup2(fd, STDOUT_FILENO); @@ -54,7 +54,7 @@ int handle_output_redirection(t_redirection *redir) } else if (redir->type == OUTPUT_APPEND) { - fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + fd = open_file(redir->specifier, O_WRONLY | O_APPEND, 0644); if (fd < 0) return (-1); dup2(fd, STDOUT_FILENO); -- cgit v1.2.3 From eceed405102e019b1f001751de60f5534be0a0ef Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 20 Jan 2025 17:32:06 +0100 Subject: Fix some more bugs --- include/minishell.h | 7 ++++++- src/collect_redirs.c | 36 +++++++++++++++++++++--------------- src/parse_cmd.c | 16 ++++++++-------- 3 files changed, 35 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/minishell.h b/include/minishell.h index df89630..ea9f687 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 14:39:38 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 17:18:48 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,4 +57,9 @@ int open_file(char *path, int flags, int mode); int eval_rec(t_node *node, t_env **env, int in_fd); int create_files(t_list *files); +typedef struct s_minidata { + t_env *env; + t_list **create_files; +} t_minidata; + #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index db4ab00..e5bc8f6 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:12:42 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 17:30:00 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,17 +14,18 @@ #include static void collect_and_check_redir(t_redirection *result, - t_token **cur, t_env *env, t_list **create_files); + t_token **cur, t_minidata *data, t_token **tokens); static t_redirection *set_redir(t_redirection *redir, int type, char *spec, t_env *env); static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env); t_redirection *collect_redirs(t_token **tokens, t_env *env, - t_list **create_files) + t_list **create_files) { t_redirection *result; t_token *cur; + t_minidata data; cur = *tokens; result = malloc(sizeof(t_redirection) * 2); @@ -32,10 +33,12 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, return (free_tokens(*tokens), NULL); set_redir(&result[0], 0, NULL, env); set_redir(&result[1], 0, NULL, env); + data.create_files = create_files; + data.env = env; while (cur != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_and_check_redir(result, &cur, env, create_files); + collect_and_check_redir(result, &cur, &data, tokens); else if (cur->type == REDIR_TOKEN) return (free(result), NULL); else @@ -47,7 +50,7 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, } static void collect_and_check_redir(t_redirection *result, t_token **cur, - t_env *env, t_list **create_files) + t_minidata *data, t_token **tokens) { t_token *next_token; char *str; @@ -56,22 +59,27 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, str = ft_strdup((*cur)->next->content.string); if ((*cur)->content.redir_type == INPUT_LIMITER) { - if (!set_heredoc_data(*cur, result, env)) + if (!set_heredoc_data(*cur, result, data->env)) return ; } else if ((*cur)->content.redir_type == INPUT_FILE) - ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[0], INPUT_FILE, format_string(str, env, 0), env))); + ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[0], + INPUT_FILE, format_string(str, data->env, 0), data->env))); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_OVERRIDE, format_string(str, env, 0), env))); + ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1], + OUTPUT_OVERRIDE, format_string(str, data->env, 0), + data->env))); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - ft_lstadd_back(create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_APPEND, format_string(str, env, 0), env))); + ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1], + OUTPUT_APPEND, format_string(str, data->env, 0), + data->env))); next_token = (*cur)->next; - // free_token_and_connect(*cur); + free_token_and_connect(*cur); if (next_token) { - free_token_and_connect(*cur); + if (next_token->previous == NULL) + *tokens = next_token->next; + // free_token_and_connect(*cur); *cur = next_token->next; free_token_and_connect(next_token); } @@ -101,8 +109,6 @@ static t_redirection *set_redir(t_redirection *redir, int type, char *spec, return (NULL); } - - static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env) { char *heredoc_data; diff --git a/src/parse_cmd.c b/src/parse_cmd.c index d13bf3f..5502f74 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 12:44:44 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:30:06 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,10 +35,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env) static char **collect_args(t_token **tokens, t_env **env) { - t_token *cur; - t_token *next;//2 - char **result; - int i; + t_token *cur; + t_token *next; // 2 + char **result; + int i; cur = *tokens; i = 0; @@ -51,14 +51,14 @@ static char **collect_args(t_token **tokens, t_env **env) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { - next = cur->next;//2 + next = cur->next; // 2 if (cur->previous) free_token(cur->previous); result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; // cur = cur->next; - cur = next;//2 + cur = next; // 2 } result[i] = NULL; return (result); -} \ No newline at end of file +} -- cgit v1.2.3 From 634a3b84b31cbcc5c0c665338e1ef1eb49f25e5d Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 20 Jan 2025 17:34:02 +0100 Subject: fixed a bunch of stuff --- .vscode/c_cpp_properties.json | 18 ++++++++++ .vscode/launch.json | 13 +++++++ .vscode/settings.json | 64 +++++++++++++++++++++++++++++++++ .vscode/tasks.json | 29 +++++++++++++++ include/env.h | 5 +-- include/minishell.h | 4 +-- lib/libft/_obj/ft_atoi.o | Bin 932 -> 0 bytes lib/libft/_obj/ft_atol.o | Bin 948 -> 0 bytes lib/libft/_obj/ft_bzero.o | Bin 676 -> 0 bytes lib/libft/_obj/ft_calloc.o | Bin 752 -> 0 bytes lib/libft/_obj/ft_isalnum.o | Bin 704 -> 0 bytes lib/libft/_obj/ft_isalpha.o | Bin 680 -> 0 bytes lib/libft/_obj/ft_isascii.o | Bin 664 -> 0 bytes lib/libft/_obj/ft_isdigit.o | Bin 664 -> 0 bytes lib/libft/_obj/ft_isprint.o | Bin 664 -> 0 bytes lib/libft/_obj/ft_isspace.o | Bin 672 -> 0 bytes lib/libft/_obj/ft_itoa.o | Bin 1236 -> 0 bytes lib/libft/_obj/ft_lstadd_back_bonus.o | Bin 716 -> 0 bytes lib/libft/_obj/ft_lstadd_front_bonus.o | Bin 652 -> 0 bytes lib/libft/_obj/ft_lstclear_bonus.o | Bin 780 -> 0 bytes lib/libft/_obj/ft_lstdelone_bonus.o | Bin 688 -> 0 bytes lib/libft/_obj/ft_lstiter_bonus.o | Bin 712 -> 0 bytes lib/libft/_obj/ft_lstlast_bonus.o | Bin 696 -> 0 bytes lib/libft/_obj/ft_lstmap_bonus.o | Bin 932 -> 0 bytes lib/libft/_obj/ft_lstnew_bonus.o | Bin 732 -> 0 bytes lib/libft/_obj/ft_lstsize_bonus.o | Bin 704 -> 0 bytes lib/libft/_obj/ft_memchr.o | Bin 724 -> 0 bytes lib/libft/_obj/ft_memcmp.o | Bin 740 -> 0 bytes lib/libft/_obj/ft_memcpy.o | Bin 764 -> 0 bytes lib/libft/_obj/ft_memmove.o | Bin 824 -> 0 bytes lib/libft/_obj/ft_memset.o | Bin 684 -> 0 bytes lib/libft/_obj/ft_printaddr.o | Bin 1128 -> 0 bytes lib/libft/_obj/ft_printf.o | Bin 2996 -> 0 bytes lib/libft/_obj/ft_printhex.o | Bin 1012 -> 0 bytes lib/libft/_obj/ft_printnbr.o | Bin 1704 -> 0 bytes lib/libft/_obj/ft_putchar_fd.o | Bin 680 -> 0 bytes lib/libft/_obj/ft_putendl_fd.o | Bin 860 -> 0 bytes lib/libft/_obj/ft_putnbr_fd.o | Bin 1088 -> 0 bytes lib/libft/_obj/ft_putstr_fd.o | Bin 740 -> 0 bytes lib/libft/_obj/ft_split.o | Bin 1936 -> 0 bytes lib/libft/_obj/ft_strcat.o | Bin 756 -> 0 bytes lib/libft/_obj/ft_strchr.o | Bin 756 -> 0 bytes lib/libft/_obj/ft_strcmp.o | Bin 748 -> 0 bytes lib/libft/_obj/ft_strcpy.o | Bin 700 -> 0 bytes lib/libft/_obj/ft_strdup.o | Bin 816 -> 0 bytes lib/libft/_obj/ft_striteri.o | Bin 696 -> 0 bytes lib/libft/_obj/ft_strjoin.o | Bin 1044 -> 0 bytes lib/libft/_obj/ft_strlcat.o | Bin 896 -> 0 bytes lib/libft/_obj/ft_strlcpy.o | Bin 872 -> 0 bytes lib/libft/_obj/ft_strlen.o | Bin 660 -> 0 bytes lib/libft/_obj/ft_strmapi.o | Bin 864 -> 0 bytes lib/libft/_obj/ft_strncmp.o | Bin 824 -> 0 bytes lib/libft/_obj/ft_strncpy.o | Bin 728 -> 0 bytes lib/libft/_obj/ft_strnstr.o | Bin 984 -> 0 bytes lib/libft/_obj/ft_strrchr.o | Bin 744 -> 0 bytes lib/libft/_obj/ft_strtrim.o | Bin 1232 -> 0 bytes lib/libft/_obj/ft_substr.o | Bin 912 -> 0 bytes lib/libft/_obj/ft_tolower.o | Bin 664 -> 0 bytes lib/libft/_obj/ft_toupper.o | Bin 664 -> 0 bytes lib/libft/_obj/get_next_line.o | Bin 1860 -> 0 bytes lib/libft/_obj/get_next_line_utils.o | Bin 1384 -> 0 bytes lib/libft/libft.a | Bin 55752 -> 0 bytes src/builtins_part_one.c | 17 ++++++--- src/builtins_part_three.c | 13 +++++-- src/env.c | 4 +-- src/execute_cmd.c | 4 +-- src/handle_redir.c | 8 ++--- 67 files changed, 161 insertions(+), 18 deletions(-) create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 .vscode/tasks.json delete mode 100644 lib/libft/_obj/ft_atoi.o delete mode 100644 lib/libft/_obj/ft_atol.o delete mode 100644 lib/libft/_obj/ft_bzero.o delete mode 100644 lib/libft/_obj/ft_calloc.o delete mode 100644 lib/libft/_obj/ft_isalnum.o delete mode 100644 lib/libft/_obj/ft_isalpha.o delete mode 100644 lib/libft/_obj/ft_isascii.o delete mode 100644 lib/libft/_obj/ft_isdigit.o delete mode 100644 lib/libft/_obj/ft_isprint.o delete mode 100644 lib/libft/_obj/ft_isspace.o delete mode 100644 lib/libft/_obj/ft_itoa.o delete mode 100644 lib/libft/_obj/ft_lstadd_back_bonus.o delete mode 100644 lib/libft/_obj/ft_lstadd_front_bonus.o delete mode 100644 lib/libft/_obj/ft_lstclear_bonus.o delete mode 100644 lib/libft/_obj/ft_lstdelone_bonus.o delete mode 100644 lib/libft/_obj/ft_lstiter_bonus.o delete mode 100644 lib/libft/_obj/ft_lstlast_bonus.o delete mode 100644 lib/libft/_obj/ft_lstmap_bonus.o delete mode 100644 lib/libft/_obj/ft_lstnew_bonus.o delete mode 100644 lib/libft/_obj/ft_lstsize_bonus.o delete mode 100644 lib/libft/_obj/ft_memchr.o delete mode 100644 lib/libft/_obj/ft_memcmp.o delete mode 100644 lib/libft/_obj/ft_memcpy.o delete mode 100644 lib/libft/_obj/ft_memmove.o delete mode 100644 lib/libft/_obj/ft_memset.o delete mode 100644 lib/libft/_obj/ft_printaddr.o delete mode 100644 lib/libft/_obj/ft_printf.o delete mode 100644 lib/libft/_obj/ft_printhex.o delete mode 100644 lib/libft/_obj/ft_printnbr.o delete mode 100644 lib/libft/_obj/ft_putchar_fd.o delete mode 100644 lib/libft/_obj/ft_putendl_fd.o delete mode 100644 lib/libft/_obj/ft_putnbr_fd.o delete mode 100644 lib/libft/_obj/ft_putstr_fd.o delete mode 100644 lib/libft/_obj/ft_split.o delete mode 100644 lib/libft/_obj/ft_strcat.o delete mode 100644 lib/libft/_obj/ft_strchr.o delete mode 100644 lib/libft/_obj/ft_strcmp.o delete mode 100644 lib/libft/_obj/ft_strcpy.o delete mode 100644 lib/libft/_obj/ft_strdup.o delete mode 100644 lib/libft/_obj/ft_striteri.o delete mode 100644 lib/libft/_obj/ft_strjoin.o delete mode 100644 lib/libft/_obj/ft_strlcat.o delete mode 100644 lib/libft/_obj/ft_strlcpy.o delete mode 100644 lib/libft/_obj/ft_strlen.o delete mode 100644 lib/libft/_obj/ft_strmapi.o delete mode 100644 lib/libft/_obj/ft_strncmp.o delete mode 100644 lib/libft/_obj/ft_strncpy.o delete mode 100644 lib/libft/_obj/ft_strnstr.o delete mode 100644 lib/libft/_obj/ft_strrchr.o delete mode 100644 lib/libft/_obj/ft_strtrim.o delete mode 100644 lib/libft/_obj/ft_substr.o delete mode 100644 lib/libft/_obj/ft_tolower.o delete mode 100644 lib/libft/_obj/ft_toupper.o delete mode 100644 lib/libft/_obj/get_next_line.o delete mode 100644 lib/libft/_obj/get_next_line_utils.o delete mode 100644 lib/libft/libft.a (limited to 'include') diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..94b2ae4 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "macos-clang-x64", + "includePath": [ + "${workspaceFolder}/**" + ], + "compilerPath": "/usr/bin/clang", + "cStandard": "${default}", + "cppStandard": "${default}", + "intelliSenseMode": "macos-clang-x64", + "compilerArgs": [ + "" + ] + } + ], + "version": 4 + } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..86fa44a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,13 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "C/C++ Runner: Debug Session", + "type": "lldb", + "request": "launch", + "args": [], + "cwd": "/Users/chuhlig/Desktop/merged_minishell/", + "program": "/Users/chuhlig/Desktop/merged_minishell/minishell" + } + ] + } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..e43ecab --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,64 @@ +{ + "C_Cpp_Runner.cCompilerPath": "clang", + "C_Cpp_Runner.cppCompilerPath": "clang++", + "C_Cpp_Runner.debuggerPath": "lldb", + "C_Cpp_Runner.cStandard": "", + "C_Cpp_Runner.cppStandard": "", + "C_Cpp_Runner.msvcBatchPath": "", + "C_Cpp_Runner.useMsvc": false, + "C_Cpp_Runner.warnings": [ + "-Wall", + "-Wextra", + "-Wpedantic", + "-Wshadow", + "-Wformat=2", + "-Wcast-align", + "-Wconversion", + "-Wsign-conversion", + "-Wnull-dereference" + ], + "C_Cpp_Runner.msvcWarnings": [ + "/W4", + "/permissive-", + "/w14242", + "/w14287", + "/w14296", + "/w14311", + "/w14826", + "/w44062", + "/w44242", + "/w14905", + "/w14906", + "/w14263", + "/w44265", + "/w14928" + ], + "C_Cpp_Runner.enableWarnings": true, + "C_Cpp_Runner.warningsAsError": false, + "C_Cpp_Runner.compilerArgs": [], + "C_Cpp_Runner.linkerArgs": [], + "C_Cpp_Runner.includePaths": [], + "C_Cpp_Runner.includeSearch": [ + "*", + "**/*" + ], + "C_Cpp_Runner.excludeSearch": [ + "**/build", + "**/build/**", + "**/.*", + "**/.*/**", + "**/.vscode", + "**/.vscode/**" + ], + "C_Cpp_Runner.useAddressSanitizer": false, + "C_Cpp_Runner.useUndefinedSanitizer": false, + "C_Cpp_Runner.useLeakSanitizer": false, + "C_Cpp_Runner.showCompilationTime": false, + "C_Cpp_Runner.useLinkTimeOptimization": false, + "C_Cpp_Runner.msvcSecureNoWarnings": false, + "files.associations": { + "minishell.h": "c", + "debug_tools.h": "c", + "token.h": "c" + } + } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..4ede37a --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,29 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: clang build active file", + "command": "/usr/bin/clang", + "args": [ + "-fcolor-diagnostics", + "-fansi-escape-codes", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/include/env.h b/include/env.h index 3fc83b7..56b50b5 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: 2025/01/18 18:43:07 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 16:48:57 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -30,7 +30,7 @@ char **env_to_strlst(t_env *env); void update_oldpwd(t_env **env); void update_pwd(t_env **env); int unset(char **av, t_env **env); -int export(char **av, t_env **env); +int export(char **av, t_env **env, int f); int echo(char **av); int pwd(t_env *env); int cd(t_env **env, char **args); @@ -38,5 +38,6 @@ int ft_env(t_env *env); int builtin_exit(char **args, t_env **env); t_env *env_new(char *name); t_env *check_existing(t_env *env, char *av); +int check_flag(int f); #endif \ No newline at end of file diff --git a/include/minishell.h b/include/minishell.h index 7a573e8..03553a7 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/18 19:30:43 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 16:48:39 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env, int is_literal); -int set_return_code(int return_code, t_env **env); +void set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); diff --git a/lib/libft/_obj/ft_atoi.o b/lib/libft/_obj/ft_atoi.o deleted file mode 100644 index efcdc0d..0000000 Binary files a/lib/libft/_obj/ft_atoi.o and /dev/null differ diff --git a/lib/libft/_obj/ft_atol.o b/lib/libft/_obj/ft_atol.o deleted file mode 100644 index 3126061..0000000 Binary files a/lib/libft/_obj/ft_atol.o and /dev/null differ diff --git a/lib/libft/_obj/ft_bzero.o b/lib/libft/_obj/ft_bzero.o deleted file mode 100644 index 64ddf3a..0000000 Binary files a/lib/libft/_obj/ft_bzero.o and /dev/null differ diff --git a/lib/libft/_obj/ft_calloc.o b/lib/libft/_obj/ft_calloc.o deleted file mode 100644 index 39c2c05..0000000 Binary files a/lib/libft/_obj/ft_calloc.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isalnum.o b/lib/libft/_obj/ft_isalnum.o deleted file mode 100644 index 3aecc58..0000000 Binary files a/lib/libft/_obj/ft_isalnum.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isalpha.o b/lib/libft/_obj/ft_isalpha.o deleted file mode 100644 index da20b01..0000000 Binary files a/lib/libft/_obj/ft_isalpha.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isascii.o b/lib/libft/_obj/ft_isascii.o deleted file mode 100644 index 2262942..0000000 Binary files a/lib/libft/_obj/ft_isascii.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isdigit.o b/lib/libft/_obj/ft_isdigit.o deleted file mode 100644 index a0e0f40..0000000 Binary files a/lib/libft/_obj/ft_isdigit.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isprint.o b/lib/libft/_obj/ft_isprint.o deleted file mode 100644 index 8cf5033..0000000 Binary files a/lib/libft/_obj/ft_isprint.o and /dev/null differ diff --git a/lib/libft/_obj/ft_isspace.o b/lib/libft/_obj/ft_isspace.o deleted file mode 100644 index f6f10e8..0000000 Binary files a/lib/libft/_obj/ft_isspace.o and /dev/null differ diff --git a/lib/libft/_obj/ft_itoa.o b/lib/libft/_obj/ft_itoa.o deleted file mode 100644 index 447812f..0000000 Binary files a/lib/libft/_obj/ft_itoa.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstadd_back_bonus.o b/lib/libft/_obj/ft_lstadd_back_bonus.o deleted file mode 100644 index 566f7fc..0000000 Binary files a/lib/libft/_obj/ft_lstadd_back_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstadd_front_bonus.o b/lib/libft/_obj/ft_lstadd_front_bonus.o deleted file mode 100644 index f456510..0000000 Binary files a/lib/libft/_obj/ft_lstadd_front_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstclear_bonus.o b/lib/libft/_obj/ft_lstclear_bonus.o deleted file mode 100644 index cbf23d7..0000000 Binary files a/lib/libft/_obj/ft_lstclear_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstdelone_bonus.o b/lib/libft/_obj/ft_lstdelone_bonus.o deleted file mode 100644 index c357699..0000000 Binary files a/lib/libft/_obj/ft_lstdelone_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstiter_bonus.o b/lib/libft/_obj/ft_lstiter_bonus.o deleted file mode 100644 index 139d20d..0000000 Binary files a/lib/libft/_obj/ft_lstiter_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstlast_bonus.o b/lib/libft/_obj/ft_lstlast_bonus.o deleted file mode 100644 index 0f262f1..0000000 Binary files a/lib/libft/_obj/ft_lstlast_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstmap_bonus.o b/lib/libft/_obj/ft_lstmap_bonus.o deleted file mode 100644 index d4ed0b6..0000000 Binary files a/lib/libft/_obj/ft_lstmap_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstnew_bonus.o b/lib/libft/_obj/ft_lstnew_bonus.o deleted file mode 100644 index 98420b2..0000000 Binary files a/lib/libft/_obj/ft_lstnew_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_lstsize_bonus.o b/lib/libft/_obj/ft_lstsize_bonus.o deleted file mode 100644 index 1ce926d..0000000 Binary files a/lib/libft/_obj/ft_lstsize_bonus.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memchr.o b/lib/libft/_obj/ft_memchr.o deleted file mode 100644 index 23efe3d..0000000 Binary files a/lib/libft/_obj/ft_memchr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memcmp.o b/lib/libft/_obj/ft_memcmp.o deleted file mode 100644 index ca68348..0000000 Binary files a/lib/libft/_obj/ft_memcmp.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memcpy.o b/lib/libft/_obj/ft_memcpy.o deleted file mode 100644 index 253e17e..0000000 Binary files a/lib/libft/_obj/ft_memcpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memmove.o b/lib/libft/_obj/ft_memmove.o deleted file mode 100644 index df18bef..0000000 Binary files a/lib/libft/_obj/ft_memmove.o and /dev/null differ diff --git a/lib/libft/_obj/ft_memset.o b/lib/libft/_obj/ft_memset.o deleted file mode 100644 index be0b314..0000000 Binary files a/lib/libft/_obj/ft_memset.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printaddr.o b/lib/libft/_obj/ft_printaddr.o deleted file mode 100644 index 64e9dea..0000000 Binary files a/lib/libft/_obj/ft_printaddr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printf.o b/lib/libft/_obj/ft_printf.o deleted file mode 100644 index 44aedad..0000000 Binary files a/lib/libft/_obj/ft_printf.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printhex.o b/lib/libft/_obj/ft_printhex.o deleted file mode 100644 index 5ef814f..0000000 Binary files a/lib/libft/_obj/ft_printhex.o and /dev/null differ diff --git a/lib/libft/_obj/ft_printnbr.o b/lib/libft/_obj/ft_printnbr.o deleted file mode 100644 index 278895f..0000000 Binary files a/lib/libft/_obj/ft_printnbr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putchar_fd.o b/lib/libft/_obj/ft_putchar_fd.o deleted file mode 100644 index e3a7ad2..0000000 Binary files a/lib/libft/_obj/ft_putchar_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putendl_fd.o b/lib/libft/_obj/ft_putendl_fd.o deleted file mode 100644 index 5ff74c8..0000000 Binary files a/lib/libft/_obj/ft_putendl_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putnbr_fd.o b/lib/libft/_obj/ft_putnbr_fd.o deleted file mode 100644 index 1de6482..0000000 Binary files a/lib/libft/_obj/ft_putnbr_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_putstr_fd.o b/lib/libft/_obj/ft_putstr_fd.o deleted file mode 100644 index bc970e3..0000000 Binary files a/lib/libft/_obj/ft_putstr_fd.o and /dev/null differ diff --git a/lib/libft/_obj/ft_split.o b/lib/libft/_obj/ft_split.o deleted file mode 100644 index d5df61b..0000000 Binary files a/lib/libft/_obj/ft_split.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strcat.o b/lib/libft/_obj/ft_strcat.o deleted file mode 100644 index f749d90..0000000 Binary files a/lib/libft/_obj/ft_strcat.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strchr.o b/lib/libft/_obj/ft_strchr.o deleted file mode 100644 index fea0b41..0000000 Binary files a/lib/libft/_obj/ft_strchr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strcmp.o b/lib/libft/_obj/ft_strcmp.o deleted file mode 100644 index 5c7286b..0000000 Binary files a/lib/libft/_obj/ft_strcmp.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strcpy.o b/lib/libft/_obj/ft_strcpy.o deleted file mode 100644 index d556c8f..0000000 Binary files a/lib/libft/_obj/ft_strcpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strdup.o b/lib/libft/_obj/ft_strdup.o deleted file mode 100644 index e052b40..0000000 Binary files a/lib/libft/_obj/ft_strdup.o and /dev/null differ diff --git a/lib/libft/_obj/ft_striteri.o b/lib/libft/_obj/ft_striteri.o deleted file mode 100644 index e8264a9..0000000 Binary files a/lib/libft/_obj/ft_striteri.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strjoin.o b/lib/libft/_obj/ft_strjoin.o deleted file mode 100644 index bb8a89e..0000000 Binary files a/lib/libft/_obj/ft_strjoin.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strlcat.o b/lib/libft/_obj/ft_strlcat.o deleted file mode 100644 index e7aff81..0000000 Binary files a/lib/libft/_obj/ft_strlcat.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strlcpy.o b/lib/libft/_obj/ft_strlcpy.o deleted file mode 100644 index 695a120..0000000 Binary files a/lib/libft/_obj/ft_strlcpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strlen.o b/lib/libft/_obj/ft_strlen.o deleted file mode 100644 index 2c66545..0000000 Binary files a/lib/libft/_obj/ft_strlen.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strmapi.o b/lib/libft/_obj/ft_strmapi.o deleted file mode 100644 index 345a34a..0000000 Binary files a/lib/libft/_obj/ft_strmapi.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strncmp.o b/lib/libft/_obj/ft_strncmp.o deleted file mode 100644 index 683c861..0000000 Binary files a/lib/libft/_obj/ft_strncmp.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strncpy.o b/lib/libft/_obj/ft_strncpy.o deleted file mode 100644 index 5585d29..0000000 Binary files a/lib/libft/_obj/ft_strncpy.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strnstr.o b/lib/libft/_obj/ft_strnstr.o deleted file mode 100644 index 4f42fd4..0000000 Binary files a/lib/libft/_obj/ft_strnstr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strrchr.o b/lib/libft/_obj/ft_strrchr.o deleted file mode 100644 index c6b2a16..0000000 Binary files a/lib/libft/_obj/ft_strrchr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_strtrim.o b/lib/libft/_obj/ft_strtrim.o deleted file mode 100644 index b4ac15c..0000000 Binary files a/lib/libft/_obj/ft_strtrim.o and /dev/null differ diff --git a/lib/libft/_obj/ft_substr.o b/lib/libft/_obj/ft_substr.o deleted file mode 100644 index eebbca7..0000000 Binary files a/lib/libft/_obj/ft_substr.o and /dev/null differ diff --git a/lib/libft/_obj/ft_tolower.o b/lib/libft/_obj/ft_tolower.o deleted file mode 100644 index 940856f..0000000 Binary files a/lib/libft/_obj/ft_tolower.o and /dev/null differ diff --git a/lib/libft/_obj/ft_toupper.o b/lib/libft/_obj/ft_toupper.o deleted file mode 100644 index 137ae03..0000000 Binary files a/lib/libft/_obj/ft_toupper.o and /dev/null differ diff --git a/lib/libft/_obj/get_next_line.o b/lib/libft/_obj/get_next_line.o deleted file mode 100644 index d5cbdc5..0000000 Binary files a/lib/libft/_obj/get_next_line.o and /dev/null differ diff --git a/lib/libft/_obj/get_next_line_utils.o b/lib/libft/_obj/get_next_line_utils.o deleted file mode 100644 index 0eecbe7..0000000 Binary files a/lib/libft/_obj/get_next_line_utils.o and /dev/null differ diff --git a/lib/libft/libft.a b/lib/libft/libft.a deleted file mode 100644 index 1a43a20..0000000 Binary files a/lib/libft/libft.a and /dev/null differ diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 7cbcdeb..629cc87 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/18 18:33:33 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:05:19 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,15 @@ int unset(char **av, t_env **env) prev = NULL; while (current) { - if (ft_strcmp(current->name, av[i]) == 0) + if ((!ft_strcmp(current->name, av[i])) && (!ft_strcmp("?", av[1]))) + { + if (prev) + prev->next = current->next; + else + *env = current->next; + free_env_node(current); + break ; + } { if (prev) prev->next = current->next; @@ -91,7 +99,7 @@ int is_valid_identifier(char *str) return (1); } -int export(char **av, t_env **env) +int export(char **av, t_env **env, int f) { char *equal_sign; int i; @@ -107,6 +115,7 @@ int export(char **av, t_env **env) write(1, "Minishell $ export: not a valid identifier\n", 43); if (equal_sign) *equal_sign = '='; + f++; continue ; } if (equal_sign) @@ -115,5 +124,5 @@ int export(char **av, t_env **env) export_export(av[i], env); } } - return (0); + return (check_flag(f)); } diff --git a/src/builtins_part_three.c b/src/builtins_part_three.c index 7c73e95..5f6fa31 100644 --- a/src/builtins_part_three.c +++ b/src/builtins_part_three.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/18 18:29:24 by chuhlig #+# #+# */ -/* Updated: 2025/01/18 18:34:29 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:08:17 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,8 +22,10 @@ int builtin_exit(char **av, t_env **env) { int exit_status; - if (av[1]) + if (av[1] && !av[2]) exit_status = ft_atoi(av[1]); + else if (av[2]) + exit_status = 1; else exit_status = 0; exit_shell(env, exit_status); @@ -74,3 +76,10 @@ int echo(char **av) write(1, "\n", 1); return (0); } + +int check_flag(int f) +{ + if (f) + return (1); + return (0); +} diff --git a/src/env.c b/src/env.c index 1972909..cc9cfae 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: 2025/01/14 14:44:34 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:05:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,7 +55,7 @@ char *env_get(t_env *env, char *name) { while (env != NULL) { - if (!ft_strncmp(env->name, name, ft_strlen(name))) + if (!ft_strncmp(env->name, name, ft_strlen(env->name))) return (env->value); env = env->next; } diff --git a/src/execute_cmd.c b/src/execute_cmd.c index e2b9d66..4b84e12 100644 --- a/src/execute_cmd.c +++ b/src/execute_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:21:35 by chuhlig #+# #+# */ -/* Updated: 2025/01/19 19:15:46 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 15:43:41 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,7 +28,7 @@ int is_builtin(char *cmd) int execute_builtin(char **args, t_env **env) { if (ft_strcmp(args[0], "export") == 0) - return (export(args, env)); + return (export(args, env, ft_atoi("0"))); else if (ft_strcmp(args[0], "unset") == 0) return (unset(args, env)); else if (ft_strcmp(args[0], "cd") == 0) diff --git a/src/handle_redir.c b/src/handle_redir.c index 29bba92..e8a1010 100644 --- a/src/handle_redir.c +++ b/src/handle_redir.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/18 18:34:51 by chuhlig #+# #+# */ -/* Updated: 2025/01/18 18:47:31 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 14:59:38 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ int handle_input_redirection(t_redirection *redir) } else if (redir->type == INPUT_LIMITER) { - fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open_file("/tmp/heredoc_tmp", O_WRONLY | O_TRUNC, 0644); if (fd < 0) return (-1); write(fd, redir->specifier, ft_strlen(redir->specifier)); @@ -46,7 +46,7 @@ int handle_output_redirection(t_redirection *redir) if (redir->type == OUTPUT_OVERRIDE) { - fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); + fd = open_file(redir->specifier, O_WRONLY | O_TRUNC, 0644); if (fd < 0) return (-1); dup2(fd, STDOUT_FILENO); @@ -54,7 +54,7 @@ int handle_output_redirection(t_redirection *redir) } else if (redir->type == OUTPUT_APPEND) { - fd = open_file(redir->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); + fd = open_file(redir->specifier, O_WRONLY | O_APPEND, 0644); if (fd < 0) return (-1); dup2(fd, STDOUT_FILENO); -- cgit v1.2.3 From 4e249399780b53dad6fa1bf6db8a7c6d32ee2d13 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 20 Jan 2025 18:41:55 +0100 Subject: I love the norme sooooooooooo much!!!! --- Makefile | 3 ++- include/debug_tools.h | 8 ++----- include/minishell.h | 21 +++++++++-------- src/collect_redirs.c | 30 +++++++------------------ src/create_files.c | 61 +++++++++++++++++++++++++++++--------------------- src/error.c | 9 +++++++- src/get_cmd_path.c | 6 ++--- src/new_node.c | 7 +++--- src/parse_cmd.c | 15 ++++++------- src/parser.c | 8 +++---- src/praise_the_norme.c | 30 +++++++++++++++++++++++++ src/repl.c | 6 ++--- 12 files changed, 117 insertions(+), 87 deletions(-) create mode 100644 src/praise_the_norme.c (limited to 'include') diff --git a/Makefile b/Makefile index 9cfa670..b3a18f3 100644 --- a/Makefile +++ b/Makefile @@ -20,7 +20,8 @@ SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ parse_cmd.c collect_redirs.c print_ast.c interpreter.c env.c \ get_cmd_path.c env_to_strlst.c execute_cmd.c format_string.c \ builtins_part_one.c builtins_part_two.c env_tools.c error.c \ - read_heredoc.c create_files.c builtins_part_three.c handle_redir.c + read_heredoc.c create_files.c builtins_part_three.c handle_redir.c \ + praise_the_norme.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/debug_tools.h b/include/debug_tools.h index 6c4bc29..a014ff1 100644 --- a/include/debug_tools.h +++ b/include/debug_tools.h @@ -6,16 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 18:34:37 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 21:08:15 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:56:01 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DEBUG_TOOLS_H # define DEBUG_TOOLS_H -# include -# include "debug_tools.h" - # include "libft.h" +# include # ifndef DEBUG # define DEBUG 0 @@ -25,8 +23,6 @@ void dbg(char *str); void panic(char *msg); - void dbg2(const char *format, ...); - #endif diff --git a/include/minishell.h b/include/minishell.h index ea9f687..ba2a6ad 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:18:48 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:41:11 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,6 @@ # include # include # include -# include int init(void); int init_signal_handling(void); @@ -46,20 +45,24 @@ int set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); +void command_not_found_error(char *cmd); char *read_heredoc(char *delimiter); int handle_input_redirection(t_redirection *redir); int handle_output_redirection(t_redirection *redir); int handle_redirections(t_redirection *redirs); int handle_pipe_parent(int p[2], t_node *node, t_env **env); -int handle_pipe_child(int p[2], t_node *node, - t_env **env, int in_fd); +int handle_pipe_child(int p[2], t_node *node, t_env **env, + int in_fd); int open_file(char *path, int flags, int mode); int eval_rec(t_node *node, t_env **env, int in_fd); -int create_files(t_list *files); +int create_files(t_list *files); +void q4fc(t_list **queue, t_redirection *redir); +void i_love_the_norme(t_token **cur, t_token **tokens); -typedef struct s_minidata { - t_env *env; - t_list **create_files; -} t_minidata; +typedef struct s_minidata +{ + t_env *env; + t_list **create_files; +} t_minidata; #endif diff --git a/src/collect_redirs.c b/src/collect_redirs.c index e5bc8f6..f274053 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:30:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:39:24 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -52,7 +52,6 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env, static void collect_and_check_redir(t_redirection *result, t_token **cur, t_minidata *data, t_token **tokens) { - t_token *next_token; char *str; if ((*cur)->content.redir_type != INPUT_LIMITER) @@ -63,28 +62,15 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur, return ; } else if ((*cur)->content.redir_type == INPUT_FILE) - ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[0], - INPUT_FILE, format_string(str, data->env, 0), data->env))); + q4fc(data->create_files, set_redir(&result[0], INPUT_FILE, + format_string(str, data->env, 0), data->env)); else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE) - ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_OVERRIDE, format_string(str, data->env, 0), - data->env))); + q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE, + format_string(str, data->env, 0), data->env)); else if ((*cur)->content.redir_type == OUTPUT_APPEND) - ft_lstadd_back(data->create_files, ft_lstnew(set_redir(&result[1], - OUTPUT_APPEND, format_string(str, data->env, 0), - data->env))); - next_token = (*cur)->next; - free_token_and_connect(*cur); - if (next_token) - { - if (next_token->previous == NULL) - *tokens = next_token->next; - // free_token_and_connect(*cur); - *cur = next_token->next; - free_token_and_connect(next_token); - } - else - *cur = NULL; + q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND, + format_string(str, data->env, 0), data->env)); + i_love_the_norme(cur, tokens); } static t_redirection *set_redir(t_redirection *redir, int type, char *spec, diff --git a/src/create_files.c b/src/create_files.c index 0550e57..eee2860 100644 --- a/src/create_files.c +++ b/src/create_files.c @@ -6,45 +6,56 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/16 16:23:51 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 15:54:00 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:30:40 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include #include +static int cant_write(char *filename); +static void create_file(char *filename, int mode); + int create_files(t_list *files) { t_redirection *file; - int fd; while (files) { - if (files->content == NULL) - { - files = files->next; - continue ; - } - file = (t_redirection *)files->content; - if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1 || access(file->specifier, R_OK) == -1)) - return (EXIT_FAILURE); - if (access(file->specifier, F_OK) != -1 && access(file->specifier, W_OK) == -1) - break ; - if (file->type == OUTPUT_OVERRIDE) - { - fd = open(file->specifier, O_WRONLY | O_CREAT | O_TRUNC, 0644); - close(fd); - } - else if (file->type == OUTPUT_APPEND) + if (files->content != NULL) { - fd = open(file->specifier, O_WRONLY | O_CREAT | O_APPEND, 0644); - close(fd); + file = (t_redirection *)files->content; + if (file->type == INPUT_FILE && (access(file->specifier, F_OK) == -1 + || access(file->specifier, R_OK) == -1)) + return (EXIT_FAILURE); + if (cant_write(file->specifier)) + break ; + if (file->type == OUTPUT_OVERRIDE) + create_file(file->specifier, O_TRUNC); + else if (file->type == OUTPUT_APPEND) + create_file(file->specifier, O_APPEND); + if (files->next == NULL) + break ; + if (((t_redirection *)files->next->content)->type == 0) + break ; } - if (files->next == NULL) - break ; - if (((t_redirection *) files->next->content)->type == 0) - break ; files = files->next; } - return (EXIT_SUCCESS); + return (EXIT_SUCCESS); +} + +static int cant_write(char *filename) +{ + return (access(filename, F_OK) != -1 && access(filename, W_OK) == -1); +} + +static void create_file(char *filename, int mode) +{ + close(open(filename, O_WRONLY | O_CREAT | mode, 0644)); +} + +void q4fc(t_list **queue, t_redirection *redir) +{ + ft_lstadd_back(queue, ft_lstnew(redir)); } diff --git a/src/error.c b/src/error.c index 628200d..2ca60b2 100644 --- a/src/error.c +++ b/src/error.c @@ -6,7 +6,7 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:19:59 by chuhlig #+# #+# */ -/* Updated: 2025/01/20 13:18:45 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 18:12:33 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -85,9 +85,7 @@ static char *find_in_path(char *cmd, t_env *env, int *return_code) path++; } *return_code = 127; - ft_printf("%s:", cmd); - ft_putstr_fd(" command not found", 2); - ft_printf("\n"); + command_not_found_error(cmd); return (NULL); } diff --git a/src/new_node.c b/src/new_node.c index bbac154..b2ab7ea 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 19:01:01 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:59:01 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -37,7 +37,8 @@ t_node *new_pipe_node(t_node *left, t_node *right) return (node); } -t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) +t_node *new_cmd_node(char **args, t_redirection redirs[2], + t_list *create_files) { t_node *node; @@ -51,7 +52,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2], t_list *create_files) node->content.cmd.redirs[1] = redirs[1]; node->content.cmd.create_files = create_files; free(redirs); - redirs = NULL;//1 + redirs = NULL; return (node); } return (NULL); diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 5502f74..b30d126 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:30:06 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 17:57:50 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -35,10 +35,10 @@ t_node *parse_cmd(t_token *tokens, t_env **env) static char **collect_args(t_token **tokens, t_env **env) { - t_token *cur; - t_token *next; // 2 - char **result; - int i; + t_token *cur; + char **result; + int i; + t_token *next; cur = *tokens; i = 0; @@ -51,13 +51,12 @@ static char **collect_args(t_token **tokens, t_env **env) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { - next = cur->next; // 2 + next = cur->next; if (cur->previous) free_token(cur->previous); result[i] = format_string(cur->content.string, *env, ft_atoi("0")); i++; - // cur = cur->next; - cur = next; // 2 + cur = next; } result[i] = NULL; return (result); diff --git a/src/parser.c b/src/parser.c index aef8d70..75f1c64 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,14 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ -/* Updated: 2025/01/19 18:59:00 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:57:20 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ +#include "env.h" #include "libft.h" #include "minishell.h" #include "token.h" -#include "env.h" static t_token *find_token_by_type(t_token *tokens, int type); t_token *split_at_first(t_token **tokens, int type); @@ -40,7 +40,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env) if (left_side_tokens == NULL) { free_tokens(tokens); - tokens = NULL;//1 + tokens = NULL; return (NULL); } else if (tokens != NULL) @@ -71,7 +71,7 @@ t_token *split_at_first(t_token **tokens, int type) if (result == split) result = NULL; free_token(split); - split = NULL;//1 + split = NULL; return (result); } diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c new file mode 100644 index 0000000..a22843b --- /dev/null +++ b/src/praise_the_norme.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* praise_the_norme.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next; + free_token_and_connect(*cur); + if (next_token) + { + if (next_token->previous == NULL) + *tokens = next_token->next; + *cur = next_token->next; + free_token_and_connect(next_token); + } + else + *cur = NULL; +} diff --git a/src/repl.c b/src/repl.c index 15b0a80..16c8e95 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 12:45:00 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 17:58:43 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,7 +23,7 @@ void repl(const char *prompt, t_env **env, int *promptflag) while (1) { input = readline(prompt); - if (input == NULL) + if (input == NULL) { if (*promptflag > 1) (*promptflag)--; @@ -41,5 +41,3 @@ void repl(const char *prompt, t_env **env, int *promptflag) free(input); } } - -//echo hi | >./outfiles/outfile01 echo bye >./test_files/invalid_permission \ No newline at end of file -- cgit v1.2.3 From 852e00cb465b05f13aa6298ce71a1067c0846556 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Mon, 20 Jan 2025 19:32:10 +0100 Subject: fixed norm tester 133 remove .vs --- .vscode/c_cpp_properties.json | 18 ------------ .vscode/launch.json | 13 --------- .vscode/settings.json | 64 ------------------------------------------- .vscode/tasks.json | 29 -------------------- include/minishell.h | 4 +-- include/token.h | 6 ++-- src/builtins_part_one.c | 12 ++------ src/builtins_part_two.c | 4 +-- src/env.c | 4 +-- src/interpreter.c | 3 +- src/new_token.c | 27 +----------------- src/parse_cmd.c | 2 +- src/parser.c | 4 +-- 13 files changed, 16 insertions(+), 174 deletions(-) delete mode 100644 .vscode/c_cpp_properties.json delete mode 100644 .vscode/launch.json delete mode 100644 .vscode/settings.json delete mode 100644 .vscode/tasks.json (limited to 'include') diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json deleted file mode 100644 index 94b2ae4..0000000 --- a/.vscode/c_cpp_properties.json +++ /dev/null @@ -1,18 +0,0 @@ -{ - "configurations": [ - { - "name": "macos-clang-x64", - "includePath": [ - "${workspaceFolder}/**" - ], - "compilerPath": "/usr/bin/clang", - "cStandard": "${default}", - "cppStandard": "${default}", - "intelliSenseMode": "macos-clang-x64", - "compilerArgs": [ - "" - ] - } - ], - "version": 4 - } \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json deleted file mode 100644 index 86fa44a..0000000 --- a/.vscode/launch.json +++ /dev/null @@ -1,13 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - { - "name": "C/C++ Runner: Debug Session", - "type": "lldb", - "request": "launch", - "args": [], - "cwd": "/Users/chuhlig/Desktop/merged_minishell/", - "program": "/Users/chuhlig/Desktop/merged_minishell/minishell" - } - ] - } \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index e43ecab..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,64 +0,0 @@ -{ - "C_Cpp_Runner.cCompilerPath": "clang", - "C_Cpp_Runner.cppCompilerPath": "clang++", - "C_Cpp_Runner.debuggerPath": "lldb", - "C_Cpp_Runner.cStandard": "", - "C_Cpp_Runner.cppStandard": "", - "C_Cpp_Runner.msvcBatchPath": "", - "C_Cpp_Runner.useMsvc": false, - "C_Cpp_Runner.warnings": [ - "-Wall", - "-Wextra", - "-Wpedantic", - "-Wshadow", - "-Wformat=2", - "-Wcast-align", - "-Wconversion", - "-Wsign-conversion", - "-Wnull-dereference" - ], - "C_Cpp_Runner.msvcWarnings": [ - "/W4", - "/permissive-", - "/w14242", - "/w14287", - "/w14296", - "/w14311", - "/w14826", - "/w44062", - "/w44242", - "/w14905", - "/w14906", - "/w14263", - "/w44265", - "/w14928" - ], - "C_Cpp_Runner.enableWarnings": true, - "C_Cpp_Runner.warningsAsError": false, - "C_Cpp_Runner.compilerArgs": [], - "C_Cpp_Runner.linkerArgs": [], - "C_Cpp_Runner.includePaths": [], - "C_Cpp_Runner.includeSearch": [ - "*", - "**/*" - ], - "C_Cpp_Runner.excludeSearch": [ - "**/build", - "**/build/**", - "**/.*", - "**/.*/**", - "**/.vscode", - "**/.vscode/**" - ], - "C_Cpp_Runner.useAddressSanitizer": false, - "C_Cpp_Runner.useUndefinedSanitizer": false, - "C_Cpp_Runner.useLeakSanitizer": false, - "C_Cpp_Runner.showCompilationTime": false, - "C_Cpp_Runner.useLinkTimeOptimization": false, - "C_Cpp_Runner.msvcSecureNoWarnings": false, - "files.associations": { - "minishell.h": "c", - "debug_tools.h": "c", - "token.h": "c" - } - } \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json deleted file mode 100644 index 4ede37a..0000000 --- a/.vscode/tasks.json +++ /dev/null @@ -1,29 +0,0 @@ -{ - "tasks": [ - { - "type": "cppbuild", - "label": "C/C++: clang build active file", - "command": "/usr/bin/clang", - "args": [ - "-fcolor-diagnostics", - "-fansi-escape-codes", - "-g", - "${file}", - "-o", - "${fileDirname}/${fileBasenameNoExtension}" - ], - "options": { - "cwd": "${fileDirname}" - }, - "problemMatcher": [ - "$gcc" - ], - "group": { - "kind": "build", - "isDefault": true - }, - "detail": "Task generated by Debugger." - } - ], - "version": "2.0.0" -} \ No newline at end of file diff --git a/include/minishell.h b/include/minishell.h index e9810bd..356df7a 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 18:41:11 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 19:12:03 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -42,7 +42,7 @@ int eval(t_node *node, t_env **env); char *get_cmd_path(char *cmd, t_env *env, int *return_code); int execute_cmd(t_cmd *cmd, t_env **env); char *format_string(char *str, t_env *env, int is_literal); -void set_return_code(int return_code, t_env **env); +void set_return_code(int return_code, t_env **env); int handle_redirections(t_redirection *redirs); void *error(int err_code, char *err_text, int exit_code, int *ret_code); diff --git a/include/token.h b/include/token.h index bb7ea94..2e8da35 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 12:37:59 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:10:30 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -43,9 +43,9 @@ t_token *new_str_token(char *str, t_token *previous, t_token *next); t_token *new_redir_token(int type, t_token *previous, t_token *next); - +void free_tokens(t_token *tokens); void free_token(t_token *token); - +void free_token_and_connect(t_token *token); void tokenizer(char *s, t_token **token_list, char quote_check); void print_token(t_token *token); diff --git a/src/builtins_part_one.c b/src/builtins_part_one.c index 629cc87..11989cc 100644 --- a/src/builtins_part_one.c +++ b/src/builtins_part_one.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/09 17:01:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/20 17:05:19 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:07:18 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,15 +26,7 @@ int unset(char **av, t_env **env) prev = NULL; while (current) { - if ((!ft_strcmp(current->name, av[i])) && (!ft_strcmp("?", av[1]))) - { - if (prev) - prev->next = current->next; - else - *env = current->next; - free_env_node(current); - break ; - } + if (ft_strcmp(current->name, av[i]) == 0) { if (prev) prev->next = current->next; diff --git a/src/builtins_part_two.c b/src/builtins_part_two.c index d3c0929..e461861 100644 --- a/src/builtins_part_two.c +++ b/src/builtins_part_two.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/10/25 20:52:16 by chuhlig #+# #+# */ -/* Updated: 2025/01/18 18:57:12 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:12:33 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -59,7 +59,7 @@ void update_pwd(t_env **env) int cd(t_env **env, char **av) { t_env *current; - + current = *env; if (av[1] == NULL) { diff --git a/src/env.c b/src/env.c index 63a0f8a..572040c 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: 2025/01/20 15:05:49 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:12:43 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -78,4 +78,4 @@ void free_env_node(t_env *node) free(node->name); free(node->value); free(node); -} \ No newline at end of file +} diff --git a/src/interpreter.c b/src/interpreter.c index 6d71022..44f4b95 100644 --- a/src/interpreter.c +++ b/src/interpreter.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/12/17 19:15:49 by chuhlig #+# #+# */ -/* Updated: 2025/01/20 12:48:49 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:12:49 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -53,4 +53,3 @@ int eval(t_node *node, t_env **env) { return (eval_rec(node, env, STDIN_FILENO)); } - diff --git a/src/new_token.c b/src/new_token.c index 6431c7d..bd65b9d 100644 --- a/src/new_token.c +++ b/src/new_token.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:29:44 by dkaiser #+# #+# */ -/* Updated: 2024/08/29 15:30:52 by chuhlig ### ########.fr */ +/* Updated: 2025/01/20 19:12:57 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -50,28 +50,3 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next) token->content.redir_type = type; return (token); } - -// void ft_append_token(int type, t_token **list)// but we need somewhere token/node head initialize with nul -// { -// t_token *node; -// t_token *last_node; - -// if (!list) -// return ; -// node = malloc(sizeof(t_token)); -// if (!node) -// return ; -// node->next = NULL; -// node->type = type; -// if (!*list) -// { -// *list = node; -// node->previous = NULL; -// } -// else -// { -// last_node = ft_lstlast(*list); -// last_node->next = node; -// node->previous = last_node; -// } -// } \ No newline at end of file diff --git a/src/parse_cmd.c b/src/parse_cmd.c index b30d126..6505384 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:57:50 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 19:09:21 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/parser.c b/src/parser.c index 75f1c64..13ab10d 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,14 +6,14 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ -/* Updated: 2025/01/20 17:57:20 by dkaiser ### ########.fr */ +/* Updated: 2025/01/20 19:13:31 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ -#include "env.h" #include "libft.h" #include "minishell.h" #include "token.h" +#include "env.h" static t_token *find_token_by_type(t_token *tokens, int type); t_token *split_at_first(t_token **tokens, int type); -- cgit v1.2.3