-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_struct.c
116 lines (105 loc) · 2.56 KB
/
make_struct.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* make_struct.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marlean <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 16:18:54 by rdanyell #+# #+# */
/* Updated: 2022/06/17 13:42:27 by marlean ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void fill_com_new1(t_com *com, char **arr)
{
int i;
i = 0;
while (arr[i])
i++;
com->arg = malloc(sizeof(char *) * i + 1);
if (!com->arg)
return ;
com->arg[i] = NULL;
i = -1;
while (arr[++i])
com->arg[i] = ft_substr(arr[i], 0, ft_strlen(arr[i]));
}
int make_arg(char ***arg, char **arr, int i)
{
int n;
int j;
j = 0;
n = ft_n_words(arr, i);
if (n == 0)
{
*arg = NULL;
return (0);
}
*arg = malloc(sizeof(char *) * (n + 1));
while (j < n)
{
(*arg)[j++] = ft_substr(arr[i], 0, ft_strlen(arr[i]));
i++;
}
(*arg)[j] = NULL;
return (n);
}
t_com *com_new1(char *name, char **arr, int delim, char *file)
{
t_com *com;
com = malloc(sizeof(t_com));
if (!com)
return (NULL);
com->name = ft_substr(name, 0, ft_strlen(name));
ft_free(name);
if (arr)
{
fill_com_new1(com, arr);
free_array(arr);
}
else
com->arg = NULL;
com->delim = delim;
com->file = ft_substr(file, 0, ft_strlen(file));
com->next = NULL;
return (com);
}
int make_new_com(int i, char **arr, t_com **new_com)
{
char *name;
char **arg;
name = ft_substr(arr[i], 0, ft_strlen(arr[i]));
i++;
i = i + make_arg(&arg, arr, i);
if (arr[i] && delimetr(arr[i]) > 1)
{
*new_com = com_new1(name, arg, delimetr(arr[i]), arr[i + 1]);
i = i + 2;
}
else if (arr[i] && delimetr(arr[i]) == 1)
{
i++;
*new_com = com_new1(name, arg, 1, 0);
}
else
*new_com = com_new1(name, arg, 0, 0);
return (i);
}
void make_struct(char **arr, t_com **com)
{
t_com *new_com;
t_com *tmp;
int i;
new_com = NULL;
*com = init_com();
tmp = *com;
i = 0;
while (arr[i])
{
make_struct1(arr, &i, &new_com);
com_add_back(com, new_com);
}
*com = (*com)->next;
ft_one_name(com);
free(tmp);
}