Commit 52d10fef authored by abenoit's avatar abenoit
Browse files

syntax check fixes

parent a6fe249a
......@@ -8,8 +8,7 @@ SRC_DIR += src/builtins/
SRC += main.c
SRC += signal_handling.c
SRC += syntax.c
#SRC += check_syntax.c
SRC += check_syntax.c
SRC += parsing_handler.c
SRC += parsing_states1.c
SRC += parsing_states2.c
......
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mvidal-a <mvidal-a@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/02/01 11:25:20 by mvidal-a #+# #+# */
/* Updated: 2021/02/02 15:06:21 by abenoit ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
void check_others(unsigned int *flags, char c)
int check_backsl(unsigned int *flags)
{
if (!(ft_isspace(c)))
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;
if (*flags & S_L_REDIR)
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
if (*flags & S_APPEND)
*flags -= S_APPEND;
if (*flags & S_EMPTY)
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
if (*flags & S_PIPE)
*flags -= S_PIPE;
if (*flags & S_SEMICOL)
*flags -= S_SEMICOL;
*flags -= S_QUOTE;
}
return (SUCCESS);
}
int check_redirs_and_cmdsep(unsigned int *flags, char c)
int check_dquote(unsigned int *flags)
{
if (c == '>')
if (*flags & S_QUOTE)
return (SUCCESS);
else if (!(*flags & S_DQUOTE))
*flags += S_DQUOTE;
else
{
if ((*flags & S_R_REDIR))
{
*flags += S_APPEND;
if (*flags & S_R_REDIR)
*flags -= S_R_REDIR;
}
else if (!(*flags & S_R_REDIR) && !(*flags & S_L_REDIR))
*flags += S_R_REDIR;
else
return (REDIR_PATH_MISSING);
}
else if (c == '<')
{
if (!(*flags & S_L_REDIR) && !(*flags & S_R_REDIR))
*flags += S_L_REDIR;
else
return (REDIR_PATH_MISSING);
}
else if (c == ';')
{
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;
else if (*flags & S_L_REDIR)
*flags -= S_L_REDIR;
else if (*flags & S_EMPTY)
*flags -= S_EMPTY;
*flags -= S_DQUOTE;
}
else if (c == '|')
return (SUCCESS);
}
int check_r_redir(unsigned int *flags)
{
if ((*flags & S_R_REDIR))
{
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;
*flags += S_APPEND;
*flags -= S_R_REDIR;
}
else if (!(*flags & S_R_REDIR) && !(*flags & S_L_REDIR))
*flags += S_R_REDIR;
else
check_others(flags, c);
return (REDIR_PATH_MISSING);
return (SUCCESS);
}
int check_bsl_and_quotes(unsigned int *flags, char c)
int check_l_redir(unsigned int *flags)
{
if (c == '\\')
{
if (*flags & S_QUOTE)
return (SUCCESS);
if (!(*flags & S_BACKSL))
*flags += S_BACKSL;
}
else if (c == '\'')
{
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;
}
}
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;
*flags -= S_DQUOTE;
}
}
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
return (check_redirs_and_cmdsep(flags, c));
*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);
}
......@@ -155,6 +148,9 @@ 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;
......@@ -173,12 +169,13 @@ int check_syntax(char *line)
flags -= S_L_REDIR;
i++;
}
else if (flags & S_QUOTE && line[i] == '\'')
else if (flags & S_QUOTE)
{
flags -= S_QUOTE;
if (line[i] == '\'')
flags -= S_QUOTE;
i++;
}
else if (flags & S_DQUOTE && (line[i] == '\'' || line[i] == '"'))
else if (flags & S_DQUOTE)
{
if (line[i] == '"')
flags -= S_DQUOTE;
......@@ -188,7 +185,11 @@ int check_syntax(char *line)
}
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++;
......
#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