-
Notifications
You must be signed in to change notification settings - Fork 0
/
syntax_check.c
61 lines (57 loc) · 2.27 KB
/
syntax_check.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* syntax_check.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ulyildiz <ulyildiz@student.42kocaeli.com.t +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/20 14:30:36 by ulyildiz #+# #+# */
/* Updated: 2024/05/20 16:11:47 by ulyildiz ### ########.fr */
/* */
/* ************************************************************************** */
#include "functions.h"
#include "42-libft/libft.h"
#include <unistd.h>
static int pipe_check(t_tokens *t)
{
if (t->type == PIPE && t->next == NULL)
return (0);
else if (t->type == CMD && ft_strnstr(t->value, "|", ft_strlen(t->value)))
return (0);
else if (t->type == PIPE && (t->next->type == HEREDOC
|| t->next->type == RDR_IN || t->next->type == RDR_OUT
|| t->next->type == PIPE || t->next->type == RDR_D_IN))
return (0);
return (1);
}
// '<>' '><' düşün
static int rdr_check(t_tokens *t)
{
if ((t->type == RDR_IN || t->type == RDR_OUT
|| t->type == HEREDOC || t->type == RDR_D_IN) && t->next == NULL)
return (0);
else if (t->next && ((t->type == HEREDOC || t->type == RDR_IN || t->type == RDR_D_IN
|| t->type == RDR_OUT) && (t->next->type == HEREDOC || t->next->type == RDR_IN
|| t->next->type == RDR_OUT || t->next->type == RDR_D_IN)))
return (0);
else if ((t->type == HEREDOC || t->type == RDR_IN
|| t->type == RDR_OUT || t->type == RDR_D_IN) && t->next->type == PIPE)
return (0);
return (1);
}
int token_check(t_tokens *token)
{
t_tokens *t;
t = token;
while (t)
{
if (!pipe_check(t))
return (ft_putstr_fd(
"Syntax error near unexpected pipe token\n", 2), 0);
else if (!rdr_check(t))
return (ft_putstr_fd(
"Syntax error near unexpected redirection token\n", 2), 0);
t = t->next;
}
return (1);
}