Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/builtins/builtins_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 6 additions & 1 deletion src/builtins/exit.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,19 @@ 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;

printf("exit\n");
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);
Expand Down
4 changes: 2 additions & 2 deletions src/executor/exec_pipe_2.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
19 changes: 9 additions & 10 deletions src/executor/executor.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/lexer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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, "&&");
Expand Down
2 changes: 2 additions & 0 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/types/envar.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down