Commit 4c040037 authored by abenoit's avatar abenoit
Browse files

check syntax remade

parent db0c076e
......@@ -8,7 +8,8 @@ SRC_DIR += src/builtins/
SRC += main.c
SRC += signal_handling.c
SRC += check_syntax.c
SRC += syntax.c
#SRC += check_syntax.c
SRC += parsing_handler.c
SRC += parsing_states1.c
SRC += parsing_states2.c
......
......@@ -16,6 +16,8 @@
# define S_SEMICOL 0x080
# define S_PIPE 0x100
# define METACHARS "\\\'\"><;| "
enum e_state
{
SPACE,
......@@ -52,6 +54,7 @@ typedef struct s_state_machine
} t_state_machine;
typedef char *(*t_parse)(t_state_machine *, char *);
typedef int (*t_syntax)(unsigned int *flags);
/*
** check_syntax.c
......
......@@ -77,6 +77,7 @@ SRCS_CUSTOM += blank_str.c
SRCS_CUSTOM += push_int_to_array.c
SRCS_CUSTOM += ft_isnumber.c
SRCS_CUSTOM += skip_spaces.c
SRCS_CUSTOM += ft_set_index.c
VPATH = part_1:part_2:bonus:custom
......
#include "libft.h"
int ft_set_index(char c, char *set)
{
int i;
i = 0;
while (set[i] != '\0')
{
if (c == set[i])
return (i);
i++;
}
return (ERROR);
}
......@@ -127,4 +127,6 @@ void sort_str_array(char **array);
int *push_int_to_array(int *array, size_t array_size, int nb);
int ft_set_index(char c, char *set);
#endif
......@@ -98,29 +98,33 @@ int check_bsl_and_quotes(unsigned int *flags, char c)
return (SUCCESS);
else if (!(*flags & S_QUOTE))
*flags += S_QUOTE;
else if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
else
{
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
*flags -= S_QUOTE;
}
}
else if (c == '\"')
else if (c == '"')
{
if (*flags & S_QUOTE)
return (SUCCESS);
else if (!(*flags & S_DQUOTE))
*flags += S_DQUOTE;
else if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
else
{
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
*flags -= S_DQUOTE;
}
}
else
return (check_redirs_and_cmdsep(flags, c));
......@@ -174,9 +178,9 @@ int check_syntax(char *line)
flags -= S_QUOTE;
i++;
}
else if (flags & S_DQUOTE && (line[i] == '\'' || line[i] == '\"'))
else if (flags & S_DQUOTE && (line[i] == '\'' || line[i] == '"'))
{
if (line[i] == '\"')
if (line[i] == '"')
flags -= S_DQUOTE;
if (line[i] == '\\')
flags += S_BACKSL;
......
#include "parsing.h"
int check_backsl(unsigned int *flags)
{
if (*flags & S_QUOTE)
return (SUCCESS);
if (!(*flags & S_BACKSL))
*flags += S_BACKSL;
return (SUCCESS);
}
int check_quote(unsigned int *flags)
{
if (*flags & S_DQUOTE)
return (SUCCESS);
else if (!(*flags & S_QUOTE))
*flags += S_QUOTE;
else
{
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
*flags -= S_QUOTE;
}
return (SUCCESS);
}
int check_dquote(unsigned int *flags)
{
if (*flags & S_QUOTE)
return (SUCCESS);
else if (!(*flags & S_DQUOTE))
*flags += S_DQUOTE;
else
{
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
*flags -= S_DQUOTE;
}
return (SUCCESS);
}
int check_r_redir(unsigned int *flags)
{
if ((*flags & S_R_REDIR))
{
*flags += S_APPEND;
*flags -= S_R_REDIR;
}
else if (!(*flags & S_R_REDIR) && !(*flags & S_L_REDIR))
*flags += S_R_REDIR;
else
return (REDIR_PATH_MISSING);
return (SUCCESS);
}
int check_l_redir(unsigned int *flags)
{
if (!(*flags & S_L_REDIR) && !(*flags & S_R_REDIR))
*flags += S_L_REDIR;
else
return (REDIR_PATH_MISSING);
return (SUCCESS);
}
int check_semicol(unsigned int *flags)
{
if (!(*flags & S_SEMICOL) && !(*flags & S_PIPE))
*flags += S_SEMICOL;
else
return (EMPTY_CMD);
if (*flags & S_R_REDIR || *flags & S_L_REDIR)
return (REDIR_PATH_MISSING);
if (*flags & S_EMPTY)
return (EMPTY_CMD);
else
*flags += S_EMPTY;
return (SUCCESS);
}
int check_pipe(unsigned int *flags)
{
if (!(*flags & S_SEMICOL) && !(*flags & S_PIPE))
*flags += S_PIPE;
else
return (EMPTY_CMD);
if (*flags & S_R_REDIR || *flags & S_L_REDIR)
return (REDIR_PATH_MISSING);
if (*flags & S_EMPTY)
return (EMPTY_CMD);
else
*flags += S_EMPTY;
return (SUCCESS);
}
int check_space(unsigned int *flags)
{
(void)*flags;
return (SUCCESS);
}
int check_others(unsigned int *flags)
{
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
if (*flags & S_APPEND)
*flags -= S_APPEND;
if (*flags & S_EMPTY)
*flags -= S_EMPTY;
if (*flags & S_PIPE)
*flags -= S_PIPE;
if (*flags & S_SEMICOL)
*flags -= S_SEMICOL;
return (SUCCESS);
}
int final_checks(int flags)
{
if (flags & S_BACKSL)
return (ESCAPE_NL);
if (flags & S_APPEND)
return (REDIR_PATH_MISSING);
if (flags & S_R_REDIR)
return (REDIR_PATH_MISSING);
if (flags & S_L_REDIR)
return (REDIR_PATH_MISSING);
if (flags & S_QUOTE)
return (SQUOTE_MISSING);
if (flags & S_DQUOTE)
return (DQUOTE_MISSING);
if (flags & S_PIPE)
return (ESCAPE_NL);
return (SUCCESS);
}
int check_syntax(char *line)
{
int i;
int ret;
unsigned int flags;
const t_syntax syn_checks[] = {check_backsl, check_quote, check_dquote,
check_r_redir, check_l_redir, check_semicol,
check_pipe, check_space};
i = 0;
flags = S_EMPTY;
while (line[i])
{
if (flags & S_BACKSL)
{
flags -= S_BACKSL;
if (flags & S_EMPTY)
flags -= S_EMPTY;
if (flags & S_APPEND)
flags -= S_APPEND;
if (flags & S_R_REDIR)
flags -= S_R_REDIR;
if (flags & S_L_REDIR)
flags -= S_L_REDIR;
i++;
}
else if (flags & S_QUOTE && line[i] == '\'')
{
flags -= S_QUOTE;
i++;
}
else if (flags & S_DQUOTE && (line[i] == '\'' || line[i] == '"'))
{
if (line[i] == '"')
flags -= S_DQUOTE;
if (line[i] == '\\')
flags += S_BACKSL;
i++;
}
else
{
// ret = check_bsl_and_quotes(&flags, line[i]);
ret = ft_set_index(line[i], METACHARS);
if (ret >= 0)
ret = syn_checks[ret](&flags);
else
ret = check_others(&flags);
if (ret != SUCCESS)
return (ret);
i++;
}
}
return (final_checks(flags));
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment