-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_command_setup.c
76 lines (69 loc) · 1.85 KB
/
ft_command_setup.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_command_setup.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/02 17:19:43 by abenamar #+# #+# */
/* Updated: 2023/09/13 05:07:54 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static size_t ft_save_character(char *str, char c1, char c2)
{
size_t i;
i = 1;
while (str[i] != str[0])
{
if (str[i] == c1)
str[i] = c2;
++i;
}
return (i);
}
static size_t ft_handle_quotes(char *str, char c1, char c2, uint8_t quote)
{
size_t i;
size_t j;
i = ft_save_character(str, c1, c2);
if (quote)
return (i + 1);
i -= 1;
j = 0;
while (j < i)
{
str[j] = str[j + 1];
++j;
}
while (str[j] && str[j + 1])
{
str[j] = str[j + 2];
++j;
}
str[j] = '\0';
return (i);
}
char *ft_command_setup(char *cmd, char c1, char c2, uint8_t quote)
{
char *str;
size_t i;
if (!cmd)
return (NULL);
str = ft_strdup(cmd);
if (!str)
return (NULL);
i = 0;
while (str[i])
{
if (str[i] == '\t')
str[i] = ' ';
else if (str[i] == '"' && ft_is_quoted(str + i))
i += ft_handle_quotes(str + i, c1, c2, quote);
else if (str[i] == '\'' && ft_is_quoted(str + i))
i += ft_handle_quotes(str + i, c1, c2, quote);
else
++i;
}
return (str);
}