/* ::: :::::::: */
/* tokenization.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
+/* By: lnikolov <lnikolov@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/21 16:34:43 by ljiriste #+# #+# */
-/* Updated: 2024/08/31 11:58:17 by ljiriste ### ########.fr */
+/* Updated: 2024/08/31 12:55:57 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
|| token->type == g_tokens[DGREAT]);
}
+int assignment_may_follow(const char *type)
+{
+ return (type == g_tokens[ASSIGNMENT_WORD]
+ || type == g_tokens[AND_IF]
+ || type == g_tokens[OR_IF]
+ || type == g_tokens[LPARA]);
+}
+
void filter_assignment_word(t_vec *tokens)
{
size_t i;
t_token *token;
const t_token *prev_token;
- i = 0;
- while (i < tokens->size)
+ i = -1;
+ while (i + 1 < tokens->size)
{
+ ++i;
token = ft_vec_access(tokens, i);
if (i == 0 || token->type != g_tokens[ASSIGNMENT_WORD])
- {
- ++i;
continue ;
- }
prev_token = ft_vec_caccess(tokens, i - 1);
- if (prev_token->type == g_tokens[ASSIGNMENT_WORD]
- || prev_token->type == g_tokens[AND_IF]
- || prev_token->type == g_tokens[OR_IF]
- || prev_token->type == g_tokens[LPARA])
- {
- ++i;
+ if (assignment_may_follow(prev_token->type))
continue ;
- }
if (i == 1 || prev_token->type != g_tokens[WORD])
{
- ++i;
token->type = (char *)g_tokens[WORD];
continue ;
}
prev_token = ft_vec_caccess(tokens, i - 2);
if (!is_redirection_operator(prev_token))
token->type = (char *)g_tokens[WORD];
- ++i;
}
}
+int categorization_step(char *line, size_t *i,
+ t_vec *current_token, t_vec *tokens)
+{
+ int res;
+
+ if (is_operator_start(current_token->vec, current_token->size)
+ && can_expand_operator(current_token, line[*i]))
+ res = (ft_vec_append(current_token, line + (*i)++) != success);
+ else if (is_operator(current_token))
+ res = finish_token(tokens, current_token, '\0');
+ else if (line[*i] == '\'')
+ res = handle_quote(current_token, line, '\'', i);
+ else if (line[*i] == '"' )
+ res = handle_quote(current_token, line, '"', i);
+ else if (is_operator_start(line + *i, 1) || ft_isspace(line[*i]))
+ {
+ if (current_token->size > 0)
+ res = finish_token(tokens, current_token, line[*i]);
+ if (!ft_isspace(line[*i]))
+ res = res || ft_vec_append(current_token, line + *i)
+ != success;
+ ++*i;
+ }
+ else if (current_token->size > 0)
+ res = ft_vec_append(current_token, line + (*i)++) != success;
+ else
+ res = ft_vec_append(current_token, line + (*i)++) != success;
+ return (res);
+}
+
int tokenize(char *line, t_vec *tokens)
{
t_vec current_token;
- t_token token;
size_t i;
int res;
i = 0;
while (line[i] && res == 0)
{
- if (is_operator_start(current_token.vec, current_token.size)
- && can_expand_operator(¤t_token, line[i]))
- res = (ft_vec_append(¤t_token, line + (i++)) != success);
- else if (is_operator(¤t_token))
- res = finish_token(tokens, ¤t_token, '\0');
- else if (line[i] == '\'')
- res = handle_quote(¤t_token, line, '\'', &i);
- else if (line[i] == '"' )
- res = handle_quote(¤t_token, line, '"', &i);
- else if (is_operator_start(line + i, 1) || ft_isspace(line[i]))
- {
- if (current_token.size > 0)
- res = finish_token(tokens, ¤t_token, line[i]);
- if (!ft_isspace(line[i]))
- res = res || ft_vec_append(¤t_token, line + i)
- != success;
- ++i;
- }
- else if (current_token.size > 0)
- res = ft_vec_append(¤t_token, line + (i++)) != success;
- else if (line[i] == '#')
+ if (line[i] == '#')
break ;
- else
- res = ft_vec_append(¤t_token, line + (i++)) != success;
+ res = categorization_step(line, &i, ¤t_token, tokens);
}
if (current_token.size > 0 && !res)
- {
- ft_vec_append(¤t_token, "");
- token.type = (char *)get_token_type(current_token.vec, '\0');
- token.str = current_token.vec;
- ft_vec_append(tokens, &token);
- }
+ res = finish_token(tokens, ¤t_token, '\0');
if (res)
ft_vec_free(tokens, free_token);
ft_vec_free(¤t_token, NULL);
/* By: lnikolov <lnikolov@student.42prague.com +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/08 10:50:26 by ljiriste #+# #+# */
-/* Updated: 2024/08/30 14:39:10 by lnikolov ### ########.fr */
+/* Updated: 2024/08/31 13:24:09 by ljiriste ### ########.fr */
/* */
/* ************************************************************************** */
return (res);
}
-int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote)
+int add_conformant_part2(t_vec *expanded, t_wildcard_info *info, char quote)
{
- if (g_last_signal != 0)
- return (2);
- 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 == '\0')
- {
- ++info->current_expand_char;
- return (expand_further(expanded, *info));
- }
- if (*info->current_expand_char == '/' && info->current_entry_char
- == info->entry)
- {
- ++info->current_expand_char;
- return (add_conformant(expanded, info, quote));
- }
if (*info->current_expand_char == '\'' || *info->current_expand_char == '"')
{
if (!quote)
return (0);
}
+int add_conformant(t_vec *expanded, t_wildcard_info *info, char quote)
+{
+ if (g_last_signal != 0)
+ return (2);
+ 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 == '\0')
+ {
+ ++info->current_expand_char;
+ return (expand_further(expanded, *info));
+ }
+ if (*info->current_expand_char == '/' && info->current_entry_char
+ == info->entry)
+ {
+ ++info->current_expand_char;
+ return (add_conformant(expanded, info, quote));
+ }
+ return (add_conformant_part2(expanded, info, quote));
+}
+
int expand_dir(t_vec *expanded, t_wildcard_info *info)
{
DIR *dir;
size_t len;
size_t i;
- tmp = *str;
len = 0;
i = 0;
while (expanded_split[i])
len += ft_strlen(expanded_split[i++]) + 2 + 1;
- *str = malloc(len + 1 + 1);
- if (!str)
- {
- *str = tmp;
+ tmp = malloc(len + 1 + 1);
+ if (!tmp)
return (1);
- }
+ free(*str);
+ *str = tmp;
str[0][0] = '\0';
i = 0;
while (expanded_split[i])
ft_strlcat(*str, "'", len + 1 + 1);
ft_strlcat(*str, " ", len + 1 + 1);
}
- free(tmp);
return (0);
}
return (res);
}
-int expand_word(char **str, const t_execution_env *env)
+int expand_word_inner(char **str, const t_execution_env *env)
{
const char *nullptr = NULL;
t_vec matched;
t_wildcard_info info;
int res;
- char last_char;
- last_char = str[0][ft_strlen(*str) - 1];
- if (last_char == '\n')
- str[0][ft_strlen(*str) - 1] = '\0';
if (ft_vec_init(&matched, sizeof(char *)) != success)
return (1);
info.to_expand = *str;
res = res || replace_str_by_joint_quoted_split(str, matched.vec);
ft_vec_free(&matched, free_str);
free((char *)info.path);
+ return (res);
+}
+
+int expand_word(char **str, const t_execution_env *env)
+{
+ int res;
+ char last_char;
+
+ last_char = str[0][ft_strlen(*str) - 1];
+ if (last_char == '\n')
+ str[0][ft_strlen(*str) - 1] = '\0';
+ res = expand_word_inner(str, env);
if (last_char == '\n')
{
str[0][ft_strlen(*str) + 1] = '\0';
if ((word[i] == '*' || (word[i] == '?' && !(i && word[i - 1] == '$')))
&& 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 (word[i] == '"' || word[i] == '\'')
{
if (quote_char == '\0')
- quote_char = '\'';
- else if (quote_char == '\'')
+ quote_char = word[i];
+ else if (quote_char == word[i])
quote_char = '\0';
}
++i;