Fix showing single quotes upon expansion
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 23 Aug 2024 14:19:22 +0000 (16:19 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 23 Aug 2024 14:26:08 +0000 (16:26 +0200)
In order for the wildcard expanded words not to be expanded again as
variables, single quotes were used for variable expansion. This however
caused single quotes to show when variable was expanded inside double
quotes.
The solution is to only quote the variables in wildcard expansion.

inc/minishell.h
src/execution.c
src/wildcards.c

index 43f0a293c48eaa950bdcbc6d752ab73b0da73fdd..6f30ad7d414a5ba3f5649fa20adc30e8667f99d9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/05/02 13:22:57 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/22 14:25:59 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/23 16:15:16 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -55,7 +55,7 @@ void  clean_env(t_execution_env *env);
 void   handle_input(char **line, t_execution_env *env);
 
 void   unquote_field(char *field);
-int            add_word(t_vec *exp_str, const char *word, const t_execution_env *env);
+int            add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int quote);
 int            add_conformant(t_vec *expanded, t_wildcard_info *info, char quote);
 int            expand_dir(t_vec *expanded, t_wildcard_info *info);
 
index 2832e0754b566695ca5cc1bf2e1cd113589257f7..b8dd62658f26485bdfa1112ba7eeeda9fe5f8ff2 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/23 15:45:57 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/23 16:17:56 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -57,7 +57,7 @@ void  clear_old(t_vec *assignments, const char *new)
        return ;
 }
 
-int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env);
+int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int quote);
 
 int    add_assignment(t_vec *assignments, const char *assignment, const t_execution_env *env)
 {
@@ -66,7 +66,7 @@ int   add_assignment(t_vec *assignments, const char *assignment, const t_execution
 
        if (ft_vec_init(&advanced_copy, sizeof(char)) != success)
                return (1);
-       if (add_word(&advanced_copy, assignment, env))
+       if (add_word(&advanced_copy, assignment, env, 0))
                return (1);
        *(char *)ft_vec_access(&advanced_copy, advanced_copy.size - 1) = '\0';
        copy = advanced_copy.vec;
@@ -401,14 +401,16 @@ int       save_redirections(t_vec *redirections, t_parse_tree_node *simple_command, co
 
 static const char      space = ' ';
 
-int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env)
+int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int quote)
 {
        size_t          i;
        char            *var;
        char            *value;
        const char      *const_val;
        int                     single_quoted;
+       int                     error;
 
+       error = 0;
        single_quoted = 0;
        i = 0;
        while (word[i])
@@ -435,14 +437,16 @@ int       add_word(t_vec *exp_str, const char *word, const t_execution_env *env)
                                        continue;
                                value = ft_strdup(const_val);
                        }
-                       if (!value || ft_vec_append(exp_str, "'") != success
-                                       || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success
-                                       || ft_vec_append(exp_str, "'") != success)
-                       {
-                               free(value);
+                       if (!value)
                                return (1);
-                       }
+                       if (quote)
+                               error = error || ft_vec_append(exp_str, "'") != success;
+                       error = error || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success;
+                       if (quote)
+                               error = error || ft_vec_append(exp_str, "'") != success;
                        free(value);
+                       if (error)
+                               return (1);
                }
                else
                        if (ft_vec_append(exp_str, word + (i++)) != success)
@@ -468,7 +472,7 @@ int expand_cmd(t_vec *exp_str, const t_parse_tree_node *node, const t_execution_
                                return (1);
                }
                else if (is_token_type(subnode, "WORD"))
-                       if (add_word(exp_str, subnode->token.str, env))
+                       if (add_word(exp_str, subnode->token.str, env, 0))
                                return (1);
                ++i;
        }
index 709dcec4ee7cb47d36503ea27964136b5ed95ea1..e386bc782fd67721a51fd99584867c86c78c90c9 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/08 10:50:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/23 14:03:58 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/23 16:15:58 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -200,7 +200,7 @@ int expand_vars(char **str, const t_execution_env *env)
 
        if (ft_vec_init(&expanded, sizeof(char)) != success)
                return (1);
-       if (add_word(&expanded, *str, env))
+       if (add_word(&expanded, *str, env, 1))
        {
                ft_vec_free(&expanded, NULL);
                return (1);