/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 10:06:59 by ljiriste ### ########.fr */
+/* Updated: 2024/08/27 11:37:08 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
dup_pipes(env);
dup_redirections(redirections);
if (!ft_strcmp(fields[0], "cd"))
- env->ret_val = cd(count_fields(fields), fields, env);
+ env->last_builtin_ret_val = cd(count_fields(fields), fields, env);
else if (!ft_strcmp(fields[0], "echo"))
- env->ret_val = echo(count_fields(fields), fields);
+ env->last_builtin_ret_val = echo(count_fields(fields), fields);
else if (!ft_strcmp(fields[0], "pwd"))
- env->ret_val = pwd();
+ env->last_builtin_ret_val = pwd();
else if (!ft_strcmp(fields[0], "env"))
- env->ret_val = ft_env(count_fields(fields), env);
+ env->last_builtin_ret_val = ft_env(count_fields(fields), env);
else if (!ft_strcmp(fields[0], "export"))
- env->ret_val = export(count_fields(fields), fields, env);
+ env->last_builtin_ret_val = export(count_fields(fields), fields, env);
else if (!ft_strcmp(fields[0], "unset"))
- env->ret_val = unset(count_fields(fields), fields, env);
+ env->last_builtin_ret_val = unset(count_fields(fields), fields, env);
else
return (1);
+ env->last_was_builtin = 1;
close_redirections(redirections);
close_pipes(env);
restore_std_filenos(fds);
int ex_fields(char **fields, t_vec *assignments, const t_vec *redirections, t_execution_env *env)
{
pid_t pid;
- int status;
char *path;
if (is_builtin(fields[0]))
return (ex_builtin(fields, assignments, redirections, env));
+ env->last_was_builtin = 0;
path = NULL;
if (contains_path(fields[0]) && file_exists(fields[0]))
path = ft_strdup(fields[0]);
execve(path, fields, assignments->vec);
exit(-1);
}
- else if (pid > 0)
+ free(path);
+ if (pid > 0)
{
- free(path);
- waitpid(pid, &status, 0);
- if (WIFEXITED(status))
- env->ret_val = WEXITSTATUS(status);
+ if (ft_vec_append(&env->child_pids, &pid) != success)
+ return (1);
}
else
- {
- free(path);
return (1);
- }
return (0);
}
}
}
-int ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env)
+void wait_for_return(t_execution_env *env)
+{
+ pid_t last_pid;
+ int status;
+
+ if (env->last_was_builtin)
+ env->ret_val = env->last_builtin_ret_val;
+ else
+ {
+ if (env->child_pids.size == 0)
+ return ;
+ last_pid = *(pid_t *)ft_vec_access(&env->child_pids, env->child_pids.size - 1);
+ waitpid(last_pid, &status, 0);
+ if (WIFEXITED(status))
+ env->ret_val = WEXITSTATUS(status);
+ }
+ ft_vec_free(&env->child_pids, NULL);
+ return ;
+}
+
+int ex_pipeline(t_parse_tree_node *pipeline, t_execution_env *env, int depth)
{
int pipe_fds[2];
if (pipeline->children.size == 1)
- return (ex_command(ft_vec_access(&pipeline->children, 0), env));
+ {
+ if (ex_command(ft_vec_access(&pipeline->children, 0), env))
+ return (1);
+ }
else
{
if (pipe(pipe_fds))
return (1);
ft_swap_int(&pipe_fds[1], &env->stdout_fd);
- ex_pipeline(ft_vec_access(&pipeline->children, 0), env);
+ ex_pipeline(ft_vec_access(&pipeline->children, 0), env, depth + 1);
ft_swap_int(&pipe_fds[1], &env->stdout_fd);
close(pipe_fds[1]);
ft_swap_int(&pipe_fds[0], &env->stdin_fd);
ft_swap_int(&pipe_fds[0], &env->stdin_fd);
close(pipe_fds[0]);
}
+ if (depth == 0)
+ wait_for_return(env);
return (0);
}
int ex_program(t_parse_tree_node *program, t_execution_env *env)
{
if (program->children.size == 1)
- return (ex_pipeline(ft_vec_access(&program->children, 0), env));
+ return (ex_pipeline(ft_vec_access(&program->children, 0), env, 0));
if (ex_program(ft_vec_access(&program->children, 0), env))
return (1);
if (is_token_type(ft_vec_caccess(&program->children, 1), "AND_IF"))
{
if (env->ret_val == 0)
- return (ex_pipeline(ft_vec_access(&program->children, 2), env));
+ return (ex_pipeline(ft_vec_access(&program->children, 2), env, 0));
}
else
{
if (env->ret_val != 0)
- return (ex_pipeline(ft_vec_access(&program->children, 2), env));
+ return (ex_pipeline(ft_vec_access(&program->children, 2), env, 0));
}
return (0);
}