gros merge
This commit is contained in:
231
srcs/parsing/ast/ast.c
Normal file
231
srcs/parsing/ast/ast.c
Normal file
@@ -0,0 +1,231 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 08:22:16 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/01 11:13:58 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
// ===================================================================
|
||||
|
||||
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent);
|
||||
|
||||
// ====================================================================
|
||||
|
||||
|
||||
void create_and_or(t_ast_n *parrent, t_node *lst, t_node *token, t_msh *msh)
|
||||
{
|
||||
t_nodell *nodell;
|
||||
|
||||
nodell = cutll(lst, token, 1);
|
||||
parrent->left = create_ast_n(nodell->node, parrent, msh);
|
||||
parrent->right = create_ast_n(nodell->next->node, parrent, msh);
|
||||
// free_lltab(sublsts);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ===================================================================
|
||||
|
||||
|
||||
|
||||
t_redir is_redir(t_node *node)
|
||||
{
|
||||
if (!node)
|
||||
return (_NR);
|
||||
if (!ft_strncmp(node->val, ">", 1))
|
||||
return (_RED_R);
|
||||
if (!ft_strncmp(node->val, ">>", 1))
|
||||
return (_RED_DR);
|
||||
if (!ft_strncmp(node->val, "<", 1))
|
||||
return (_RED_L);
|
||||
return (_NR);
|
||||
}
|
||||
|
||||
char **lltotab(t_node *lst, t_node *limiter)
|
||||
{
|
||||
char **out;
|
||||
int count;
|
||||
t_node *cpy;
|
||||
|
||||
count = 0;
|
||||
cpy = lst;
|
||||
while (cpy && cpy != limiter)
|
||||
{
|
||||
count++;
|
||||
cpy = cpy->next;
|
||||
}
|
||||
out = ft_calloc(count + 1, sizeof(char *));
|
||||
if (!out)
|
||||
return (NULL);
|
||||
cpy = lst;
|
||||
count = 0;
|
||||
while (cpy && cpy != limiter)
|
||||
{
|
||||
out[count] = ft_strdup(cpy->val);
|
||||
count++;
|
||||
cpy = cpy->next;
|
||||
}
|
||||
out[count] = NULL;
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_node *get_redir_node(t_node *head)
|
||||
{
|
||||
t_node *cpy;
|
||||
|
||||
cpy = head;
|
||||
while (cpy && !is_redir(cpy))
|
||||
cpy = cpy->next;
|
||||
return (cpy);
|
||||
}
|
||||
|
||||
void create_cmd(t_ast_n *self, t_node *lst)
|
||||
{
|
||||
t_node *sec_redir_node;
|
||||
|
||||
self->state = _CMD;
|
||||
self->infile = NULL;
|
||||
self->outfile = NULL;
|
||||
if (is_redir(lst) != _NR)
|
||||
{
|
||||
self->cmd = ft_strdup(lst->next->next->val);
|
||||
self->args = lltotab(lst->next->next, NULL);
|
||||
self->infile = ft_strdup(lst->next->val);
|
||||
}
|
||||
else
|
||||
{
|
||||
self->cmd = ft_strdup(lst->val);
|
||||
self->args = lltotab(lst, get_redir_node(lst->next));
|
||||
}
|
||||
sec_redir_node = get_redir_node(lst->next);
|
||||
if (lst->next && sec_redir_node)
|
||||
{
|
||||
self->outfile = ft_strdup(sec_redir_node->next->val);
|
||||
}
|
||||
self->redir = is_redir(get_redir_node(lst));
|
||||
}
|
||||
|
||||
|
||||
//=====================================================================
|
||||
|
||||
|
||||
|
||||
void create_pline(t_ast_n *self, t_node *lst, t_node *token, t_msh *msh)
|
||||
{
|
||||
t_nodell *nodell;
|
||||
t_nodell *cpy;
|
||||
int count;
|
||||
int i;
|
||||
|
||||
nodell = cutll(lst, token, -1);
|
||||
cpy = nodell;
|
||||
count = 0;
|
||||
while (cpy)
|
||||
{
|
||||
count++;
|
||||
cpy = cpy->next;
|
||||
}
|
||||
self->pline = ft_calloc(count + 1, sizeof(t_ast_n *));
|
||||
cpy = nodell;
|
||||
i = 0;
|
||||
while (cpy)
|
||||
{
|
||||
self->pline[i] = create_ast_n(cpy->node, self, t_msh *msh);
|
||||
cpy = cpy->next;
|
||||
i++;
|
||||
}
|
||||
cpy = NULL;
|
||||
// free_lltab(sublsts);
|
||||
}
|
||||
|
||||
//======================================================================
|
||||
|
||||
|
||||
|
||||
void remove_parentheses(t_node **lst)
|
||||
{
|
||||
t_node *tmp;
|
||||
t_node *cpy;
|
||||
|
||||
if (!lst || !*lst || !(*lst)->next)
|
||||
return;
|
||||
|
||||
tmp = *lst;
|
||||
*lst = (*lst)->next;
|
||||
free(tmp->val);
|
||||
free(tmp);
|
||||
cpy = *lst;
|
||||
while (cpy->next && cpy->next->next)
|
||||
cpy = cpy->next;
|
||||
if (cpy->next)
|
||||
{
|
||||
free(cpy->next->val);
|
||||
free(cpy->next);
|
||||
cpy->next = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void create_subsh(t_ast_n *parent, t_node *lst, t_msh *msh)
|
||||
{
|
||||
t_node *cpy;
|
||||
|
||||
cpy = lst;
|
||||
while (cpy)
|
||||
{
|
||||
ft_printf("%s\n", cpy->val);
|
||||
cpy = cpy->next;
|
||||
}
|
||||
remove_parentheses(&lst);
|
||||
ft_printf("\n\n");
|
||||
cpy = lst;
|
||||
while (cpy)
|
||||
{
|
||||
ft_printf("%s\n", cpy->val);
|
||||
cpy = cpy->next;
|
||||
}
|
||||
parent->left = create_ast_n(lst, parent, msh);
|
||||
}
|
||||
|
||||
|
||||
|
||||
//==================================================================
|
||||
|
||||
|
||||
|
||||
t_ast_n *create_ast_n(t_node *lst, t_ast_n *parent, t_msh *msh)
|
||||
{
|
||||
t_ast_n *node;
|
||||
t_node *token;
|
||||
|
||||
node = malloc(sizeof(t_ast_n));
|
||||
token = get_top_token(lst, &node->state);
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->pline = NULL;
|
||||
node->msh = msh;
|
||||
node->parent = parent;
|
||||
if (node->state == _AND || node->state == _OR)
|
||||
create_and_or(node, lst, token, msh);
|
||||
else if (node->state == _SUBSH)
|
||||
create_subsh(node, lst, msh);
|
||||
else if (node->state == _PLINE)
|
||||
create_pline(node, lst, token, msh);
|
||||
else
|
||||
create_cmd(node, lst);
|
||||
return (node);
|
||||
}
|
||||
|
||||
t_ast_n *get_ast(t_msh *msh, t_node *lst)
|
||||
{
|
||||
t_ast_n *head;
|
||||
|
||||
head = create_ast_n(lst, NULL, msh);
|
||||
return (head);
|
||||
}
|
||||
74
srcs/parsing/ast/cutll.c
Normal file
74
srcs/parsing/ast/cutll.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* cutll.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/31 09:56:34 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/31 14:05:12 by lderidde ### ########.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 t_node *get_node(t_node **lst, t_node *expected, int limiter)
|
||||
{
|
||||
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) && ft_strncmp((*lst)->val, expected->val, ft_strlen((*lst)->val)))
|
||||
{
|
||||
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;
|
||||
|
||||
i = 0;
|
||||
out = NULL;
|
||||
while (i <= limiter)
|
||||
{
|
||||
node = get_node(&lst, expected, limiter);
|
||||
if (!node)
|
||||
break;
|
||||
add_nodell(&out, node);
|
||||
t_nodell *tmp = out;
|
||||
while (tmp)
|
||||
tmp = tmp->next;
|
||||
if(lst && lst->next)
|
||||
lst = lst->next;
|
||||
i++;
|
||||
}
|
||||
return (out);
|
||||
}
|
||||
76
srcs/parsing/ast/top_token.c
Normal file
76
srcs/parsing/ast/top_token.c
Normal file
@@ -0,0 +1,76 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* top_token.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/31 17:14:26 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/31 17:14:26 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.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 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);
|
||||
else 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
|
||||
{
|
||||
*state = UNDEF;
|
||||
return (NULL);
|
||||
}
|
||||
}
|
||||
35
srcs/parsing/drawio/drawio_ast.c
Normal file
35
srcs/parsing/drawio/drawio_ast.c
Normal file
@@ -0,0 +1,35 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* drawio_ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 17:15:39 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/27 17:15:39 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
void set_ast_rect(t_dio_elem *rect)
|
||||
{
|
||||
rect->type = RECT;
|
||||
rect->rounded = 0;
|
||||
rect->x = 50;
|
||||
rect->y = 150;
|
||||
rect->w = 150;
|
||||
rect->h = 120;
|
||||
}
|
||||
|
||||
void gen_dio_ast(t_ast_n *head, int fd)
|
||||
{
|
||||
t_elems e;
|
||||
|
||||
set_ast_rect(&e.rect);
|
||||
e.arrow.type = ARROW;
|
||||
e.arrow.id_src = 0;
|
||||
e.arrow.id_dst = 0;
|
||||
print_ast(head, &e, fd);
|
||||
return ;
|
||||
}
|
||||
75
srcs/parsing/drawio/drawio_ast_utils.c
Normal file
75
srcs/parsing/drawio/drawio_ast_utils.c
Normal file
@@ -0,0 +1,75 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* drawio_ast_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/29 09:21:09 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/29 09:21:09 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
const char *translate_state(t_state state)
|
||||
{
|
||||
const char *out;
|
||||
|
||||
if (state == _AND)
|
||||
out = "CMD_AND";
|
||||
else if (state == _OR)
|
||||
out = "CMD_OR";
|
||||
else if (state == _PLINE)
|
||||
out = "CMD_PIPELINE";
|
||||
else if (state == _CMD)
|
||||
out = "SIMPLE_CMD";
|
||||
else if (state == _SUBSH)
|
||||
out = "SUBSHELL";
|
||||
else
|
||||
out = "UNDEFINED";
|
||||
return (out);
|
||||
}
|
||||
|
||||
const char *translate_redir(t_redir redir)
|
||||
{
|
||||
const char *out;
|
||||
|
||||
if (redir == _RED_L)
|
||||
out = "redir : RED_L ";
|
||||
else if (redir == _RED_R)
|
||||
out = "redir : RED_R ";
|
||||
else if (redir == _RED_DR)
|
||||
out = "redir : _RED_DR ";
|
||||
else
|
||||
out = "Not redirected ";
|
||||
return (out);
|
||||
}
|
||||
|
||||
t_dio_node get_cmd_txt(t_ast_n *node)
|
||||
{
|
||||
t_dio_node txt;
|
||||
char *args;
|
||||
|
||||
txt.st = translate_state(node->state);
|
||||
if (node->state == _CMD)
|
||||
{
|
||||
txt.cmd = ft_sprintf("%s%s", NL, node->cmd);
|
||||
txt.cmd = replace_left_red(txt.cmd);
|
||||
args = ft_tabstr(node->args);
|
||||
txt.args = ft_sprintf("%s%s%s", NL, args, NL);
|
||||
free(args);
|
||||
txt.redir = translate_redir(node->redir);
|
||||
txt.inf = ft_sprintf("Infile : %s%s", node->infile, NL);
|
||||
txt.outf = ft_sprintf("Outfile : %s", node->outfile);
|
||||
}
|
||||
else
|
||||
{
|
||||
txt.cmd = ft_calloc(1, 1);
|
||||
txt.args = ft_calloc(1, 1);
|
||||
txt.redir = "";
|
||||
txt.inf = ft_calloc(1, 1);
|
||||
txt.outf = ft_calloc(1, 1);
|
||||
}
|
||||
return (txt);
|
||||
}
|
||||
50
srcs/parsing/drawio/drawio_linklist.c
Normal file
50
srcs/parsing/drawio/drawio_linklist.c
Normal file
@@ -0,0 +1,50 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* drawio_linklist.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/24 14:41:02 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/27 17:16:53 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
void set_ll_rect(t_dio_elem *rect)
|
||||
{
|
||||
static int y = 50;
|
||||
|
||||
rect->type = RECT;
|
||||
rect->rounded = 0;
|
||||
rect->x = 50;
|
||||
rect->y = y;
|
||||
rect->w = 100;
|
||||
rect->h = 50;
|
||||
y += 100;
|
||||
}
|
||||
|
||||
void gen_dio_linked_list(t_node *head, int fd)
|
||||
{
|
||||
t_dio_elem rect;
|
||||
t_dio_elem arrow;
|
||||
t_node *current;
|
||||
|
||||
current = head;
|
||||
set_ll_rect(&rect);
|
||||
arrow.id_src = 0;
|
||||
arrow.type = ARROW;
|
||||
while (current != NULL)
|
||||
{
|
||||
rect.text = ft_sprintf("%s", current->val);
|
||||
rect.text = replace_ampercent(rect.text);
|
||||
rect.text = replace_left_red(rect.text);
|
||||
arrow.id_dst = drawio_create_elem(fd, &rect);
|
||||
drawio_create_elem(fd, &arrow);
|
||||
arrow.id_src = arrow.id_dst;
|
||||
rect.x += 150;
|
||||
free(rect.text);
|
||||
current = current->next;
|
||||
}
|
||||
}
|
||||
73
srcs/parsing/drawio/drawio_print_ast.c
Normal file
73
srcs/parsing/drawio/drawio_print_ast.c
Normal file
@@ -0,0 +1,73 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* drawio_print_ast.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/29 09:54:31 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/29 09:54:31 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
char *get_node_txt(t_ast_n *node)
|
||||
{
|
||||
t_dio_node txt;
|
||||
char *out;
|
||||
|
||||
txt = get_cmd_txt(node);
|
||||
out = ft_sprintf("%s%s%s%s%s%s%s", txt.st, txt.cmd, txt.args,
|
||||
NL, txt.redir, txt.inf, txt.outf);
|
||||
free(txt.cmd);
|
||||
free(txt.args);
|
||||
free(txt.inf);
|
||||
free(txt.outf);
|
||||
return (out);
|
||||
}
|
||||
|
||||
void draw_bin_part(t_ast_n *node, t_elems *e, int fd, int node_id)
|
||||
{
|
||||
e->rect.y += e->rect.h + 50;
|
||||
e->arrow.id_dst = print_ast(node->left, e, fd);
|
||||
e->arrow.id_src = node_id;
|
||||
drawio_create_elem(fd, &e->arrow);
|
||||
e->rect.x += e->rect.w + 50;
|
||||
e->arrow.id_dst = print_ast(node->right, e, fd);
|
||||
e->arrow.id_src = node_id;
|
||||
drawio_create_elem(fd, &e->arrow);
|
||||
e->rect.y -= e->rect.h + 50;
|
||||
if (node->state == _CMD)
|
||||
e->rect.x -= e->rect.w + 50;
|
||||
}
|
||||
|
||||
void draw_pline_part(t_ast_n *node, t_elems *e, int fd, int node_id)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
e->rect.y += e->rect.h + 50;
|
||||
while (node->pline && node->pline[i])
|
||||
{
|
||||
e->arrow.id_dst = print_ast(node->pline[i++], e, fd);
|
||||
e->arrow.id_src = node_id;
|
||||
drawio_create_elem(fd, &e->arrow);
|
||||
e->rect.x += e->rect.w + 50;
|
||||
}
|
||||
}
|
||||
|
||||
int print_ast(t_ast_n *node, t_elems *e, int fd)
|
||||
{
|
||||
int node_id;
|
||||
|
||||
if (!node)
|
||||
return (-1);
|
||||
e->rect.text = get_node_txt(node);
|
||||
node_id = drawio_create_elem(fd, &e->rect);
|
||||
if (node->state != _PLINE)
|
||||
draw_bin_part(node, e, fd, node_id);
|
||||
else
|
||||
draw_pline_part(node, e, fd, node_id);
|
||||
return (node_id);
|
||||
}
|
||||
92
srcs/parsing/drawio/drawio_utils.c
Normal file
92
srcs/parsing/drawio/drawio_utils.c
Normal file
@@ -0,0 +1,92 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* drawio_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/27 15:09:16 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/27 15:09:16 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
static int get_char_count(char *str, char c)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = 0;
|
||||
count = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (str[i] == c)
|
||||
count++;
|
||||
i++;
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
char *replace_ampercent(char *src)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int amp_count;
|
||||
char *out;
|
||||
|
||||
i = -1;
|
||||
j = 0;
|
||||
amp_count = get_char_count(src, '&');
|
||||
out = malloc(ft_strlen(src) + amp_count * 4 + 1);
|
||||
while (src[++i])
|
||||
{
|
||||
if (src[i] == '&')
|
||||
{
|
||||
out[j] = '&';
|
||||
out[j + 1] = 'a';
|
||||
out[j + 2] = 'm';
|
||||
out[j + 3] = 'p';
|
||||
out[j + 4] = ';';
|
||||
j += 5;
|
||||
}
|
||||
else
|
||||
out[j++] = src[i];
|
||||
}
|
||||
out[j] = 0;
|
||||
free(src);
|
||||
return (out);
|
||||
}
|
||||
|
||||
char *replace_left_red(char *src)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
int redl_count;
|
||||
char *out;
|
||||
|
||||
i = -1;
|
||||
j = 0;
|
||||
redl_count = get_char_count(src, '<');
|
||||
out = malloc(ft_strlen(src) + redl_count * 7 + 1);
|
||||
while (src[++i])
|
||||
{
|
||||
if (src[i] == '<')
|
||||
{
|
||||
out[j] = '&';
|
||||
out[j + 1] = 'a';
|
||||
out[j + 2] = 'm';
|
||||
out[j + 3] = 'p';
|
||||
out[j + 4] = ';';
|
||||
out[j + 5] = 'l';
|
||||
out[j + 6] = 't';
|
||||
out[j + 7] = ';';
|
||||
j += 8;
|
||||
}
|
||||
else
|
||||
out[j++] = src[i];
|
||||
}
|
||||
out[j] = 0;
|
||||
free(src);
|
||||
return (out);
|
||||
}
|
||||
65
srcs/parsing/parser.c
Normal file
65
srcs/parsing/parser.c
Normal file
@@ -0,0 +1,65 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* test.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 08:23:41 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/03 11:49:21 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../includes/minishell.h"
|
||||
|
||||
void truncate_comment(char *str)
|
||||
{
|
||||
int i;
|
||||
char inquote;
|
||||
|
||||
if (!str)
|
||||
return ;
|
||||
i = 0;
|
||||
inquote = 0;
|
||||
while (str[i])
|
||||
{
|
||||
if (!inquote && (str[i] == 39 || str[i] == '"'))
|
||||
inquote = str[i];
|
||||
else if (inquote && (str[i] == inquote))
|
||||
inquote = 0;
|
||||
if (str[i] == '#' && !inquote && str[i - 1] && str[i - 1] != '\\')
|
||||
{
|
||||
str[i] = 0;
|
||||
return ;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
t_ast_n *parser(char *input, t_msh *msh)
|
||||
{
|
||||
t_node *lst;
|
||||
t_ast_n *ast;
|
||||
int dio;
|
||||
|
||||
truncate_comment(input);
|
||||
lst = tokenize(input);
|
||||
if (!lst)
|
||||
return (NULL);
|
||||
if (DEBUG)
|
||||
{
|
||||
dio = drawio_init("ast.xml");
|
||||
gen_dio_linked_list(lst, dio);
|
||||
}
|
||||
ast = get_ast(msh, lst);
|
||||
if (!ast)
|
||||
return (NULL);
|
||||
if (DEBUG)
|
||||
{
|
||||
gen_dio_ast(ast, dio);
|
||||
drawio_end_file(dio);
|
||||
ft_debug(" draw.io file generated !\n");
|
||||
}
|
||||
free_linked_list(lst);
|
||||
return (ast);
|
||||
}
|
||||
93
srcs/parsing/tokenizer/linked_list.c
Normal file
93
srcs/parsing/tokenizer/linked_list.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* linked_list.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 13:38:49 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/31 13:20:13 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
t_node *create_node(char *val, t_token token)
|
||||
{
|
||||
t_node *node;
|
||||
|
||||
if (!val)
|
||||
return (NULL);
|
||||
node = malloc(sizeof(t_node));
|
||||
if (!node)
|
||||
return (NULL);
|
||||
node->val = ft_strdup(val);
|
||||
node->token = token;
|
||||
node->pressision = 0;
|
||||
node->next = NULL;
|
||||
return (node);
|
||||
}
|
||||
|
||||
int add_node_back(t_node **head, char *val, t_token token, t_pres pres)
|
||||
{
|
||||
t_node *tmp;
|
||||
|
||||
tmp = *head;
|
||||
if (!val)
|
||||
return (0);
|
||||
if (!head || !(*head))
|
||||
{
|
||||
(*head) = create_node(val, token);
|
||||
(*head)->pressision = pres;
|
||||
return (1);
|
||||
}
|
||||
while (tmp->next != NULL)
|
||||
tmp = tmp->next;
|
||||
tmp->next = create_node(val, token);
|
||||
tmp->next->pressision = pres;
|
||||
if (tmp->next == NULL)
|
||||
return (0);
|
||||
return (1);
|
||||
}
|
||||
|
||||
void free_linked_list(t_node *head)
|
||||
{
|
||||
t_node *tmp;
|
||||
|
||||
while (head)
|
||||
{
|
||||
tmp = head;
|
||||
head = head->next;
|
||||
free(tmp->val);
|
||||
free(tmp);
|
||||
}
|
||||
}
|
||||
|
||||
int create_node_after(t_node *elem, char *val)
|
||||
{
|
||||
t_node *tmp_next;
|
||||
|
||||
tmp_next = elem->next;
|
||||
elem->next = create_node(val, 0);
|
||||
if (!elem->next)
|
||||
return (0);
|
||||
elem->next->next = tmp_next;
|
||||
return (1);
|
||||
}
|
||||
|
||||
int merge_with_next_node(t_node *node)
|
||||
{
|
||||
char *tmp_val;
|
||||
t_node *tmp_next;
|
||||
|
||||
tmp_val = ft_strjoin(node->val, node->next->val);
|
||||
if (!tmp_val)
|
||||
return (0);
|
||||
ft_free(&node->val);
|
||||
node->val = tmp_val;
|
||||
ft_free(&node->next->val);
|
||||
tmp_next = node->next->next;
|
||||
free(node->next);
|
||||
node->next = tmp_next;
|
||||
return (1);
|
||||
}
|
||||
64
srcs/parsing/tokenizer/token_and_pres.c
Normal file
64
srcs/parsing/tokenizer/token_and_pres.c
Normal file
@@ -0,0 +1,64 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* token_and_pres.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/21 08:54:32 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/21 08:54:32 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
static t_pres get_operator(char *s)
|
||||
{
|
||||
if (s[0] == '&' && s[1] && s[1] == '&')
|
||||
return (AND);
|
||||
else if (s[0] == '|' && s[1] && s[1] == '|')
|
||||
return (OR);
|
||||
else if (s[0] == '|' && !s[1])
|
||||
return (PIPE);
|
||||
else if (s[0] == '(' && !s[1])
|
||||
return (SUBSH_S);
|
||||
else if (s[0] == ')' && !s[1])
|
||||
return (SUBSH_E);
|
||||
else if (s[0] == '<' && s[1] && s[1] == '<')
|
||||
return (HEREDOC);
|
||||
else if (s[0] == '>' && s[1] && s[1] == '>')
|
||||
return (D_RED_R);
|
||||
else if (s[0] == '<' && !s[1])
|
||||
return (RED_L);
|
||||
else if (s[0] == '>' && !s[1])
|
||||
return (RED_R);
|
||||
return (UNDEFINED);
|
||||
}
|
||||
|
||||
t_token get_token(char *str)
|
||||
{
|
||||
t_token token;
|
||||
|
||||
if (!ft_strncmp(str, "&", 1) || !ft_strncmp(str, "|", 1)
|
||||
|| !ft_strncmp(str, "(", 1) || !ft_strncmp(str, ")", 1)
|
||||
|| !ft_strncmp(str, "<", 1) || !ft_strncmp(str, ">", 1))
|
||||
token = OPERATOR;
|
||||
else
|
||||
token = WORD;
|
||||
return (token);
|
||||
}
|
||||
|
||||
t_pres get_pressision(char *s, t_token token,
|
||||
t_token last_token, t_pres last_pres)
|
||||
{
|
||||
if (token == OPERATOR)
|
||||
return (get_operator(s));
|
||||
else if (last_token == OPERATOR && (last_pres == RED_R
|
||||
|| last_pres == RED_L || last_pres == D_RED_R))
|
||||
return (RED_FILE);
|
||||
else if (last_token == OPERATOR && last_pres == HEREDOC)
|
||||
return (LIM);
|
||||
else if (last_token == OPERATOR || last_token == UNSET)
|
||||
return (COMMAND);
|
||||
return (PARAMETER);
|
||||
}
|
||||
120
srcs/parsing/tokenizer/tokenizer.c
Normal file
120
srcs/parsing/tokenizer/tokenizer.c
Normal file
@@ -0,0 +1,120 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tokenizer.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lderidde <lderidde@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/15 13:27:57 by lderidde #+# #+# */
|
||||
/* Updated: 2025/01/31 13:24:28 by lderidde ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
static t_node *tokenize_base(char *str)
|
||||
{
|
||||
int i;
|
||||
t_node *head;
|
||||
char **tab;
|
||||
|
||||
tab = ft_split_keep(str, " \t\n");
|
||||
if (!tab)
|
||||
return (NULL);
|
||||
head = NULL;
|
||||
i = 0;
|
||||
while (tab[i])
|
||||
{
|
||||
if (!add_node_back(&head, tab[i], 0, 0))
|
||||
return (free(tab), NULL);
|
||||
i++;
|
||||
}
|
||||
free_tab(tab);
|
||||
return (head);
|
||||
}
|
||||
|
||||
static void set_token(t_node *head)
|
||||
{
|
||||
t_node *it;
|
||||
t_token last_token;
|
||||
t_pres last_pres;
|
||||
|
||||
it = head;
|
||||
last_token = UNSET;
|
||||
last_pres = UNDEFINED;
|
||||
while (it != NULL)
|
||||
{
|
||||
it->token = get_token(it->val);
|
||||
it->pressision = get_pressision(it->val, it->token, last_token, last_pres);
|
||||
last_token = it->token;
|
||||
last_pres = it->pressision;
|
||||
it = it->next;
|
||||
}
|
||||
}
|
||||
|
||||
static int unstick_nodes(t_node *head)
|
||||
{
|
||||
t_node *it;
|
||||
char *first_str;
|
||||
char *second_str;
|
||||
int copied;
|
||||
|
||||
it = head;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (is_sticked(it->val))
|
||||
{
|
||||
if (is_meta(it->val[0]))
|
||||
first_str = copy_meta_xor(it->val, &copied, 0);
|
||||
else
|
||||
first_str = copy_meta_xor(it->val, &copied, 1);
|
||||
second_str = ft_substr(it->val, copied, ft_strlen(it->val)
|
||||
- copied);
|
||||
ft_free(&it->val);
|
||||
it->val = ft_strdup(first_str);
|
||||
create_node_after(it, second_str);
|
||||
ft_free(&first_str);
|
||||
ft_free(&second_str);
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
static int stick_quote_node(t_node *head, char q)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = head;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (it->val[0] == q && !ft_strchr(&it->val[1], q)
|
||||
&& find_quote_node(it->next, q))
|
||||
{
|
||||
while (it->next->val[0] != q)
|
||||
if (!merge_with_next_node(it))
|
||||
return (0);
|
||||
if (!merge_with_next_node(it))
|
||||
return (0);
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
t_node *tokenize(char *str)
|
||||
{
|
||||
t_node *head;
|
||||
|
||||
head = tokenize_base(str);
|
||||
if (!head)
|
||||
return (NULL);
|
||||
if (!trim_nodes(head))
|
||||
return (NULL);
|
||||
if (!unstick_nodes(head))
|
||||
return (NULL);
|
||||
stick_quote_node(head, 39);
|
||||
stick_quote_node(head, '"');
|
||||
set_token(head);
|
||||
return (head);
|
||||
}
|
||||
101
srcs/parsing/tokenizer/tokenizer_utils.c
Normal file
101
srcs/parsing/tokenizer/tokenizer_utils.c
Normal file
@@ -0,0 +1,101 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* tokenizer_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/01/22 14:24:05 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/01/22 14:24:05 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
int is_meta(char c)
|
||||
{
|
||||
if (c == '&' || c == '|' || c == '<' || c == '>' || c == '(' || c == ')'
|
||||
|| c == '"' || c == 39)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int is_sticked(char *val)
|
||||
{
|
||||
int i;
|
||||
int meta;
|
||||
int unmeta;
|
||||
|
||||
i = 0;
|
||||
meta = 0;
|
||||
unmeta = 0;
|
||||
while (val[i])
|
||||
{
|
||||
if (is_meta(val[i]))
|
||||
meta = 1;
|
||||
if (!is_meta(val[i]))
|
||||
unmeta = 1;
|
||||
i++;
|
||||
}
|
||||
if (meta && unmeta)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
||||
|
||||
int ft_str_count(char *s, char c)
|
||||
{
|
||||
int i;
|
||||
int count;
|
||||
|
||||
i = -1;
|
||||
count = 0;
|
||||
while (s[++i])
|
||||
if (s[i] == c)
|
||||
count++;
|
||||
return (count);
|
||||
}
|
||||
|
||||
int trim_nodes(t_node *head)
|
||||
{
|
||||
t_node *it;
|
||||
char *tmp;
|
||||
char in_quote;
|
||||
|
||||
it = head;
|
||||
in_quote = 0;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (ft_str_count(it->val, 39) == 1
|
||||
|| ft_str_count(it->val, '"') == 1)
|
||||
{
|
||||
if (!in_quote)
|
||||
in_quote = it->val[0];
|
||||
else if (it->val[0] == in_quote)
|
||||
in_quote = 0;
|
||||
}
|
||||
if (!in_quote)
|
||||
{
|
||||
tmp = ft_strtrim(it->val, " \t\n");
|
||||
if (!tmp)
|
||||
return (0);
|
||||
free(it->val);
|
||||
it->val = tmp;
|
||||
}
|
||||
it = it->next;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int find_quote_node(t_node *head, char q)
|
||||
{
|
||||
t_node *it;
|
||||
|
||||
it = head;
|
||||
while (it != NULL)
|
||||
{
|
||||
if (it->val[0] == q)
|
||||
return (1);
|
||||
it = it->next;
|
||||
}
|
||||
return (0);
|
||||
}
|
||||
31
srcs/parsing/tokenizer/unstick_node_utils.c
Normal file
31
srcs/parsing/tokenizer/unstick_node_utils.c
Normal file
@@ -0,0 +1,31 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* unstick_node_utils.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2025/02/03 12:53:08 by nalebrun #+# #+# */
|
||||
/* Updated: 2025/02/03 12:53:08 by nalebrun ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "../../../includes/minishell.h"
|
||||
|
||||
char *copy_meta_xor(char *val, int *copied, int rev)
|
||||
{
|
||||
int i;
|
||||
int j;
|
||||
char *out;
|
||||
|
||||
i = 0;
|
||||
while (is_meta(val[i]) ^ rev)
|
||||
i++;
|
||||
*copied = i;
|
||||
out = malloc(i + 1);
|
||||
j = -1;
|
||||
while (++j < i)
|
||||
out[j] = val[j];
|
||||
out[i] = 0;
|
||||
return (out);
|
||||
}
|
||||
Reference in New Issue
Block a user