|
@ -6,7 +6,7 @@ |
|
|
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ |
|
|
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ |
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ |
|
|
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ |
|
|
/* Updated: 2022/05/05 16:28:15 by mea ### ########.fr */ |
|
|
/* Updated: 2022/05/05 20:53:48 by narnaud ### ########.fr */ |
|
|
/* */ |
|
|
/* */ |
|
|
/* ************************************************************************** */ |
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
|
@ -24,21 +24,10 @@ void handle_status(t_datas *datas, int status) |
|
|
datas->exit_code = WEXITSTATUS(status); |
|
|
datas->exit_code = WEXITSTATUS(status); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
int caller(t_datas *datas, t_command *cmd) |
|
|
pid_t caller(t_datas *datas, t_command *cmd) |
|
|
{ |
|
|
{ |
|
|
pid_t pid; |
|
|
pid_t pid; |
|
|
int pip[2]; |
|
|
|
|
|
int status; |
|
|
|
|
|
|
|
|
|
|
|
if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
pipe(pip); |
|
|
|
|
|
cmd->fd[1] = pip[1]; |
|
|
|
|
|
cmd->next->fd[0] = pip[0]; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (!cmd->prev && !cmd->next && command_call(datas, cmd) != -1)//check_builtin(cmd->argv[0]))
|
|
|
|
|
|
return (1); |
|
|
|
|
|
pid = fork(); |
|
|
pid = fork(); |
|
|
if (!pid) |
|
|
if (!pid) |
|
|
{ |
|
|
{ |
|
@ -46,14 +35,30 @@ int caller(t_datas *datas, t_command *cmd) |
|
|
dup2(cmd->fd[0], STDIN_FILENO); |
|
|
dup2(cmd->fd[0], STDIN_FILENO); |
|
|
if (cmd->fd[1]) |
|
|
if (cmd->fd[1]) |
|
|
dup2(cmd->fd[1], STDOUT_FILENO); |
|
|
dup2(cmd->fd[1], STDOUT_FILENO); |
|
|
if (command_call(datas, cmd) == -1) |
|
|
if (builtin_call(datas, cmd) == -1) |
|
|
exe(datas, cmd); |
|
|
exe(datas, cmd); |
|
|
exit(EXIT_SUCCESS); |
|
|
exit(EXIT_SUCCESS); |
|
|
} |
|
|
} |
|
|
|
|
|
return pid; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
int piper(t_datas *datas, t_command *cmd) |
|
|
|
|
|
{ |
|
|
|
|
|
int pip[2]; |
|
|
|
|
|
int status; |
|
|
|
|
|
pid_t pid; |
|
|
|
|
|
|
|
|
|
|
|
if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0) |
|
|
|
|
|
{ |
|
|
|
|
|
pipe(pip); |
|
|
|
|
|
cmd->fd[1] = pip[1]; |
|
|
|
|
|
cmd->next->fd[0] = pip[0]; |
|
|
|
|
|
} |
|
|
|
|
|
pid = caller(datas, cmd); |
|
|
if (cmd->fd[1]) |
|
|
if (cmd->fd[1]) |
|
|
close(cmd->fd[1]); |
|
|
close(cmd->fd[1]); |
|
|
if (cmd->next) |
|
|
if (cmd->next) |
|
|
caller(datas, cmd->next); |
|
|
piper(datas, cmd->next); |
|
|
waitpid(pid, &status, 0); |
|
|
waitpid(pid, &status, 0); |
|
|
handle_status(datas, status); |
|
|
handle_status(datas, status); |
|
|
if (cmd->fd[0]) |
|
|
if (cmd->fd[0]) |
|
@ -122,19 +127,15 @@ t_token *lexer(t_datas *datas, char *line) |
|
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp) |
|
|
int main(int argc, char **argv, char **envp) |
|
|
{ |
|
|
{ |
|
|
t_datas datas; |
|
|
t_command *cmd; |
|
|
char *line; |
|
|
t_datas datas; |
|
|
char **env_copy; |
|
|
char *line; |
|
|
int i; |
|
|
int i; |
|
|
|
|
|
|
|
|
env_copy = ft_calloc(1024, sizeof(char *)); |
|
|
|
|
|
i = 0; |
|
|
i = 0; |
|
|
while (envp[i]) |
|
|
datas.envp = ft_calloc(ENVP_LIMIT, sizeof(char *)); |
|
|
{ |
|
|
while (*envp) |
|
|
env_copy[i] = ft_strdup(envp[i]); |
|
|
datas.envp[i++] = ft_strdup(*(envp++)); |
|
|
i++; |
|
|
|
|
|
} |
|
|
|
|
|
datas.envp = env_copy; |
|
|
|
|
|
datas.exit_code = 0; |
|
|
datas.exit_code = 0; |
|
|
if (argc < 2) |
|
|
if (argc < 2) |
|
|
(void)argv; |
|
|
(void)argv; |
|
@ -147,7 +148,11 @@ int main(int argc, char **argv, char **envp) |
|
|
halt(EXIT_FAILURE); |
|
|
halt(EXIT_FAILURE); |
|
|
if (is_empty(line)) |
|
|
if (is_empty(line)) |
|
|
continue ; |
|
|
continue ; |
|
|
caller(&datas, parser(&datas, lexer(&datas, line), NULL)); |
|
|
cmd = parser(&datas, lexer(&datas, line), NULL); |
|
|
|
|
|
if (!cmd->prev && !cmd->next && is_builtin(cmd->argv[0])) |
|
|
|
|
|
builtin_call(&datas, cmd); |
|
|
|
|
|
else |
|
|
|
|
|
piper(&datas, cmd); |
|
|
add_history(line); |
|
|
add_history(line); |
|
|
} |
|
|
} |
|
|
clear_history(); |
|
|
clear_history(); |
|
|