aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorDominik Kaiser2025-01-23 12:48:05 +0100
committerDominik Kaiser2025-01-23 12:48:05 +0100
commit202a5a26d1c0411c441cf12be2b22318abc4f3f9 (patch)
tree1d234e7b217707b84fa796e3162d8d542c6ae125 /src
parentd6b4b8116084f7aec25c101a82fc457ac9989033 (diff)
downloadminishell-202a5a26d1c0411c441cf12be2b22318abc4f3f9.tar.gz
minishell-202a5a26d1c0411c441cf12be2b22318abc4f3f9.zip
Fix leak
TODO: Fix segfault
Diffstat (limited to 'src')
-rw-r--r--src/repl.c36
1 files changed, 35 insertions, 1 deletions
diff --git a/src/repl.c b/src/repl.c
index 859dde3..3afb2a3 100644
--- a/src/repl.c
+++ b/src/repl.c
@@ -6,13 +6,44 @@
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/24 16:07:04 by dkaiser #+# #+# */
-/* Updated: 2025/01/22 15:22:00 by chuhlig ### ########.fr */
+/* Updated: 2025/01/23 12:42:57 by dkaiser ### ########.fr */
/* */
/* ************************************************************************** */
#include "../include/minishell.h"
#include "token.h"
+t_token *shallow_copy_token(t_token *token)
+{
+ if (token == NULL)
+ return (NULL);
+
+ if (token->type == STRING_TOKEN)
+ return (new_str_token(token->content.string, NULL, NULL));
+ else if (token->type == REDIR_TOKEN)
+ return (new_redir_token(token->content.redir_type, NULL, NULL));
+ else
+ return (new_redir_token(token->type, NULL, NULL));
+}
+
+t_token *shallow_copy_tokens(t_token *tokens)
+{
+ t_token *result;
+ t_token *cur;
+
+ result = shallow_copy_token(tokens);
+ if (!result)
+ return (NULL);
+ cur = result;
+ while (tokens->next != NULL)
+ {
+ tokens = tokens->next;
+ cur->next = shallow_copy_tokens(tokens);
+ cur = cur->next;
+ }
+ return (result);
+}
+
void free_repl(char *input, t_node *ast)
{
free(input);
@@ -25,6 +56,7 @@ void repl(const char *prompt, t_env **env, int *promptflag)
char *input;
t_token *token_list;
t_node *ast;
+ t_token *tokens_copy;
(*promptflag)++;
while (1)
@@ -42,9 +74,11 @@ void repl(const char *prompt, t_env **env, int *promptflag)
add_history(input);
token_list = NULL;
tokenizer(input, &token_list, '\0');
+ tokens_copy = shallow_copy_tokens(token_list);
ast = parse(token_list, env);
if (ast)
set_return_code(eval(ast, env), env);
free_repl(input, ast);
+ free_tokens(tokens_copy);
}
}