From c5afaf3d27dd18234386f9e2fccd0a2779403f2b Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Thu, 28 Dec 2023 20:19:35 +0100 Subject: [PATCH 01/12] chore: Re-order the function define --- include/utils.h | 59 +++++++++++++++++++++++++++++-------------------- 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/include/utils.h b/include/utils.h index e672c7dd..6b60896f 100644 --- a/include/utils.h +++ b/include/utils.h @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* utils.h :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/17 13:38:17 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:01:02 by ldulling ### ########.fr */ +/* Updated: 2023/12/28 19:59:15 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,27 +14,38 @@ # define UTILS_H # include "defines.h" -bool drop_num_stack(t_list **stack, int num, void (*del)(void *)); -t_list *pop_num_stack(t_list **stack, int num); - -t_token *init_token_node(int type, char *data); -void free_token_node(t_token *token); - -t_ast *init_ast_node(int type, char *data, t_list *children); -void free_ast_node(t_ast *ast); -void free_ast_data(t_ast *ast); - -char *ft_get_token_type_str(int type); - -int get_state_from_stack(t_list *node); -t_token *get_token_from_stack(t_list *node); -t_ast *get_ast_from_stack(t_list *node); - -int get_token_type_from_list(t_list *token_list); -char *get_token_data_from_list(t_list *token_list); -void print_state_stack(t_list *stack); -void print_parse_stack(t_list *stack); - -void print_token_list(t_list *token_list); +/* Token list utils */ +t_token *init_token_node(int type, char *data); +void free_token_node(t_token *token); +int get_token_type_from_list(t_list *token_list); +char *get_token_data_from_list(t_list *token_list); +void print_token_list(t_list *token_list); +t_token *dup_token_node(t_token *token); +t_list *dup_token_list(t_list *token_list); + +/* AST utils */ +t_ast *init_ast_node(int type, char *data, t_list *children); +void free_ast_node(t_ast *ast); +void free_ast_data(t_ast *ast); + +/* Redirect utils */ +t_io_red *init_io_red(void); +void free_io_red(t_io_red *io_red); + +/* Type utils */ +char *ft_get_token_type_str(int type); +bool is_control_op(int token_type); +bool is_identifier(int token_type); +bool is_rule(int token_type); +bool is_io_redirect_op(int token_type); + +/* Stack utils */ +int get_state_from_stack(t_list *node); +t_token *get_token_from_stack(t_list *node); +t_ast *get_ast_from_stack(t_list *node); +void print_state_stack(t_list *stack); +void print_parse_stack(t_list *stack); +bool drop_num_stack(t_list **stack, int num, void (*del)(void *)); +t_list *pop_num_stack(t_list **stack, int num); #endif From 4ef372cfd3bf1c6ee5e5490ec3590bdefa6a66a1 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Thu, 28 Dec 2023 20:20:53 +0100 Subject: [PATCH 02/12] chore: Remove unnecessory header include --- source/utils/ast_utils.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/source/utils/ast_utils.c b/source/utils/ast_utils.c index 1a3a09aa..7ff3ea45 100644 --- a/source/utils/ast_utils.c +++ b/source/utils/ast_utils.c @@ -3,16 +3,14 @@ /* ::: :::::::: */ /* ast_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 19:16:10 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:42:28 by ldulling ### ########.fr */ +/* Updated: 2023/12/28 19:46:25 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ -#include "minishell.h" -#include "parser.h" -#include "utils.h" +#include "defines.h" t_ast *init_ast_node(int type, char *data, t_list *children) { From 37dc22be3088a9ebf966c2e767013322181ac900 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Thu, 28 Dec 2023 20:22:33 +0100 Subject: [PATCH 03/12] refact: Parse from dup token list --- source/frontend/parser/parse.c | 21 ++++++++++------- source/utils/token_utils.c | 42 ++++++++++++++++++++++++++++++++-- 2 files changed, 53 insertions(+), 10 deletions(-) diff --git a/source/frontend/parser/parse.c b/source/frontend/parser/parse.c index c6407de9..8e5a7eb5 100644 --- a/source/frontend/parser/parse.c +++ b/source/frontend/parser/parse.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* parse.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:29:55 by ldulling ### ########.fr */ +/* Updated: 2023/12/28 20:17:37 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,7 +63,6 @@ bool parse( ret = true; while (true) { - print_token_list(*token_list); pt_entry = get_next_pt_entry( get_state_from_stack(*state_stack), get_token_type_from_list(*token_list), @@ -94,18 +93,24 @@ t_ast *extract_ast_from_parse_stack(t_list **parse_stack) return (ast); } -// TODO: Should we free the token_list here? +// TODO: dup the token_list in parse() or init_parse()? bool ft_parse(t_shell *shell) { t_list *state_stack; t_list *parse_stack; + t_list *token_list; - if (!init_parse(&state_stack, &parse_stack)) + token_list = dup_token_list(shell->token_list); + if (!token_list) return (false); - if (!parse(&shell->token_list, &state_stack, &parse_stack)) - return (free_parse(&state_stack, &parse_stack), false); + if (!init_parse(&state_stack, &parse_stack)) + return (ft_lstclear(&token_list, (void *)free_token_node), false); + if (!parse(&token_list, &state_stack, &parse_stack)) + return (ft_lstclear(&token_list, (void *)free_token_node), + free_parse(&state_stack, &parse_stack), false); printf("ACCEPT\n"); - shell->ast = extract_ast_from_parse_stack(&parse_stack); + // create cmd_table list from the token_list + print_token_list(shell->token_list); ft_lstclear(&shell->token_list, (void *)free_token_node); return (free_parse(&state_stack, &parse_stack), true); } diff --git a/source/utils/token_utils.c b/source/utils/token_utils.c index 0f8ad5b3..6bea43d2 100644 --- a/source/utils/token_utils.c +++ b/source/utils/token_utils.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* token_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 22:01:28 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:39:02 by ldulling ### ########.fr */ +/* Updated: 2023/12/28 20:16:45 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -33,6 +33,44 @@ void free_token_node(t_token *token) free(token); } +t_token *dup_token_node(t_token *token) +{ + t_token *dup_token; + char *dup_data; + + dup_data = NULL; + if (token->data) + { + dup_data = ft_strdup(token->data); + if (!dup_data) + return (NULL); + } + dup_token = init_token_node(token->type, dup_data); + if (!dup_token) + return (free(dup_data), NULL); + return (dup_token); +} + +t_list *dup_token_list(t_list *token_list) +{ + t_list *dup_list; + t_token *token; + + dup_list = NULL; + while (token_list) + { + token = dup_token_node(token_list->content); + if (!token) + { + ft_lstclear(&dup_list, (void *)free_token_node); + return (NULL); + } + ft_lstadd_back(&dup_list, ft_lstnew(token)); + token_list = token_list->next; + } + return (dup_list); +} + int get_token_type_from_list(t_list *token_list) { if (!token_list) From d6be95ecd06a57a8dbe3567180117e71a7b4f6b1 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Thu, 28 Dec 2023 20:24:52 +0100 Subject: [PATCH 04/12] chore: Rename core file name of parser module --- source/frontend/parser/{parse.c => parser.c} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename source/frontend/parser/{parse.c => parser.c} (100%) diff --git a/source/frontend/parser/parse.c b/source/frontend/parser/parser.c similarity index 100% rename from source/frontend/parser/parse.c rename to source/frontend/parser/parser.c From 5f84f69e4034d43f47f7a46c691efc2189422a71 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Fri, 29 Dec 2023 13:55:16 +0100 Subject: [PATCH 05/12] chore: Change file name --- source/frontend/parser/parser.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/source/frontend/parser/parser.c b/source/frontend/parser/parser.c index 8e5a7eb5..e18a9a93 100644 --- a/source/frontend/parser/parser.c +++ b/source/frontend/parser/parser.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* parse.c :+: :+: :+: */ +/* parser.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/28 20:17:37 by lyeh ### ########.fr */ +/* Updated: 2023/12/28 22:42:06 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -94,6 +94,7 @@ t_ast *extract_ast_from_parse_stack(t_list **parse_stack) } // TODO: dup the token_list in parse() or init_parse()? +// TODO: Need to refactor the code if AST is not necessary bool ft_parse(t_shell *shell) { t_list *state_stack; From a30a1786471df87638ba9f5fab5eeca167bdd675 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Fri, 29 Dec 2023 21:56:51 +0100 Subject: [PATCH 06/12] feat: Generate command table list and debug utils At this state the command table is not compelete yet, during the execution phase, the executor will finish the command table * Parent process need to... - handle heredoc - handle redirection - handle expansion - open red_out file then assign the fd back to the cmd table --- include/debug.h | 6 +- include/defines.h | 40 +++++-- include/parser.h | 7 +- include/utils.h | 19 +++- source/debug/print_cmd_table.c | 94 ++++++++++++++++ source/frontend/parser/ast.c | 12 -- source/frontend/parser/cmd_table_list.c | 53 +++++++++ source/frontend/parser/cmd_table_symbol.c | 130 ++++++++++++++++++++++ source/frontend/parser/cmd_table_word.c | 79 +++++++++++++ source/frontend/parser/parser.c | 5 +- source/init_shell.c | 7 +- source/main.c | 11 +- source/shell_clean.c | 7 +- source/utils/cmd_table_utils.c | 91 +++++++++++++++ source/utils/io_redirect_utils.c | 36 ++++++ source/utils/token_utils.c | 9 +- source/utils/type_utils.c | 34 +++++- 17 files changed, 596 insertions(+), 44 deletions(-) create mode 100644 source/debug/print_cmd_table.c delete mode 100644 source/frontend/parser/ast.c create mode 100644 source/frontend/parser/cmd_table_list.c create mode 100644 source/frontend/parser/cmd_table_symbol.c create mode 100644 source/frontend/parser/cmd_table_word.c create mode 100644 source/utils/cmd_table_utils.c create mode 100644 source/utils/io_redirect_utils.c diff --git a/include/debug.h b/include/debug.h index 694efbec..3ebcf30a 100644 --- a/include/debug.h +++ b/include/debug.h @@ -6,15 +6,19 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 18:02:03 by lyeh #+# #+# */ -/* Updated: 2023/12/23 20:10:01 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 21:11:11 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ #ifndef DEBUG_H # define DEBUG_H +# include "defines.h" + void ft_show_env_list(t_shell *shell); void ft_show_token_list(t_shell *shell); void print_ast_bfs(t_ast *root); +void print_cmd_table_list(t_list_d *cmd_table_list); +void print_cmd_table(t_cmd_table *cmd_table); #endif \ No newline at end of file diff --git a/include/defines.h b/include/defines.h index 1c4b5448..fdf9a38f 100644 --- a/include/defines.h +++ b/include/defines.h @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 15:56:26 by lyeh #+# #+# */ -/* Updated: 2023/12/23 18:41:18 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 20:17:33 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,6 +107,17 @@ typedef enum e_token_type T_R_BRACKET } t_token_type; +typedef enum e_cmdtable_type +{ + C_NONE = -1, + C_SIMPLE_CMD = 0, + C_PIPE, + C_AND, + C_OR, + C_SUBSHELL_START, + C_SUBSHELL_END +} t_cmdtable_type; + typedef struct s_env { char *key; @@ -144,17 +155,28 @@ typedef struct s_pt_node int num_reduced; } t_pt_node; -typedef struct s_io_redirect +typedef struct s_io_red { - char *redir_in; - char *redir_out; -} t_io_redirect; + int type; + char *in_file; + char *out_file; + char *here_end; + int red_in; + int red_out; +} t_io_red; + +// TODO: Need to record each io_redirect in a list, need to touch the filename +// case: echo 1 | > out1 > out2 cat +// - if cmd_table with no cmd_name, just touch the filename in the io_red_list +// - in other cases, need execute the cmd_table with io redirect typedef struct s_cmd_table { + int type; char *cmd_name; - char *cmd_args; - t_io_redirect *io_redirect; + t_list *cmd_args; + t_list *assignment_list; + t_list *io_red_list; } t_cmd_table; typedef struct s_shell @@ -162,8 +184,8 @@ typedef struct s_shell int exit_code; t_list *env_list; t_list *token_list; - t_list *final_cmd_table; - t_ast *ast; + t_list_d *cmd_table_list; + // t_ast *ast; char *input_line; } t_shell; diff --git a/include/parser.h b/include/parser.h index c3c95c00..0da26c41 100644 --- a/include/parser.h +++ b/include/parser.h @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 19:57:56 by lyeh #+# #+# */ -/* Updated: 2023/12/22 21:25:26 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 21:12:48 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -28,4 +28,9 @@ bool init_parse(t_list **state_stack, t_list **parse_stack); void free_parse(t_list **state_stack, t_list **parse_stack); bool ft_parse(t_shell *shell); +/* Command table */ +t_list_d *build_cmd_table_list(t_list *token_list); +bool handle_symbol_token(t_list **token_list, t_list_d **cmd_table_list); +bool handle_word_token(t_list **token_list, t_list_d **cmd_table_list); + #endif diff --git a/include/utils.h b/include/utils.h index 6b60896f..2316024e 100644 --- a/include/utils.h +++ b/include/utils.h @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/17 13:38:17 by lyeh #+# #+# */ -/* Updated: 2023/12/28 19:59:15 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 20:19:15 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,6 +17,7 @@ /* Token list utils */ t_token *init_token_node(int type, char *data); void free_token_node(t_token *token); +t_token *get_token_from_list(t_list *token_list); int get_token_type_from_list(t_list *token_list); char *get_token_data_from_list(t_list *token_list); void print_token_list(t_list *token_list); @@ -32,12 +33,22 @@ void free_ast_data(t_ast *ast); t_io_red *init_io_red(void); void free_io_red(t_io_red *io_red); +/* Cmd table utils */ +t_cmd_table *init_cmd_table(void); +void free_cmd_table(t_cmd_table *cmd_table); +t_cmd_table *get_last_simple_cmd_table(t_list_d *cmd_table_list); +bool append_empty_cmd_table(t_list_d **cmd_table_list); +bool append_cmd_table_by_scenario( + int token_type, t_list_d **cmd_table_list); + /* Type utils */ char *ft_get_token_type_str(int type); bool is_control_op(int token_type); -bool is_identifier(int token_type); -bool is_rule(int token_type); -bool is_io_redirect_op(int token_type); +bool is_io_red_op(int token_type); +bool is_word(int token_type); +bool is_subshell_symbol(int token_type); +// bool is_identifier(int token_type); +// bool is_rule(int token_type); /* Stack utils */ int get_state_from_stack(t_list *node); diff --git a/source/debug/print_cmd_table.c b/source/debug/print_cmd_table.c new file mode 100644 index 00000000..a7818d6b --- /dev/null +++ b/source/debug/print_cmd_table.c @@ -0,0 +1,94 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_cmd_table.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lyeh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/26 19:46:53 by lyeh #+# #+# */ +/* Updated: 2023/12/29 21:12:08 by lyeh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "defines.h" +#include "utils.h" + +void print_io_red_list(t_list *io_red_list) +{ + t_list *node; + t_io_red *io_red; + + printf("io_red_list:\n"); + printf("-----------------\n"); + node = io_red_list; + while (node) + { + io_red = (t_io_red *)node->content; + printf("\tin_file: %s,\n", io_red->in_file); + printf("\tout_file: %s,\n", io_red->out_file); + printf("\there_end: %s,\n", io_red->here_end); + printf("\tred_in: %d,\n", io_red->red_in); + printf("\tred_out: %d,\n", io_red->red_out); + printf("-----------------\n"); + node = node->next; + } + printf("(NULL)\n"); +} + +void print_cmd_args_list(t_list *cmd_args) +{ + t_list *node; + + printf("cmd_args: "); + node = cmd_args; + while (node && node->content) + { + printf("%s -> ", (char *)node->content); + node = node->next; + } + printf("(NULL)\n"); +} + +void print_assignment_list(t_list *assignment_list) +{ + t_list *node; + + printf("assignment_list: "); + node = assignment_list; + while (node) + { + printf("%s -> ", (char *)node->content); + node = node->next; + } + printf("(NULL)\n"); +} + +void print_cmd_table(t_cmd_table *cmd_table) +{ + printf("type: %d\n", cmd_table->type); + printf("cmd_name: %s\n", cmd_table->cmd_name); + print_cmd_args_list(cmd_table->cmd_args); + print_assignment_list(cmd_table->assignment_list); + print_io_red_list(cmd_table->io_red_list); +} + +void print_cmd_table_list(t_list_d *cmd_table_list) +{ + t_list_d *node; + t_cmd_table *cmd_table; + int i; + + printf("\n\n========= cmd_table_list =========\n"); + node = cmd_table_list; + i = 0; + while (node && node->content) + { + printf("========= %d =========\n", i); + cmd_table = (t_cmd_table *)node->content; + print_cmd_table(cmd_table); + node = node->next; + printf("=====================\n"); + i++; + } + printf("===================================\n\n"); +} diff --git a/source/frontend/parser/ast.c b/source/frontend/parser/ast.c deleted file mode 100644 index 1e99ad90..00000000 --- a/source/frontend/parser/ast.c +++ /dev/null @@ -1,12 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ast.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2023/12/20 21:37:46 by lyeh #+# #+# */ -/* Updated: 2023/12/20 21:37:47 by lyeh ### ########.fr */ -/* */ -/* ************************************************************************** */ - diff --git a/source/frontend/parser/cmd_table_list.c b/source/frontend/parser/cmd_table_list.c new file mode 100644 index 00000000..44c2cf01 --- /dev/null +++ b/source/frontend/parser/cmd_table_list.c @@ -0,0 +1,53 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmd_table_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lyeh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/28 20:25:20 by lyeh #+# #+# */ +/* Updated: 2023/12/29 20:20:34 by lyeh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "parser.h" +#include "utils.h" + +bool handle_current_token(t_list **token_list, t_list_d **cmd_table_list) +{ + int token_type; + + token_type = get_token_type_from_list(*token_list); + if (!append_cmd_table_by_scenario(token_type, cmd_table_list)) + return (false); + if (is_word(token_type)) + { + if (handle_word_token(token_list, cmd_table_list)) + return (true); + } + else if (is_control_op(token_type) || is_io_red_op(token_type) || \ + is_subshell_symbol(token_type)) + { + if (handle_symbol_token(token_list, cmd_table_list)) + return (true); + } + else if (token_type == T_END) + return (true); + return (false); +} + +t_list_d *build_cmd_table_list(t_list *token_list) +{ + t_list_d *cmd_table_list; + + cmd_table_list = NULL; + print_token_list(token_list); + while (token_list) + { + if (!handle_current_token(&token_list, &cmd_table_list)) + return ( + ft_lstclear_d(&cmd_table_list, (void *)free_cmd_table), NULL); + token_list = token_list->next; + } + return (cmd_table_list); +} diff --git a/source/frontend/parser/cmd_table_symbol.c b/source/frontend/parser/cmd_table_symbol.c new file mode 100644 index 00000000..6dc956a1 --- /dev/null +++ b/source/frontend/parser/cmd_table_symbol.c @@ -0,0 +1,130 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmd_table_symbol.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lyeh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/29 14:07:59 by lyeh #+# #+# */ +/* Updated: 2023/12/29 21:40:16 by lyeh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +bool fill_bracket(t_list **token_list, t_list_d **cmd_table_list) +{ + t_token *token; + t_cmd_table *cmd_table; + + token = get_token_from_list(*token_list); + cmd_table = ft_lstlast_d(*cmd_table_list)->content; + if (token->type == T_L_BRACKET) + cmd_table->type = C_SUBSHELL_START; + else if (token->type == T_R_BRACKET) + cmd_table->type = C_SUBSHELL_END; + return (true); +} + +bool fill_red_node(t_io_red *io_red, int type, char *data) +{ + char *dup_data; + + dup_data = ft_strdup(data); + if (!dup_data) + return (false); + if (type == T_RED_IN) + io_red->in_file = dup_data; + else if (type == T_HERE_DOC) + io_red->here_end = dup_data; + else if (type == T_RED_OUT || type == T_APPEND) + io_red->out_file = dup_data; + return (true); +} + +bool fill_redirect(t_list **token_list, t_cmd_table *cmd_table) +{ + t_token *red_op; + t_token *filename; + t_io_red *io_red; + t_list *node; + + red_op = get_token_from_list(*token_list); + filename = get_token_from_list((*token_list)->next); + cmd_table->type = C_SIMPLE_CMD; + io_red = init_io_red(); + if (!io_red) + return (false); + if (!fill_red_node(io_red, red_op->type, filename->data)) + return (free_io_red(io_red), false); + node = ft_lstnew(io_red); + if (!node) + return (free_io_red(io_red), false); + ft_lstadd_back(&cmd_table->io_red_list, node); + return (true); +} + +bool handle_redirect(t_list **token_list, t_list_d **cmd_table_list) +{ + t_cmd_table *cmd_table; + t_list_d *cmd_table_node; + int subshell_cnt; + + cmd_table_node = ft_lstlast_d(*cmd_table_list); + subshell_cnt = 0; + while (cmd_table_node && subshell_cnt >= 0) + { + cmd_table = cmd_table_node->content; + if (cmd_table->type == C_SUBSHELL_END) + subshell_cnt++; + else if (cmd_table->type == C_SUBSHELL_START) + subshell_cnt--; + else if (cmd_table->type == C_SIMPLE_CMD || cmd_table->type == C_NONE) + if (!fill_redirect(token_list, cmd_table)) + return (false); + if (subshell_cnt == 0) + break ; + cmd_table_node = cmd_table_node->prev; + } + (*token_list) = (*token_list)->next; + return (true); +} + +void fill_control_op(t_list **token_list, t_list_d **cmd_table_list) +{ + t_token *token; + t_cmd_table *cmd_table; + + token = get_token_from_list(*token_list); + cmd_table = ft_lstlast_d(*cmd_table_list)->content; + if (token->type == T_AND) + cmd_table->type = C_AND; + else if (token->type == T_OR) + cmd_table->type = C_OR; + else if (token->type == T_PIPE) + cmd_table->type = C_PIPE; +} + +bool handle_symbol_token(t_list **token_list, t_list_d **cmd_table_list) +{ + int token_type; + + token_type = get_token_type_from_list(*token_list); + if (is_io_red_op(token_type)) + { + if (handle_redirect(token_list, cmd_table_list)) + return (true); + } + else if (is_control_op(token_type)) + { + fill_control_op(token_list, cmd_table_list); + return (true); + } + else if (is_subshell_symbol(token_type)) + { + if (!fill_bracket(token_list, cmd_table_list)) + return (false); + return (true); + } + return (false); +} diff --git a/source/frontend/parser/cmd_table_word.c b/source/frontend/parser/cmd_table_word.c new file mode 100644 index 00000000..ac8e3bdc --- /dev/null +++ b/source/frontend/parser/cmd_table_word.c @@ -0,0 +1,79 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmd_table_word.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lyeh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/29 14:07:04 by lyeh #+# #+# */ +/* Updated: 2023/12/29 20:21:47 by lyeh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "utils.h" + +bool fill_cmd_name(t_token *token, t_cmd_table *cmd_table) +{ + char *cmd_name; + + if (cmd_table->cmd_name != NULL) + return (false); + cmd_name = ft_strdup(token->data); + if (!cmd_name) + return (false); + cmd_table->cmd_name = cmd_name; + return (true); +} + +bool fill_cmd_args(t_token *token, t_cmd_table *cmd_table) +{ + char *cmd_arg; + t_list *node; + + cmd_arg = ft_strdup(token->data); + if (!cmd_arg) + return (false); + node = ft_lstnew(cmd_arg); + if (!node) + return (free(cmd_arg), false); + ft_lstadd_back(&cmd_table->cmd_args, node); + return (true); +} + +bool fill_assignment(t_token *token, t_cmd_table *cmd_table) +{ + char *assignment; + t_list *node; + + assignment = ft_strdup(token->data); + if (!assignment) + return (false); + node = ft_lstnew(assignment); + if (!node) + return (free(assignment), false); + ft_lstadd_back(&cmd_table->assignment_list, node); + return (true); +} + +bool handle_word_token(t_list **token_list, t_list_d **cmd_table_list) +{ + t_token *token; + t_cmd_table *cmd_table; + + token = get_token_from_list(*token_list); + cmd_table = get_last_simple_cmd_table(*cmd_table_list); + cmd_table->type = C_SIMPLE_CMD; + if (token->type == T_WORD) + { + if (fill_cmd_name(token, cmd_table)) + return (true); + else if (fill_cmd_args(token, cmd_table)) + return (true); + } + else if (token->type == T_ASSIGNMENT_WORD) + { + if (fill_assignment(token, cmd_table)) + return (true); + } + return (false); +} diff --git a/source/frontend/parser/parser.c b/source/frontend/parser/parser.c index e18a9a93..c51fbc26 100644 --- a/source/frontend/parser/parser.c +++ b/source/frontend/parser/parser.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/28 22:42:06 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 16:07:40 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -110,8 +110,7 @@ bool ft_parse(t_shell *shell) return (ft_lstclear(&token_list, (void *)free_token_node), free_parse(&state_stack, &parse_stack), false); printf("ACCEPT\n"); - // create cmd_table list from the token_list - print_token_list(shell->token_list); + shell->cmd_table_list = build_cmd_table_list(shell->token_list); ft_lstclear(&shell->token_list, (void *)free_token_node); return (free_parse(&state_stack, &parse_stack), true); } diff --git a/source/init_shell.c b/source/init_shell.c index 1be92d22..1aebe6f7 100644 --- a/source/init_shell.c +++ b/source/init_shell.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* init_shell.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 20:06:39 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:35:38 by ldulling ### ########.fr */ +/* Updated: 2023/12/29 21:51:38 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -80,7 +80,8 @@ bool ft_init_shell(t_shell *shell, char **env) shell->exit_code = EXIT_SUCCESS; shell->env_list = NULL; shell->token_list = NULL; - shell->ast = NULL; + // shell->ast = NULL; + shell->cmd_table_list = NULL; shell->input_line = NULL; if (!env && !ft_setup_default_env(shell)) return (false); diff --git a/source/main.c b/source/main.c index 49966b4f..1c4d8f12 100644 --- a/source/main.c +++ b/source/main.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* main.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 16:09:49 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:42:30 by ldulling ### ########.fr */ +/* Updated: 2023/12/29 21:46:10 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,6 +14,7 @@ #include "clean.h" #include "lexer.h" #include "parser.h" +#include "executor.h" #include "debug.h" #include "tests.h" #include "utils.h" @@ -40,10 +41,8 @@ int main(int argc, char **argv, char **env) return (ft_clean_shell(&shell), EXIT_FAILED); if (!ft_parse(&shell)) return (ft_clean_shell(&shell), EXIT_FAILED); - - // Free ast after executor - free_ast_node(shell.ast); - shell.ast = NULL; + print_cmd_table_list(shell.cmd_table_list); + ft_lstclear_d(&shell.cmd_table_list, (void *)free_cmd_table); // ft_clean_shell(&shell); // do executor } diff --git a/source/shell_clean.c b/source/shell_clean.c index 596a36cf..c1911ccd 100644 --- a/source/shell_clean.c +++ b/source/shell_clean.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* shell_clean.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 19:02:15 by lyeh #+# #+# */ -/* Updated: 2023/12/26 19:42:33 by ldulling ### ########.fr */ +/* Updated: 2023/12/29 20:19:32 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,8 @@ void ft_clean_shell(t_shell *shell) { ft_lstclear(&shell->env_list, (void *)free_env_node); ft_lstclear(&shell->token_list, (void *)free_token_node); - free_ast_node(shell->ast); + ft_lstclear_d(&shell->cmd_table_list, (void *)free_cmd_table); + // free_ast_node(shell->ast); ft_free_and_null((void **)&shell->input_line); exit(shell->exit_code); } diff --git a/source/utils/cmd_table_utils.c b/source/utils/cmd_table_utils.c new file mode 100644 index 00000000..aa9deac9 --- /dev/null +++ b/source/utils/cmd_table_utils.c @@ -0,0 +1,91 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* cmd_table_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lyeh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/26 15:16:11 by lyeh #+# #+# */ +/* Updated: 2023/12/29 21:44:17 by lyeh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "defines.h" +#include "utils.h" +#include "debug.h" + +void free_cmd_table(t_cmd_table *cmd_table) +{ + ft_free_and_null((void **)&cmd_table->cmd_name); + ft_lstclear(&cmd_table->cmd_args, free); + ft_lstclear(&cmd_table->assignment_list, free); + ft_lstclear(&cmd_table->io_red_list, (void *)free_io_red); + ft_free_and_null((void **)&cmd_table); +} + +t_cmd_table *init_cmd_table(void) +{ + t_cmd_table *cmd_table; + + cmd_table = (t_cmd_table *)malloc(sizeof(t_cmd_table)); + if (!cmd_table) + return (NULL); + cmd_table->type = C_NONE; + cmd_table->assignment_list = NULL; + cmd_table->cmd_name = NULL; + cmd_table->cmd_args = NULL; + cmd_table->io_red_list = NULL; + return (cmd_table); +} + +bool append_empty_cmd_table(t_list_d **cmd_table_list) +{ + t_list_d *cmd_table_node; + t_cmd_table *cmd_table; + + cmd_table = init_cmd_table(); + if (!cmd_table) + return (false); + cmd_table_node = ft_lstnew_d(cmd_table); + if (!cmd_table_node) + return (free_cmd_table(cmd_table), false); + ft_lstadd_back_d(cmd_table_list, cmd_table_node); + return (true); +} + +t_cmd_table *get_last_simple_cmd_table(t_list_d *cmd_table_list) +{ + t_list_d *node; + t_cmd_table *last_simple_cmd_table; + int cur_type; + + node = cmd_table_list; + last_simple_cmd_table = NULL; + while (node && node->content) + { + cur_type = ((t_cmd_table *)node->content)->type; + if (cur_type == C_PIPE || cur_type == C_AND || cur_type == C_OR) + last_simple_cmd_table = NULL; + if (cur_type == C_SIMPLE_CMD || cur_type == C_NONE) + last_simple_cmd_table = node->content; + node = node->next; + } + return (last_simple_cmd_table); +} + +bool append_cmd_table_by_scenario(int token_type, t_list_d **cmd_table_list) +{ + t_cmd_table *cmd_table; + + if (*cmd_table_list) + { + cmd_table = ft_lstlast_d(*cmd_table_list)->content; + if (token_type == T_END || cmd_table->type == C_NONE) + return (true); + if (get_last_simple_cmd_table(*cmd_table_list) && \ + (is_io_red_op(token_type) || is_word(token_type))) + return (true); + } + printf("[%s] Create new cmd_table\n", ft_get_token_type_str(token_type)); + return (append_empty_cmd_table(cmd_table_list)); +} diff --git a/source/utils/io_redirect_utils.c b/source/utils/io_redirect_utils.c new file mode 100644 index 00000000..1cb0965c --- /dev/null +++ b/source/utils/io_redirect_utils.c @@ -0,0 +1,36 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* io_redirect_utils.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: lyeh +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2023/12/28 19:44:52 by lyeh #+# #+# */ +/* Updated: 2023/12/29 16:15:26 by lyeh ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "defines.h" + +t_io_red *init_io_red(void) +{ + t_io_red *io_red; + + io_red = (t_io_red *)malloc(sizeof(t_io_red)); + if (!io_red) + return (NULL); + io_red->in_file = NULL; + io_red->out_file = NULL; + io_red->here_end = NULL; + io_red->red_in = STDIN_FILENO; + io_red->red_out = STDOUT_FILENO; + return (io_red); +} + +void free_io_red(t_io_red *io_red) +{ + ft_free_and_null((void **)&io_red->in_file); + ft_free_and_null((void **)&io_red->out_file); + ft_free_and_null((void **)&io_red->here_end); + ft_free_and_null((void **)&io_red); +} diff --git a/source/utils/token_utils.c b/source/utils/token_utils.c index 6bea43d2..1660e6d8 100644 --- a/source/utils/token_utils.c +++ b/source/utils/token_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 22:01:28 by lyeh #+# #+# */ -/* Updated: 2023/12/28 20:16:45 by lyeh ### ########.fr */ +/* Updated: 2023/12/28 22:20:52 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -71,6 +71,13 @@ t_list *dup_token_list(t_list *token_list) return (dup_list); } +t_token *get_token_from_list(t_list *token_list) +{ + if (!token_list) + return (NULL); + return ((t_token *)token_list->content); +} + int get_token_type_from_list(t_list *token_list) { if (!token_list) diff --git a/source/utils/type_utils.c b/source/utils/type_utils.c index ad995f6c..c884ef1a 100644 --- a/source/utils/type_utils.c +++ b/source/utils/type_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 19:33:20 by lyeh #+# #+# */ -/* Updated: 2023/12/20 18:21:01 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 17:11:37 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -40,3 +40,35 @@ char *ft_get_token_type_str(int type) return ("END"); return (NULL); } + +bool is_control_op(int token_type) +{ + return (token_type == T_AND || token_type == T_OR || token_type == T_PIPE); +} + +// bool is_identifier(int token_type) +// { +// return (token_type < 100 && token_type >= 0); +// } + + +// bool is_rule(int token_type) +// { +// return (token_type >= 100); +// } + +bool is_word(int token_type) +{ + return (token_type == T_WORD || token_type == T_ASSIGNMENT_WORD); +} + +bool is_io_red_op(int token_type) +{ + return (token_type == T_HERE_DOC || token_type == T_APPEND || \ + token_type == T_RED_IN || token_type == T_RED_OUT); +} + +bool is_subshell_symbol(int token_type) +{ + return (token_type == T_L_BRACKET || token_type == T_R_BRACKET); +} From 5ee3095336636fa6cd1c0adbada3072beaafc5c6 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Fri, 29 Dec 2023 22:19:44 +0100 Subject: [PATCH 07/12] chore: Remove the executor header inculde in this PR --- source/main.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/source/main.c b/source/main.c index 1c4d8f12..18f9dcc5 100644 --- a/source/main.c +++ b/source/main.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/08 16:09:49 by lyeh #+# #+# */ -/* Updated: 2023/12/29 21:46:10 by lyeh ### ########.fr */ +/* Updated: 2023/12/29 22:18:59 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,6 @@ #include "clean.h" #include "lexer.h" #include "parser.h" -#include "executor.h" #include "debug.h" #include "tests.h" #include "utils.h" From 4e7e3b6f07457acc123fd63a6610fd6b37a4f3a1 Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Sat, 30 Dec 2023 15:05:33 +0100 Subject: [PATCH 08/12] fix: Protect memory allocate failed --- source/frontend/parser/parser.c | 5 ++++- source/utils/token_utils.c | 10 ++++++++-- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/source/frontend/parser/parser.c b/source/frontend/parser/parser.c index c51fbc26..109a9102 100644 --- a/source/frontend/parser/parser.c +++ b/source/frontend/parser/parser.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/29 16:07:40 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:02:46 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -111,6 +111,9 @@ bool ft_parse(t_shell *shell) free_parse(&state_stack, &parse_stack), false); printf("ACCEPT\n"); shell->cmd_table_list = build_cmd_table_list(shell->token_list); + if (!shell->cmd_table_list) + return (ft_lstclear(&token_list, (void *)free_token_node), + free_parse(&state_stack, &parse_stack), false); ft_lstclear(&shell->token_list, (void *)free_token_node); return (free_parse(&state_stack, &parse_stack), true); } diff --git a/source/utils/token_utils.c b/source/utils/token_utils.c index 1660e6d8..ebc40981 100644 --- a/source/utils/token_utils.c +++ b/source/utils/token_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 22:01:28 by lyeh #+# #+# */ -/* Updated: 2023/12/28 22:20:52 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:00:57 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -54,6 +54,7 @@ t_token *dup_token_node(t_token *token) t_list *dup_token_list(t_list *token_list) { t_list *dup_list; + t_list *node; t_token *token; dup_list = NULL; @@ -65,7 +66,12 @@ t_list *dup_token_list(t_list *token_list) ft_lstclear(&dup_list, (void *)free_token_node); return (NULL); } - ft_lstadd_back(&dup_list, ft_lstnew(token)); + node = ft_lstnew(token); + if (!node) + return (free_token_node(token), + ft_lstclear(&dup_list, (void *)free_token_node), + NULL); + ft_lstadd_back(&dup_list, node); token_list = token_list->next; } return (dup_list); From 116f3f78c8017aa5db130c8fb646691a00dd071a Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Sat, 30 Dec 2023 15:13:41 +0100 Subject: [PATCH 09/12] fix: Replace unnecessory reset null function call --- source/utils/cmd_table_utils.c | 6 +++--- source/utils/io_redirect_utils.c | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/source/utils/cmd_table_utils.c b/source/utils/cmd_table_utils.c index aa9deac9..b98d9486 100644 --- a/source/utils/cmd_table_utils.c +++ b/source/utils/cmd_table_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/26 15:16:11 by lyeh #+# #+# */ -/* Updated: 2023/12/29 21:44:17 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:12:38 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,11 +16,11 @@ void free_cmd_table(t_cmd_table *cmd_table) { - ft_free_and_null((void **)&cmd_table->cmd_name); + free(cmd_table->cmd_name); ft_lstclear(&cmd_table->cmd_args, free); ft_lstclear(&cmd_table->assignment_list, free); ft_lstclear(&cmd_table->io_red_list, (void *)free_io_red); - ft_free_and_null((void **)&cmd_table); + free(cmd_table); } t_cmd_table *init_cmd_table(void) diff --git a/source/utils/io_redirect_utils.c b/source/utils/io_redirect_utils.c index 1cb0965c..2040111a 100644 --- a/source/utils/io_redirect_utils.c +++ b/source/utils/io_redirect_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/28 19:44:52 by lyeh #+# #+# */ -/* Updated: 2023/12/29 16:15:26 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:11:30 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -32,5 +32,5 @@ void free_io_red(t_io_red *io_red) ft_free_and_null((void **)&io_red->in_file); ft_free_and_null((void **)&io_red->out_file); ft_free_and_null((void **)&io_red->here_end); - ft_free_and_null((void **)&io_red); + free(io_red); } From fdb35c7688168506b1c9e98dde25ece062eca47f Mon Sep 17 00:00:00 2001 From: Lea Yeh Date: Sat, 30 Dec 2023 16:10:22 +0100 Subject: [PATCH 10/12] fix: Fine-tune to make the code more readable --- source/frontend/parser/cmd_table_symbol.c | 10 ++++------ source/frontend/parser/cmd_table_word.c | 17 ++++++----------- source/frontend/parser/parser.c | 10 +++++----- source/utils/cmd_table_utils.c | 12 +++++------- source/utils/io_redirect_utils.c | 8 ++++---- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/source/frontend/parser/cmd_table_symbol.c b/source/frontend/parser/cmd_table_symbol.c index 6dc956a1..83703f0b 100644 --- a/source/frontend/parser/cmd_table_symbol.c +++ b/source/frontend/parser/cmd_table_symbol.c @@ -6,13 +6,13 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/29 14:07:59 by lyeh #+# #+# */ -/* Updated: 2023/12/29 21:40:16 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 16:00:53 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ #include "utils.h" -bool fill_bracket(t_list **token_list, t_list_d **cmd_table_list) +void fill_bracket(t_list **token_list, t_list_d **cmd_table_list) { t_token *token; t_cmd_table *cmd_table; @@ -23,7 +23,6 @@ bool fill_bracket(t_list **token_list, t_list_d **cmd_table_list) cmd_table->type = C_SUBSHELL_START; else if (token->type == T_R_BRACKET) cmd_table->type = C_SUBSHELL_END; - return (true); } bool fill_red_node(t_io_red *io_red, int type, char *data) @@ -72,7 +71,7 @@ bool handle_redirect(t_list **token_list, t_list_d **cmd_table_list) cmd_table_node = ft_lstlast_d(*cmd_table_list); subshell_cnt = 0; - while (cmd_table_node && subshell_cnt >= 0) + while (cmd_table_node) { cmd_table = cmd_table_node->content; if (cmd_table->type == C_SUBSHELL_END) @@ -122,8 +121,7 @@ bool handle_symbol_token(t_list **token_list, t_list_d **cmd_table_list) } else if (is_subshell_symbol(token_type)) { - if (!fill_bracket(token_list, cmd_table_list)) - return (false); + fill_bracket(token_list, cmd_table_list); return (true); } return (false); diff --git a/source/frontend/parser/cmd_table_word.c b/source/frontend/parser/cmd_table_word.c index ac8e3bdc..dc3801ca 100644 --- a/source/frontend/parser/cmd_table_word.c +++ b/source/frontend/parser/cmd_table_word.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/29 14:07:04 by lyeh #+# #+# */ -/* Updated: 2023/12/29 20:21:47 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 16:07:36 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,8 +16,6 @@ bool fill_cmd_name(t_token *token, t_cmd_table *cmd_table) { char *cmd_name; - if (cmd_table->cmd_name != NULL) - return (false); cmd_name = ft_strdup(token->data); if (!cmd_name) return (false); @@ -65,15 +63,12 @@ bool handle_word_token(t_list **token_list, t_list_d **cmd_table_list) cmd_table->type = C_SIMPLE_CMD; if (token->type == T_WORD) { - if (fill_cmd_name(token, cmd_table)) - return (true); - else if (fill_cmd_args(token, cmd_table)) - return (true); + if (cmd_table->cmd_name == NULL) + return (fill_cmd_name(token, cmd_table)); + else + return (fill_cmd_args(token, cmd_table)); } else if (token->type == T_ASSIGNMENT_WORD) - { - if (fill_assignment(token, cmd_table)) - return (true); - } + return (fill_assignment(token, cmd_table)); return (false); } diff --git a/source/frontend/parser/parser.c b/source/frontend/parser/parser.c index 109a9102..f706fac2 100644 --- a/source/frontend/parser/parser.c +++ b/source/frontend/parser/parser.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/30 15:02:46 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:58:17 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -111,9 +111,9 @@ bool ft_parse(t_shell *shell) free_parse(&state_stack, &parse_stack), false); printf("ACCEPT\n"); shell->cmd_table_list = build_cmd_table_list(shell->token_list); - if (!shell->cmd_table_list) - return (ft_lstclear(&token_list, (void *)free_token_node), - free_parse(&state_stack, &parse_stack), false); + free_parse(&state_stack, &parse_stack); ft_lstclear(&shell->token_list, (void *)free_token_node); - return (free_parse(&state_stack, &parse_stack), true); + if (!shell->cmd_table_list) + return (false); + return (true); } diff --git a/source/utils/cmd_table_utils.c b/source/utils/cmd_table_utils.c index b98d9486..69f12f84 100644 --- a/source/utils/cmd_table_utils.c +++ b/source/utils/cmd_table_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/26 15:16:11 by lyeh #+# #+# */ -/* Updated: 2023/12/30 15:12:38 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:55:47 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -55,20 +55,18 @@ bool append_empty_cmd_table(t_list_d **cmd_table_list) t_cmd_table *get_last_simple_cmd_table(t_list_d *cmd_table_list) { - t_list_d *node; t_cmd_table *last_simple_cmd_table; int cur_type; - node = cmd_table_list; last_simple_cmd_table = NULL; - while (node && node->content) + while (cmd_table_list && cmd_table_list->content) { - cur_type = ((t_cmd_table *)node->content)->type; + cur_type = ((t_cmd_table *)cmd_table_list->content)->type; if (cur_type == C_PIPE || cur_type == C_AND || cur_type == C_OR) last_simple_cmd_table = NULL; if (cur_type == C_SIMPLE_CMD || cur_type == C_NONE) - last_simple_cmd_table = node->content; - node = node->next; + last_simple_cmd_table = cmd_table_list->content; + cmd_table_list = cmd_table_list->next; } return (last_simple_cmd_table); } diff --git a/source/utils/io_redirect_utils.c b/source/utils/io_redirect_utils.c index 2040111a..3193bdfc 100644 --- a/source/utils/io_redirect_utils.c +++ b/source/utils/io_redirect_utils.c @@ -6,7 +6,7 @@ /* By: lyeh +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/28 19:44:52 by lyeh #+# #+# */ -/* Updated: 2023/12/30 15:11:30 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 16:02:24 by lyeh ### ########.fr */ /* */ /* ************************************************************************** */ @@ -29,8 +29,8 @@ t_io_red *init_io_red(void) void free_io_red(t_io_red *io_red) { - ft_free_and_null((void **)&io_red->in_file); - ft_free_and_null((void **)&io_red->out_file); - ft_free_and_null((void **)&io_red->here_end); + free(io_red->in_file); + free(io_red->out_file); + free(io_red->here_end); free(io_red); } From 8660cefe603220bfcc9559d090662d85a314dbee Mon Sep 17 00:00:00 2001 From: Lukas <129603980+itislu@users.noreply.github.com> Date: Sat, 30 Dec 2023 15:24:07 +0000 Subject: [PATCH 11/12] fix: Change 2 other ft_free_and_null() to free() --- source/frontend/parser/parser.c | 8 ++++---- source/frontend/parser/parser_operation.c | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/source/frontend/parser/parser.c b/source/frontend/parser/parser.c index f706fac2..b42722c6 100644 --- a/source/frontend/parser/parser.c +++ b/source/frontend/parser/parser.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* parser.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ +/* By: codespace +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/11 17:28:20 by lyeh #+# #+# */ -/* Updated: 2023/12/30 15:58:17 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:20:18 by codespace ### ########.fr */ /* */ /* ************************************************************************** */ @@ -75,9 +75,9 @@ bool parse( ret = false; break ; } - ft_free_and_null((void **)&pt_entry); + free(pt_entry); } - ft_free_and_null((void **)&pt_entry); + free(pt_entry); return (ret); } diff --git a/source/frontend/parser/parser_operation.c b/source/frontend/parser/parser_operation.c index 5d226ab4..91fc78f9 100644 --- a/source/frontend/parser/parser_operation.c +++ b/source/frontend/parser/parser_operation.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* parser_operation.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: ldulling +#+ +:+ +#+ */ +/* By: codespace +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/16 19:52:51 by lyeh #+# #+# */ -/* Updated: 2023/12/26 18:43:08 by ldulling ### ########.fr */ +/* Updated: 2023/12/30 15:22:01 by codespace ### ########.fr */ /* */ /* ************************************************************************** */ @@ -49,7 +49,7 @@ bool parse_shift(t_token *token_node, t_ast *ast_node; ast_node = init_ast_node(token_node->type, token_node->data, NULL); - ft_free_and_null((void **)&token_node); + free(token_node); if (!ast_node) return (false); if (!push_node(parse_stack, ast_node)) From f5e7aec034daf895bc916601cb0f36f222c02dc7 Mon Sep 17 00:00:00 2001 From: Lukas <129603980+itislu@users.noreply.github.com> Date: Sat, 30 Dec 2023 15:40:18 +0000 Subject: [PATCH 12/12] fix: Change if to else if --- source/utils/cmd_table_utils.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/utils/cmd_table_utils.c b/source/utils/cmd_table_utils.c index 69f12f84..ffd0cdb5 100644 --- a/source/utils/cmd_table_utils.c +++ b/source/utils/cmd_table_utils.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* cmd_table_utils.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: lyeh +#+ +:+ +#+ */ +/* By: codespace +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/26 15:16:11 by lyeh #+# #+# */ -/* Updated: 2023/12/30 15:55:47 by lyeh ### ########.fr */ +/* Updated: 2023/12/30 15:28:58 by codespace ### ########.fr */ /* */ /* ************************************************************************** */ @@ -64,7 +64,7 @@ t_cmd_table *get_last_simple_cmd_table(t_list_d *cmd_table_list) cur_type = ((t_cmd_table *)cmd_table_list->content)->type; if (cur_type == C_PIPE || cur_type == C_AND || cur_type == C_OR) last_simple_cmd_table = NULL; - if (cur_type == C_SIMPLE_CMD || cur_type == C_NONE) + else if (cur_type == C_SIMPLE_CMD || cur_type == C_NONE) last_simple_cmd_table = cmd_table_list->content; cmd_table_list = cmd_table_list->next; }