42_minishell/syntax/syntax.c

107 lines
2.4 KiB
C
Raw Permalink Normal View History

2023-02-21 08:38:09 -05:00
/* ************************************************************************** */
/* */
/* ::: :::::::: */
2023-03-29 13:07:57 -04:00
/* syntax.c :+: :+: :+: */
2023-02-21 08:38:09 -05:00
/* +:+ +:+ +:+ */
/* By: cchauvet <cchauvet@student.42angoulem +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/21 13:00:05 by cchauvet #+# #+# */
2023-04-07 07:09:59 -04:00
/* Updated: 2023/04/07 11:07:52 by alouis-j ### ########.fr */
2023-02-21 08:38:09 -05:00
/* */
/* ************************************************************************** */
#include "../libftx/libftx.h"
#include "../utils/utils.h"
2023-02-09 12:47:05 -05:00
static int ft_quote_verif(const char *str)
{
if (ft_is_in_quote(str, ft_strlen(str)))
{
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: Quote is not closed\n");
return (1);
}
else
return (0);
}
2023-02-15 15:31:49 -05:00
static int ft_pipe_is_alone(const char *str)
{
2023-03-29 13:07:57 -04:00
ssize_t i;
2023-02-15 15:31:49 -05:00
int check;
2023-02-15 15:31:49 -05:00
check = 0;
2023-03-29 13:07:57 -04:00
i = -1;
while (str[++i] != '\0')
{
2023-03-29 13:07:57 -04:00
while (str[i] == ' ' || ft_is_in_quote(str, i))
2023-02-24 14:30:01 -05:00
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-15 15:31:49 -05:00
if (check == 0)
2023-03-29 13:07:57 -04:00
break ;
2023-02-15 15:31:49 -05:00
check = 0;
}
}
2023-02-16 08:54:19 -05:00
if (check == 0)
2023-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: Pipe must be followed and %s",
2023-03-29 13:07:57 -04:00
"preced by a command or redirection\n");
2023-02-16 08:54:19 -05:00
return (check == 0);
}
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-04-14 10:17:35 -04:00
ft_eprintf("bozoshell: 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);
}
static int ft_empty_verif(const char *str)
2023-02-15 15:31:49 -05:00
{
size_t i;
i = 0;
2023-04-07 07:09:59 -04:00
while (str[i] == ' ' || str[i] == '\t')
2023-02-15 15:31:49 -05:00
i++;
return (str[i] == '\0');
2023-02-09 12:47:05 -05:00
}
int ft_syntax_verif(t_data *data, const char *str)
{
2023-03-22 11:07:27 -04:00
if (ft_empty_verif(str))
return (1);
2023-02-24 14:30:01 -05:00
if (ft_quote_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))
{
2023-04-05 11:32:04 -04:00
*data->exit_code = 2;
2023-02-24 14:30:01 -05:00
return (1);
}
return (0);
}