forked from nicolas-arnaud/Minishell
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.
142 lines
3.1 KiB
142 lines
3.1 KiB
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* minishell.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: narnaud <narnaud@student.42.fr> +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2022/05/02 12:14:09 by narnaud #+# #+# */
|
|
/* Updated: 2022/05/03 16:25:14 by narnaud ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
# include "minishell.h"
|
|
|
|
// Note: Pipes are broken
|
|
int caller(t_command *cmd)
|
|
{
|
|
int cmd_ret;
|
|
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];
|
|
}
|
|
pid = fork();
|
|
if (!pid)
|
|
{
|
|
cmd_ret = 1; //command_call(command->argv);
|
|
if (cmd_ret == 1)
|
|
{
|
|
if (cmd->fd[0])
|
|
dup2(cmd->fd[0], STDIN_FILENO);
|
|
if (cmd->fd[1])
|
|
dup2(cmd->fd[1], STDOUT_FILENO);
|
|
exe(cmd->argv, cmd->envp);
|
|
exit(1);
|
|
}
|
|
}
|
|
if (cmd->fd[1])
|
|
close(cmd->fd[1]);
|
|
if (cmd->next)
|
|
return (caller(cmd->next));
|
|
waitpid(pid, &status, 0);
|
|
if (cmd->fd[0])
|
|
close(cmd->fd[0]);
|
|
|
|
return (cmd_ret);
|
|
}
|
|
|
|
t_command *parser(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->argc = count_arguments(tok);
|
|
cmd->argv = ft_calloc(cmd->argc + 1, sizeof(char *));
|
|
while(tok && tok->type != PIPE)
|
|
{
|
|
if (tok->type)
|
|
update_redir(cmd, tok);
|
|
else
|
|
cmd->argv[i++] = tok->value;
|
|
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]);
|
|
cmd->argv[i] = NULL;
|
|
if (tok && tok->type == PIPE)
|
|
cmd->next = parser(tok->next, cmd);
|
|
return (cmd);
|
|
}
|
|
|
|
t_token *lexer(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(&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 launcher(t_env env)
|
|
{
|
|
char *line;
|
|
|
|
(void)env;
|
|
// TODO: handle ret
|
|
line = readline("$ ");
|
|
if (line == NULL)
|
|
halt(EXIT_FAILURE);
|
|
if (is_empty(line))
|
|
return (EXIT_FAILURE);
|
|
caller(parser(lexer(line), NULL));
|
|
add_history(line);
|
|
return (0);
|
|
}
|
|
|
|
int main(int argc, char **argv, char**envp)
|
|
{
|
|
if (argc < 2)
|
|
(void)argv;
|
|
(void)envp;
|
|
t_env env;
|
|
env.null = 0;
|
|
while (1)
|
|
launcher(env);
|
|
clear_history();
|
|
return (EXIT_SUCCESS);
|
|
}
|
|
|