-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdtrim.c
123 lines (114 loc) · 2.92 KB
/
cmdtrim.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* cmdtrim.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: kristori <kristori@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/23 14:38:28 by kristori #+# #+# */
/* Updated: 2023/04/05 12:21:10 by kristori ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void ft_cmdsubsplit_state(char **ris, int *i, int *flag, char **cmd)
{
while (cmd[i[0]][i[1]])
{
if (cmd[i[0]][i[1]] == 60 || cmd[i[0]][i[1]] == 62
|| cmd[i[0]][i[1]] == 124)
{
*flag = 1;
if (i[1] - 1 != -1)
{
if (cmd[i[0]][i[1] - 1] != 0 && cmd[i[0]][i[1] - 1] != ' ')
ris[i[2]++] = ft_strlcpy_quote(cmd[i[0]], i[1], 0);
}
ris[i[2]] = ft_strdup((char [2]){cmd[i[0]][i[1]], '\0'});
i[2]++;
if (cmd[i[0]][i[1] + 1] != 0)
{
ris[i[2]] = ft_strlcpy_quote(cmd[i[0]],
ft_strlen(cmd[i[0]]), i[1] + 1);
i[2]++;
}
break ;
}
i[1]++;
}
}
char **ft_cmdsubsplit(char **cmd)
{
char **ris;
int flag;
int i[3];
flag = 0;
i[0] = 0;
i[1] = 0;
i[2] = 0;
ris = (char **)malloc(sizeof(char *)
* ((ft_countlist(cmd) + ft_pipecount(cmd) + 1)));
while (cmd[i[0]])
{
ft_cmdsubsplit_state(ris, i, &flag, cmd);
if (flag == 0)
{
ris[i[2]] = ft_strdup(cmd[i[0]]);
i[2]++;
}
i[0]++;
i[1] = 0;
flag = 0;
}
ris[i[2]] = 0;
ft_free(cmd);
return (ris);
}
static int ft_check_quotes(char *str, int *flags, int i)
{
if (str[i] == 34 && flags[0] == 1)
flags[0] = 0;
else if (str[i] == 34 && flags[0] == 0)
flags[0] = 1;
if (str[i] == 39 && flags[1] == 1)
flags[1] = 0;
else if (str[i] == 39 && flags[1] == 0)
flags[1] = 1;
return (flags[0] || flags[1]);
}
char **ft_cmdtrim(char *str, char set)
{
char **ris;
int flag[2];
int index[3];
index[0] = 0;
index[1] = 0;
index[2] = 0;
flag[0] = 0;
flag[1] = 0;
ris = (char **)malloc(sizeof(char *) * (ft_wordcount(str, set) + 1));
while (str[index[0]] != '\0')
{
if (!ft_check_quotes(str, flag, index[0]) && str[index[0]] == set)
{
ris[index[2]] = ft_strlcpy_quote(str, index[0], index[1]);
index[1] = index[0];
index[2]++;
}
index[0]++;
}
ris[index[2]] = ft_strlcpy_quote(str, index[0], index[1]);
ris[++index[2]] = 0;
ft_search_env(ris);
ft_expand_path(ris);
return (ris);
}
void ft_strtrim_all(char **cmd)
{
int i;
i = 0;
while (cmd[i])
{
cmd[i] = ft_strtrim2(cmd[i], " ");
i++;
}
}