From 8f386bcd29425b96017c1e9b2cd1bbd670939563 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 9 Jul 2024 19:23:17 +0200 Subject: [PATCH] Adding working parsing TODO: Refactoring --- include/ast.h | 10 ++-- include/token.h | 10 ++-- src/free_token.c | 4 +- src/parse_cmd.c | 122 +++++++++++++++++++++++++++++++++++------------ 4 files changed, 102 insertions(+), 44 deletions(-) diff --git a/include/ast.h b/include/ast.h index 6479272..b0ed996 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: dkaiser type == STRING_TOKEN) - free(token->content.string); if (token->previous != NULL) token->previous->next = token->next; if (token->next != NULL) diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 739c022..4fa2bc2 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,17 +6,13 @@ /* By: dkaiser -#include -static t_redirection **collect_redirs(t_token **tokens); +static t_redirection *collect_redirs(t_token **tokens); static t_assign **collect_assigns(t_token **tokens); static char **collect_args(t_token **tokens); static t_assign *to_assign(char *str); @@ -25,7 +21,7 @@ t_node *parse_cmd(t_token *tokens) { char **args; t_assign **assigns; - t_redirection redirs[2]; + t_redirection *redirs; redirs = collect_redirs(&tokens); assigns = collect_assigns(&tokens); @@ -33,80 +29,144 @@ t_node *parse_cmd(t_token *tokens) return (new_cmd_node(args, assigns, redirs)); } -static t_redirection **collect_redirs(t_token **tokens) +static t_redirection *collect_redirs(t_token **tokens) { - return (NULL); + t_redirection *result; + t_token *cur; + + cur = *tokens; + result = malloc(sizeof(t_redirection) * 2); + if (result == NULL) + { + // free all tokens + return (NULL); + } + result[0].specifier = NULL; + result[0].type = 0; + result[1].specifier = NULL; + result[1].type = 0; + while (cur != NULL && cur->next != NULL) + { + if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) + { + if (cur->content.redir_type & (INPUT_FILE | INPUT_LIMITER)) + { + result[0].type = cur->content.redir_type; + result[0].specifier = cur->next->content.string; + } + else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) + { + result[1].type = cur->content.redir_type; + result[1].specifier = cur->next->content.string; + } + cur = cur->next; + free_token_and_connect(cur->previous); + if (cur->next != NULL) + { + cur = cur->next; + free_token_and_connect(cur->previous); + } + else + { + free_token(cur); + } + } + else if (cur->type == REDIR_TOKEN) + { + // err Parsing error + free(result); + return (NULL); + } + else + { + cur = cur->next; + } + } + return (result); } static t_assign **collect_assigns(t_token **tokens) { - int i; + t_token *cur; t_assign **result; + int i; + cur = *tokens; i = 0; - while (ft_strchr(tokens[i]->content.string, '=') != NULL) + while (cur != NULL && cur->type == STRING_TOKEN + && cur->content.string[0] != '"' && cur->content.string[0] != '\'' + && ft_strchr(cur->content.string, '=') != NULL) { i++; + cur = cur->next; } result = malloc(sizeof(t_assign *) * (i + 1)); - if (result == NULL) + if (!result) { - // free everything + // free all tokens return (NULL); } - result[i] = NULL; - i--; - while (i >= 0) + cur = *tokens; + i = 0; + while (cur != NULL && cur->type == STRING_TOKEN + && cur->content.string[0] != '"' && cur->content.string[0] != '\'' + && ft_strchr(cur->content.string, '=') != NULL) { - result[i] = to_assign(tokens[i]->content.string); - i--; + result[i] = to_assign(cur->content.string); + i++; + cur = cur->next; + free_token(cur->previous); } + *tokens = cur; + result[i] = NULL; return (result); } static t_assign *to_assign(char *str) { t_assign *result; - char *split; + char *split_pos; + split_pos = ft_strchr(str, '='); + *split_pos = '\0'; result = malloc(sizeof(t_assign)); if (result == NULL) + { return (NULL); - split = ft_strchr(str, '='); - if (split == NULL) - return (NULL); - *split = '\0'; + } result->var = str; - result->value = split + 1; + result->value = split_pos + 1; return (result); } static char **collect_args(t_token **tokens) { - t_token *cur; - char **result; - int i; + t_token *cur; + char **result; + int i; cur = *tokens; i = 0; - while (cur != NULL) { + while (cur != NULL) + { i++; cur = cur->next; } - result = malloc(sizeof(char*) * (i + 1)); + result = malloc(sizeof(char *) * (i + 1)); if (!result) { - //free all tokens; + // free all tokens; return (NULL); } cur = *tokens; i = 0; - while(cur != NULL) + while (cur != NULL) { result[i] = cur->content.string; // free token i++; cur = cur->next; } + result[i] = NULL; return (result); } -- 2.47.2