diff --git a/README.md b/README.md index 7f958f2..14a93ca 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,13 @@ make test ### Valgrind ```bash -valgrind --suppressions=readline.supp --show-leak-kinds=all --leak-check=full ./minishell +valgrind \ + --suppressions=readline.supp \ + --show-leak-kinds=all \ + --leak-check=full \ + --track-fds=yes \ + --trace-children=yes \ + ./minishell ``` ## Resources diff --git a/src/builtins/builtins_utils.c b/src/builtins/builtins_utils.c index 4bd61b4..88e0cc2 100644 --- a/src/builtins/builtins_utils.c +++ b/src/builtins/builtins_utils.c @@ -14,18 +14,18 @@ void cd(char **options, t_env *env); void echo(char **options, t_env *env); -void exit_minishell(char **options, t_env *env); +void exit_minishell(char **options, int fds[2], t_env *env); void export(char **options, t_env *env); void pwd(char **options, t_env *env); void unset(char **options, t_env *env); void print_env(char **options, t_env *env); -void exec_builtins(char *cmd, char **options, t_env *env) +void exec_builtins(char *cmd, char **options, int fds[2], t_env *env) { if (ft_strcmp(cmd, "pwd") == 0) pwd(options, env); else if (ft_strcmp(cmd, "exit") == 0) - exit_minishell(options, env); + exit_minishell(options, fds, env); else if (ft_strcmp(cmd, "env") == 0) print_env(options, env); else if (ft_strcmp(cmd, "unset") == 0) diff --git a/src/builtins/exit.c b/src/builtins/exit.c index 46cf0c7..19784c2 100644 --- a/src/builtins/exit.c +++ b/src/builtins/exit.c @@ -45,7 +45,7 @@ static bool check_args(char **args, t_env *env) return (true); } -void exit_minishell(char **options, t_env *env) +void exit_minishell(char **options, int fds[2], t_env *env) { int exit_code; @@ -53,6 +53,11 @@ void exit_minishell(char **options, t_env *env) if (!check_args(options, env)) return ; exit_code = env->exit_code; + if (fds) + { + close(fds[0]); + close(fds[1]); + } rl_clear_history(); env->gc->clean(env->gc); exit(exit_code); diff --git a/src/executor/exec_pipe_2.c b/src/executor/exec_pipe_2.c index bed22df..5a70d0b 100644 --- a/src/executor/exec_pipe_2.c +++ b/src/executor/exec_pipe_2.c @@ -18,7 +18,7 @@ bool apply_redirs(t_cmd *cmd, t_env *env); void exec_subshell(t_btree *ast, t_env *env); -void exec_builtins(char *cmd, char **options, t_env *env); +void exec_builtins(char *cmd, char **options, int fds[2], t_env *env); bool strs_eq(void *s1, void *s2); static void flatten_recur(t_btree *ast, t_darray *nodes) @@ -85,7 +85,7 @@ static void exec_cmd_child(t_btree *node, t_env *env) if (cmd->argv->len == 0) env->exit_code = 0; else if (env->builtins->any(env->builtins, strs_eq, argv[0])) - exec_builtins(argv[0], argv, env); + exec_builtins(argv[0], argv, NULL, env); else env->exit_code = gc_execvp(argv[0], argv, (char **)env->export_envp(env), env->gc); diff --git a/src/executor/executor.c b/src/executor/executor.c index b21c93d..ecdffa1 100644 --- a/src/executor/executor.c +++ b/src/executor/executor.c @@ -21,27 +21,26 @@ int exec_pipe(t_btree *ast, t_env *env); bool apply_redirs(t_cmd *cmd, t_env *env); void execute(t_btree *ast, t_env *env); -void exec_builtins(char *cmd, char **options, t_env *env); +void exec_builtins(char *cmd, char **options, int fds[2], t_env *env); bool strs_eq(void *s1, void *s2); void exec_subshell(t_btree *ast, t_env *env); static void exec_builtin_cmd(t_cmd *cmd, t_env *env) { - int saved_stdin; - int saved_stdout; + int fds[2]; char **argv; - saved_stdin = dup(STDIN_FILENO); - saved_stdout = dup(STDOUT_FILENO); + fds[0] = dup(STDIN_FILENO); + fds[1] = dup(STDOUT_FILENO); if (apply_redirs(cmd, env)) { argv = (char **)cmd->argv->to_arr(cmd->argv); - exec_builtins(argv[0], argv, env); + exec_builtins(argv[0], argv, fds, env); } - dup2(saved_stdin, STDIN_FILENO); - dup2(saved_stdout, STDOUT_FILENO); - close(saved_stdin); - close(saved_stdout); + dup2(fds[0], STDIN_FILENO); + dup2(fds[1], STDOUT_FILENO); + close(fds[0]); + close(fds[1]); } static void exec_fork_child(t_cmd *cmd, t_env *env) diff --git a/src/lexer.c b/src/lexer.c index 3330834..81c6fe3 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -38,7 +38,7 @@ static size_t process(t_darray *tokens, char *cmd, int len, char *operator) t_token *token; token_value = gc_substr(cmd, 0, len, tokens->gc); - token_value = gc_strtrim(token_value, " ", tokens->gc); + token_value = gc_strtrim(token_value, " \t\n\v\f\r", tokens->gc); if (token_value[0]) { token = init_token(token_value, tokens->gc); @@ -56,7 +56,7 @@ static size_t process(t_darray *tokens, char *cmd, int len, char *operator) static size_t lookup(t_darray *tokens, char *cmd, size_t start, size_t i) { - if (cmd[i] == ' ') + if (ft_isspace(cmd[i])) start += process(tokens, &cmd[start], i - start, NULL); else if (ft_strncmp(&cmd[i], "&&", 2) == 0) start += process(tokens, &cmd[start], i - start, "&&"); diff --git a/src/main.c b/src/main.c index dedcba9..04fd7c0 100644 --- a/src/main.c +++ b/src/main.c @@ -33,6 +33,8 @@ bool loop(t_env *env) } if (!input) return (false); + if (!input[0]) + return (true); add_history(input); ast = parse(input, env); if (!ast) diff --git a/src/types/envar.c b/src/types/envar.c index ede6567..5ee2f99 100644 --- a/src/types/envar.c +++ b/src/types/envar.c @@ -19,6 +19,8 @@ static char *to_str(t_envar *self) { char *str; + if (!self->value) + return (self->key); str = gc_strjoin(self->key, "=", self->gc); return (gc_strjoin(str, self->value, self->gc)); }