-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax_validation.c
87 lines (78 loc) · 2.08 KB
/
syntax_validation.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
85
86
87
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* syntax_validation.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: houazzan <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/25 13:56:40 by aouhadou #+# #+# */
/* Updated: 2022/06/28 10:51:29 by houazzan ### ########.fr */
/* */
/* ************************************************************************** */
#include "../../includes/minishell.h"
int check_is_var(char *var)
{
int i;
i = 0;
while (var[i])
{
if (var[i] == '"')
return (34);
if (var[i] == '\'')
return (39);
i++;
}
return (0);
}
int invalid_token(char *node)
{
int i;
i = 0;
if ((node[0] != '"' && node[0] != '\''))
{
while (node[i])
{
if ((node[i] == '(' || node[i] == ')'))
return (0);
i++;
}
}
else if (!ft_strncmp(node, "()", 2) || !ft_strncmp(node, "||", 2)
|| !ft_strncmp(node, "&&", 2))
return (0);
return (1);
}
int is_operator(char *tok)
{
if (!ft_strcmp1(tok, ">") || !ft_strcmp1(tok, "<")
|| !ft_strcmp1(tok, ">>") || !ft_strcmp1(tok, "|")
|| !ft_strcmp1(tok, "<>"))
return (1);
return (0);
}
int is_heredoc(char *tok)
{
if (!ft_strcmp1(tok, "<<"))
return (1);
return (0);
}
int syntax_validation(t_token *list)
{
t_token *tmp;
tmp = list;
while (tmp != NULL)
{
if (!invalid_token(tmp->data))
return (0);
else if (is_operator(tmp->data)
&& (tmp->next == NULL
|| !ft_strcmp1(tmp->next->data, "|")))
return (0);
else if (is_heredoc(tmp->data)
&& (tmp->next == NULL
|| is_operator(tmp->next->data) || is_heredoc(tmp->next->data)))
return (0);
tmp = tmp->next;
}
return (1);
}