From 42c693f70ff375ce15b5783c251bfa723ac528ce Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 1 Jul 2024 11:44:06 +0200 Subject: Remove unneeded struct s_sequence --- include/ast.h | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) (limited to 'include') diff --git a/include/ast.h b/include/ast.h index e6ad25d..356ccdd 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,18 +6,13 @@ /* 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 5316c9880416b77b4b97b07fd6ae47f171a0ba23 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 8 Jul 2024 16:15:32 +0200 Subject: Move assign into cmd node --- include/ast.h | 8 +++----- src/free_node.c | 24 +++++++++++++++++++----- src/new_node.c | 17 +++-------------- 3 files changed, 25 insertions(+), 24 deletions(-) (limited to 'include') diff --git a/include/ast.h b/include/ast.h index 356ccdd..6479272 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* By: dkaiser type == ASSIGN_NODE) - free_assign_node(node); - else if (node->type == PIPE_NODE) + if (node->type == PIPE_NODE) free_pipe_node(node); else if (node->type == CMD_NODE) free_cmd_node(node); @@ -54,6 +52,22 @@ static void free_cmd_node(t_node *node) i++; } free(node->content.cmd.args); + free_assigns(node->content.cmd.assigns); free(node->content.cmd.redirs[0].specifier); free(node->content.cmd.redirs[1].specifier); } + +static void free_assigns(t_assign **assigns) +{ + int i; + + i = 0; + while (assigns[i] != NULL) + { + free(assigns[i]->var); + free(assigns[i]->value); + free(assigns[i]); + i++; + } + free(assigns); +} diff --git a/src/new_node.c b/src/new_node.c index 4cdbf9a..c334eb6 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.assign.var = var; - node->content.assign.value = value; - return (node); -} - t_node *new_pipe_node(t_node *left, t_node *right) { t_node *node; @@ -47,7 +35,7 @@ t_node *new_pipe_node(t_node *left, t_node *right) return (node); } -t_node *new_cmd_node(char **args, t_redirection redirs[2]) +t_node *new_cmd_node(char **args, t_assign **assigns, t_redirection redirs[2]) { t_node *node; @@ -55,6 +43,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2]) if (node == NULL) return (NULL); node->content.cmd.args = args; + node->content.cmd.assigns = assigns; node->content.cmd.redirs[0] = redirs[0]; node->content.cmd.redirs[1] = redirs[1]; return (node); -- cgit v1.2.3 From 16ca09f57525cd9a4e7eb82ce70e4c1e0cb85a31 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Tue, 9 Jul 2024 12:56:09 +0200 Subject: Remove redundant funcs and add parse funcs to header and Makefile --- Makefile | 2 +- include/minishell.h | 6 +++++- src/free_node.c | 8 +------- src/parser.c | 15 +-------------- 4 files changed, 8 insertions(+), 23 deletions(-) (limited to 'include') diff --git a/Makefile b/Makefile index 41551ec..8896ded 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ - free_token.c new_node.c free_node.c + free_token.c new_node.c free_node.c parser.c parse_cmd.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/minishell.h b/include/minishell.h index a528e4a..2411cc5 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser content.assign.var); - free(node->content.assign.value); -} - static void free_pipe_node(t_node *node) { free_node(node->content.pipe.left); diff --git a/src/parser.c b/src/parser.c index 5bf82f6..66c6785 100644 --- a/src/parser.c +++ b/src/parser.c @@ -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 47a68b82e0463f5dfd4bd8a9c4b32b4c5fbcb610 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 22 Jul 2024 15:07:59 +0200 Subject: Fix errors and make the parser work --- include/ast.h | 9 ++++++--- src/free_node.c | 13 ++++++++----- src/new_node.c | 3 ++- src/parse_cmd.c | 29 ++++++++++++++++------------- 4 files changed, 32 insertions(+), 22 deletions(-) (limited to 'include') diff --git a/include/ast.h b/include/ast.h index b0ed996..bf19083 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,12 +6,12 @@ /* By: dkaiser content.cmd.args[i] != NULL) + while (node->content.cmd.args != NULL && node->content.cmd.args[i] != NULL) { free(node->content.cmd.args[i]); i++; } free(node->content.cmd.args); free_assigns(node->content.cmd.assigns); - free(node->content.cmd.redirs[0].specifier); - free(node->content.cmd.redirs[1].specifier); + 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) + free(node->content.cmd.redirs[1].specifier); } static void free_assigns(t_assign **assigns) @@ -56,10 +58,11 @@ static void free_assigns(t_assign **assigns) int i; i = 0; + if (assigns == 0) + return ; while (assigns[i] != NULL) { free(assigns[i]->var); - free(assigns[i]->value); free(assigns[i]); i++; } diff --git a/src/new_node.c b/src/new_node.c index c334eb6..c2458d0 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.assigns = assigns; node->content.cmd.redirs[0] = redirs[0]; node->content.cmd.redirs[1] = redirs[1]; + free(redirs); return (node); } diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 4fa2bc2..0173caa 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser 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; - } + idx = 0; else if (cur->content.redir_type & (OUTPUT_APPEND | OUTPUT_OVERRIDE)) - { - result[1].type = cur->content.redir_type; - result[1].specifier = cur->next->content.string; - } + 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) @@ -170,3 +167,9 @@ static char **collect_args(t_token **tokens) result[i] = NULL; return (result); } + +static void set_redir(t_redirection *redir, int type, char *specifier) +{ + redir->type = type; + redir->specifier = specifier; +} -- cgit v1.2.3 From c16b4655f77db2f152625f226dc66ccc922671b4 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Mon, 22 Jul 2024 16:33:08 +0200 Subject: Add print_ast function --- Makefile | 3 ++- include/minishell.h | 4 ++-- src/print_ast.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 src/print_ast.c (limited to 'include') diff --git a/Makefile b/Makefile index 8896ded..a56ada3 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,8 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ - free_token.c new_node.c free_node.c parser.c parse_cmd.c + free_token.c new_node.c free_node.c parser.c parse_cmd.c \ + print_ast.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/minishell.h b/include/minishell.h index 2411cc5..d980d42 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser type == CMD_NODE) + print_cmd_node(ast, indent); + else if (ast->type == PIPE_NODE) + { + printf("%*s%s", indent, "", "* PIPE"); + print_ast_rec(ast->content.pipe.left, indent + 2); + print_ast_rec(ast->content.pipe.right, indent + 2); + } +} + +static void print_cmd_node(t_node *ast, int indent) +{ + int i; + + printf("\n%*s%s", indent, "", "* CMD"); + i = 0; + printf("\n%*sARGS:", indent + 2, ""); + while (ast->content.cmd.args[i] != NULL) + { + printf(" %s", ast->content.cmd.args[i]); + i++; + } + i = 0; + printf("\n%*sASSIGNS:", indent + 2, ""); + while (ast->content.cmd.assigns[i] != NULL) + { + printf(" %s=%s", ast->content.cmd.assigns[i]->var, + ast->content.cmd.assigns[i]->value); + i++; + } + printf("\n%*sREDIRS:", indent + 2, ""); + printf("\n%*sIN: %d %s", indent + 4, "", ast->content.cmd.redirs[0].type, + ast->content.cmd.redirs[0].specifier); + printf("\n%*sOUT: %d %s", indent + 4, "", ast->content.cmd.redirs[1].type, + ast->content.cmd.redirs[1].specifier); +} -- 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 'include') 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 'include') 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 From 99e8655aaf9827c7d5248c7f3d0913fcb1377cfb Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sun, 11 Aug 2024 12:27:02 +0200 Subject: Remove assigns I found out that there's a difference between shell variables and env variables. We don't have to implement shell variables, so I removed all code that handles them. --- Makefile | 2 +- include/ast.h | 12 ++------ include/minishell.h | 3 +- src/collect_assigns.c | 84 --------------------------------------------------- src/free_node.c | 20 +----------- src/new_node.c | 5 ++- src/parse_cmd.c | 6 ++-- src/print_ast.c | 10 +----- 8 files changed, 10 insertions(+), 132 deletions(-) delete mode 100644 src/collect_assigns.c (limited to 'include') diff --git a/Makefile b/Makefile index d4778bf..827c317 100644 --- a/Makefile +++ b/Makefile @@ -13,7 +13,7 @@ HEADERS = -I include -I $(LIB_DIR)/libft VPATH := src SRC := main.c debug_tools.c init.c signal_handling.c repl.c new_token.c \ free_token.c new_node.c free_node.c tokenizer.c parser.c \ - parse_cmd.c collect_assigns.c collect_redirs.c print_ast.c + parse_cmd.c collect_redirs.c print_ast.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/include/ast.h b/include/ast.h index bf19083..cd2f9c9 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,7 +6,7 @@ /* 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/free_node.c b/src/free_node.c index f387c0a..6eae059 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) free(node->content.cmd.redirs[0].specifier); @@ -54,19 +52,3 @@ static void free_cmd_node(t_node *node) && node->content.cmd.redirs[0].specifier != NULL) free(node->content.cmd.redirs[1].specifier); } - -static void free_assigns(t_assign **assigns) -{ - int i; - - i = 0; - if (assigns == 0) - return ; - while (assigns[i] != NULL) - { - free(assigns[i]->var); - free(assigns[i]); - i++; - } - free(assigns); -} diff --git a/src/new_node.c b/src/new_node.c index c2458d0..6da9f9e 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.args = args; - node->content.cmd.assigns = assigns; node->content.cmd.redirs[0] = redirs[0]; node->content.cmd.redirs[1] = redirs[1]; free(redirs); diff --git a/src/parse_cmd.c b/src/parse_cmd.c index eb70f0d..2755cae 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser content.cmd.args[i]); i++; } - i = 0; - printf("\n%*sASSIGNS:", indent + 2, ""); - while (ast->content.cmd.assigns[i] != NULL) - { - printf(" %s=%s", ast->content.cmd.assigns[i]->var, - ast->content.cmd.assigns[i]->value); - i++; - } printf("\n%*sREDIRS:", indent + 2, ""); printf("\n%*sIN: %d %s", indent + 4, "", ast->content.cmd.redirs[0].type, ast->content.cmd.redirs[0].specifier); -- cgit v1.2.3 From 0d4f9e94f6d28a154ae4be3b918bfb014b4fa1e0 Mon Sep 17 00:00:00 2001 From: Christopher Uhlig Date: Sun, 11 Aug 2024 15:15:06 +0200 Subject: fixed |> fixed norm added new function --- include/token.h | 5 +++-- lib/libft/Makefile | 1 + lib/libft/ft_isspace.c | 20 ++++++++++++++++++++ lib/libft/libft.h | 3 ++- src/repl.c | 4 ++-- src/tokenizer.c | 17 +++++++++-------- 6 files changed, 37 insertions(+), 13 deletions(-) create mode 100644 lib/libft/ft_isspace.c (limited to 'include') diff --git a/include/token.h b/include/token.h index d7ff9f9..80ace03 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 13:23:27 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 13:46:22 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -45,6 +45,7 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next); void free_token(t_token *token); -void tokenizer(char *s, t_token **token_list); +void tokenizer(char *s, t_token **token_list, + char quote_check); #endif \ No newline at end of file diff --git a/lib/libft/Makefile b/lib/libft/Makefile index 6951c43..6f2950c 100644 --- a/lib/libft/Makefile +++ b/lib/libft/Makefile @@ -10,6 +10,7 @@ SRC = ft_atoi.c \ ft_isascii.c \ ft_isdigit.c \ ft_isprint.c \ + ft_isspace.c \ ft_itoa.c \ ft_memchr.c \ ft_memcmp.c \ diff --git a/lib/libft/ft_isspace.c b/lib/libft/ft_isspace.c new file mode 100644 index 0000000..63f21fa --- /dev/null +++ b/lib/libft/ft_isspace.c @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_isspace.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/11 13:59:45 by chuhlig #+# #+# */ +/* Updated: 2024/08/11 14:04:23 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +int ft_isspace(char c) +{ + if (c == ' ' || c == '\t') + return (1); + return (0); +} diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 67fbb0f..abb739d 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/10 16:37:54 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 13:55:13 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 14:01:57 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,6 +24,7 @@ int ft_isalpha(int c); int ft_isdigit(int c); int ft_isalnum(int c); int ft_isprint(int c); +int ft_isspace(char c); int ft_isascii(int c); int ft_strlen(const char *str); void *ft_memset(void *b, int c, size_t len); diff --git a/src/repl.c b/src/repl.c index 1fd7be7..fe9faf3 100644 --- a/src/repl.c +++ b/src/repl.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/08/09 15:27:11 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 14:41:29 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,8 +27,8 @@ void repl(const char *prompt) return ; add_history(input); token_list = NULL; - tokenizer(input, &token_list); current = token_list; + tokenizer(input, &token_list, '\0'); while (current != NULL) { next = current->next; diff --git a/src/tokenizer.c b/src/tokenizer.c index 34685ac..a9a86ca 100644 --- a/src/tokenizer.c +++ b/src/tokenizer.c @@ -6,7 +6,7 @@ /* By: chuhlig +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */ -/* Updated: 2024/08/09 15:40:00 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 14:52:54 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -36,7 +36,7 @@ void print_token(t_token *token) } } -void conditional_print(char *string, int start_of_string, int i, +void snap_string_token(char *string, int start_of_string, int i, t_token **token_list) { char *line; @@ -64,7 +64,7 @@ void conditional_print(char *string, int start_of_string, int i, void handle_special_chars(char *s, int *i, int *start, t_token **token_list) { - conditional_print(s, *start, *i - 1, token_list); + snap_string_token(s, *start, *i - 1, token_list); if (s[*i] == '<' && s[*i + 1] == '<') *token_list = new_redir_token(INPUT_LIMITER, *token_list, NULL); else if (s[*i] == '>' && s[*i + 1] == '>') @@ -78,14 +78,15 @@ void handle_special_chars(char *s, int *i, int *start, t_token **token_list) else if (s[*i] == '\n') *token_list = new_token(NEWLINE_TOKEN, *token_list, NULL); print_token(*token_list); - if (s[*i + 1] == '<' || s[*i + 1] == '>') + if (s[*i] == '<' && s[*i + 1] == '<') + (*i)++; + if (s[*i] == '>' && s[*i + 1] == '>') (*i)++; *start = *i + 1; } -void tokenizer(char *s, t_token **token_list) +void tokenizer(char *s, t_token **token_list, char quote_check) { - char quote_check; int pos; int i; int f; @@ -104,9 +105,9 @@ void tokenizer(char *s, t_token **token_list) f = 1; quote_check = s[i]; } - if ((!f && (s[i] == ' ' || s[i] == '\t')) || i == ft_strlen(s) - 1) + if ((!f && (ft_isspace(s[i + 1]))) || i == ft_strlen(s) - 1) { - conditional_print(s, pos, i, token_list); + snap_string_token(s, pos, i, token_list); pos = i + 1; } } -- cgit v1.2.3 From 60adeb49de9458b4e2af0abd1c7b256da0950bc3 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Fri, 13 Sep 2024 16:27:13 +0200 Subject: Fix norme issues for env.h and repl.c TODO: Fix for tokenizer.c as well. This was probably already solved in another branch though. --- include/env.h | 23 ++++++++++++----------- src/repl.c | 4 ++-- 2 files changed, 14 insertions(+), 13 deletions(-) (limited to 'include') diff --git a/include/env.h b/include/env.h index 1ea6f2e..f3d3c75 100644 --- a/include/env.h +++ b/include/env.h @@ -6,18 +6,19 @@ /* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */ -/* Updated: 2024/08/29 15:37:27 by dkaiser ### ########.fr */ +/* Updated: 2024/09/13 16:26:35 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ void repl(const char *prompt) { char *input; t_token *token_list; - t_list *lines; + t_list *lines; while (1) { -- cgit v1.2.3 From ae5512ea0d6d8be833ca3a9b39f93239109f45b4 Mon Sep 17 00:00:00 2001 From: cuhlig Date: Thu, 17 Oct 2024 15:42:11 +0200 Subject: Env (#23) * Add data structure and prototypes for env * added env linked lis c file * fixed normitte and fixed return issue * changed prototypes in header file --------- Co-authored-by: Dominik Kaiser Co-authored-by: Christopher Uhlig --- include/env.h | 12 +++++------- src/env.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) create mode 100644 src/env.c (limited to 'include') diff --git a/include/env.h b/include/env.h index f3d3c75..5a65333 100644 --- a/include/env.h +++ b/include/env.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* env.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: dkaiser +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/08/08 16:53:39 by dkaiser #+# #+# */ -/* Updated: 2024/09/13 16:26:16 by dkaiser ### ########.fr */ +/* Updated: 2024/10/17 15:37:32 by chuhlig ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,8 +17,6 @@ typedef struct s_env struct s_env *next; } t_env; -char *env_get(t_env *env, char *name); -void env_export(t_env *env, char *name, char *value); -void env_unset(t_env *env, char *name); -char **env_to_strlst(t_env *env); -t_env **env_from_strlst(char **lst); +void getenvlst(t_env **env, char **en); +void free_envlst(t_env **env); + diff --git a/src/env.c b/src/env.c new file mode 100644 index 0000000..8105bf4 --- /dev/null +++ b/src/env.c @@ -0,0 +1,50 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* env.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: chuhlig +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/10/17 14:31:07 by chuhlig #+# #+# */ +/* Updated: 2024/10/17 15:18:44 by chuhlig ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "env.h" + +void getenvlst(t_env **env, char **en) +{ + char *tmp; + int i; + t_env *current; + + i = 0; + while (en[i] != NULL) + { + tmp = ft_strchr(en[i], '='); + tmp = '\0'; + current = *env; + current = malloc(sizeof(t_env)); + current->name = ft_strdup(en[i]); + current->value = ft_strdup(tmp + 1); + current->next = *env; + *env = current; + i++; + } +} + +void free_envlst(t_env **env) +{ + t_env *cur; + t_env *new; + + cur = *env; + while (cur) + { + new = cur->next; + free(cur->name); + free(cur->value); + free(cur); + cur = new; + } +} \ No newline at end of file -- cgit v1.2.3