|
|
@ -6,13 +6,13 @@ |
|
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */ |
|
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
|
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ |
|
|
|
/* Updated: 2022/05/03 20:31:44 by narnaud ### ########.fr */ |
|
|
|
/* Updated: 2022/05/05 10:04:02 by narnaud ### ########.fr */ |
|
|
|
/* */ |
|
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
|
# include "minishell.h" |
|
|
|
|
|
|
|
int caller(t_command *cmd) |
|
|
|
int caller(t_datas *datas, t_command *cmd) |
|
|
|
{ |
|
|
|
pid_t pid; |
|
|
|
int pip[2]; |
|
|
@ -24,6 +24,8 @@ int caller(t_command *cmd) |
|
|
|
cmd->fd[1] = pip[1]; |
|
|
|
cmd->next->fd[0] = pip[0]; |
|
|
|
} |
|
|
|
if (!cmd->prev && !cmd->next && command_call(datas, cmd) != -1) |
|
|
|
return (1); |
|
|
|
pid = fork(); |
|
|
|
if (!pid) |
|
|
|
{ |
|
|
@ -31,25 +33,28 @@ int caller(t_command *cmd) |
|
|
|
dup2(cmd->fd[0], STDIN_FILENO); |
|
|
|
if (cmd->fd[1]) |
|
|
|
dup2(cmd->fd[1], STDOUT_FILENO); |
|
|
|
if (command_call(cmd) == -1) |
|
|
|
exe(cmd); |
|
|
|
exit(1); |
|
|
|
if (command_call(datas, cmd) == -1) |
|
|
|
exe(datas, cmd); |
|
|
|
exit(EXIT_SUCCESS); |
|
|
|
} |
|
|
|
if (cmd->fd[1]) |
|
|
|
close(cmd->fd[1]); |
|
|
|
if (cmd->next) |
|
|
|
return (caller(cmd->next)); |
|
|
|
return (caller(datas, cmd->next)); |
|
|
|
waitpid(pid, &status, 0); |
|
|
|
//if (WIFEXITED(status) && WEXITSTATUS(status))
|
|
|
|
// write(1, "\n", 1);
|
|
|
|
if (WIFSIGNALED(status)) |
|
|
|
printf("QUIT: %d\n", WTERMSIG(status)); |
|
|
|
{ |
|
|
|
datas->exit_code = WTERMSIG(status); |
|
|
|
if (datas->exit_code == 3) |
|
|
|
printf("Quit: 3\n"); |
|
|
|
} |
|
|
|
|
|
|
|
if (cmd->fd[0]) |
|
|
|
close(cmd->fd[0]); |
|
|
|
return (1); |
|
|
|
} |
|
|
|
|
|
|
|
t_command *parser(char ***envp, t_token *tok, t_command *prev) |
|
|
|
t_command *parser(t_token *tok, t_command *prev) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
t_command *cmd; |
|
|
@ -60,9 +65,7 @@ t_command *parser(char ***envp, t_token *tok, t_command *prev) |
|
|
|
if (prev) |
|
|
|
cmd->prev = prev; |
|
|
|
i = 0; |
|
|
|
cmd->argc = count_arguments(tok); |
|
|
|
cmd->argv = ft_calloc(cmd->argc + 1, sizeof(char *)); |
|
|
|
cmd->envp = envp; |
|
|
|
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); |
|
|
|
while(tok && tok->type != PIPE) |
|
|
|
{ |
|
|
|
if (tok->type) |
|
|
@ -72,12 +75,12 @@ t_command *parser(char ***envp, t_token *tok, t_command *prev) |
|
|
|
tok = tok->next; |
|
|
|
} |
|
|
|
if (DEBUG) |
|
|
|
printf("######\n->cmd : %s, argc : %d, in_fd : %d, out_fd : %d\n", \ |
|
|
|
cmd->argv[0], cmd->argc, cmd->fd[0], cmd->fd[1]); |
|
|
|
printf("######\n->cmd : %s, in_fd : %d, out_fd : %d\n", \ |
|
|
|
cmd->argv[0], cmd->fd[0], cmd->fd[1]); |
|
|
|
cmd->argv[i] = NULL; |
|
|
|
if (tok && tok->type == PIPE) |
|
|
|
cmd->next = parser(envp, tok->next, cmd); |
|
|
|
return (cmd); |
|
|
|
cmd->next = parser(tok->next, cmd); |
|
|
|
return(cmd); |
|
|
|
} |
|
|
|
|
|
|
|
t_token *lexer(char *line) |
|
|
@ -112,7 +115,11 @@ t_token *lexer(char *line) |
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp) |
|
|
|
{ |
|
|
|
t_datas datas; |
|
|
|
char *line; |
|
|
|
|
|
|
|
datas.envp = envp; |
|
|
|
datas.exit_code = 0; |
|
|
|
if (argc < 2) |
|
|
|
(void)argv; |
|
|
|
while (1) |
|
|
@ -124,7 +131,7 @@ int main(int argc, char **argv, char **envp) |
|
|
|
halt(EXIT_FAILURE); |
|
|
|
if (is_empty(line)) |
|
|
|
continue ; |
|
|
|
caller(parser(&envp, lexer(line), NULL)); |
|
|
|
caller(&datas, parser(lexer(line), NULL)); |
|
|
|
add_history(line); |
|
|
|
} |
|
|
|
clear_history(); |
|
|
|