From 526eba189e99330acb09cff72c8403f46d686698 Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Wed, 5 Mar 2025 21:18:52 +0100 Subject: [PATCH] refactor command get arguments --- inc/parser.h | 3 ++ inc/token.h | 3 +- src/lexer/lexer_token_next.c | 2 +- src/parser/parser_concatenate.c | 23 +++++++++ src/parser/parser_get_arguments.c | 84 +++++++------------------------ src/parser/parser_process_token.c | 60 ++++++++++++++++++++++ src/token/token_list_index.c | 25 +++++++++ 7 files changed, 131 insertions(+), 69 deletions(-) create mode 100644 src/parser/parser_concatenate.c create mode 100644 src/parser/parser_process_token.c create mode 100644 src/token/token_list_index.c diff --git a/inc/parser.h b/inc/parser.h index cfcc5c5..b198232 100644 --- a/inc/parser.h +++ b/inc/parser.h @@ -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 diff --git a/inc/token.h b/inc/token.h index cbdbf41..51c7a15 100644 --- a/inc/token.h +++ b/inc/token.h @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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 diff --git a/src/lexer/lexer_token_next.c b/src/lexer/lexer_token_next.c index e8d7e34..52deb97 100644 --- a/src/lexer/lexer_token_next.c +++ b/src/lexer/lexer_token_next.c @@ -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; diff --git a/src/parser/parser_concatenate.c b/src/parser/parser_concatenate.c new file mode 100644 index 0000000..9eaa80b --- /dev/null +++ b/src/parser/parser_concatenate.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_concatenate.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/src/parser/parser_get_arguments.c b/src/parser/parser_get_arguments.c index 4d47b6b..16756c7 100644 --- a/src/parser/parser_get_arguments.c +++ b/src/parser/parser_get_arguments.c @@ -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; diff --git a/src/parser/parser_process_token.c b/src/parser/parser_process_token.c new file mode 100644 index 0000000..9e153b2 --- /dev/null +++ b/src/parser/parser_process_token.c @@ -0,0 +1,60 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser_process_token.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/src/token/token_list_index.c b/src/token/token_list_index.c new file mode 100644 index 0000000..663b36f --- /dev/null +++ b/src/token/token_list_index.c @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* token_list_index.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}