norm
This commit is contained in:
@@ -13,68 +13,42 @@
|
||||
#include "../../../includes/minishell.h"
|
||||
#include <string.h>
|
||||
|
||||
int syntax_err_mess(char *token_base, int selected)
|
||||
static int parenthesis_error(t_node *cpy)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
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));
|
||||
return (0);
|
||||
}
|
||||
|
||||
static int check_unclosed_quote(char *c, t_node *node)
|
||||
static int redir_error(t_node *cpy)
|
||||
{
|
||||
t_node *cpy;
|
||||
int count;
|
||||
int len;
|
||||
if (is_redir(cpy) && !cpy->next)
|
||||
return (syntax_err_mess(cpy->val, 3));
|
||||
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;
|
||||
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);
|
||||
static int aop_error(t_node *cpy)
|
||||
{
|
||||
if (!is_aop_operator(cpy))
|
||||
return (0);
|
||||
if (cpy->next == NULL)
|
||||
return (syntax_err_mess(cpy->val, 3));
|
||||
if (is_aop_operator(cpy->next))
|
||||
return (syntax_err_mess(cpy->next->val, 0));
|
||||
if (ft_strlen(cpy->val) > 2)
|
||||
return (syntax_err_mess(cpy->val, 0));
|
||||
return (0);
|
||||
}
|
||||
|
||||
int unclosed(t_node *head)
|
||||
@@ -88,16 +62,6 @@ int unclosed(t_node *head)
|
||||
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)
|
||||
{
|
||||
t_node *cpy;
|
||||
@@ -107,24 +71,10 @@ int syntax_error(t_node *head)
|
||||
return (syntax_err_mess(cpy->val, 0));
|
||||
while (cpy)
|
||||
{
|
||||
if (is_redir(cpy) && !cpy->next)
|
||||
return (syntax_err_mess(cpy->val, 3));
|
||||
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 (redir_error(cpy) || parenthesis_error(cpy) || aop_error(cpy))
|
||||
return (1);
|
||||
if (unexpected_token(cpy))
|
||||
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;
|
||||
}
|
||||
if (unclosed(head))
|
||||
|
||||
Reference in New Issue
Block a user