Fix variable expansion inside quoted single quotes
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 30 Aug 2024 12:23:10 +0000 (14:23 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 30 Aug 2024 12:23:10 +0000 (14:23 +0200)
src/execution.c

index 85a707a92660b1200e144b4dc262f079f80138bd..447b4361f7621afa24d573c838eb24080ab4e4e1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: lnikolov <lnikolov@student.42prague.com    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/07/21 08:57:54 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/29 18:24:21 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/30 14:18:21 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -352,23 +352,25 @@ 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 quote)
+int    add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int enquote_result)
 {
        size_t          i;
        char            *var;
        char            *value;
        const char      *const_val;
-       int                     single_quoted;
+       char            quote;
        int                     error;
 
        error = 0;
-       single_quoted = 0;
+       quote = '\0';
        i = 0;
        while (word[i])
        {
-               if (word[i] == '\'')
-                       single_quoted = !single_quoted;
-               if (word[i] == '$' && !single_quoted && (ft_isalnum(word[i + 1]) || word[i + 1] == '_' || word[i + 1] == '?'))
+               if (word[i] == '\'' && quote != '"')
+                       quote = '\'' * !quote;
+               else if (word[i] == '"' && quote != '\'')
+                       quote = '"' * !quote;
+               if (word[i] == '$' && quote != '\'' && (ft_isalnum(word[i + 1]) || word[i + 1] == '_' || word[i + 1] == '?'))
                {
                        ++i;
                        if (word[i] == '?')
@@ -390,10 +392,10 @@ int       add_word(t_vec *exp_str, const char *word, const t_execution_env *env, int q
                        }
                        if (!value)
                                return (1);
-                       if (quote)
+                       if (enquote_result)
                                error = error || ft_vec_append(exp_str, "'") != success;
                        error = error || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success;
-                       if (quote)
+                       if (enquote_result)
                                error = error || ft_vec_append(exp_str, "'") != success;
                        free(value);
                        if (error)