diff options
| -rw-r--r-- | Makefile | 3 | ||||
| -rw-r--r-- | include/ast.h | 26 | ||||
| -rw-r--r-- | include/minishell.h | 14 | ||||
| -rw-r--r-- | include/token.h | 14 | ||||
| -rw-r--r-- | src/collect_assigns.c | 84 | ||||
| -rw-r--r-- | src/collect_redirs.c | 80 | ||||
| -rw-r--r-- | src/free_node.c | 41 | ||||
| -rw-r--r-- | src/free_token.c | 23 | ||||
| -rw-r--r-- | src/new_node.c | 18 | ||||
| -rw-r--r-- | src/parse_cmd.c | 56 | ||||
| -rw-r--r-- | src/parser.c | 95 | ||||
| -rw-r--r-- | src/print_ast.c | 65 |
12 files changed, 462 insertions, 57 deletions
@@ -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 tokenizer.c + free_token.c new_node.c free_node.c tokenizer.c parser.c \ + parse_cmd.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 e6ad25d..bf19083 100644 --- a/include/ast.h +++ b/include/ast.h @@ -6,21 +6,15 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:48:27 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 14:56:55 by dkaiser ### ########.fr */ +/* Updated: 2024/07/10 12:31:39 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ -#include "stdlib.h" #include "debug_tools.h" - -typedef struct s_sequence -{ - struct s_node **nodes; -} t_sequence; +#include "stdlib.h" enum e_node_type { - ASSIGN_NODE, PIPE_NODE, CMD_NODE, STRING_NODE @@ -40,10 +34,10 @@ typedef struct s_pipe enum e_redirection_type { - INPUT_FILE, - INPUT_LIMITER, - OUTPUT_OVERRIDE, - OUTPUT_APPEND + INPUT_FILE = 1, + INPUT_LIMITER = 2, + OUTPUT_OVERRIDE = 4, + OUTPUT_APPEND = 8 }; typedef struct s_redirection @@ -55,12 +49,12 @@ typedef struct s_redirection typedef struct s_cmd { char **args; + struct s_assign **assigns; struct s_redirection redirs[2]; } t_cmd; union u_node_content { - struct s_assign assign; struct s_pipe pipe; struct s_cmd cmd; char *string; @@ -73,7 +67,9 @@ typedef struct s_node } t_node; t_node *new_node(int type); -t_node *new_assign_node(char *var, char *value); t_node *new_pipe_node(t_node *left, t_node *right); -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 *new_string_node(char *string); + +void free_node(t_node *node); diff --git a/include/minishell.h b/include/minishell.h index c108d93..4f04682 100644 --- a/include/minishell.h +++ b/include/minishell.h @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/22 17:14:49 by dkaiser #+# #+# */ -/* Updated: 2024/08/08 17:10:12 by dkaiser ### ########.fr */ +/* Updated: 2024/08/11 10:59:16 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,9 +26,15 @@ # include <termios.h> # include <unistd.h> -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_redirection *collect_redirs(t_token **tokens); +t_assign **collect_assigns(t_token **tokens); + +void print_ast(t_node *ast); #endif diff --git a/include/token.h b/include/token.h index d7ff9f9..49d51e1 100644 --- a/include/token.h +++ b/include/token.h @@ -6,7 +6,7 @@ /* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 13:27:18 by dkaiser #+# #+# */ -/* Updated: 2024/08/05 13:23:27 by chuhlig ### ########.fr */ +/* Updated: 2024/08/11 11:00:22 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,10 +17,10 @@ enum e_token_type { - STRING_TOKEN, - PIPE_TOKEN, - REDIR_TOKEN, - NEWLINE_TOKEN + STRING_TOKEN = 1, + PIPE_TOKEN = 2, + REDIR_TOKEN = 4, + NEWLINE_TOKEN = 8 }; union u_token_content @@ -45,6 +45,8 @@ t_token *new_redir_token(int type, t_token *previous, t_token *next); void free_token(t_token *token); +void free_token_and_connect(t_token *token); +void free_tokens(t_token *tokens); void tokenizer(char *s, t_token **token_list); -#endif
\ No newline at end of file +#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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 13:54:36 by dkaiser #+# #+# */ +/* Updated: 2024/08/02 14:37:41 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" +#include "token.h" + +static t_assign *to_assign(char *str); +static int count_tokens(t_token *tokens); +static int is_quote(char c); + +t_assign **collect_assigns(t_token **tokens) +{ + t_token *cur; + t_assign **result; + int i; + + result = malloc(sizeof(t_assign *) * (count_tokens(*tokens) + 1)); + if (result == NULL) + return (free_tokens(*tokens), NULL); + cur = *tokens; + i = 0; + while (cur != NULL && cur->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 <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */ +/* Updated: 2024/08/02 15:21:04 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void collect_redir(t_token **tokens, t_redirection *result, + t_token *cur); +static void set_redir(t_redirection *redir, int type, char *specifier); +static int is_output_redir(int i); + +t_redirection *collect_redirs(t_token **tokens) +{ + t_redirection *result; + t_token *cur; + + cur = *tokens; + result = malloc(sizeof(t_redirection) * 2); + if (result == NULL) + return (free_tokens(*tokens), NULL); + set_redir(&result[0], 0, NULL); + set_redir(&result[1], 0, NULL); + while (cur != NULL && cur->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 8f32c12..f387c0a 100644 --- a/src/free_node.c +++ b/src/free_node.c @@ -6,21 +6,19 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:41:46 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 14:55:50 by dkaiser ### ########.fr */ +/* Updated: 2024/08/02 13:28:47 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ #include "ast.h" -static void free_assign_node(t_node *node); static void free_pipe_node(t_node *node); static void free_cmd_node(t_node *node); +static void free_assigns(t_assign **assigns); void free_node(t_node *node) { - if (node->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); @@ -31,12 +29,6 @@ void free_node(t_node *node) free(node); } -static void free_assign_node(t_node *node) -{ - free(node->content.assign.var); - free(node->content.assign.value); -} - static void free_pipe_node(t_node *node) { free_node(node->content.pipe.left); @@ -48,12 +40,33 @@ static void free_cmd_node(t_node *node) int i; i = 0; - while (node->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(node->content.cmd.redirs[0].specifier); - free(node->content.cmd.redirs[1].specifier); + 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); + 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) +{ + 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/free_token.c b/src/free_token.c index 9f5c4e9..9b035ac 100644 --- a/src/free_token.c +++ b/src/free_token.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 14:38:57 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 14:55:12 by dkaiser ### ########.fr */ +/* Updated: 2024/08/02 14:23:56 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,11 +14,28 @@ void free_token(t_token *token) { - if (token->type == STRING_TOKEN) - free(token->content.string); if (token->previous != NULL) token->previous->next = NULL; if (token->next != NULL) token->next->previous = NULL; free(token); } + +void free_token_and_connect(t_token *token) +{ + if (token->previous != NULL) + token->previous->next = token->next; + if (token->next != NULL) + token->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); +} diff --git a/src/new_node.c b/src/new_node.c index 4cdbf9a..c2458d0 100644 --- a/src/new_node.c +++ b/src/new_node.c @@ -6,7 +6,7 @@ /* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/06/27 11:21:03 by dkaiser #+# #+# */ -/* Updated: 2024/06/28 15:04:15 by dkaiser ### ########.fr */ +/* Updated: 2024/07/22 14:25:03 by dkaiser ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,18 +23,6 @@ t_node *new_node(int type) return (node); } -t_node *new_assign_node(char *var, char *value) -{ - t_node *node; - - node = new_node(ASSIGN_NODE); - if (node == NULL) - return (NULL); - node->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,8 +43,10 @@ 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]; + free(redirs); return (node); } diff --git a/src/parse_cmd.c b/src/parse_cmd.c new file mode 100644 index 0000000..068a4c1 --- /dev/null +++ b/src/parse_cmd.c @@ -0,0 +1,56 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parse_cmd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */ +/* Updated: 2024/08/02 14:22:32 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static char **collect_args(t_token **tokens); + +t_node *parse_cmd(t_token *tokens) +{ + char **args; + t_assign **assigns; + t_redirection *redirs; + + redirs = collect_redirs(&tokens); + assigns = collect_assigns(&tokens); + args = collect_args(&tokens); + return (new_cmd_node(args, assigns, redirs)); +} + +static char **collect_args(t_token **tokens) +{ + t_token *cur; + char **result; + int i; + + cur = *tokens; + i = 0; + while (cur != NULL) + { + i++; + cur = cur->next; + } + result = malloc(sizeof(char *) * (i + 1)); + if (result == NULL) + return (free_tokens(*tokens), NULL); + cur = *tokens; + i = 0; + while (cur != NULL && cur->type == STRING_TOKEN) + { + result[i] = cur->content.string; + i++; + cur = cur->next; + free_token(cur->previous); + } + result[i] = NULL; + return (result); +} diff --git a/src/parser.c b/src/parser.c new file mode 100644 index 0000000..f9484c9 --- /dev/null +++ b/src/parser.c @@ -0,0 +1,95 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* parser.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */ +/* Updated: 2024/08/02 13:48:09 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static t_token *find_token_by_type(t_token *tokens, int type); +t_token *split_at_first(t_token **tokens, int type); +static t_node *parse_statement(t_token *tokens); +static void free_node_wrapper(void *node); + +t_list *parse(t_token *tokens) +{ + t_list *result; + t_list *current; + t_token *current_tokens; + + result = NULL; + current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); + while (current_tokens != NULL) + { + if (result == NULL) + { + current = ft_lstnew(parse_statement(current_tokens)); + result = current; + } + else + { + current->next = ft_lstnew(parse_statement(current_tokens)); + current = current->next; + } + if (current == NULL) + return (ft_lstclear(&result, free_node_wrapper), NULL); + current_tokens = split_at_first(&tokens, NEWLINE_TOKEN); + } + return (result); +} + +static t_node *parse_statement(t_token *tokens) +{ + t_token *left_side_tokens; + + left_side_tokens = split_at_first(&tokens, PIPE_TOKEN); + if (tokens != NULL) + { + return (new_pipe_node(parse_cmd(left_side_tokens), + parse_statement(tokens))); + } + else + { + return (parse_cmd(left_side_tokens)); + } +} + +t_token *split_at_first(t_token **tokens, int type) +{ + t_token *split; + t_token *result; + + split = find_token_by_type(*tokens, type); + if (split == NULL) + { + result = *tokens; + *tokens = NULL; + return (result); + } + result = *tokens; + *tokens = split->next; + free_token(split); + return (result); +} + +static t_token *find_token_by_type(t_token *tokens, int type) +{ + while (tokens != NULL) + { + if (tokens->type == type) + return (tokens); + tokens = tokens->next; + } + return (NULL); +} + +static void free_node_wrapper(void *node) +{ + free_node((t_node *)node); +} diff --git a/src/print_ast.c b/src/print_ast.c new file mode 100644 index 0000000..94f813f --- /dev/null +++ b/src/print_ast.c @@ -0,0 +1,65 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_ast.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/07/22 15:16:53 by dkaiser #+# #+# */ +/* Updated: 2024/08/02 13:18:48 by dkaiser ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void print_ast_rec(t_node *ast, int indent); +static void print_cmd_node(t_node *ast, int indent); + +void print_ast(t_node *ast) +{ + if (DEBUG) + { + printf("\e[94m[AST]\n"); + print_ast_rec(ast, 0); + printf("\e[0m\n"); + } +} + +static void print_ast_rec(t_node *ast, int indent) +{ + if (ast->type == CMD_NODE) + print_cmd_node(ast, indent); + else if (ast->type == PIPE_NODE) + { + printf("\n%*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 != NULL && 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); +} |
