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 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'Makefile') 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)) -- 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 'Makefile') 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 f53acc629b1b3e7f4097eef1e26841ef0b9b24c6 Mon Sep 17 00:00:00 2001 From: Dominik Kaiser Date: Sun, 11 Aug 2024 12:15:40 +0200 Subject: Fix parser bugs --- Makefile | 2 +- src/collect_redirs.c | 13 +++++++++---- src/parse_cmd.c | 10 ++++------ src/tokenizer.c | 4 +++- 4 files changed, 17 insertions(+), 12 deletions(-) (limited to 'Makefile') diff --git a/Makefile b/Makefile index 3450e3c..d4778bf 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 print_ast.c + parse_cmd.c collect_assigns.c collect_redirs.c print_ast.c OBJ_DIR := _obj OBJ := $(addprefix $(OBJ_DIR)/, $(SRC:%.c=%.o)) diff --git a/src/collect_redirs.c b/src/collect_redirs.c index 76b08da..d50bec4 100644 --- a/src/collect_redirs.c +++ b/src/collect_redirs.c @@ -6,13 +6,13 @@ /* By: dkaiser next != NULL) { if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN) - collect_redir(tokens, result, cur); + cur = collect_redir(tokens, result, cur); else if (cur->type == REDIR_TOKEN) { dbg("TODO: Add parsing errmsg"); @@ -43,7 +43,8 @@ t_redirection *collect_redirs(t_token **tokens) return (result); } -static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur) +static t_token *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); @@ -57,7 +58,11 @@ static void collect_redir(t_token **tokens, t_redirection *result, t_token *cur) free_token_and_connect(cur->previous); } else + { free_token(cur); + return (NULL); + } + return (cur); } static void set_redir(t_redirection *redir, int type, char *specifier) diff --git a/src/parse_cmd.c b/src/parse_cmd.c index 068a4c1..eb70f0d 100644 --- a/src/parse_cmd.c +++ b/src/parse_cmd.c @@ -6,7 +6,7 @@ /* By: dkaiser next; - } result = malloc(sizeof(char *) * (i + 1)); if (result == NULL) return (free_tokens(*tokens), NULL); @@ -46,10 +43,11 @@ static char **collect_args(t_token **tokens) i = 0; while (cur != NULL && cur->type == STRING_TOKEN) { + if (cur->previous) + free_token(cur->previous); result[i] = cur->content.string; i++; cur = cur->next; - free_token(cur->previous); } result[i] = NULL; return (result); diff --git a/src/tokenizer.c b/src/tokenizer.c index 34685ac..efbf723 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 11:40:35 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -110,4 +110,6 @@ void tokenizer(char *s, t_token **token_list) pos = i + 1; } } + while ((*token_list)->previous) + *token_list = (*token_list)->previous; } -- 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 'Makefile') 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