Improve wildcard recognition
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 22 Aug 2024 11:04:08 +0000 (13:04 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Thu, 22 Aug 2024 13:27:30 +0000 (15:27 +0200)
src/wildcards.c

index 010c36d7dda7a40e983e71f069a86f70d8618162..62edf860133a17ed2258f9c10a04c3d502238f75 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/08/08 10:50:26 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/08/22 11:30:03 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/08/22 13:02:23 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -169,10 +169,7 @@ int        expand_word(char **str, const t_execution_env *env)
                if (str[0][1] == '/' || str[0][1] == '\0')
                        info.path = ft_strdup(get_env_var_value(env, "HOME"));
                else
-               {
-                       ft_printf("Advanced tilde expansion not implemented.\n");
                        return (1);
-               }
        }
        else
                info.path = ft_strdup("");
@@ -193,6 +190,36 @@ int        expand_word(char **str, const t_execution_env *env)
        return (res);
 }
 
+int    should_be_expanded(const char *word)
+{
+       size_t  i;
+       char    quote_char;
+
+       quote_char = '\0';
+       i = 0;
+       while (word[i])
+       {
+               if ((word[i] == '*' || word[i] == '?') && quote_char == '\0')
+                       return (1);
+               if (word[i] == '"')
+               {
+                       if (quote_char == '\0')
+                               quote_char = '"';
+                       else if (quote_char == '"')
+                               quote_char = '\0';
+               }
+               if (word[i] == '\'')
+               {
+                       if (quote_char == '\0')
+                               quote_char = '\'';
+                       else if (quote_char == '\'')
+                               quote_char = '\0';
+               }
+               ++i;
+       }
+       return (0);
+}
+
 int    expand_wildcards(char **input, const t_execution_env *env)
 {
        char    **split;
@@ -205,7 +232,7 @@ int expand_wildcards(char **input, const t_execution_env *env)
        i = 0;
        while (split[i])
        {
-               if (split[i][0] != '"' && split[i][0] != '\'' && (ft_strchr(split[i], '*') != NULL || ft_strchr(split[i], '?') != NULL))
+               if (should_be_expanded(split[i]))
                        if (expand_word(split + i, env))
                        {
                                ft_free_split(split);