You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.3 KiB

3 years ago
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* minishell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
/* Updated: 2022/05/05 11:10:24 by narnaud ### ########.fr */
3 years ago
/* */
/* ************************************************************************** */
# include "minishell.h"
int caller(t_datas *datas, t_command *cmd)
3 years ago
{
pid_t pid;
int pip[2];
int status;
3 years ago
if (cmd->fd[1] == 0 && cmd->next && cmd->next->fd[0] == 0)
3 years ago
{
pipe(pip);
cmd->fd[1] = pip[1];
cmd->next->fd[0] = pip[0];
}
if (!cmd->prev && !cmd->next && command_call(datas, cmd) != -1)
return (1);
3 years ago
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 (command_call(datas, cmd) == -1)
exe(datas, cmd);
exit(EXIT_SUCCESS);
3 years ago
}
if (cmd->fd[1])
close(cmd->fd[1]);
if (cmd->next)
return (caller(datas, cmd->next));
waitpid(pid, &status, 0);
if (WIFSIGNALED(status))
{
datas->exit_code = WTERMSIG(status);
if (datas->exit_code == 3)
printf("Quit: 3\n");
if (datas->exit_code == 9)
printf("Kill: 15\n");
if (datas->exit_code == 15)
printf("Terminated: 15\n");
}
3 years ago
if (cmd->fd[0])
close(cmd->fd[0]);
return (1);
3 years ago
}
t_command *parser(t_token *tok, t_command *prev)
3 years ago
{
int i;
t_command *cmd;
if (!tok)
return (NULL);
3 years ago
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)
3 years ago
{
if (tok->type)
update_redir(cmd, tok);
3 years ago
else
cmd->argv[i++] = tok->value;
tok = tok->next;
3 years ago
}
3 years ago
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(tok->next, cmd);
return(cmd);
3 years ago
}
t_token *lexer(char *line)
{
t_lexer *lex;
char *tmp;
int tmp_i;
lex = ft_calloc(1, sizeof *lex);
lex->state = ROOT_ST;
3 years ago
tmp = ft_calloc(1024, sizeof *tmp);
tmp_i = 0;
while (*line)
{
if (check_state(lex, &line))
3 years ago
continue;
if (lex->state != S_QUOTE_ST && *line == '$')
tmp_i = replace_var(&line, tmp, tmp_i);
if (check_register(lex, &line, tmp))
{
tmp_i = 0;
3 years ago
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)
3 years ago
{
t_datas datas;
char *line;
datas.envp = envp;
datas.exit_code = 0;
3 years ago
if (argc < 2)
(void)argv;
while (1)
{
termios(0);
line = readline("$ ");
termios(1);
if (line == NULL)
halt(EXIT_FAILURE);
if (is_empty(line))
continue ;
caller(&datas, parser(lexer(line), NULL));
add_history(line);
}
3 years ago
clear_history();
return (EXIT_SUCCESS);
}