aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/collect_redirs.c85
-rw-r--r--src/free_node.c23
-rw-r--r--src/free_token.c23
-rw-r--r--src/new_node.c15
-rw-r--r--src/parse_cmd.c52
-rw-r--r--src/parser.c95
-rw-r--r--src/print_ast.c57
-rw-r--r--src/tokenizer.c4
8 files changed, 323 insertions, 31 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
new file mode 100644
index 0000000..d50bec4
--- /dev/null
+++ b/src/collect_redirs.c
@@ -0,0 +1,85 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* collect_redirs.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
+/* Updated: 2024/08/11 12:12:16 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+static t_token *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)
+ cur = 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 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);
+ 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);
+ return (NULL);
+ }
+ return (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..6eae059 100644
--- a/src/free_node.c
+++ b/src/free_node.c
@@ -6,21 +6,18 @@
/* 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/11 12:26:20 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);
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 +28,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 +39,16 @@ 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);
+ 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);
}
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..6da9f9e 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/08/11 12:22:45 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;
@@ -57,6 +45,7 @@ t_node *new_cmd_node(char **args, t_redirection redirs[2])
node->content.cmd.args = args;
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..2755cae
--- /dev/null
+++ b/src/parse_cmd.c
@@ -0,0 +1,52 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* parse_cmd.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
+/* Updated: 2024/08/11 12:20:06 by dkaiser ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "minishell.h"
+
+static char **collect_args(t_token **tokens);
+
+t_node *parse_cmd(t_token *tokens)
+{
+ char **args;
+ t_redirection *redirs;
+
+ redirs = collect_redirs(&tokens);
+ args = collect_args(&tokens);
+ return (new_cmd_node(args, 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)
+ {
+ if (cur->previous)
+ free_token(cur->previous);
+ result[i] = cur->content.string;
+ i++;
+ cur = cur->next;
+ }
+ 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..a511246
--- /dev/null
+++ b/src/print_ast.c
@@ -0,0 +1,57 @@
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* print_ast.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/07/22 15:16:53 by dkaiser #+# #+# */
+/* Updated: 2024/08/11 12:26:00 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++;
+ }
+ 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);
+}
diff --git a/src/tokenizer.c b/src/tokenizer.c
index a9a86ca..ab8a6ac 100644
--- a/src/tokenizer.c
+++ b/src/tokenizer.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/28 20:55:50 by chuhlig #+# #+# */
-/* Updated: 2024/08/11 14:52:54 by chuhlig ### ########.fr */
+/* Updated: 2024/08/29 15:26:55 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
@@ -111,4 +111,6 @@ void tokenizer(char *s, t_token **token_list, char quote_check)
pos = i + 1;
}
}
+ while ((*token_list)->previous)
+ *token_list = (*token_list)->previous;
}