-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipex_command.c
104 lines (97 loc) · 2.86 KB
/
pipex_command.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipex_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bsyvasal <bsyvasal@student.hive.fi> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/22 15:11:39 by vshchuki #+# #+# */
/* Updated: 2024/02/18 00:02:00 by bsyvasal ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/**
* Replaces all the whitespaces inside the command to special char.
* It should not affect whitespces inside quotes.
*
* @return pointer to a new command string.
*/
char *replace_spaces(char *cmd)
{
int i;
char *new_cmd;
new_cmd = ft_strdup(cmd);
if (!new_cmd)
return (NULL);
i = 0;
while (new_cmd[i])
{
if (ft_isspace(new_cmd[i]))
new_cmd[i] = 31;
if ((new_cmd[i] == '\'' || new_cmd[i] == '"')
&& get_quote_length(&new_cmd[i], new_cmd[i]) != 1)
i += get_quote_length(&new_cmd[i], new_cmd[i]) - 1;
i++;
}
return (new_cmd);
}
static char **interpret_commands(char **command, t_pipe *data)
{
int i;
int new_size;
char *interpreted_str;
i = 0;
new_size = 0;
while (command[i])
{
interpreted_str = interpret(command[i], data);
if (!interpreted_str)
return (NULL);
if (!interpreted_str[0] && command[i][0] != '"'
&& command[i][0] != '\'')
{
free(interpreted_str);
interpreted_str = NULL;
}
free(command[i]);
command[i] = interpreted_str;
if (command[i++])
new_size++;
}
if (new_size != i)
command = reallocate_arraylist(command, new_size);
return (command);
}
/**
* Splits the shell command. Preserves whitespaces inside quotes, expands env
* variables and trims wrapping quotes.
* If there were any redirections and filenames in the string, they should be
* removed and replaced with whitespaces before using the function.
*
* Some kind of a smart make_args()
*
* @return double dimension array of command and it's flags
*/
char **split_shell_cmd(char *cmd, t_pipe *data)
{
char *new_str;
char **command;
char **interpreted_command;
int size;
new_str = replace_spaces(cmd);
if (!new_str)
return (NULL);
command = ft_split(new_str, 31);
free(new_str);
if (!command)
return (NULL);
size = sizeof_arraylist(command);
interpreted_command = interpret_commands(command, data);
if (interpreted_command == NULL)
{
while (size >= 0)
free(command[size--]);
free(command);
}
return (interpreted_command);
}