107 lines
2.4 KiB
C
107 lines
2.4 KiB
C
/* ************************************************************************** */
|
|
/* */
|
|
/* ::: :::::::: */
|
|
/* syntax.c :+: :+: :+: */
|
|
/* +:+ +:+ +:+ */
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
/* +#+#+#+#+#+ +#+ */
|
|
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
|
|
/* Updated: 2023/04/07 11:07:52 by alouis-j ### ########.fr */
|
|
/* */
|
|
/* ************************************************************************** */
|
|
|
|
#include "../libftx/libftx.h"
|
|
#include "../utils/utils.h"
|
|
|
|
static int ft_quote_verif(const char *str)
|
|
{
|
|
if (ft_is_in_quote(str, ft_strlen(str)))
|
|
{
|
|
ft_eprintf("bozoshell: Quote is not closed\n");
|
|
return (1);
|
|
}
|
|
else
|
|
return (0);
|
|
}
|
|
|
|
static int ft_pipe_is_alone(const char *str)
|
|
{
|
|
ssize_t i;
|
|
int check;
|
|
|
|
check = 0;
|
|
i = -1;
|
|
while (str[++i] != '\0')
|
|
{
|
|
while (str[i] == ' ' || ft_is_in_quote(str, i))
|
|
i++;
|
|
if (str[i] == '\0')
|
|
break ;
|
|
if (str[i] != '|')
|
|
check = 1;
|
|
else
|
|
{
|
|
if (check == 0)
|
|
break ;
|
|
check = 0;
|
|
}
|
|
}
|
|
if (check == 0)
|
|
ft_eprintf("bozoshell: Pipe must be followed and %s",
|
|
"preced by a command or redirection\n");
|
|
return (check == 0);
|
|
}
|
|
|
|
static int ft_special_char_dub(const char *str)
|
|
{
|
|
size_t i;
|
|
size_t y;
|
|
|
|
i = 0;
|
|
while (str[i] != '\0')
|
|
{
|
|
while (ft_is_in_quote(str, i))
|
|
i++;
|
|
if (ft_is_in("|<>", str[i]))
|
|
{
|
|
y = 0;
|
|
while (str[i] == str[i + y])
|
|
y++;
|
|
if ((y > 2 && (str[i] == '>' || str[i] == '<'))
|
|
|| (y > 1 && str[i] == '|'))
|
|
{
|
|
ft_eprintf("bozoshell: too many %s in a row\n", str);
|
|
return (1);
|
|
}
|
|
i = i + y;
|
|
}
|
|
else if (str[i] != '\0')
|
|
i++;
|
|
}
|
|
return (0);
|
|
}
|
|
|
|
static int ft_empty_verif(const char *str)
|
|
{
|
|
size_t i;
|
|
|
|
i = 0;
|
|
while (str[i] == ' ' || str[i] == '\t')
|
|
i++;
|
|
return (str[i] == '\0');
|
|
}
|
|
|
|
int ft_syntax_verif(t_data *data, const char *str)
|
|
{
|
|
if (ft_empty_verif(str))
|
|
return (1);
|
|
if (ft_quote_verif(str)
|
|
|| ft_pipe_is_alone(str)
|
|
|| ft_special_char_dub(str))
|
|
{
|
|
*data->exit_code = 2;
|
|
return (1);
|
|
}
|
|
return (0);
|
|
}
|