-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill_commands.c
109 lines (100 loc) · 2.89 KB
/
fill_commands.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* fill_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: emohamed <emohamed@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/18 10:56:34 by emohamed #+# #+# */
/* Updated: 2023/09/30 21:41:30 by emohamed ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void count_cmds(char **ptr, t_vars *vars)
{
int i;
i = 0;
vars->n_commandes = 1;
while (ptr[i])
{
if (ptr[i + 1] && (ptr[i + 1][0] != '\0'
&& ft_strncmp("|", ptr[i + 1], ft_strlen(ptr[i + 1])) == 0))
vars->n_commandes++;
i++;
}
}
void allocat_cmds(t_vars *vars, char **ptr)
{
int i;
int b;
i = 0;
count_cmds(ptr, vars);
vars->cmds = malloc_((sizeof(t_cmds) * (vars->n_commandes + 1)), NULL, 0,
NULL);
i = 0;
b = 0;
while (i < vars->n_commandes)
{
while (ptr[b] && (ft_strncmp("|", ptr[b], ft_strlen(ptr[b]))))
b++;
if (!ft_strncmp("|", ptr[b], ft_strlen(ptr[b])))
b++;
vars->cmds[i].cmds_args = malloc_((sizeof(char *) * (b + 1)), NULL, 0,
NULL);
i++;
}
}
void alocate_redirection(t_vars *vars, int i, int size_of_direc, int k)
{
vars->cmds[i].cmds_args[k] = 0;
if (size_of_direc > 0)
{
vars->cmds[i].opera_derec = get_redirectinsv(size_of_direc,
vars->cmds[i].cmds_args);
vars->cmds[i].file_derec = get_files(size_of_direc,
vars->cmds[i].cmds_args);
vars->cmds[i].has_redirections = 1;
vars->cmds[i].cmds_args
= clear_cmds_arg_from_direct(vars->cmds[i].cmds_args);
}
else
vars->cmds[i].has_redirections = 0;
}
void fell_redandpipe(t_vars *vars, char **ptr, t_int *intt, int i)
{
vars->cmds[i].cmd = ptr[intt->b];
while (ptr[intt->b] && ft_strncmp("|", ptr[intt->b],
ft_strlen(ptr[intt->b])))
{
if (is_redirection(ptr[intt->b]))
intt->size_of_direc++;
vars->cmds[i].cmds_args[intt->k] = ptr[intt->b];
intt->b++;
intt->k++;
}
alocate_redirection(vars, i, intt->size_of_direc, intt->k);
if (ptr[intt->b] && ptr[intt->b][0] != '\0' && (!ft_strncmp("|",
ptr[intt->b], ft_strlen(ptr[intt->b]))))
{
vars->cmds[i].is_nex_pip = 1;
intt->k++;
}
else
vars->cmds[i].is_nex_pip = 0;
intt->b++;
}
void fill_commands(char **ptr, t_vars *vars)
{
t_int intt;
int i;
intt.size_of_direc = 0;
allocat_cmds(vars, ptr);
i = 0;
intt.b = 0;
while (i < vars->n_commandes)
{
intt.k = 0;
fell_redandpipe(vars, ptr, &intt, i);
i++;
}
}