Implement $? handling
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 23 Aug 2024 13:42:36 +0000 (15:42 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 23 Aug 2024 13:48:39 +0000 (15:48 +0200)
src/execution.c
src/wildcards.c

index b1cf8ec8e5fa283e96a779b26b63c584adf628d9..bd8d367784447381c06efd343ca3549ab6ff7755 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 13:41:12 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/23 15:45:57 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -405,7 +405,8 @@ int add_word(t_vec *exp_str, const char *word, const t_execution_env *env)
 {
        size_t          i;
        char            *var;
-       const char      *value;
+       char            *value;
+       const char      *const_val;
        int                     single_quoted;
 
        single_quoted = 0;
@@ -414,20 +415,34 @@ int       add_word(t_vec *exp_str, const char *word, const t_execution_env *env)
        {
                if (word[i] == '\'')
                        single_quoted = !single_quoted;
-               if (word[i] == '$' && !single_quoted && (ft_isalnum(word[i + 1]) || word[i + 1] == '_'))
+               if (word[i] == '$' && !single_quoted && (ft_isalnum(word[i + 1]) || word[i + 1] == '_' || word[i + 1] == '?'))
                {
                        ++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(exp_str, "'") != success
-                                               || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success
-                                               || ft_vec_append(exp_str, "'") != success)
+                       if (word[i] == '?')
+                       {
+                               ++i;
+                               value = ft_itoa(env->ret_val);
+                       }
+                       else
+                       {
+                               var = get_var_name(word + i);
+                               if (!var)
                                        return (1);
+                               i += ft_strlen(var);
+                               const_val = get_env_var_value(env, var);
+                               free(var);
+                               if (!const_val)
+                                       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);
+                               return (1);
+                       }
+                       free(value);
                }
                else
                        if (ft_vec_append(exp_str, word + (i++)) != success)
index a7ad6f3bf4c4b091c264831c28c7a1046ccc0baf..709dcec4ee7cb47d36503ea27964136b5ed95ea1 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/08 10:50:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/22 16:15:32 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/23 14:03:58 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -279,7 +279,7 @@ int should_be_expanded(const char *word)
        i = 0;
        while (word[i])
        {
-               if ((word[i] == '*' || word[i] == '?') && quote_char == '\0')
+               if ((word[i] == '*' || (word[i] == '?' && !(i && word[i - 1] == '$'))) && quote_char == '\0')
                        return (1);
                if (word[i] == '"')
                {