norm
This commit is contained in:
@@ -21,6 +21,12 @@ t_ast_n *parser(char *input, t_msh *msh);
|
|||||||
|
|
||||||
int unexpected_token(t_node *node);
|
int unexpected_token(t_node *node);
|
||||||
int is_aop_operator(t_node *node);
|
int is_aop_operator(t_node *node);
|
||||||
|
int is_redir(t_node *cpy);
|
||||||
|
int is_basic_word(t_node *cpy);
|
||||||
|
int syntax_err_mess(char *token_base, int selected);
|
||||||
|
int check_unclosed(char *c, t_node *node);
|
||||||
|
int check_unclosed_quote(char *c, t_node *node);
|
||||||
|
|
||||||
void interpret_cmd(char **input, t_msh *msh);
|
void interpret_cmd(char **input, t_msh *msh);
|
||||||
void end_heredoc(char *buf, t_msh *msh, t_node *lst);
|
void end_heredoc(char *buf, t_msh *msh, t_node *lst);
|
||||||
void exit_heredoc(char *limiter, t_msh *msh, t_node *lst);
|
void exit_heredoc(char *limiter, t_msh *msh, t_node *lst);
|
||||||
|
|||||||
@@ -31,17 +31,18 @@ static void add_nodell(t_nodell **nodell, t_node *node)
|
|||||||
tmp->next->next = NULL;
|
tmp->next->next = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void update_subsh_l(int *shlvl, t_node *lst)
|
static void update_subsh_l(int *shlvl, t_node *lst)
|
||||||
{
|
{
|
||||||
if (!lst)
|
if (!lst)
|
||||||
return;
|
return ;
|
||||||
if (!ft_strncmp(")", lst->val, 1))
|
if (!ft_strncmp(")", lst->val, 1))
|
||||||
*shlvl = 0;
|
*shlvl = 0;
|
||||||
if (!ft_strncmp("(", lst->val, 1))
|
if (!ft_strncmp("(", lst->val, 1))
|
||||||
*shlvl = 1;
|
*shlvl = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static t_node *get_node(t_node **lst, t_node *expected, int limiter, int *shlvl)
|
static t_node *get_node(t_node **lst, t_node *expected,
|
||||||
|
int limiter, int *shlvl)
|
||||||
{
|
{
|
||||||
t_node *node;
|
t_node *node;
|
||||||
|
|
||||||
@@ -51,9 +52,11 @@ static t_node *get_node(t_node **lst, t_node *expected, int limiter, int *shlvl)
|
|||||||
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
|
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
|
||||||
(*lst) = (*lst)->next;
|
(*lst) = (*lst)->next;
|
||||||
}
|
}
|
||||||
while (limiter == -1 && (*lst) && ( *shlvl == 1 || ft_strncmp((*lst)->val, expected->val, ft_strlen((*lst)->val))))
|
while (limiter == -1 && (*lst)
|
||||||
|
&& (*shlvl == 1
|
||||||
|
|| ft_strncmp((*lst)->val, expected->val, ft_strlen((*lst)->val))))
|
||||||
{
|
{
|
||||||
update_subsh_l(shlvl, *lst);
|
update_subsh_l(shlvl, *lst);
|
||||||
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
|
add_node_back(&node, (*lst)->val, (*lst)->token, (*lst)->pressision);
|
||||||
(*lst) = (*lst)->next;
|
(*lst) = (*lst)->next;
|
||||||
}
|
}
|
||||||
@@ -66,14 +69,14 @@ t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter)
|
|||||||
t_node *node;
|
t_node *node;
|
||||||
size_t i;
|
size_t i;
|
||||||
t_nodell *tmp;
|
t_nodell *tmp;
|
||||||
int shlvl;
|
int shlvl;
|
||||||
|
|
||||||
i = 0;
|
i = 0;
|
||||||
out = NULL;
|
out = NULL;
|
||||||
shlvl = 0;
|
shlvl = 0;
|
||||||
while (i <= limiter)
|
while (i <= limiter)
|
||||||
{
|
{
|
||||||
update_subsh_l(&shlvl, lst);
|
update_subsh_l(&shlvl, lst);
|
||||||
node = get_node(&lst, expected, limiter, &shlvl);
|
node = get_node(&lst, expected, limiter, &shlvl);
|
||||||
if (!node)
|
if (!node)
|
||||||
break ;
|
break ;
|
||||||
@@ -82,7 +85,7 @@ t_nodell *cutll(t_node *lst, t_node *expected, size_t limiter)
|
|||||||
while (tmp)
|
while (tmp)
|
||||||
tmp = tmp->next;
|
tmp = tmp->next;
|
||||||
if (!lst)
|
if (!lst)
|
||||||
break;
|
break ;
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -29,12 +29,11 @@ static int last_tok_redir(t_node *lst)
|
|||||||
while (lst)
|
while (lst)
|
||||||
{
|
{
|
||||||
if ((lst->next == NULL
|
if ((lst->next == NULL
|
||||||
|| lst->next->pressision == D_RED_R
|
|| lst->next->pressision == D_RED_R
|
||||||
|| lst->next->pressision == RED_R
|
|| lst->next->pressision == RED_R
|
||||||
|| lst->next->pressision == RED_L
|
|| lst->next->pressision == RED_L
|
||||||
|| lst->next->pressision == HEREDOC
|
|| lst->next->pressision == HEREDOC)
|
||||||
)&& !ft_strncmp(lst->val, ")",
|
&& !ft_strncmp(lst->val, ")", 1))
|
||||||
1))
|
|
||||||
return (1);
|
return (1);
|
||||||
lst = lst->next;
|
lst = lst->next;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,15 @@ void truncate_comment(char *str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void debug_dio_ast(t_ast_n *ast, int dio)
|
||||||
|
{
|
||||||
|
if (DEBUG)
|
||||||
|
{
|
||||||
|
gen_dio_ast(ast, dio);
|
||||||
|
drawio_end_file(dio);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
t_ast_n *parser(char *input, t_msh *msh)
|
t_ast_n *parser(char *input, t_msh *msh)
|
||||||
{
|
{
|
||||||
t_node *lst;
|
t_node *lst;
|
||||||
@@ -45,7 +54,10 @@ t_ast_n *parser(char *input, t_msh *msh)
|
|||||||
truncate_comment(input);
|
truncate_comment(input);
|
||||||
lst = tokenize(input);
|
lst = tokenize(input);
|
||||||
if (!lst)
|
if (!lst)
|
||||||
|
{
|
||||||
|
msh->ex_code = 2;
|
||||||
return (NULL);
|
return (NULL);
|
||||||
|
}
|
||||||
create_heredoc(lst, msh);
|
create_heredoc(lst, msh);
|
||||||
if (DEBUG)
|
if (DEBUG)
|
||||||
{
|
{
|
||||||
@@ -53,13 +65,9 @@ t_ast_n *parser(char *input, t_msh *msh)
|
|||||||
gen_dio_linked_list(lst, dio);
|
gen_dio_linked_list(lst, dio);
|
||||||
}
|
}
|
||||||
ast = get_ast(msh, lst);
|
ast = get_ast(msh, lst);
|
||||||
|
debug_dio_ast(ast, dio);
|
||||||
if (!ast)
|
if (!ast)
|
||||||
return (NULL);
|
return (NULL);
|
||||||
if (DEBUG)
|
|
||||||
{
|
|
||||||
gen_dio_ast(ast, dio);
|
|
||||||
drawio_end_file(dio);
|
|
||||||
}
|
|
||||||
free_linked_list(lst);
|
free_linked_list(lst);
|
||||||
return (ast);
|
return (ast);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,68 +13,42 @@
|
|||||||
#include "../../../includes/minishell.h"
|
#include "../../../includes/minishell.h"
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
int syntax_err_mess(char *token_base, int selected)
|
static int parenthesis_error(t_node *cpy)
|
||||||
{
|
{
|
||||||
char *token;
|
if (!ft_strncmp(cpy->val, "(", 1) && cpy->next
|
||||||
|
&& !ft_strncmp(cpy->next->val, ")", 1))
|
||||||
token = ft_strdup(token_base);
|
return (syntax_err_mess(cpy->next->val, 0));
|
||||||
if (selected == 0)
|
if (!ft_strncmp(cpy->val, ")", 1) && cpy->next
|
||||||
ft_fprintf(2, "minishell : syntax error : unexpected token `%s'\n",
|
&& !ft_strncmp(cpy->next->val, "(", 1))
|
||||||
token);
|
return (syntax_err_mess(cpy->next->val, 0));
|
||||||
if (selected == 2)
|
if (is_aop_operator(cpy) && !ft_strncmp(cpy->next->val, ")", 1))
|
||||||
ft_fprintf(2, "minishell : syntax error : unoppened token `%s'\n",
|
return (syntax_err_mess(cpy->next->val, 0));
|
||||||
&token[1]);
|
|
||||||
token[1] = 0;
|
|
||||||
if (selected == 1)
|
|
||||||
ft_fprintf(2, "minishell : syntax error : unclosed token `%s'\n",
|
|
||||||
token);
|
|
||||||
if (selected == 3)
|
|
||||||
ft_fprintf(2, "minishell : syntax error : unexpected end of file\n");
|
|
||||||
free(token);
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
static int check_unclosed(char *c, t_node *node)
|
|
||||||
{
|
|
||||||
t_node *cpy;
|
|
||||||
int count;
|
|
||||||
|
|
||||||
cpy = node;
|
|
||||||
count = 0;
|
|
||||||
while (cpy)
|
|
||||||
{
|
|
||||||
if (!ft_strncmp(&c[0], cpy->val, 1))
|
|
||||||
count++;
|
|
||||||
if (!ft_strncmp(&c[1], cpy->val, 1) && count != 0)
|
|
||||||
count--;
|
|
||||||
cpy = cpy->next;
|
|
||||||
}
|
|
||||||
if (count > 0)
|
|
||||||
return (1);
|
|
||||||
if (count < 0)
|
|
||||||
return (2);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int check_unclosed_quote(char *c, t_node *node)
|
static int redir_error(t_node *cpy)
|
||||||
{
|
{
|
||||||
t_node *cpy;
|
if (is_redir(cpy) && !cpy->next)
|
||||||
int count;
|
return (syntax_err_mess(cpy->val, 3));
|
||||||
int len;
|
if (is_redir(cpy) && cpy->next && !is_basic_word(cpy->next))
|
||||||
|
return (syntax_err_mess(cpy->next->val, 0));
|
||||||
|
if (is_redir(cpy) && cpy->next && cpy->next->next
|
||||||
|
&& !ft_strncmp(cpy->next->next->val, "(", 1))
|
||||||
|
return (syntax_err_mess(cpy->next->next->val, 0));
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
cpy = node;
|
static int aop_error(t_node *cpy)
|
||||||
count = 0;
|
{
|
||||||
while (cpy)
|
if (!is_aop_operator(cpy))
|
||||||
{
|
return (0);
|
||||||
len = ft_strlen(cpy->val);
|
if (cpy->next == NULL)
|
||||||
if (len > 0 && cpy->val[0] == c[0])
|
return (syntax_err_mess(cpy->val, 3));
|
||||||
{
|
if (is_aop_operator(cpy->next))
|
||||||
if (len == 1 || cpy->val[len - 1] != c[0])
|
return (syntax_err_mess(cpy->next->val, 0));
|
||||||
count++;
|
if (ft_strlen(cpy->val) > 2)
|
||||||
}
|
return (syntax_err_mess(cpy->val, 0));
|
||||||
cpy = cpy->next;
|
return (0);
|
||||||
}
|
|
||||||
return (count % 2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int unclosed(t_node *head)
|
int unclosed(t_node *head)
|
||||||
@@ -88,16 +62,6 @@ int unclosed(t_node *head)
|
|||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
int is_redir(t_node *cpy)
|
|
||||||
{
|
|
||||||
if (cpy->pressision == RED_L
|
|
||||||
|| cpy->pressision == RED_R
|
|
||||||
|| cpy->pressision == HEREDOC
|
|
||||||
|| cpy->pressision == D_RED_R)
|
|
||||||
return (1);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
int syntax_error(t_node *head)
|
int syntax_error(t_node *head)
|
||||||
{
|
{
|
||||||
t_node *cpy;
|
t_node *cpy;
|
||||||
@@ -107,24 +71,10 @@ int syntax_error(t_node *head)
|
|||||||
return (syntax_err_mess(cpy->val, 0));
|
return (syntax_err_mess(cpy->val, 0));
|
||||||
while (cpy)
|
while (cpy)
|
||||||
{
|
{
|
||||||
if (is_redir(cpy) && !cpy->next)
|
if (redir_error(cpy) || parenthesis_error(cpy) || aop_error(cpy))
|
||||||
return (syntax_err_mess(cpy->val, 3));
|
return (1);
|
||||||
if (is_redir(cpy) && cpy->next && cpy->next->next && !ft_strncmp(cpy->next->next->val, "(", 1))
|
|
||||||
return (syntax_err_mess(cpy->next->next->val, 0));
|
|
||||||
if (unexpected_token(cpy))
|
if (unexpected_token(cpy))
|
||||||
return (syntax_err_mess(cpy->val, 0));
|
return (syntax_err_mess(cpy->val, 0));
|
||||||
if (cpy->next == NULL && is_aop_operator(cpy))
|
|
||||||
return (syntax_err_mess(cpy->val, 3));
|
|
||||||
if (!ft_strncmp(cpy->val, "(", 1) && cpy->next && !ft_strncmp(cpy->next->val, ")", 1))
|
|
||||||
return (syntax_err_mess(cpy->next->val, 0));
|
|
||||||
if (!ft_strncmp(cpy->val, ")", 1) && cpy->next && !ft_strncmp(cpy->next->val, "(", 1))
|
|
||||||
return (syntax_err_mess(cpy->next->val, 0));
|
|
||||||
if (is_aop_operator(cpy) && !ft_strncmp(cpy->next->val, ")", 1))
|
|
||||||
return (syntax_err_mess(cpy->next->val, 0));
|
|
||||||
if (is_aop_operator(cpy) && is_aop_operator(cpy->next))
|
|
||||||
return (syntax_err_mess(cpy->next->val, 0));
|
|
||||||
if (is_aop_operator(cpy) && ft_strlen(cpy->val) > 2)
|
|
||||||
return (syntax_err_mess(cpy->val, 0));
|
|
||||||
cpy = cpy->next;
|
cpy = cpy->next;
|
||||||
}
|
}
|
||||||
if (unclosed(head))
|
if (unclosed(head))
|
||||||
|
|||||||
@@ -31,3 +31,23 @@ int unexpected_token(t_node *node)
|
|||||||
return (1);
|
return (1);
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int is_redir(t_node *cpy)
|
||||||
|
{
|
||||||
|
if (cpy->pressision == RED_L
|
||||||
|
|| cpy->pressision == RED_R
|
||||||
|
|| cpy->pressision == HEREDOC
|
||||||
|
|| cpy->pressision == D_RED_R)
|
||||||
|
return (1);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int is_basic_word(t_node *cpy)
|
||||||
|
{
|
||||||
|
if (is_aop_operator(cpy)
|
||||||
|
|| is_redir(cpy)
|
||||||
|
|| !ft_strncmp(cpy->val, ")", 1)
|
||||||
|
|| !ft_strncmp(cpy->val, "(", 1))
|
||||||
|
return (0);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|||||||
77
srcs/parsing/syntax/syntax_utils2.c
Normal file
77
srcs/parsing/syntax/syntax_utils2.c
Normal file
@@ -0,0 +1,77 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* syntax_utils2.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: nalebrun <nalebrun@student.s19.be> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/14 10:04:44 by nalebrun #+# #+# */
|
||||||
|
/* Updated: 2025/02/14 10:04:44 by nalebrun ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "../../../includes/minishell.h"
|
||||||
|
|
||||||
|
int syntax_err_mess(char *token_base, int selected)
|
||||||
|
{
|
||||||
|
char *token;
|
||||||
|
|
||||||
|
token = ft_strdup(token_base);
|
||||||
|
if (selected == 0)
|
||||||
|
ft_fprintf(2, "minishell : syntax error : unexpected token `%s'\n",
|
||||||
|
token);
|
||||||
|
if (selected == 2)
|
||||||
|
ft_fprintf(2, "minishell : syntax error : unoppened token `%s'\n",
|
||||||
|
&token[1]);
|
||||||
|
token[1] = 0;
|
||||||
|
if (selected == 1)
|
||||||
|
ft_fprintf(2, "minishell : syntax error : unclosed token `%s'\n",
|
||||||
|
token);
|
||||||
|
if (selected == 3)
|
||||||
|
ft_fprintf(2, "minishell : syntax error : unexpected end of file\n");
|
||||||
|
free(token);
|
||||||
|
return (1);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_unclosed(char *c, t_node *node)
|
||||||
|
{
|
||||||
|
t_node *cpy;
|
||||||
|
int count;
|
||||||
|
|
||||||
|
cpy = node;
|
||||||
|
count = 0;
|
||||||
|
while (cpy)
|
||||||
|
{
|
||||||
|
if (!ft_strncmp(&c[0], cpy->val, 1))
|
||||||
|
count++;
|
||||||
|
if (!ft_strncmp(&c[1], cpy->val, 1) && count != 0)
|
||||||
|
count--;
|
||||||
|
cpy = cpy->next;
|
||||||
|
}
|
||||||
|
if (count > 0)
|
||||||
|
return (1);
|
||||||
|
if (count < 0)
|
||||||
|
return (2);
|
||||||
|
return (0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int check_unclosed_quote(char *c, t_node *node)
|
||||||
|
{
|
||||||
|
t_node *cpy;
|
||||||
|
int count;
|
||||||
|
int len;
|
||||||
|
|
||||||
|
cpy = node;
|
||||||
|
count = 0;
|
||||||
|
while (cpy)
|
||||||
|
{
|
||||||
|
len = ft_strlen(cpy->val);
|
||||||
|
if (len > 0 && cpy->val[0] == c[0])
|
||||||
|
{
|
||||||
|
if (len == 1 || cpy->val[len - 1] != c[0])
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
cpy = cpy->next;
|
||||||
|
}
|
||||||
|
return (count % 2);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user