Refactor the subshell execution
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 10:58:56 +0000 (12:58 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 10:58:56 +0000 (12:58 +0200)
src/execution.c

index eb4876874cae0894239d6d20872477da75e9de64..05c6e310c296b28add48755e32eb0a1a8df2c46a 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/07/21 09:10:19 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/21 12:56:41 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -38,6 +38,19 @@ int  ex_simple_command(t_parse_tree_node *simple_command, t_execution_info *info)
        return (0);
 }
 
+int    subshell(t_parse_tree_node *program, const t_execution_info *info)
+{
+       int                                     res;
+       t_execution_info        info_copy;
+
+       if (copy_info(&info_copy, info))
+               return (1);
+       res = ex_program(program, &info_copy);
+       info->ret_val = info_copy->ret_val;
+       free_info(info_copy);
+       return (res);
+}
+
 int    ex_command(t_parse_tree_node *command, t_execution_info *info)
 {
        if (is_token_type(ft_vec_caccess(&command->children, 0), "simple_command"))
@@ -45,12 +58,7 @@ int  ex_command(t_parse_tree_node *command, t_execution_info *info)
        else
        {
                comp_command = ft_vec_access(&command->children, 0);
-               info_copy = copy_info(info);
-               if (!info_copy)
-                       return (1);
-               res = ex_program(ft_vec_access(&comp_command->children, 1), info_copy);
-               free_info(info_copy);
-               return (res);
+               return (subshell(ft_vec_access(&comp_command->children, 1), info));
        }
 }