aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/collect_redirs.c22
-rw-r--r--src/debug_tools.c22
-rw-r--r--src/format_string.c6
-rw-r--r--src/parse_cmd.c99
-rw-r--r--src/parser.c35
-rw-r--r--src/praise_the_norme.c22
-rw-r--r--src/repl.c6
7 files changed, 143 insertions, 69 deletions
diff --git a/src/collect_redirs.c b/src/collect_redirs.c
index d40cee4..8f5dadc 100644
--- a/src/collect_redirs.c
+++ b/src/collect_redirs.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 16:08:25 by chuhlig ### ########.fr */
+/* Updated: 2025/01/23 18:22:04 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,7 +21,7 @@ static int set_heredoc_data(t_token *cur, t_redirection *result,
t_env *env);
t_redirection *collect_redirs(t_token **tokens, t_env *env,
- t_list **create_files)
+ t_list **create_files)// tokes is possition after pipe or first token
{
t_redirection *result;
t_token *cur;
@@ -31,14 +31,15 @@ t_redirection *collect_redirs(t_token **tokens, t_env *env,
result = malloc(sizeof(t_redirection) * 2);
if (result == NULL)
return (free_tokens(*tokens), NULL);
- free(set_redir(&result[0], 0, NULL, env));
- free(set_redir(&result[1], 0, NULL, env));
+ free(set_redir(&result[0], 0, NULL, env));// no token use
+ free(set_redir(&result[1], 0, NULL, env));// no token use
data.create_files = create_files;
data.env = env;
while (cur != NULL)
{
- if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
- collect_and_check_redir(result, &cur, &data, tokens);
+ if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)//could this be a problem with recursion?
+ collect_and_check_redir(result, &cur, &data, tokens);// cur=token is just in there to free and connect dont know if its ok
+ //return or better said does
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
@@ -56,20 +57,21 @@ static void collect_and_check_redir(t_redirection *result, t_token **cur,
str = ft_strdup((*cur)->next->content.string);
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
- if (!set_heredoc_data(*cur, result, data->env))
+ if (!set_heredoc_data(*cur, result, data->env))// set here doc data with token head or first token after pipe
return ;
}
else if ((*cur)->content.redir_type == INPUT_FILE)
q4fc(data->create_files, set_redir(&result[0], INPUT_FILE,
- format_string(str, data->env, 0), data->env));
+ format_string(str, data->env, 0), data->env));//here toke list should be not messed up
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE,
format_string(str, data->env, 0), data->env));
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND,
format_string(str, data->env, 0), data->env));
- i_love_the_norme(cur, tokens);
-}
+ i_love_the_norme(cur, tokens);// takes adress of token head or pos after pipe and the token specifer
+}// i love the norm simple does free token and connect
+//later more detailes
static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
t_env *env)
diff --git a/src/debug_tools.c b/src/debug_tools.c
index 6bee1b0..2c8ee7e 100644
--- a/src/debug_tools.c
+++ b/src/debug_tools.c
@@ -6,13 +6,15 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 15:34:14 by dkaiser #+# #+# */
-/* Updated: 2025/01/20 12:50:36 by chuhlig ### ########.fr */
+/* Updated: 2025/01/23 15:21:11 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "debug_tools.h"
#include <stdio.h>
#include <stdarg.h>
+#include "token.h"
+#include <stdio.h>
void dbg(char *msg)
{
@@ -33,3 +35,21 @@ void panic(char *msg)
ft_putendl_fd("\e[0m", 1);
}
}
+
+
+
+void print_token_list(t_token *token_list) {
+ t_token *current = token_list;
+ while (current != NULL) {
+ if (current->type == STRING_TOKEN) {
+ printf("STRING_TOKEN: %s\n", current->content.string);
+ } else if (current->type == REDIR_TOKEN) {
+ printf("REDIR_TOKEN: %d\n", current->content.redir_type);
+ } else if (current->type == PIPE_TOKEN) {
+ printf("PIPE_TOKEN\n");
+ } else if (current->type == NEWLINE_TOKEN) {
+ printf("NEWLINE_TOKEN\n");
+ }
+ current = current->next;
+ }
+}
diff --git a/src/format_string.c b/src/format_string.c
index ccc7084..06e5210 100644
--- a/src/format_string.c
+++ b/src/format_string.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/12/17 19:30:11 by chuhlig #+# #+# */
-/* Updated: 2025/01/22 14:31:29 by chuhlig ### ########.fr */
+/* Updated: 2025/01/23 14:34:35 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -49,8 +49,8 @@ static void append_slice(char **dst, char *src, int start, int end)
i++;
}
result[len + i] = '\0';
- if (*dst != NULL)
- free(*dst);
+ // if (*dst != NULL)
+ // free(*dst);
*dst = result;
// free(src);
// src = *dst;
diff --git a/src/parse_cmd.c b/src/parse_cmd.c
index 02c4bda..a978c99 100644
--- a/src/parse_cmd.c
+++ b/src/parse_cmd.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/08 15:06:25 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 17:26:14 by dkaiser ### ########.fr */
+/* Updated: 2025/01/23 18:04:50 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,10 +21,16 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
t_list *create_files;
create_files = NULL;
- redirs = collect_redirs(&tokens, *env, &create_files);
+ printf("parse_cmd\n");
+ print_token_list(tokens);
+ redirs = collect_redirs(&tokens, *env, &create_files);// takes pos next to pipe or first token
+ printf("parse_cmdafter collect redir\n");
+ print_token_list(tokens);// still existing tokenlist
if (redirs == NULL)
return (NULL);
args = collect_args(&tokens, env);
+ printf("parse_cmdafter collect args\n");
+ // print_token_list(tokens);//here it gone
if (args == NULL)
{
free(redirs);
@@ -33,33 +39,64 @@ t_node *parse_cmd(t_token *tokens, t_env **env)
return (new_cmd_node(args, redirs, create_files));
}
-static char **collect_args(t_token **tokens, t_env **env)
-{
- t_token *cur;
- char **result;
- int i;
- t_token *next;
+// static char **collect_args(t_token **tokens, t_env **env)
+// {
+// t_token *cur;
+// char **result;
+// int i;
+// t_token *next;
- 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)
- {
- next = cur->next;
- if (cur->previous)
- free_token(cur->previous);
- result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
- i++;
- if (cur->next == NULL)
- free_token(cur);
- cur = next;
- }
- result[i] = NULL;
- return (result);
-}
+// 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)
+// {
+// next = cur->next;
+// if (cur->previous)
+// free_token(cur->previous);
+// result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
+// i++;
+// if (cur->next == NULL)
+// free_token(cur);
+// cur = next;
+// }
+// result[i] = NULL;
+// return (result);
+// }
+
+static char **collect_args(t_token **tokens, t_env **env) {
+ t_token *cur;
+ char **result;
+ int i;
+ t_token *next;
+
+ 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) {
+ next = cur->next;
+ result[i] = format_string(cur->content.string, *env, ft_atoi("0"));
+ i++;
+ if (cur->previous)
+ cur->previous->next = cur->next;
+ if (cur->next)
+ cur->next->previous = cur->previous;
+ free_token(cur);
+ cur = next;
+ }
+ result[i] = NULL;
+ *tokens = cur; // Update the head of the token list
+ return (result);
+}// need to later the rest of the tokenlist
diff --git a/src/parser.c b/src/parser.c
index 2d748c9..68ab372 100644
--- a/src/parser.c
+++ b/src/parser.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/29 15:53:29 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 17:13:50 by dkaiser ### ########.fr */
+/* Updated: 2025/01/23 18:00:18 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -21,7 +21,7 @@ static t_node *parse_statement(t_token *tokens, t_env **env);
t_node *parse(t_token *tokens, t_env **env)
{
- t_node *result;
+ t_node *result;//
if ((*tokens).type == PIPE_TOKEN)
result = NULL;
@@ -29,15 +29,20 @@ t_node *parse(t_token *tokens, t_env **env)
result = parse_statement(tokens, env);
if (result == NULL)
printf("Parsing error.\n");
+ // if (tokens != NULL)
+ // print_token_list(tokens);
return (result);
}
static t_node *parse_statement(t_token *tokens, t_env **env)
{
t_token *left_side_tokens;
-
- left_side_tokens = split_at_first(&tokens, PIPE_TOKEN);
- if (left_side_tokens == NULL)
+
+ print_token_list(tokens);//until her is fine
+ left_side_tokens = split_at_first(&tokens, PIPE_TOKEN);//by pipe usage parse cmd gets reacls so also check there the token list changes
+ //leftside toke has the pos of pipe -> next
+ //or if we have pipe we return token head and also change the *tokens to after pipe
+ if (left_side_tokens == NULL)// we never return NULL exept token is already null
{
free_tokens(tokens);
tokens = NULL;
@@ -46,12 +51,13 @@ static t_node *parse_statement(t_token *tokens, t_env **env)
else if (tokens != NULL)
{
return (new_pipe_node(parse_cmd(left_side_tokens, env),
- parse_statement(tokens, env)));
+ parse_statement(tokens, env)));//here new pipe node
}
else
{
- return (parse_cmd(left_side_tokens, env));
- }
+ print_token_list(left_side_tokens);
+ return (parse_cmd(left_side_tokens, env));//here return is cmd node
+ }// here he takt left side token so
}
t_token *split_at_first(t_token **tokens, int type)
@@ -59,21 +65,24 @@ t_token *split_at_first(t_token **tokens, int type)
t_token *split;
t_token *result;
- split = find_token_by_type(*tokens, type);
+ split = find_token_by_type(*tokens, type);//split has the pos of where pipe appears// if no pipe in tokenlist 1st if case
if (split == NULL)
{
result = *tokens;
- *tokens = NULL;
+ *tokens = NULL;//we are change to pointing token to NULL
return (result);
}
result = *tokens;
- *tokens = split->next;
+ *tokens = split->next;// is this part enought reconnetion
if (result == split)
result = NULL;
- free_token2(split);
+ free_token2(split);//why free here? bv would free pipe node but what is with the connection of the tokenlist does this mess up the connection?
split = NULL;
- return (result);
+ return (result);// at this return return is at tokenlist pos of split next and result is the tokenlist before split
}
+//free token seems not right here even
+// or at least no the right funtion here
+//
static t_token *find_token_by_type(t_token *tokens, int type)
{
diff --git a/src/praise_the_norme.c b/src/praise_the_norme.c
index 6af28c3..5639b7d 100644
--- a/src/praise_the_norme.c
+++ b/src/praise_the_norme.c
@@ -3,10 +3,10 @@
/* ::: :::::::: */
/* praise_the_norme.c :+: :+: :+: */
/* +:+ +:+ +:+ */
-/* By: dkaiser <dkaiser@student.42heilbronn.de +#+ +:+ +#+ */
+/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/01/20 18:35:41 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 17:30:02 by dkaiser ### ########.fr */
+/* Updated: 2025/01/23 18:31:49 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -16,15 +16,19 @@ void i_love_the_norme(t_token **cur, t_token **tokens)
{
t_token *next_token;
- next_token = (*cur)->next;
- free_token_and_connect2(*cur);
- if (next_token)
+ next_token = (*cur)->next;//setting next token to the adress of the next token
+ free_token_and_connect(*cur);// do i neee a double call here
+ //but technically it would remove the redir token
+ if (next_token)// if after redir is stuff
{
- if (next_token->previous == NULL)
- *tokens = next_token->next;
+ if (next_token->previous == NULL)// then if is the first token or token after pipe
+ *tokens = next_token->next;// how does here come no error
+ //anyways i twould the redir adress token to the next token
*cur = next_token->next;
- free_token_and_connect2(next_token);
+ free_token_and_connect(next_token);
}
- else
+ else // else makes sense
*cur = NULL;
}
+
+// takes adress of token head or pos after pipe and the token specifer \ No newline at end of file
diff --git a/src/repl.c b/src/repl.c
index 3afb2a3..3988a42 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -6,7 +6,7 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/23 12:42:57 by dkaiser ### ########.fr */
+/* Updated: 2025/01/23 17:42:33 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
@@ -75,10 +75,12 @@ void repl(const char *prompt, t_env **env, int *promptflag)
token_list = NULL;
tokenizer(input, &token_list, '\0');
tokens_copy = shallow_copy_tokens(token_list);
+ free_tokens(tokens_copy);
+ // print_token_list(token_list);
ast = parse(token_list, env);
if (ast)
set_return_code(eval(ast, env), env);
free_repl(input, ast);
- free_tokens(tokens_copy);
+ print_token_list(tokens_copy);
}
}