-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_syntax.c
84 lines (76 loc) · 1.96 KB
/
check_syntax.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdanyell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 16:18:54 by rdanyell #+# #+# */
/* Updated: 2022/06/16 16:49:53 by rdanyell ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_check_eve_quotes(char *str)
{
int count_one;
int count_double;
int i;
i = 0;
count_one = 0;
count_double = 0;
while (str[i])
{
if (str[i] == '\'' && count_double % 2 == 0)
count_one++;
if (str[i] == '\"' && count_one % 2 == 0)
count_double++;
i++;
}
if (count_double % 2 == 0 && count_one % 2 == 0)
return (0);
else
return (1);
}
int ft_check_first_end(char *str)
{
int i;
char *tmp;
i = 0;
tmp = ft_strtrim(str, WHITE_SPACES);
if (tmp[i] == '|')
{
free(tmp);
return (1);
}
while (tmp[i])
i++;
i--;
if (tmp[i] == '|' || tmp[i] == '<' || tmp[i] == '>')
{
free(tmp);
return (1);
}
free(tmp);
return (0);
}
int check_syntax(char *str)
{
if (!ft_strlen(str) || ft_check_eve_quotes(str) || ft_check_first_end(str))
{
printf("syntax error near unexpected token `newline'\n");
return (1);
}
else
return (0);
}
int check_double_delim(char **arr)
{
int i;
i = 0;
if (ft_isdelim(arr[i]) && !arr[i + 1])
return (1);
while (arr[i++])
if (arr[i] && ft_isdelim(arr[i - 1]) && ft_isdelim(arr[i]))
return (1);
return (0);
}