Skip to content

Commit

Permalink
Added syntactical input checks
Browse files Browse the repository at this point in the history
  • Loading branch information
romanmikh committed Jun 12, 2024
1 parent 927236c commit 13c2d8d
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Empty file removed To
Empty file.
125 changes: 125 additions & 0 deletions src/parser/input_checks.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* str_checks.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/11 16:51:08 by rocky #+# #+# */
/* Updated: 2024/06/11 16:53:40 by rocky ### ########.fr */
/* */
/* ************************************************************************** */

#include "tokens.h"


int check_operators(char *str)
{
int single_quotes;
int double_quotes;
int cmd_flag;

single_quotes = 0;
double_quotes = 0;
cmd_flag = 0;
if (*str == '&' || *str == '|')
return (1);
while (*str)
{

if (*str == "\'")
single_quotes++;
if (*str == "\"")
double_quotes++;
if (*str == '|' && !(single_quotes % 2) && !(double_quotes % 2))
{
if (cmd_flag)
return (1);
cmd_flag = 1;
}
else if (ft_strchr(" \t\n\r\v\f", *str) != NULL)
cmd_flag = 0;
str++;
}
if (cmd_flag)
return (1);
return (0);
}

char *exclude_delimiters(char *str)
{
while (*str && (ft_strchr(" \t\n\r\v\f", *str) != NULL))
str++;
return (str);
}

int valid_operator(char **str)
{
char *start;

start = *str++;
if (*start == **str)
(*str)++;
*str = exclude_delimiters(*str);
if (**str == '<' || **str == '>' || **str == '|' || **str == '\0')
return (0);
return (1);
}

int check_redirections(char *str)
{
int single_quotes;
int double_quotes;

single_quotes = 0;
double_quotes = 0;
while (*str)
{
if (*str == "\'")
single_quotes++;
if (*str == "\"")
double_quotes++;
if ((!(single_quotes % 2) && !(double_quotes %2))
&& (*str == '>' || *str == '<'))
{
if (!valid_operator(&str))
return (1);
}
else
str++;
}
return (0);
}

int check_open_quotes(const char *str)
{
int single_quote_open;
int double_quote_open;

single_quote_open = 0;
double_quote_open = 0;
while (*str)
{
if (*str == '\'' && !double_quote_open)
{
single_quote_open = !single_quote_open;
}
else if (*str == '"' && !single_quote_open)
double_quote_open = !double_quote_open;
str++;
}
return (single_quote_open || double_quote_open);
}

int str_error_checks(char *str)
{
if (check_redirections(str))
ft_printf("str error: invalid redirection.\n");
else if (check_operators(str))
ft_printf("str error: invalid operator.\n");
else if (check_open_quotes(str))
ft_printf("str error: open quote.\n");
else
return (0);
return (1);
}

0 comments on commit 13c2d8d

Please sign in to comment.