/* 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 */
/* */
/* ************************************************************************** */
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);
/* 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 */
/* */
/* ************************************************************************** */
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;
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);
}
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);