-
Notifications
You must be signed in to change notification settings - Fork 1
/
extra_args_pa.c
125 lines (114 loc) · 2.5 KB
/
extra_args_pa.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
119
120
121
122
123
124
125
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* extra_args_pa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asfaihi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/09/15 15:51:28 by asfaihi #+# #+# */
/* Updated: 2021/09/28 12:16:44 by asfaihi ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static int args_counter(char *s)
{
int i;
int ret;
i = 0;
ret = 0;
while (s[i] == ' ')
i++;
while (s[i])
{
if (s[i] == ' ')
{
while (s[i] == ' ')
i++;
ret++;
}
i++;
}
return (ret);
}
int count_extra_args(char *s, int i, int j, t_list *env_lst)
{
char *temp;
char *tmp;
int ret;
ret = 0;
while (s[i])
{
while (s[i] && s[i] != '$')
i++;
if (s[i] == '$')
{
i++;
j = i;
while (s[i] && s[i] != ' ' && s[i] != '"'
&& s[i] != '\'' && s[i] != '$')
i++;
temp = ft_substr(s, j, i - j);
tmp = variable_expander(temp, env_lst);
free(temp);
if (ft_strchr(tmp, ' '))
ret += args_counter(tmp);
free(tmp);
}
}
return (ret);
}
char *remove_spaces(char *s)
{
char *res;
char *temp;
int i;
int j;
j = 0;
i = 0;
if (s == NULL)
return (NULL);
res = malloc(sizeof(char) * (ft_strlen(s) + 1));
if (res == NULL)
return (NULL);
temp = s;
s = ft_strtrim(s, " ");
free(temp);
while (s[i])
{
if (s[i] == ' ' && s[i + 1] == ' ')
i++;
else
res[j++] = s[i++];
}
res[j] = '\0';
free(s);
return (res);
}
void add_extra_args(t_cmd *new, t_prs *prs)
{
int i;
i = 1;
while (prs->extra_args[i])
{
new->args[prs->arg_num + i] = prs->extra_args[i];
i++;
}
i = 1;
while (prs->extra_args[i])
{
prs->arg_num++;
i++;
}
free(prs->extra_args);
prs->extra_args = NULL;
}
void fill_arg(char *temp, t_cmd *new, t_list *env_lst, t_prs *prs)
{
char *tmp;
tmp = env_var_checker(temp, env_lst, prs);
free(temp);
temp = new->args[prs->arg_num];
new->args[prs->arg_num] = ft_strjoin(new->args[prs->arg_num], tmp);
free(temp);
free(tmp);
}