-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsyntax.c
executable file
·102 lines (94 loc) · 2.79 KB
/
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mazaroua <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/03/20 00:01:28 by mazaroua #+# #+# */
/* Updated: 2023/05/12 15:22:57 by mazaroua ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int redirections_syntax(t_token_list **tokens)
{
t_token_list *tokens_;
tokens_ = *tokens;
while (tokens_ && tokens_->type != NLINE)
{
if (tokens_ && (tokens_->type == RIGHTRED || tokens_->type == LEFTRED
|| tokens_->type == APPEND || tokens_->type == HEREDOC))
{
if (tokens_ && (tokens_->next && tokens_->next->type == SPACE))
tokens_ = tokens_->next;
if (tokens_ && (tokens_->next->type == NLINE
|| tokens_->next->type != WORD))
{
g_var.exit_state = 258;
printf("minishell: syntax error near unexpected token `%s'\n",
tokens_->next->value);
return (0);
}
}
tokens_ = tokens_->next;
}
return (1);
}
int pipe_syntax(t_token_list **tokens)
{
t_token_list *tokens_;
tokens_ = *tokens;
if (tokens_->type == PIPE)
{
g_var.exit_state = 258;
printf("minishell: syntax error near unexpected token `|'\n");
return (0);
}
while (tokens_->type != NLINE)
{
if (tokens_->type == PIPE)
{
if (tokens_->next && tokens_->next->type == SPACE)
tokens_ = tokens_->next;
if (tokens_->next->type == NLINE || tokens_->next->type == PIPE)
{
g_var.exit_state = 258;
printf("minishell: syntax error near unexpected token `|'\n");
return (0);
}
}
tokens_ = tokens_->next;
}
return (1);
}
int heredoc_syntax(t_token_list **tokens)
{
t_token_list *token;
token = *tokens;
while (token && token->type != NLINE)
{
if (token->type == HEREDOC)
{
token = token->next;
if (token && token->type == SPACE)
token = token->next;
if (token && token->type != DOLLAR && token->type != WORD)
{
g_var.exit_state = 258;
printf("minishell: syntax error near unexpected token `%s'\n",
token->value);
return (0);
}
}
token = token->next;
}
return (1);
}
int syntax(t_token_list *tokens)
{
if (!tokens || tokens->type == NLINE)
return (0);
if (redirections_syntax(&tokens) && pipe_syntax(&tokens))
return (1);
return (0);
}