folders and makefile
This commit is contained in:
103
srcs/parsing/ast/utils/cutll.c
Normal file
103
srcs/parsing/ast/utils/cutll.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cutll.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/31 09:56:34 by lderidde #+# #+# */
|
||||
/* Updated: 2025/02/07 17:56:37 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../../includes/minishell.h"
|
||||
|
||||
static void add_nodell(t_nodell **nodell, t_node *node)
|
||||
{
|
||||
t_nodell *tmp;
|
||||
|
||||
if (!nodell || !(*nodell))
|
||||
{
|
||||
*nodell = malloc(sizeof(t_nodell));
|
||||
(*nodell)->node = node;
|
||||
(*nodell)->next = NULL;
|
||||
return ;
|
||||
}
|
||||
tmp = *nodell;
|
||||
while (tmp->next)
|
||||
tmp = tmp->next;
|
||||
tmp->next = malloc(sizeof(t_nodell));
|
||||
tmp->next->node = node;
|
||||
tmp->next->next = NULL;
|
||||
}
|
||||
|
||||
static void update_subsh_l(int *shlvl, t_node *lst)
|
||||
{
|
||||
if (!lst)
|
||||
return;
|
||||
if (!ft_strncmp(")", lst->val, 1))
|
||||
*shlvl = 0;
|
||||
if (!ft_strncmp("(", lst->val, 1))
|
||||
*shlvl = 1;
|
||||
}
|
||||
|
||||
static t_node *get_node(t_node **lst, t_node *expected, int limiter, int *shlvl)
|
||||
{
|
||||
t_node *node;
|
||||
|
||||
node = NULL;
|
||||
while (limiter != -1 && (*lst) && (*lst) != expected)
|
||||
{
|
||||
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
|
||||
(*lst) = (*lst)->next;
|
||||
}
|
||||
while (limiter == -1 && (*lst) && ( *shlvl == 1 || ft_strncmp((*lst)->val, expected->val, ft_strlen((*lst)->val))))
|
||||
{
|
||||
update_subsh_l(shlvl, *lst);
|
||||
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
|
||||
(*lst) = (*lst)->next;
|
||||
}
|
||||
return (node);
|
||||
}
|
||||
|
||||
t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter)
|
||||
{
|
||||
t_nodell *out;
|
||||
t_node *node;
|
||||
size_t i;
|
||||
t_nodell *tmp;
|
||||
int shlvl;
|
||||
|
||||
i = 0;
|
||||
out = NULL;
|
||||
shlvl = 0;
|
||||
while (i <= limiter)
|
||||
{
|
||||
update_subsh_l(&shlvl, lst);
|
||||
node = get_node(&lst, expected, limiter, &shlvl);
|
||||
if (!node)
|
||||
break ;
|
||||
add_nodell(&out, node);
|
||||
tmp = out;
|
||||
while (tmp)
|
||||
tmp = tmp->next;
|
||||
if (!lst)
|
||||
break;
|
||||
lst = lst->next;
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
|
||||
void free_lltab(t_nodell *nodell)
|
||||
{
|
||||
t_nodell *tmp;
|
||||
|
||||
while (nodell)
|
||||
{
|
||||
free_linked_list(nodell->node);
|
||||
tmp = nodell->next;
|
||||
free(nodell);
|
||||
nodell = tmp;
|
||||
}
|
||||
}
|
||||
62
srcs/parsing/ast/utils/free_ast.c
Normal file
62
srcs/parsing/ast/utils/free_ast.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* free_ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 14:39:28 by lderidde #+# #+# */
|
||||
/* Updated: 2025/02/07 17:57:21 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);
|
||||
}
|
||||
|
||||
static void free_cmd(t_ast_n *node)
|
||||
{
|
||||
ft_free(&node->cmd);
|
||||
free_redirs(node);
|
||||
free_tab(node->args);
|
||||
}
|
||||
|
||||
static void free_pline(t_ast_n *node)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = -1;
|
||||
while (node->pline[++i])
|
||||
free_ast(node->pline[i]);
|
||||
free(node->pline);
|
||||
}
|
||||
|
||||
void free_ast(t_ast_n *node)
|
||||
{
|
||||
if (node->state == _AND || node->state == _OR)
|
||||
{
|
||||
free_ast(node->left);
|
||||
free_ast(node->right);
|
||||
}
|
||||
else if (node->state == _SUBSH)
|
||||
{
|
||||
free_ast(node->left);
|
||||
free_redirs(node);
|
||||
}
|
||||
else if (node->state == _PLINE)
|
||||
free_pline(node);
|
||||
else
|
||||
free_cmd(node);
|
||||
free(node);
|
||||
}
|
||||
|
||||
void free_child(t_msh *msh)
|
||||
{
|
||||
free_ast(msh->head);
|
||||
free_msh(msh);
|
||||
}
|
||||
103
srcs/parsing/ast/utils/redirections.c
Normal file
103
srcs/parsing/ast/utils/redirections.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* redirections.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/05 07:38:26 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/07 17:57:42 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../../includes/minishell.h"
|
||||
|
||||
t_redir get_redir(t_node *node)
|
||||
{
|
||||
if (!node)
|
||||
return (_NR);
|
||||
else if (!ft_strncmp(node->val, ">>", 2))
|
||||
return (_RED_DR);
|
||||
else if (!ft_strncmp(node->val, ">", 1))
|
||||
return (_RED_R);
|
||||
else if (!ft_strncmp(node->val, "<<", 2))
|
||||
return (_RED_DL);
|
||||
else if (!ft_strncmp(node->val, "<", 1))
|
||||
return (_RED_L);
|
||||
return (_NR);
|
||||
}
|
||||
|
||||
static void add_redir(t_redir redir, t_redir **redir_list)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
t_redir *tmp;
|
||||
|
||||
i = 0;
|
||||
while ((*redir_list)[i] != _NR)
|
||||
i++;
|
||||
tmp = ft_calloc(i + 2, sizeof(t_redir));
|
||||
j = -1;
|
||||
while ((*redir_list)[++j] != _NR)
|
||||
tmp[j] = (*redir_list)[j];
|
||||
tmp[j++] = redir;
|
||||
tmp[j] = _NR;
|
||||
free(*redir_list);
|
||||
*redir_list = tmp;
|
||||
}
|
||||
|
||||
void create_redir(t_node *head, t_ast_n *self)
|
||||
{
|
||||
t_redir redir;
|
||||
|
||||
while (head)
|
||||
{
|
||||
while (head && get_redir(head) == _NR)
|
||||
head = head->next;
|
||||
if (!head)
|
||||
break ;
|
||||
redir = get_redir(head);
|
||||
add_redir(redir, &self->redir);
|
||||
add_to_tab(&self->files, head->next->val);
|
||||
head = head->next;
|
||||
while (head && head->next && get_redir(head) == _NR)
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
|
||||
int in_parenthesis(t_node *head)
|
||||
{
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
while (head)
|
||||
{
|
||||
if (head->pressision == SUBSH_S)
|
||||
count += 1;
|
||||
if (head->pressision == SUBSH_E)
|
||||
count -= 1;
|
||||
head = head->next;
|
||||
}
|
||||
if (count != 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
void create_redir_subsh(t_node *head, t_ast_n *self)
|
||||
{
|
||||
t_redir redir;
|
||||
|
||||
while (head)
|
||||
{
|
||||
while (head && (get_redir(head) == _NR || in_parenthesis(head)))
|
||||
head = head->next;
|
||||
if (!head)
|
||||
break ;
|
||||
redir = get_redir(head);
|
||||
add_redir(redir, &self->redir);
|
||||
add_to_tab(&self->files, head->next->val);
|
||||
head = head->next;
|
||||
while (head && head->next && get_redir(head) == _NR)
|
||||
head = head->next;
|
||||
}
|
||||
}
|
||||
96
srcs/parsing/ast/utils/top_token.c
Normal file
96
srcs/parsing/ast/utils/top_token.c
Normal file
@@ -0,0 +1,96 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* top_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/31 17:14:26 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/07 17:56:02 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../../includes/minishell.h"
|
||||
#include <string.h>
|
||||
|
||||
static int last_tok_subsh(t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
if ((lst->next == NULL) && !ft_strncmp(lst->val, ")", 1))
|
||||
return (1);
|
||||
lst = lst->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int last_tok_redir(t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
if ((lst->next == NULL
|
||||
|| lst->next->pressision == D_RED_R
|
||||
|| lst->next->pressision == RED_R
|
||||
|| lst->next->pressision == RED_L
|
||||
|| lst->next->pressision == HEREDOC
|
||||
)&& !ft_strncmp(lst->val, ")",
|
||||
1))
|
||||
return (1);
|
||||
lst = lst->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
|
||||
static void skip_parentheses(t_node **lst)
|
||||
{
|
||||
if (!ft_strncmp((*lst)->val, "(", 1))
|
||||
{
|
||||
while ((*lst)->next && ft_strncmp((*lst)->next->val, ")", 1))
|
||||
{
|
||||
if (!ft_strncmp((*lst)->val, "(", 1))
|
||||
skip_parentheses(&(*lst)->next);
|
||||
*lst = (*lst)->next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static t_node *find_token(char *tok, t_node *lst)
|
||||
{
|
||||
while (lst)
|
||||
{
|
||||
skip_parentheses(&lst);
|
||||
if (!ft_strncmp(lst->val, tok, ft_strlen(tok)))
|
||||
return (lst);
|
||||
lst = lst->next;
|
||||
}
|
||||
return (NULL);
|
||||
}
|
||||
|
||||
t_node *get_top_token(t_node *lst, t_state *state)
|
||||
{
|
||||
*state = _SUBSH;
|
||||
if (!ft_strncmp(lst->val, "(", 1) && last_tok_subsh(lst))
|
||||
return (lst);
|
||||
if (find_token("&&", lst))
|
||||
{
|
||||
*state = _AND;
|
||||
return (find_token("&&", lst));
|
||||
}
|
||||
else if (find_token("||", lst))
|
||||
{
|
||||
*state = _OR;
|
||||
return (find_token("||", lst));
|
||||
}
|
||||
else if (find_token("|", lst))
|
||||
{
|
||||
*state = _PLINE;
|
||||
return (find_token("|", lst));
|
||||
}
|
||||
else if (!ft_strncmp(lst->val, "(", 1) && last_tok_redir(lst))
|
||||
return (lst);
|
||||
else
|
||||
{
|
||||
*state = UNDEF;
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user