-
Notifications
You must be signed in to change notification settings - Fork 1
/
parse.c
116 lines (106 loc) · 1.98 KB
/
parse.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
113
114
115
116
#include "minishell.h"
char **parse_pipe_arg(int count, t_source *src, char **pipe)
{
char **arg;
int i;
i = 0;
arg = malloc((count + 1) * sizeof(char *));
src->ispipered = 1;
src->fd_r_c = 0;
src->tmp2 = 0;
while (i < count)
{
arg[i] = find_argument(pipe[src->c], src);
i++;
}
arg[i] = NULL;
return (arg);
}
t_pipe *new_pipe(char **pipe, t_source *src)
{
t_pipe *tmp;
char **arg;
int count;
int i;
tmp = (t_pipe *) malloc(sizeof(t_pipe));
tmp->next = NULL;
tmp->pipef = NULL;
src->ptemp = tmp;
src->tmp2 = 0;
tmp->cmd = find_command(pipe[src->c], src);
free(src->ra);
count = count_argument(pipe[src->c], src);
arg = parse_pipe_arg(count, src, pipe);
if (src->fd_r_c == 50 && tmp->pipef != NULL)
tmp->pipef->fd_r_c = 50;
init_arg_pipe(arg, tmp, src->tmp2);
i = 0;
while (arg[i])
free(arg[i++]);
free(arg);
return (tmp);
}
void loop_pipe(t_source *src, t_node *head, char **pipe)
{
int c;
t_pipe *tmp;
int i;
c = 0;
i = 0;
while (c < src->npipe)
{
tmp = get_last_node_p(head->pipe);
if (tmp == NULL)
head->pipe = new_pipe(pipe, src);
else
tmp->next = new_pipe(pipe, src);
c++;
}
}
void init_parse_arg(int count, char **pipe, t_source *src, t_node *head)
{
char **arg;
int i;
i = 0;
arg = malloc((count + 1) * sizeof(char *));
src->tmp2 = 0;
while (i < count)
{
arg[i] = find_argument(pipe[src->c], src);
i++;
}
arg[i] = NULL;
init_arg(head, arg, src->tmp2);
i = 0;
while (arg[i] != NULL)
{
free(arg[i]);
i++;
}
free(arg);
}
int init_parse(t_source *src, t_node *head, char **pipe)
{
int i;
int c;
int count;
i = 0;
c = 0;
src->dollarused = 0;
src->ispipered = 0;
src->tmp2 = 0;
head->cmd = find_command(pipe[src->c], src);
if (parse_check(head, src) == 1)
return (0);
count = count_argument(pipe[src->c], src);
src->count = count;
src->dquotes = 0;
src->squotes = 0;
init_parse_arg(count, pipe, src, head);
if (src->foundpipe == 1)
{
head->pipe = NULL;
loop_pipe(src, head, pipe);
}
return (0);
}