l'execution marche (un petit peu) ( je crois)
This commit is contained in:
99
srcs/exec_bonus.c
Normal file → Executable file
99
srcs/exec_bonus.c
Normal file → Executable file
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
|
||||
/* Updated: 2024/04/03 00:15:39 by tomoron ### ########.fr */
|
||||
/* Updated: 2024/04/03 17:46:14 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -22,7 +22,7 @@ void get_redirections(t_msh *msh, t_cmd *cmds)
|
||||
}
|
||||
else
|
||||
{
|
||||
get_out_type(msh,cmds);
|
||||
get_out_type(msh, cmds);
|
||||
if (!g_return_code)
|
||||
get_in_type(msh, cmds);
|
||||
}
|
||||
@ -33,28 +33,32 @@ void exec_command_bonus(t_msh *msh, char *cmd_str)
|
||||
t_cmd *cmds;
|
||||
t_cmd *tmp;
|
||||
|
||||
//printf("cmd : %s\n",cmd_str);
|
||||
cmds = parsing_bonus(cmd_str);
|
||||
print_parsed_cmd(cmds); // debug
|
||||
tmp = check_cmds_syntax(cmds);
|
||||
if(tmp)
|
||||
if (tmp)
|
||||
{
|
||||
print_syntax_error_bonus(tmp);
|
||||
printf("error\n");
|
||||
printf("error\n");//debug
|
||||
free_cmd(cmds);
|
||||
return;
|
||||
return ;
|
||||
}
|
||||
while (cmds)
|
||||
{
|
||||
if (cmds->cmd_type == CMD)
|
||||
if ((cmds->cmd_type == AND && !g_return_code) || (cmds->cmd_type == OR
|
||||
&& g_return_code) || !is_operand_type(cmds))
|
||||
{
|
||||
msh->cmds = parse_command(cmds->value, msh->env);
|
||||
if(is_operand_type(cmds))
|
||||
cmds = cmds->next;
|
||||
msh->tokens = parse_command(cmds->value, msh->env);
|
||||
msh->cmds = cmds;
|
||||
get_redirections(msh, cmds);
|
||||
print_msh_struct(msh);
|
||||
print_parsed_cmd(cmds);
|
||||
print_parsed_token(msh->cmds);
|
||||
// exec_commands(msh);
|
||||
print_msh_struct(msh); // debug
|
||||
print_parsed_token(msh->tokens); // debug
|
||||
exec_commands(msh);
|
||||
}
|
||||
cmds = cmds->next;
|
||||
while(cmds && (is_cmd_type(cmds) || cmds->cmd_type == PIPE))
|
||||
cmds = cmds->next;
|
||||
}
|
||||
}
|
||||
|
||||
@ -67,7 +71,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
||||
{
|
||||
if (pipe(fds) == -1)
|
||||
{
|
||||
perror("pipe");
|
||||
perror("minishell: pipe");
|
||||
ft_exit(msh, 1);
|
||||
}
|
||||
msh->in_fd = fds[0];
|
||||
@ -76,7 +80,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
||||
pid = fork();
|
||||
if (pid == -1)
|
||||
{
|
||||
perror("fork");
|
||||
perror("minishell: fork");
|
||||
ft_exit(msh, 1);
|
||||
}
|
||||
if (pid == 0)
|
||||
@ -93,7 +97,7 @@ int exec(t_msh *msh, char **cmd_args, int i, int cmd_count)
|
||||
void exec_command(t_msh *msh, int i, int cmd_count)
|
||||
{
|
||||
g_return_code = 0;
|
||||
if (!cmd_is_builtin(msh, msh->cmds->value))
|
||||
if (!cmd_is_builtin(msh, msh->tokens->value))
|
||||
get_cmd_path(msh);
|
||||
exec(msh, get_cmd_args(msh), i, cmd_count);
|
||||
remove_command_from_msh(msh);
|
||||
@ -110,11 +114,68 @@ void end_execution(t_msh *msh, int cmd_count)
|
||||
if (!g_return_code && WIFEXITED(status))
|
||||
g_return_code = WEXITSTATUS(status);
|
||||
if (WIFSIGNALED(status) && WTERMSIG(status) == SIGQUIT)
|
||||
printf("Quit\n");
|
||||
//TODO: (core dumped) WCOREDUMP
|
||||
printf("Quit\n");
|
||||
// TODO: (core dumped) WCOREDUMP
|
||||
free(msh->pids);
|
||||
msh->pids = 0;
|
||||
//signal(SIGINT, signal_handler_interactive); //enables ctrl-C
|
||||
// signal(SIGINT, signal_handler_interactive); //enables ctrl-C
|
||||
signal(SIGQUIT, signal_handler_interactive);
|
||||
set_echoctl(0);
|
||||
}
|
||||
|
||||
int get_cmd_count(t_cmd *cmds)
|
||||
{
|
||||
int nb;
|
||||
|
||||
nb = 0;
|
||||
while(cmds && !is_operand_type(cmds))
|
||||
{
|
||||
if(is_cmd_type(cmds))
|
||||
nb++;
|
||||
cmds = cmds->next;
|
||||
}
|
||||
return(nb);
|
||||
}
|
||||
|
||||
void exec_commands(t_msh *msh)
|
||||
{
|
||||
int cmd_count;
|
||||
int i;
|
||||
int status;
|
||||
|
||||
if (!msh->tokens)
|
||||
return ;
|
||||
cmd_count = get_cmd_count(msh->cmds);
|
||||
msh->fds = ft_calloc(cmd_count, sizeof(int **));
|
||||
msh->pids = ft_calloc(cmd_count, sizeof(int *));
|
||||
if (!msh->pids || !msh->fds)
|
||||
ft_exit(msh, 1);
|
||||
|
||||
i = 0;
|
||||
while (i < cmd_count)
|
||||
{
|
||||
exec_command(msh, i, cmd_count);
|
||||
i++;
|
||||
}
|
||||
i = 0;
|
||||
while (i < cmd_count)
|
||||
{
|
||||
waitpid(msh->pids[i], &status, 0);
|
||||
i++;
|
||||
}
|
||||
if (!g_return_code && WIFEXITED(status))
|
||||
g_return_code = WEXITSTATUS(status);
|
||||
i = 0;
|
||||
while (i < cmd_count)
|
||||
{
|
||||
free(msh->fds[i]);
|
||||
msh->fds[i] = 0;
|
||||
i++;
|
||||
}
|
||||
free(msh->fds);
|
||||
msh->fds = 0;
|
||||
free(msh->pids);
|
||||
msh->pids = 0;
|
||||
//signal(SIGINT, signal_handler_interactive);
|
||||
signal(SIGQUIT, signal_handler_interactive);
|
||||
}
|
||||
|
Reference in New Issue
Block a user