Implement expansion inside execution.c
authorLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 17:40:50 +0000 (19:40 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 17:46:13 +0000 (19:46 +0200)
Due to that the expansion.c file is removed.

Makefile
inc/minishell.h
src/execution.c
src/expansion.c [deleted file]

index 43bd56464c70afdf18820ee740713b90372ebac5..fc1e0a8c7dbb5b017da88d59b8cef7758744f17e 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -19,7 +19,6 @@ SOURCES :=    main.c                          \
                        input_handling.c        \
                        tokenization.c          \
                        parsing.c                       \
-                       expansion.c                     \
                        execution.c                     \
                        helpers.c                       \
 
index 0835940d9a5b12fb0dd922738aebac972bc0c351..d70886698b7eb2c4bd87626a77541c7f663f741c 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:22:57 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/07/21 17:19:11 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/21 17:46:51 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -47,7 +47,6 @@ void  handle_input(char **line, t_vars *vars);
 
 int            tokenize(char **line, t_vec *tokens);
 int            parse(t_vec *tokens, t_tree **parse_tree);
-int            expand(t_tree *parse_tree, t_vars *vars);
 int            execute(t_tree *parse_tree, t_vars *vars);
 
 int            init_tree(t_tree *tree);
index 4033c2353b27c89c4c5f1a337ac1eb7ffb8201ad..b730c6f7314a4366e1fede21e9629292e148fdad 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 18:29:22 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/21 19:45:29 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -360,6 +360,81 @@ int        save_redirections(t_vec *redirections, t_parse_tree_node *simple_command, co
        return (0);
 }
 
+int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env)
+{
+       size_t          i;
+       char            *var;
+       const char      *value;
+
+       while (word[i])
+       {
+               if (word[i] == '$')
+               {
+                       ++i;
+                       var = get_var_name(word + i);
+                       if (!var)
+                               return (1);
+                       value = get_env_var_value(env, var);
+                       i += ft_strlen(var);
+                       free(var);
+                       if (value)
+                               if (ft_vec_append_range(exp_str, value, ft_strlen(value)) != success)
+                                       return (1);
+               }
+               else
+                       if (ft_vec_append(exp_str, word + (i++)) != success)
+                               return (1);
+       }
+       return (0);
+}
+
+int    expand_cmd(t_vec *exp_str, const t_parse_tree_node *node, const t_execution_env *env)
+{
+       size_t                                  i;
+       const t_parse_tree_node *subnode;
+
+       i = 0;
+       while (i < node->children.size)
+       {
+               subnode = ft_vec_caccess(&node->children, i);
+               if (is_token_type(subnode, "cmd_suffix"))
+               {
+                       if (expand_cmd(exp_str, subnode, env))
+                               return (1);
+               }
+               else if (is_token_type(subnode, "WORD"))
+                       if (add_word(exp_str, subnode->token.str, env))
+                               return (1);
+               ++i;
+       }
+       return (0);
+}
+
+char   **expand(t_parse_tree_node *simple_command, const t_execution_env *env)
+{
+       size_t                          i;
+       t_vec                           expanded_str;
+       t_parse_tree_node       *subnode;
+
+       ft_vec_init(&expanded_str, sizeof(char));
+       ft_vec_reserve(&expanded_str, 64);
+       i = 0;
+       while (i < simple_command->children.size)
+       {
+               subnode = ft_vec_access(&simple_command->children, i);
+               if (is_token_type(subnode, "cmd_name")
+                               || is_token_type(subnode, "cmd_word")
+                               || is_token_type(subnode, "cmd_suffix"))
+                       if (expand_cmd(&expanded_str, subnode, env))
+                       {
+                               ft_vec_free(&expanded_str, NULL);
+                               return (NULL);
+                       }
+               ++i;
+       }
+       return (expanded_str.vec);
+}
+
 int    ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env)
 {
        t_vec   redirections;
@@ -370,7 +445,7 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env)
        res = save_redirections(&redirections, simple_command, env);
        res = res || save_assignments(&assignments, simple_command);
        if (!res)
-               fields = expand(simple_command, env, &redirections, &assignments);
+               fields = expand(simple_command, env);
        if (!fields || res)
        {
                ft_vec_free(&redirections, void_free_redirection);
@@ -379,7 +454,7 @@ int ex_simple_command(t_parse_tree_node *simple_command, t_execution_env *env)
        }
        if (!*fields)
        {
-               save_assignments(&assignments, env);
+               assignments_to_env(&assignments, env);
                ex_redirections(&redirections);
                ft_vec_free(&redirections, void_free_redirection);
                ft_vec_free(&assignments, void_free_assignments);
diff --git a/src/expansion.c b/src/expansion.c
deleted file mode 100644 (file)
index 697241e..0000000
+++ /dev/null
@@ -1,19 +0,0 @@
-/* ************************************************************************** */
-/*                                                                            */
-/*                                                        :::      ::::::::   */
-/*   expansion.c                                        :+:      :+:    :+:   */
-/*                                                    +:+ +:+         +:+     */
-/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
-/*                                                +#+#+#+#+#+   +#+           */
-/*   Created: 2024/06/21 16:29:48 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/21 16:31:57 by ljiriste         ###   ########.fr       */
-/*                                                                            */
-/* ************************************************************************** */
-
-#include "minishell.h"
-
-int    expand(__attribute__((unused))t_tree *parse_tree, __attribute__((unused))t_vars *vars)
-{
-       ft_printf("Variable expansion has yet to be implemented.\n");
-       return (0);
-}