-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathft_parsecommand.c
More file actions
71 lines (64 loc) · 2.21 KB
/
ft_parsecommand.c
File metadata and controls
71 lines (64 loc) · 2.21 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parsecommand.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: makbulut <makbulut@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/30 16:02:21 by makbulut #+# #+# */
/* Updated: 2022/09/05 13:54:58 by makbulut ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include "42-Libft/libft.h"
static void *erroroccured(t_command *cmd)
{
ft_freecommand(cmd);
return (NULL);
}
static t_command *createcommand(void)
{
t_command *cmd;
cmd = ft_calloc(sizeof(t_command), 1);
cmd->in = 0;
cmd->out = 1;
return (cmd);
}
int parsetoken(t_token **tokens, int *start, int end, t_command *cmd)
{
if (tokens[*start]->type == LPAR && \
!ft_parsesubshell(cmd, tokens, start, end))
return (0);
else if (*start < end && tokens[*start]->type == RPAR)
{
ft_syntaxerror(tokens[*start]);
return (0);
}
else if (*start < end && tokens[*start]->type == WORD && \
!ft_parsewordtoken(cmd, tokens, *start))
return (0);
else if (*start < end && tokens[*start]->type == HEREDOC && \
!ft_parseheredoc(cmd, tokens, start))
return (0);
else if (*start < end && tokens[*start]->type == RED_INPUT && \
!ft_parseredinput(cmd, tokens, start))
return (0);
else if (*start < end && \
(tokens[*start]->type == RED_CREATE || \
tokens[*start]->type == RED_APPEND))
if (!ft_parseredoutput(cmd, tokens, start))
return (0);
return (1);
}
t_command *ft_parsecommand(t_token **tokens, int start, int end)
{
t_command *cmd;
cmd = createcommand();
while (tokens[start] && start < end)
{
if (!parsetoken(tokens, &start, end, cmd))
return (erroroccured(cmd));
start++;
}
return (cmd);
}