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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* s_com.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif
#include "d_define.h"
#include "f_fail.h"
#include "p_args.h"
#include "p_lblock.h"
#include "p_lblock_next.h"
#include "p_subst_alias.h"
#include "p_subst_home.h"
#include "p_subst_vars.h"
#include "p_redirs.h"
#include "s_lredir.h"
#include "s_struct.h"
static char s_fill_com(char *words[], t_com **com)
{
unsigned long i;
unsigned long j;
if (words[0] == NULL)
return (0);
if (((*com)->bin = (char*)malloc((ft_strlen(words[0]) + 1)
* sizeof(char))) == NULL)
return (-1);
ft_strlcpy((*com)->bin, words[0], ft_strlen(words[0]) + 1);
i = 0;
while (words[i] != NULL)
i++;
if (((*com)->argv = (char**)malloc((i + 1) * sizeof(char*))) == NULL)
return (-1);
j = 0;
while (i > 0 && j < i)
{
if (((*com)->argv[j] = ft_strdup(words[j])) == NULL)
return (-1);
j++;
}
(*com)->argv[j] = 0;
return (0);
}
static void s_com_cpy_env_fork(t_com **com, t_msh *msh)
{
size_t i;
size_t j;
i = 0;
while (msh->env_fork_tmp[i][0] != '\0')
i++;
if (((*com)->env_fork = (char**)malloc((i + 1) * sizeof(char*))) == NULL)
f_alloc_and_destroy_msh(msh);
j = 0;
while (j < i)
{
if (((*com)->env_fork[j] = ft_strdup(msh->env_fork_tmp[j])) == NULL)
f_alloc_and_destroy_msh(msh);
j++;
}
(*com)->env_fork[j] = NULL;
}
void s_com_destroy(t_com **com)
{
t_com *ptr;
if (*com == NULL)
return ;
ptr = *com;
ft_memdel((void*)&ptr->bin);
if (ptr->argv != NULL)
ft_delwords(ptr->argv);
if (ptr->env_fork != NULL)
ft_delwords(ptr->env_fork);
if (ptr->rdr != NULL)
s_lredir_clear(&ptr->rdr);
ft_memdel((void*)&ptr);
}
static void *s_get_nword(char nword[], char word[], t_com *com, t_msh *msh)
{
size_t ret;
nword[0] = C_NUL;
ft_strlcpy(nword, word, ARG_MAX);
if (p_redirs(nword, &com, msh) != 0)
{
msh->ret = 1;
ft_memdel((void*)&com);
return (NULL);
}
if (msh->alias != NULL)
{
ret = p_subst_alias(nword, TRUE, msh);
while (ret != 0)
ret = p_subst_alias(nword, FALSE, msh);
}
p_subst_vars(nword, msh);
return (nword);
}
t_com *s_com_new(char word[], t_msh *msh)
{
char nword[ARG_MAX];
t_com *com;
char **words;
if ((com = (t_com*)malloc(sizeof(t_com))) == NULL)
return (NULL);
com->argv = NULL;
com->env_fork = NULL;
com->bin = NULL;
com->rdr = NULL;
if (s_get_nword(nword, word, com, msh) == NULL
|| (words = p_split_args(nword)) == NULL
|| (words = p_subst_home(words, msh)) == NULL)
return (NULL);
words = p_check_args_equals(words, msh);
if (msh->env_fork_tmp[0][0] != '\0')
s_com_cpy_env_fork(&com, msh);
if (s_fill_com(words, &com) < 0)
{
ft_delwords(words);
return (NULL);
}
ft_delwords(words);
return (com);
}
|