/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/21 08:57:54 by ljiriste #+# #+# */
-/* Updated: 2024/08/02 18:35:53 by ljiriste ### ########.fr */
+/* Updated: 2024/08/22 14:27:10 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
int add_word(t_vec *exp_str, const char *word, const t_execution_env *env);
-void unquote_field(char *field);
-
int add_assignment(t_vec *assignments, const char *assignment, const t_execution_env *env)
{
t_vec advanced_copy;
i += ft_strlen(var);
free(var);
if (value)
- if (ft_vec_append_range(exp_str, value, ft_strlen(value)) != success)
+ if (ft_vec_append(exp_str, "'") != success
+ || ft_vec_append_range(exp_str, value, ft_strlen(value)) != success
+ || ft_vec_append(exp_str, "'") != success)
return (1);
}
else
quote = 0;
i = 0;
- while (field[i])
+ while (field && field[i])
{
if (field[i] == '"' || field[i] == '\'')
{
/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/08 10:50:26 by ljiriste #+# #+# */
-/* Updated: 2024/08/22 13:02:23 by ljiriste ### ########.fr */
+/* Updated: 2024/08/22 14:35:25 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
const size_t start_size = expanded->size;
++info->current_expand_char;
- if (add_conformant(expanded, info))
+ if (add_conformant(expanded, info, '\0'))
return (1);
if (start_size != expanded->size || *info->current_entry_char == '\0')
return (0);
--info->current_expand_char;
++info->current_entry_char;
- return (add_conformant(expanded, info));
+ return (add_conformant(expanded, info, '\0'));
}
int expand_further(t_vec *expanded, t_wildcard_info info)
return (res);
}
-int add_conformant(t_vec *expanded, t_wildcard_info *info)
+int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote)
{
if (*info->current_expand_char == '\0' && *info->current_entry_char == '\0')
return (add_entry(expanded, info));
if (*info->current_expand_char == '/' && info->current_entry_char == info->entry)
{
++info->current_expand_char;
- return (add_conformant(expanded, info));
+ return (add_conformant(expanded, info, quote));
+ }
+ if (*info->current_expand_char == '\'' || *info->current_expand_char == '"')
+ {
+ if (!quote)
+ quote = *info->current_expand_char;
+ else if (quote == *info->current_expand_char)
+ quote = '\0';
+ else
+ --info->current_expand_char;
+ ++info->current_expand_char;
+ return (add_conformant(expanded, info, quote));
}
if ((*info->current_expand_char == '?' && *info->current_entry_char != '\0')
- || *info->current_expand_char == *info->current_entry_char)
+ || (*info->current_expand_char == *info->current_entry_char && (*info->current_expand_char != '*' || quote)))
{
++info->current_expand_char;
++info->current_entry_char;
- return (add_conformant(expanded, info));
+ return (add_conformant(expanded, info, quote));
}
if (*info->current_expand_char == '*' && (*info->current_entry_char != '.' || info->current_entry_char != info->entry))
return (branch_at_star(expanded, info));
info->entry = dir_entry->d_name;
info->current_entry_char = info->entry;
info->current_expand_char = info->to_expand;
- if (add_conformant(expanded, info))
+ if (add_conformant(expanded, info, '\0'))
{
closedir(dir);
return (1);
return (0);
}
+int expand_vars(char **str, const t_execution_env *env)
+{
+ t_vec expanded;
+
+ if (ft_vec_init(&expanded, sizeof(char)) != success)
+ return (1);
+ if (add_word(&expanded, *str, env))
+ {
+ ft_vec_free(&expanded, NULL);
+ return (1);
+ }
+ *(char *)ft_vec_access(&expanded, expanded.size - 1) = '\0';
+ free(*str);
+ *str = expanded.vec;
+ return (0);
+}
+
+char *get_start_path(const char *str, const t_execution_env *env)
+{
+ char *unquoted_str;
+ char *res;
+
+ unquoted_str = ft_strdup(str);
+ unquote_field(unquoted_str);
+ if (unquoted_str[0] == '/')
+ res = ft_strdup("/");
+ else if (unquoted_str[0] == '~' && (ft_isalpha(unquoted_str[1]) || unquoted_str[1] == '/' || unquoted_str[1] == '~' || unquoted_str[1] == '+' || unquoted_str[1] == '-'))
+ {
+ if (unquoted_str[1] == '/' || unquoted_str[1] == '\0')
+ res = ft_strjoin(get_env_var_value(env, "HOME"), "/");
+ else
+ {
+ free(unquoted_str);
+ return (NULL);
+ }
+ }
+ else
+ res = ft_strdup("");
+ free(unquoted_str);
+ return (res);
+}
+
int expand_word(char **str, const t_execution_env *env)
{
const char *nullptr = NULL;
if (ft_vec_init(&matched, sizeof(char *)) != success)
return (1);
info.to_expand = *str;
- if (str[0][0] == '/')
- info.path = ft_strdup("/");
- else if (str[0][0] == '~' && (ft_isalpha(str[0][1]) || str[0][1] == '/' || str[0][1] == '~' || str[0][1] == '+' || str[0][1] == '-'))
- {
- if (str[0][1] == '/' || str[0][1] == '\0')
- info.path = ft_strdup(get_env_var_value(env, "HOME"));
- else
- return (1);
- }
- else
- info.path = ft_strdup("");
+ info.path = get_start_path(*str, env);
if (!info.path)
+ {
+ ft_vec_free(&matched, free_str);
return (1);
+ }
if (info.path[0] && ft_strchr("/~", info.path[0]) != NULL)
++info.to_expand;
res = expand_dir(&matched, &info);
while (split[i])
{
if (should_be_expanded(split[i]))
- if (expand_word(split + i, env))
+ {
+ if (expand_vars(split + i, env) || expand_word(split + i, env))
{
ft_free_split(split);
return (1);
}
+ }
++i;
}
res = replace_str_by_joint_split(input, split);