-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands2.c
118 lines (107 loc) · 2.58 KB
/
commands2.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
117
118
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* commands2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: darodrig <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/03/10 16:35:34 by darodrig #+# #+# */
/* Updated: 2020/03/10 16:37:57 by darodrig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int
parse_com(t_comm *com, int n)
{
int i;
int len;
char **aux;
i = 0;
len = 0;
if (com->pipesplit && com->pipesplit[n])
len = ft_strlen(com->pipesplit[n]);
while (com->pipesplit[n][i] != ' ' && com->pipesplit[n][i] != '\0')
i++;
com->sub[n].command = ft_substr(com->pipesplit[n], 0, i);
while (ft_isspace(com->pipesplit[n][i]))
i++;
com->sub[n].parstr = ft_substr(com->pipesplit[n], i, len - i);
find_redirections(&com->sub[n]);
com->sub[n].params = ft_split2(com->sub[n].parstr, ' ');
aux = new_params(com, 1 + array_counter(com->sub[n].params), n);
free(com->sub[n].params);
free(com->pipesplit[n]);
com->sub[n].params = aux;
return (0);
}
void
command_trim(t_sh *sh)
{
int i;
int j;
char *aux;
i = 0;
while (sh->split[i])
{
j = 0;
while (sh->com[i].pipesplit[j])
{
aux = ft_strtrim(sh->com[i].pipesplit[j], " \t");
free(sh->com[i].pipesplit[j]);
sh->com[i].pipesplit[j] = aux;
j++;
}
i++;
}
}
int
get_commands(t_sh *sh)
{
int i;
i = 0;
sh->split = ft_split2(sh->string, ';');
while (sh->split[i])
i++;
sh->com = malloc(sizeof(t_comm) * (i + 1));
i = 0;
while (sh->split[i])
{
sh->com[i].pipesplit = ft_split2(sh->split[i], '|');
i++;
}
command_trim(sh);
return (0);
}
char
**new_params(t_comm *com, int i, int n)
{
char **new;
int j;
new = malloc(sizeof(char*) * (i + 2));
new[0] = ft_strdup("");
j = 0;
while (j < i)
{
new[j + 1] = com->sub[n].params[j];
j++;
}
new[j + 1] = NULL;
return (new);
}
int
init_com(t_comm *com)
{
int i;
int j;
i = 0;
j = 0;
while (com->pipesplit && com->pipesplit[i])
i++;
com->sub = malloc(sizeof(t_comm) * (i + 1));
while (j < i)
{
parse_com(com, j);
j++;
}
return (0);
}