|
|
@ -6,125 +6,11 @@ |
|
|
|
/* By: mea <marvin@42.fr> +#+ +:+ +#+ */ |
|
|
|
/* +#+#+#+#+#+ +#+ */ |
|
|
|
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */ |
|
|
|
/* Updated: 2022/05/05 20:53:48 by narnaud ### ########.fr */ |
|
|
|
/* Updated: 2022/05/06 11:23:19 by narnaud ### ########.fr */ |
|
|
|
/* */ |
|
|
|
/* ************************************************************************** */ |
|
|
|
|
|
|
|
# include "minishell.h" |
|
|
|
|
|
|
|
void handle_status(t_datas *datas, int status) |
|
|
|
{ |
|
|
|
if (WIFSIGNALED(status)) |
|
|
|
{ |
|
|
|
datas->exit_code = 128 + WTERMSIG(status); |
|
|
|
if (datas->exit_code == 131) |
|
|
|
printf("Quit: 3\n"); |
|
|
|
} |
|
|
|
else |
|
|
|
datas->exit_code = WEXITSTATUS(status); |
|
|
|
} |
|
|
|
|
|
|
|
pid_t caller(t_datas *datas, t_command *cmd) |
|
|
|
{ |
|
|
|
pid_t pid; |
|
|
|
|
|
|
|
pid = fork(); |
|
|
|
if (!pid) |
|
|
|
{ |
|
|
|
if (cmd->fd[0]) |
|
|
|
dup2(cmd->fd[0], STDIN_FILENO); |
|
|
|
if (cmd->fd[1]) |
|
|
|
dup2(cmd->fd[1], STDOUT_FILENO); |
|
|
|
if (builtin_call(datas, cmd) == -1) |
|
|
|
exe(datas, cmd); |
|
|
|
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]) |
|
|
|
close(cmd->fd[1]); |
|
|
|
if (cmd->next) |
|
|
|
piper(datas, cmd->next); |
|
|
|
waitpid(pid, &status, 0); |
|
|
|
handle_status(datas, status); |
|
|
|
if (cmd->fd[0]) |
|
|
|
close(cmd->fd[0]); |
|
|
|
return (1); |
|
|
|
} |
|
|
|
|
|
|
|
t_command *parser(t_datas *datas, t_token *tok, t_command *prev) |
|
|
|
{ |
|
|
|
int i; |
|
|
|
t_command *cmd; |
|
|
|
|
|
|
|
if (!tok) |
|
|
|
return (NULL); |
|
|
|
cmd = ft_calloc(1, sizeof(t_command)); |
|
|
|
if (prev) |
|
|
|
cmd->prev = prev; |
|
|
|
i = 0; |
|
|
|
cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); |
|
|
|
while(tok && tok->type != PIPE) |
|
|
|
{ |
|
|
|
if (tok->type) |
|
|
|
update_redir(datas, cmd, tok); |
|
|
|
else |
|
|
|
cmd->argv[i++] = tok->value; |
|
|
|
tok = tok->next; |
|
|
|
} |
|
|
|
if (DEBUG) |
|
|
|
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(datas, tok->next, cmd); |
|
|
|
return(cmd); |
|
|
|
} |
|
|
|
|
|
|
|
t_token *lexer(t_datas *datas, char *line) |
|
|
|
{ |
|
|
|
t_lexer *lex; |
|
|
|
char *tmp; |
|
|
|
int tmp_i; |
|
|
|
|
|
|
|
lex = ft_calloc(1, sizeof *lex); |
|
|
|
lex->state = ROOT_ST; |
|
|
|
tmp = ft_calloc(1024, sizeof *tmp); |
|
|
|
tmp_i = 0; |
|
|
|
while (*line) |
|
|
|
{ |
|
|
|
if (check_state(lex, &line)) |
|
|
|
continue; |
|
|
|
if (lex->state != S_QUOTE_ST && *line == '$') |
|
|
|
tmp_i = replace_var(datas, &line, tmp, tmp_i); |
|
|
|
if (check_register(lex, &line, tmp)) |
|
|
|
{ |
|
|
|
tmp_i = 0; |
|
|
|
ft_bzero(tmp, 1024); |
|
|
|
continue ; |
|
|
|
} |
|
|
|
tmp[tmp_i] = *line; |
|
|
|
line++; |
|
|
|
tmp_i++; |
|
|
|
} |
|
|
|
create_token(lex, tmp); |
|
|
|
return (lex->tokens); |
|
|
|
} |
|
|
|
|
|
|
|
int main(int argc, char **argv, char **envp) |
|
|
|
{ |
|
|
|
t_command *cmd; |
|
|
|