2023-02-21 08:38:09 -05:00
|
|
|
/* ************************************************************************** */
|
|
|
|
/* */
|
|
|
|
/* ::: :::::::: */
|
|
|
|
/* syntatics.c :+: :+: :+: */
|
|
|
|
/* +:+ +:+ +:+ */
|
|
|
|
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
|
|
|
|
/* +#+#+#+#+#+ +#+ */
|
|
|
|
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
|
2023-02-21 18:26:32 -05:00
|
|
|
/* Updated: 2023/02/21 23:40:20 by cchauvet ### ########.fr */
|
2023-02-21 08:38:09 -05:00
|
|
|
/* */
|
|
|
|
/* ************************************************************************** */
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
#include "../libftx/libftx.h"
|
|
|
|
#include "../utils/utils.h"
|
2023-02-06 14:41:10 -05:00
|
|
|
|
2023-02-09 12:47:05 -05:00
|
|
|
static int ft_quote_verif(const char *str)
|
2023-02-06 14:41:10 -05:00
|
|
|
{
|
|
|
|
if (ft_is_in_quote(str, ft_strlen(str)))
|
|
|
|
{
|
2023-02-21 18:26:32 -05:00
|
|
|
ft_eprintf("minishell: Quote is not closed\n");
|
2023-02-06 14:41:10 -05:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-02-15 15:31:49 -05:00
|
|
|
static int ft_pipe_is_alone(const char *str)
|
2023-02-06 14:41:10 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
2023-02-15 15:31:49 -05:00
|
|
|
int check;
|
2023-02-06 14:41:10 -05:00
|
|
|
|
2023-02-15 15:31:49 -05:00
|
|
|
check = 0;
|
2023-02-06 14:41:10 -05:00
|
|
|
i = 0;
|
|
|
|
while (str[i] != '\0')
|
|
|
|
{
|
2023-02-16 08:54:19 -05:00
|
|
|
while (str[i] == ' ')
|
|
|
|
i++;
|
2023-02-24 14:30:01 -05:00
|
|
|
while (ft_is_in_quote(str, i))
|
|
|
|
i++;
|
2023-02-16 10:29:46 -05:00
|
|
|
if (str[i] == '\0')
|
|
|
|
break ;
|
|
|
|
if (str[i] != '|')
|
2023-02-15 15:31:49 -05:00
|
|
|
check = 1;
|
|
|
|
else
|
2023-02-06 14:41:10 -05:00
|
|
|
{
|
2023-02-15 15:31:49 -05:00
|
|
|
if (check == 0)
|
2023-02-06 14:41:10 -05:00
|
|
|
{
|
2023-02-15 15:31:49 -05:00
|
|
|
ft_eprintf("minishell: Pipe must be followed and ");
|
|
|
|
ft_eprintf("preced by a command or redirection\n");
|
2023-02-06 14:41:10 -05:00
|
|
|
return (1);
|
|
|
|
}
|
2023-02-15 15:31:49 -05:00
|
|
|
check = 0;
|
2023-02-06 14:41:10 -05:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
2023-02-16 08:54:19 -05:00
|
|
|
if (check == 0)
|
|
|
|
{
|
|
|
|
ft_eprintf("minishell: Pipe must be followed and ");
|
|
|
|
ft_eprintf("preced by a command or redirection\n");
|
|
|
|
}
|
|
|
|
return (check == 0);
|
2023-02-06 14:41:10 -05:00
|
|
|
}
|
|
|
|
|
2023-02-15 15:31:49 -05:00
|
|
|
static int ft_special_char_dub(const char *str)
|
2023-02-09 12:47:05 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
2023-02-15 15:31:49 -05:00
|
|
|
size_t y;
|
2023-02-09 12:47:05 -05:00
|
|
|
|
2023-02-15 15:31:49 -05:00
|
|
|
i = 0;
|
|
|
|
while (str[i] != '\0')
|
2023-02-09 12:47:05 -05:00
|
|
|
{
|
2023-02-16 07:53:49 -05:00
|
|
|
while (ft_is_in_quote(str, i))
|
|
|
|
i++;
|
2023-02-15 15:31:49 -05:00
|
|
|
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] == '|'))
|
|
|
|
{
|
2023-03-06 07:21:22 -05:00
|
|
|
ft_eprintf("minishell: too many %s in a row\n", str);
|
2023-02-15 15:31:49 -05:00
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
i = i + y;
|
|
|
|
}
|
2023-03-13 14:28:46 -04:00
|
|
|
else if (str[i] != '\0')
|
2023-02-17 07:21:50 -05:00
|
|
|
i++;
|
2023-02-09 12:47:05 -05:00
|
|
|
}
|
2023-02-15 15:31:49 -05:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
static int ft_empty_verif(const char *str)
|
2023-02-15 15:31:49 -05:00
|
|
|
{
|
|
|
|
size_t i;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (str[i] == ' ')
|
|
|
|
i++;
|
|
|
|
return (str[i] == '\0');
|
2023-02-09 12:47:05 -05:00
|
|
|
}
|
|
|
|
|
2023-03-10 06:32:39 -05:00
|
|
|
int ft_syntax_verif(t_data *data, const char *str)
|
2023-02-06 14:41:10 -05:00
|
|
|
{
|
2023-02-24 14:30:01 -05:00
|
|
|
if (ft_quote_verif(str)
|
2023-02-15 15:31:49 -05:00
|
|
|
|| ft_empty_verif(str)
|
2023-02-16 10:29:46 -05:00
|
|
|
|| ft_pipe_is_alone(str)
|
2023-02-24 14:30:01 -05:00
|
|
|
|| ft_special_char_dub(str))
|
|
|
|
{
|
|
|
|
data->exit_code = 2;
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
return (0);
|
2023-02-06 14:41:10 -05:00
|
|
|
}
|