free start

This commit is contained in:
gazhonsepaskwa
2025-02-05 15:50:47 +01:00
parent 2369795bd2
commit 8b3d4868dd
6 changed files with 57 additions and 5 deletions

View File

@@ -13,7 +13,7 @@
#ifndef MINISHELL_H #ifndef MINISHELL_H
# define MINISHELL_H # define MINISHELL_H
# define DEBUG 1 # define DEBUG 0
typedef struct s_ast_n t_ast_n; typedef struct s_ast_n t_ast_n;
typedef struct s_node t_node; typedef struct s_node t_node;

View File

@@ -80,4 +80,7 @@ void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh);
// and_or // and_or
void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh); void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh);
// free
void free_ast(t_ast_n *node);
#endif #endif

View File

@@ -43,12 +43,10 @@ static char **add_space_to_tab(char **tab, int count)
void add_to_tab(char ***tab, char *str) void add_to_tab(char ***tab, char *str)
{ {
char **tmp;
int i; int i;
if (!str) if (!str)
return ; return ;
tmp = *tab;
i = 0; i = 0;
if (*tab) if (*tab)
{ {
@@ -60,8 +58,6 @@ void add_to_tab(char ***tab, char *str)
{ {
*tab = malloc(sizeof(char *) * 2); *tab = malloc(sizeof(char *) * 2);
} }
if (tmp)
free(tmp);
(*tab)[i] = ft_strdup(str); (*tab)[i] = ft_strdup(str);
(*tab)[i + 1] = NULL; (*tab)[i + 1] = NULL;
} }

View File

@@ -73,6 +73,8 @@ void interpret_cmd(char **input, t_msh *msh)
{ {
msh->head = parser(*input, msh); msh->head = parser(*input, msh);
msh->ex_code = execute_command(msh->head); msh->ex_code = execute_command(msh->head);
free_ast(msh->head);
msh->head = NULL;
free(*input); free(*input);
*input = NULL; *input = NULL;
} }

View File

@@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* free_ast.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/05 14:39:28 by nalebrun #+# #+# */
/* Updated: 2025/02/05 14:39:28 by nalebrun ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../../includes/minishell.h"
static void free_redirs(t_ast_n *node)
{
free(node->redir);
if (node->files)
free_tab(node->files);
free_tab(node->args);
}
void free_ast(t_ast_n *node)
{
int i;
if (node->state == _AND || node->state == _OR)
{
free_ast(node->left);
free_ast(node->right);
}
else if (node->state == _PLINE)
{
i = -1;
while (node->pline[++i])
free_ast(node->pline[i]);
}
else if (node->state == _SUBSH)
{
free_ast(node->left);
free_redirs(node);
}
else
{
free(node->cmd);
free_redirs(node);
}
free(node);
}

View File

@@ -46,4 +46,5 @@ void create_subsh(t_ast_n *self, t_node *lst, t_msh *msh)
self->redir = ft_calloc(1, sizeof(t_redir)); self->redir = ft_calloc(1, sizeof(t_redir));
self->redir[0] = _NR; self->redir[0] = _NR;
create_redir_subsh(lst, self); create_redir_subsh(lst, self);
// free(cutted);
} }