-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_command_split.c
48 lines (43 loc) · 1.54 KB
/
ft_command_split.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_command_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/08/07 23:10:30 by abenamar #+# #+# */
/* Updated: 2023/09/13 01:50:30 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static char *ft_command_expand(char *cmd, t_list **env, char c)
{
char *tmp;
char *str;
tmp = ft_expand(cmd, env, 0, 0);
str = ft_command_setup(tmp, c, '\n', 0);
free(tmp);
return (str);
}
char **ft_command_split(char *cmd, t_list **env, char c, uint8_t expand)
{
char *str;
char **argv;
size_t i;
if (expand)
str = ft_command_expand(cmd, env, c);
else
str = ft_command_setup(cmd, c, '\n', 1);
argv = ft_split(str, c);
if (!argv)
return (free(str), NULL);
i = 0;
while (argv[i])
{
ft_str_replace(argv[i], '\n', c);
if (expand)
ft_str_replace(ft_str_replace(argv[i], -1, '\''), -2, '"');
++i;
}
return (free(str), argv);
}