refactor command get arguments
This commit is contained in:
parent
3ed0e2ae9e
commit
526eba189e
@ -24,5 +24,8 @@ void parser_create_command(t_minishell *msh,
|
||||
char **parser_get_arguments(t_list *list, t_minishell *msh);
|
||||
int parser_validate_command(t_command *command);
|
||||
int parser_count_arguments(t_list *list);
|
||||
char *parser_concatenate(t_minishell *msh, char *str1, char *str2);
|
||||
char *parser_process_token(t_minishell *msh, char **args, t_list *prev
|
||||
, t_list *t_head);
|
||||
|
||||
#endif
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/05 13:11:41 by qmennen #+# #+# */
|
||||
/* Updated: 2025/03/05 13:11:58 by qmennen ### ########.fr */
|
||||
/* Updated: 2025/03/05 20:54:45 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -16,5 +16,6 @@
|
||||
# include "minishell.h"
|
||||
|
||||
t_token *token_from_list(t_list *list);
|
||||
t_list *token_list_index(t_list *tokens, int index);
|
||||
|
||||
#endif
|
||||
|
||||
@ -22,7 +22,7 @@ static t_token_type get_word_type(char c)
|
||||
return (T_WORD);
|
||||
}
|
||||
|
||||
static t_token * process_word(t_minishell *msh, t_lexer *lexer, int pos)
|
||||
static t_token *process_word(t_minishell *msh, t_lexer *lexer, int pos)
|
||||
{
|
||||
t_token_type word_type;
|
||||
t_token *token;
|
||||
|
||||
23
src/parser/parser_concatenate.c
Normal file
23
src/parser/parser_concatenate.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_concatenate.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/05 20:14:54 by qmennen #+# #+# */
|
||||
/* Updated: 2025/03/05 20:18:30 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
char *parser_concatenate(t_minishell *msh, char *str1, char *str2)
|
||||
{
|
||||
char *cat;
|
||||
|
||||
cat = malloc_safe(msh, ft_strlen(str1) + ft_strlen(str2) + 1);
|
||||
ft_strlcpy(cat, str1, ft_strlen(str1) + 1);
|
||||
ft_strlcat(cat, str2, ft_strlen(str2) + ft_strlen(str1) + 1);
|
||||
return (cat);
|
||||
}
|
||||
@ -12,97 +12,47 @@
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int parser_should_expand(t_list *value)
|
||||
static int parser_should_concact(t_minishell *msh, int argi, t_list *tkns)
|
||||
{
|
||||
t_token *token;
|
||||
int i;
|
||||
t_token *c_tkn;
|
||||
t_token *p_tkn;
|
||||
char lexer_char;
|
||||
|
||||
token = (t_token *)value->content;
|
||||
if (!token)
|
||||
if (argi < 1)
|
||||
return (0);
|
||||
i = 0;
|
||||
if (token->type == T_DQWORD || token->type == T_WORD)
|
||||
{
|
||||
while (token->value[i])
|
||||
{
|
||||
if (token->value[i] == '$'
|
||||
&& (expander_character_valid(token->value[i + 1]) || token->value[i + 1] == '?'))
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
p_tkn = token_from_list(token_list_index(tkns, argi - 1));
|
||||
c_tkn = token_from_list(token_list_index(tkns, argi));
|
||||
lexer_char = 0;
|
||||
if (!p_tkn || !c_tkn || c_tkn->position <= 0)
|
||||
return (0);
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int parser_should_concact(t_minishell *msh, int argi, t_list *prev)
|
||||
{
|
||||
t_token *prev_tkn;
|
||||
char c;
|
||||
|
||||
prev_tkn = token_from_list(prev);
|
||||
c = 0;
|
||||
if (!prev_tkn || argi < 1)
|
||||
return (0);
|
||||
if (prev_tkn->position > 0)
|
||||
c = msh->lexer->input[prev_tkn->position - 1];
|
||||
return (ft_strcmp(prev_tkn->value, "") == 0 && c && !ft_isspace(c));
|
||||
}
|
||||
|
||||
static char *parser_process_token(t_minishell *msh, char **args, t_list *prev, t_list *t_head)
|
||||
{
|
||||
char *str;
|
||||
t_token *token;
|
||||
|
||||
if (!t_head)
|
||||
return (NULL);
|
||||
token = (t_token *)t_head->content;
|
||||
str = NULL;
|
||||
if (ft_strcmp(token->value, "") == 0)
|
||||
return (NULL);
|
||||
if (parser_should_expand(t_head))
|
||||
str = expander_parse_string(token->value, msh);
|
||||
if (!str)
|
||||
str = ft_strdup_safe(msh, token->value);
|
||||
if (str && token->type == T_WORD && ft_strchr(str, '"'))
|
||||
str = parser_sanitize_string(msh, str, '"');
|
||||
else if (str && token->type == T_WORD && ft_strchr(str, '\''))
|
||||
str = parser_sanitize_string(msh, str, '\'');
|
||||
return (str);
|
||||
lexer_char = msh->lexer->input[c_tkn->position - 1];
|
||||
// if current & previous token is word and inbetween is no space, concat
|
||||
return (c_tkn->type < 3 && p_tkn->type < 3 && !ft_isspace(lexer_char));
|
||||
}
|
||||
|
||||
char **parser_get_arguments(t_list *list, t_minishell *msh)
|
||||
{
|
||||
t_list *current;
|
||||
t_list *prev;
|
||||
char *str;
|
||||
char **args;
|
||||
int argc;
|
||||
int i;
|
||||
char *str;
|
||||
char *cat;
|
||||
|
||||
argc = parser_count_arguments(list);
|
||||
args = malloc_safe(msh, (argc + 1) * sizeof(char *));
|
||||
current = list;
|
||||
i = 0;
|
||||
prev = NULL;
|
||||
while (argc > 0 && current)
|
||||
{
|
||||
if (token_from_list(current)->type < 3 && (!prev || token_from_list(prev)->type < 3))
|
||||
if (token_from_list(current)->type < 3)
|
||||
{
|
||||
str = parser_process_token(msh, args, prev, current);
|
||||
if (parser_should_concact(msh, i, prev))
|
||||
{
|
||||
cat = malloc_safe(msh, ft_strlen(str) + ft_strlen(args[i - 1]) + 1);
|
||||
ft_strlcpy(cat, args[i - 1], ft_strlen(args[i - 1]) + 1);
|
||||
ft_strlcat(cat, str, ft_strlen(str) + ft_strlen(args[i - 1]) + 1);
|
||||
args[i - 1] = cat;
|
||||
}
|
||||
str = parser_process_token(msh, args, token_list_index(list, i - 1), current);
|
||||
if (parser_should_concact(msh, i, list))
|
||||
args[i - 1] = parser_concatenate(msh, args[i - 1], str);
|
||||
else if (str)
|
||||
args[i++] = str;
|
||||
argc--;
|
||||
}
|
||||
prev = current;
|
||||
current = current->next;
|
||||
}
|
||||
args[i] = 0;
|
||||
|
||||
60
src/parser/parser_process_token.c
Normal file
60
src/parser/parser_process_token.c
Normal file
@ -0,0 +1,60 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* parser_process_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/05 21:12:15 by qmennen #+# #+# */
|
||||
/* Updated: 2025/03/05 21:14:09 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
static int parser_should_expand(t_list *value)
|
||||
{
|
||||
t_token *token;
|
||||
char *t_val;
|
||||
int i;
|
||||
|
||||
token = (t_token *)value->content;
|
||||
if (!token)
|
||||
return (0);
|
||||
i = 0;
|
||||
if (token->type != T_DQWORD && token->type != T_WORD)
|
||||
return (0);
|
||||
while (token->value[i])
|
||||
{
|
||||
t_val = token->value;
|
||||
if (t_val[i] == '$' && expander_character_valid(t_val[i + 1]))
|
||||
return (1);
|
||||
else if (t_val[i] == '$' && t_val[i + 1] == '?')
|
||||
return (1);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
char *parser_process_token(t_minishell *msh, char **args, t_list *prev
|
||||
, t_list *t_head)
|
||||
{
|
||||
char *str;
|
||||
t_token *token;
|
||||
|
||||
if (!t_head)
|
||||
return (NULL);
|
||||
token = (t_token *)t_head->content;
|
||||
str = NULL;
|
||||
if (ft_strcmp(token->value, "") == 0)
|
||||
return (NULL);
|
||||
if (parser_should_expand(t_head))
|
||||
str = expander_parse_string(token->value, msh);
|
||||
if (!str)
|
||||
str = ft_strdup_safe(msh, token->value);
|
||||
if (str && token->type == T_WORD && ft_strchr(str, '"'))
|
||||
str = parser_sanitize_string(msh, str, '"');
|
||||
else if (str && token->type == T_WORD && ft_strchr(str, '\''))
|
||||
str = parser_sanitize_string(msh, str, '\'');
|
||||
return (str);
|
||||
}
|
||||
25
src/token/token_list_index.c
Normal file
25
src/token/token_list_index.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* token_list_index.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/03/05 20:53:34 by qmennen #+# #+# */
|
||||
/* Updated: 2025/03/05 21:00:01 by qmennen ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "minishell.h"
|
||||
|
||||
t_list *token_list_index(t_list *tokens, int index)
|
||||
{
|
||||
t_list *current;
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
current = tokens;
|
||||
while ((i++) < index && current)
|
||||
current = current->next;
|
||||
return (current);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user