-
Notifications
You must be signed in to change notification settings - Fork 0
/
nodes.c
executable file
·112 lines (104 loc) · 1.86 KB
/
nodes.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
103
104
105
106
107
108
109
110
111
112
#include "minishell.h"
int complement_fill_node(t_list *node, char **args, int i)
{
if (args[i][0] == '>' && args[i][1] == '>')
{
ft_get_outfile2(node, args, i);
return (2);
}
else if (!(args[i][0] == '|' && !args[i][1]) && args[i][0])
{
node->cmd = ft_add_cmd(node->cmd, args[i]);
return (1);
}
else
return (-1);
}
int fill_node(t_list *node, char **args, int i)
{
int check;
if (args[i][0] == '>' && ft_strlen(args[i]) == 1)
{
ft_get_outfile(node, args, i);
return (2);
}
else if (args[i][0] == '<' && ft_strlen(args[i]) == 1)
{
ft_get_infile(node, args, i);
return (2);
}
else if (args[i][0] == '<' && args[i][1] == '<')
{
if (ft_get_infile2(node, args, i) < 0)
return (-20);
return (2);
}
else
check = complement_fill_node(node, args, i);
return (check);
}
void ft_add_node(t_list **lst)
{
t_list *temp;
t_list *cur;
t_list *prev;
temp = *lst;
if (!temp)
return (ft_lstadd_back(lst, cmd_init()));
while (temp)
{
prev = cur;
cur = temp;
temp = temp->next;
}
if ((!cur->cmd))
{
close_free(cur);
if (!(*lst)->next)
*lst = NULL;
else
{
prev->next = NULL;
cur = NULL;
}
}
ft_lstadd_back(lst, cmd_init());
}
int verifying_fill_node(t_list *cmds, int check)
{
if (check == -20)
return (0);
if (check < 0)
{
free_stack(&cmds);
return (1);
}
return (0);
}
t_list *create_nodes(char **args, t_env *new_envp)
{
t_list *cmds;
t_list *current_node;
int i;
int check;
i = 0;
check = 0;
current_node = NULL;
cmds = NULL;
g_status = 0;
while (check != -20 && args[i] && args[i][0])
{
if (check_for_cmd(args, i))
{
i += args[i][0] == '|';
ft_add_node(&cmds);
}
current_node = ft_lstlast(cmds);
current_node->envp = new_envp;
check = fill_node(current_node, args, i);
if (verifying_fill_node(cmds, check))
return (NULL);
i += check;
}
return (return_node(cmds, check, args));
}