Expand filenames in redirections
authorLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 14:42:42 +0000 (16:42 +0200)
committerLukáš Jiřiště <jiriste@icpf.cas.cz>
Tue, 27 Aug 2024 14:42:42 +0000 (16:42 +0200)
src/execution.c

index ab8e4da3d9c3d661bb3b0ac45a0d24d7e0c53bd5..b8db20e2b4649a522354b1863d0bd73203f4b1e2 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/27 15:29:43 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/27 16:32:09 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -110,7 +110,25 @@ char       *get_word(const t_parse_tree_node *parent)
        return (res);
 }
 
-int    add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd)
+int    expand_filename(char **filename, const t_execution_env *env)
+{
+       t_vec   advanced_copy;
+
+       if (ft_vec_init(&advanced_copy, sizeof(char)) != success)
+               return (1);
+       if (add_word(&advanced_copy, *filename, env, 0))
+       {
+               ft_vec_free(&advanced_copy, NULL);
+               return (1);
+       }
+       free(*filename);
+       *filename = advanced_copy.vec;
+       filename[0][advanced_copy.size - 1] = '\0';
+       unquote_field(*filename);
+       return (0);
+}
+
+int    add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd, const t_execution_env *env)
 {
        t_redirection                   redir;
        const t_parse_tree_node *operator;
@@ -120,6 +138,7 @@ int add_redirection_file(t_vec *redirections, t_parse_tree_node *io_file, int fd
        filename = get_word(ft_vec_caccess(&io_file->children, 1));
        if (!filename)
                return (1);
+       expand_filename(&filename, env);
        operator = ft_vec_caccess(&io_file->children, 0);
        if (is_token_type(operator, "LESS"))
        {
@@ -287,7 +306,7 @@ int add_redirection(t_vec *redirections, t_parse_tree_node *redirect, const t_ex
                subnode = ft_vec_access(&redirect->children, 1);
        }
        if (is_token_type(subnode, "io_file"))
-               return (add_redirection_file(redirections, subnode, fd));
+               return (add_redirection_file(redirections, subnode, fd, env));
        else
                return (add_redirection_here(redirections, subnode, fd, env));
 }