From bc5e39df2154abfdf308b6cbe644b8953586be2d Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 8 Jul 2024 16:03:48 +0200 Subject: Add free_token_and_connect() function --- src/free_token.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/free_token.c') diff --git a/src/free_token.c b/src/free_token.c index 9f5c4e9..7be6b0e 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -6,7 +6,7 @@ /* By: dkaiser next->previous = NULL; free(token); } + +void free_token_and_connect(t_token *token) +{ + if (token->type == STRING_TOKEN) + free(token->content.string); + if (token->previous != NULL) + token->previous->next = token->next; + if (token->next != NULL) + token->next->previous = token->previous; + free(token); +} -- cgit v1.2.3 From a5dd924bea37a0786dcf2b95690452f7714043f6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 8 Jul 2024 17:30:58 +0200 Subject: Add most of parse_cmd --- src/free_token.c | 4 +- src/parse_cmd.c | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 src/parse_cmd.c (limited to 'src/free_token.c') diff --git a/src/free_token.c b/src/free_token.c index 7be6b0e..083484c 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -6,7 +6,7 @@ /* By: dkaiser type == STRING_TOKEN) - free(token->content.string); if (token->previous != NULL) token->previous->next = NULL; if (token->next != NULL) diff --git a/src/parse_cmd.c b/src/parse_cmd.c new file mode 100644 index 0000000..981bbbb --- /dev/null +++ b/src/parse_cmd.c @@ -0,0 +1,109 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser +#include + +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); + +t_node *parse_cmd(t_token *tokens) +{ + char **args; + t_assign **assigns; + t_redirection redirs[2]; + + redirs = collect_redirs(&tokens); + assigns = collect_assigns(&tokens); + args = collect_args(&tokens); + return (new_cmd_node(args, assigns, redirs)); +} + +static t_redirection **collect_redirs(t_token **tokens) +{ + return (NULL); +} + +static t_assign **collect_assigns(t_token **tokens) +{ + int i; + t_assign **result; + + i = 0; + while (ft_strchr(tokens[i]->content.string, '=') != NULL) + { + i++; + } + result = malloc(sizeof(t_assign *) * (i + 1)); + if (result == NULL) + { + // free everything + return (NULL); + } + result[i] = NULL; + i--; + while (i >= 0) + { + result[i] = to_assign(tokens[i]->content.string); + i--; + } + return (result); +} + +static char **collect_args(t_token **tokens) +{ + int i; + char **result; + + i = 0; + while (tokens[i] != NULL) + { + i++; + } + result = malloc(sizeof(char *) * (i + 1)); + if (result == NULL) + { + // free everything in the token + return (NULL); + } + i = 0; + while (tokens[i] != NULL) + { + result[i] = tokens[i]->content.string; + free_token(tokens[i]); + i++; + } + result[i] = NULL; + return (result); +} + +static t_assign *to_assign(char *str) +{ + t_assign *result; + char *split; + + 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; + return (result); +} -- cgit v1.2.3 From 8f386bcd29425b96017c1e9b2cd1bbd670939563 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 9 Jul 2024 19:23:17 +0200 Subject: 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(-) (limited to 'src/free_token.c') 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); } -- cgit v1.2.3 From 82c1eed4f7795ef135a586af2fb334145b64b2f6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 2 Aug 2024 14:14:42 +0200 Subject: Add function free_tokens() --- include/token.h | 3 ++- src/free_token.c | 12 +++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) (limited to 'src/free_token.c') diff --git a/include/token.h b/include/token.h index d61195d..72ca5e5 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: dkaiser next->previous = token->previous; free(token); } + +void free_tokens(t_token *tokens) +{ + while (tokens->next != NULL) + { + tokens = tokens->next; + free_token(tokens->previous); + } + free_token(tokens); +} -- cgit v1.2.3 From c7a4494fd97b7e80665cbd47ed96bc37bf5800e5 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 2 Aug 2024 15:29:00 +0200 Subject: Refactor parser * Fix norme for free_node.c * Uglify parser.c * Merge parser updates into refactor-parser * Outsource collect_assigns and collect_redirs * Make parse_cmd.c norme conform * Make free_tokens() norme conform * Make collect_assigns.c norme conform * Some refactoring of collect_redirs * Refactor collect_redirs--- include/minishell.h | 16 +++--- src/collect_assigns.c | 84 ++++++++++++++++++++++++++++++ src/collect_redirs.c | 80 +++++++++++++++++++++++++++++ src/free_node.c | 8 +-- src/free_token.c | 4 +- src/parse_cmd.c | 138 ++------------------------------------------------ src/parser.c | 14 ++--- 7 files changed, 193 insertions(+), 151 deletions(-) create mode 100644 src/collect_assigns.c create mode 100644 src/collect_redirs.c (limited to 'src/free_token.c') diff --git a/include/minishell.h b/include/minishell.h index d980d42..b21acbc 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser # include -int init(void); -int init_signal_handling(void); +int init(void); +int init_signal_handling(void); -void repl(const char *prompt); +void repl(const char *prompt); -t_list *parse(t_token *tokens); -t_node *parse_cmd(t_token *tokens); +t_list *parse(t_token *tokens); +t_node *parse_cmd(t_token *tokens); +t_redirection *collect_redirs(t_token **tokens); +t_assign **collect_assigns(t_token **tokens); -void print_ast(t_node *ast); +void print_ast(t_node *ast); #endif diff --git a/src/collect_assigns.c b/src/collect_assigns.c new file mode 100644 index 0000000..da43503 --- /dev/null +++ b/src/collect_assigns.c @@ -0,0 +1,84 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collect_assigns.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser type == STRING_TOKEN + && !is_quote(cur->content.string[0]) && ft_strchr(cur->content.string, + '=') != NULL) + { + result[i++] = to_assign(cur->content.string); + if (cur->next != NULL) + { + cur = cur->next; + free_token(cur->previous); + } + else + free_token(cur); + } + *tokens = cur; + result[i] = NULL; + return (result); +} + +static t_assign *to_assign(char *str) +{ + t_assign *result; + char *split_pos; + + split_pos = ft_strchr(str, '='); + *split_pos = '\0'; + result = malloc(sizeof(t_assign)); + if (result == NULL) + { + return (NULL); + } + result->var = str; + result->value = split_pos + 1; + return (result); +} + +static int count_tokens(t_token *tokens) +{ + int len; + + len = 0; + while (tokens != NULL && tokens->type == STRING_TOKEN + && !is_quote(tokens->content.string[0]) + && ft_strchr(tokens->content.string, '=') != NULL) + { + len++; + tokens = tokens->next; + } + return (len); +} + +static int is_quote(char c) +{ + return (c == '"' || c == '\''); +} diff --git a/src/collect_redirs.c b/src/collect_redirs.c new file mode 100644 index 0000000..76b08da --- /dev/null +++ b/src/collect_redirs.c @@ -0,0 +1,80 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* collect_redirs.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser next != NULL) + { + if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) + collect_redir(tokens, result, cur); + else if (cur->type == REDIR_TOKEN) + { + dbg("TODO: Add parsing errmsg"); + return (free(result), NULL); + } + else + cur = cur->next; + } + return (result); +} + +static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur) +{ + set_redir(&result[is_output_redir(cur->content.redir_type)], + cur->content.redir_type, cur->next->content.string); + cur = cur->next; + free_token_and_connect(cur->previous); + if (cur->next != NULL) + { + if (cur->previous == NULL) + *tokens = cur->next; + cur = cur->next; + free_token_and_connect(cur->previous); + } + else + free_token(cur); +} + +static void set_redir(t_redirection *redir, int type, char *specifier) +{ + redir->type = type; + redir->specifier = specifier; +} + +static int is_output_redir(int i) +{ + if (i & (INPUT_FILE | INPUT_LIMITER)) + return (0); + else if (i & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) + return (1); + else + { + panic(UNREACHABLE); + return (-1); + } +} diff --git a/src/free_node.c b/src/free_node.c index f8677d5..f387c0a 100644 --- a/src/free_node.c +++ b/src/free_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.args); free_assigns(node->content.cmd.assigns); - if (node->content.cmd.redirs[0].type != 0 && node->content.cmd.redirs[0].specifier != NULL) + if (node->content.cmd.redirs[0].type != 0 + && node->content.cmd.redirs[0].specifier != NULL) free(node->content.cmd.redirs[0].specifier); - if (node->content.cmd.redirs[1].type != 0 && node->content.cmd.redirs[0].specifier != NULL) + if (node->content.cmd.redirs[1].type != 0 + && node->content.cmd.redirs[0].specifier != NULL) free(node->content.cmd.redirs[1].specifier); } diff --git a/src/free_token.c b/src/free_token.c index 3c8ce1a..9b035ac 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -6,7 +6,7 @@ /* By: dkaiser next != NULL) { diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 8ed9785..068a4c1 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,18 +6,13 @@ /* By: dkaiser next != NULL) - { - if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - { - if (cur->content.redir_type & (INPUT_FILE | INPUT_LIMITER)) - idx = 0; - else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) - idx = 1; - set_redir(&result[idx], cur->content.redir_type, - cur->next->content.string); - cur = cur->next; - free_token_and_connect(cur->previous); - if (cur->next != NULL) - { - if (cur->previous == NULL) - *tokens = cur->next; - 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) -{ - t_token *cur; - t_assign **result; - int i; - - 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) - { - i++; - cur = cur->next; - } - result = malloc(sizeof(t_assign *) * (i + 1)); - if (!result) - { - // free all tokens - return (NULL); - } - 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(cur->content.string); - i++; - if (cur->next != NULL) - { - cur = cur->next; - free_token(cur->previous); - } - else - { - free_token(cur); - } - } - *tokens = cur; - result[i] = NULL; - return (result); -} - -static t_assign *to_assign(char *str) -{ - t_assign *result; - char *split_pos; - - split_pos = ft_strchr(str, '='); - *split_pos = '\0'; - result = malloc(sizeof(t_assign)); - if (result == NULL) - { - return (NULL); - } - result->var = str; - result->value = split_pos + 1; - return (result); -} - static char **collect_args(t_token **tokens) { t_token *cur; @@ -159,26 +40,17 @@ static char **collect_args(t_token **tokens) cur = cur->next; } result = malloc(sizeof(char *) * (i + 1)); - if (!result) - { - // free all tokens; - return (NULL); - } + if (result == NULL) + return (free_tokens(*tokens), NULL); cur = *tokens; i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { result[i] = cur->content.string; - // free token i++; cur = cur->next; + free_token(cur->previous); } result[i] = NULL; return (result); } - -static void set_redir(t_redirection *redir, int type, char *specifier) -{ - redir->type = type; - redir->specifier = specifier; -} diff --git a/src/parser.c b/src/parser.c index 66c6785..f9484c9 100644 --- a/src/parser.c +++ b/src/parser.c @@ -6,7 +6,7 @@ /* By: dkaiser next; } if (current == NULL) - { - // Free: ft_lstclear(&result, free_node); - return (NULL); - } + return (ft_lstclear(&result, free_node_wrapper), NULL); current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); } return (result); @@ -91,3 +88,8 @@ static t_token *find_token_by_type(t_token *tokens, int type) } return (NULL); } + +static void free_node_wrapper(void *node) +{ + free_node((t_node *)node); +} -- cgit v1.2.3