/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/26 09:08:46 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 16:59:59 by ljiriste ### ########.fr */
+/* Updated: 2024/08/28 09:36:55 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int stdout_fd;
int ret_val;
int exit;
+ int subshell;
int last_was_builtin;
int last_builtin_ret_val;
t_vars *vars;
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 21:24:52 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 16:37:47 by ljiriste ### ########.fr */
+/* Updated: 2024/08/28 09:36:37 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
env->ret_val = 0;
env->exit = 0;
env->last_was_builtin = 0;
+ env->subshell = 0;
env->vars = malloc(sizeof(t_vars));
if (!env->vars)
return (1);
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 16:59:59 by ljiriste ### ########.fr */
+/* Updated: 2024/08/28 09:54:48 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (success);
}
-int copy_env(t_execution_env *copy, const t_execution_env *env)
-{
- *copy = *env;
- copy->vars = malloc(sizeof(t_vars));
- if (!copy->vars)
- return (1);
- ft_vec_init(©->vars->exported, sizeof(char *));
- ft_vec_init(©->vars->other, sizeof(char *));
- ft_vec_copy(©->vars->exported, &env->vars->exported, v_strcopy, free);
- ft_vec_copy(©->vars->other, &env->vars->other, v_strcopy, free);
- return (0);
-}
-
-void free_env(t_execution_env *env)
-{
- clean_vars(env->vars);
- free(env->vars);
- return ;
-}
-
int ex_program(t_parse_tree_node *program, t_execution_env *env);
int subshell(t_parse_tree_node *program, t_execution_env *env)
{
- int res;
- t_execution_env env_copy;
+ int res;
+ int status;
+ pid_t pid;
- if (copy_env(&env_copy, env))
+ pid = fork();
+ if (pid == 0)
+ {
+ env->subshell = 1;
+ res = ex_program(program, env);
+ env->exit = 1;
+ return (res);
+ }
+ else if (pid < 0)
return (1);
- res = ex_program(program, &env_copy);
- env->ret_val = env_copy.ret_val;
- free_env(&env_copy);
- return (res);
+ waitpid(pid, &status, 0);
+ if (!WIFEXITED(status))
+ return (1);
+ env->ret_val = WEXITSTATUS(status);
+ return (0);
}
int ex_command(t_parse_tree_node *command, t_execution_env *env)
{
t_parse_tree_node *comp_command;
+ if (env->exit)
+ return (0);
if (is_token_type(ft_vec_caccess(&command->children, 0), "simple_command"))
return (ex_simple_command(ft_vec_access(&command->children, 0), env));
else
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/26 13:11:47 by ljiriste #+# #+# */
-/* Updated: 2024/08/27 15:08:06 by ljiriste ### ########.fr */
+/* Updated: 2024/08/28 09:38:51 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
handle_input(&line, &env);
free(line);
}
+ if (!env.subshell)
+ ft_printf("exit\n");
res = env.ret_val;
clean_env(&env);
parse(NULL, NULL);
terminate_input();
- ft_printf("exit\n");
return (res);
}