-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathallocate_pa.c
77 lines (70 loc) · 2.03 KB
/
allocate_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* allocate_pa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asfaihi <asfaihi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/07/29 14:20:42 by asfaihi #+# #+# */
/* Updated: 2021/09/28 12:22:43 by asfaihi ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static int skip_space_and_redir(char *s, int i)
{
while (s[i] == ' ')
i++;
while (s[i] == '>' || s[i] == '<')
{
i++;
while (s[i] == ' ')
i++;
while (s[i] && s[i] != ' ' && s[i] != '<' && s[i] != '>')
i++;
}
return (i);
}
int check_for_quote(char c, int quote)
{
if (c == '"' && quote == 0)
quote = 2;
else if (c == '\'' && quote == 0)
quote = 1;
else if ((c == '"' && quote == 2) || (c == '\'' && quote == 1))
quote = 0;
return (quote);
}
int get_size(char *s)
{
int i;
int quote;
int ret;
ret = 0;
quote = 0;
i = 0;
while (s[i])
{
i = skip_space_and_redir(s, i);
if (s[i] && s[i] != ' ' && s[i] != '<' && s[i] != '>')
{
while ((s[i] && s[i] != ' ' && s[i] != '<' && s[i] != '>')
|| (s[i] == ' ' && quote)
|| ((s[i] == '>' || s[i] == '<') && quote))
quote = check_for_quote(s[i++], quote);
ret++;
}
}
return (ret);
}
void allocate_args(char *s, t_cmd *cmd, t_list *env_lst)
{
int ret;
int i;
ret = get_size(s);
if (ft_strchr(s, '$'))
ret += count_extra_args(s, 0, 0, env_lst);
cmd->args = malloc(sizeof(char *) * (ret + 1));
i = 0;
while (i <= ret)
cmd->args[i++] = NULL;
}