-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit_by_words.c
122 lines (111 loc) · 2.84 KB
/
split_by_words.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* split_by_words.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: marlean <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/17 10:45:44 by marlean #+# #+# */
/* Updated: 2022/06/17 13:24:29 by marlean ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *write_redir(char *str, int *ind)
{
char *res;
int i;
i = 0;
if (str[i] == '>')
{
i++;
if (str[i] == '>')
i++;
}
else if (str[i] == '<')
{
i++;
if (str[i] == '<')
i++;
}
res = ft_substr(str, 0, i);
*ind += i;
return (res);
}
int count_space(char *str)
{
int i;
int count_one;
int count_double;
i = 0;
count_one = 0;
count_double = 0;
while (i < (int)ft_strlen(str))
{
if (ft_separator(str[i]) && count_one % 2 == 0 && count_double % 2 == 0)
return (i);
if (str[i] == '\'' && count_double % 2 == 0)
count_one++;
if (str[i] == '\"' && count_one % 2 == 0)
count_double++;
i++;
}
return (i);
}
char *write_words(char *str, int *ind)
{
t_words *write_w;
char *result;
write_w = init_write_w(str);
while (write_w->i < write_w->len)
{
if (str[write_w->i] == '\'' && write_w->count_double % 2 == 0)
{
write_w->count_one++;
write_w->i++;
}
if (str[write_w->i] == '\"' && write_w->count_one % 2 == 0)
write_w->count_double++;
else
write_w->res[write_w->j++] = str[write_w->i];
write_w->i++;
}
*ind += write_w->i;
result = write_w->res;
free(write_w);
return (result);
}
static t_split *split_by_w1(char *tmp, t_split *split_w)
{
int i;
int j;
i = 0;
j = 0;
while (tmp[i])
{
while (tmp[i] && ft_isspace(tmp[i]))
i++;
if (tmp[i] == '|')
split_w->split_by_words[j++] = ft_substr(&tmp[i++], 0, 1);
else if (tmp[i] == '<' || tmp[i] == '>')
split_w->split_by_words[j++] = write_redir(&tmp[i], &i);
else if (tmp[i + 1] && (!ft_strncmp(&tmp[i], "\'\'", 2)
|| !ft_strncmp(&tmp[i], "\"\"", 2)))
i += 2;
else
split_w->split_by_words[j++] = write_words(&tmp[i], &i);
}
split_w->split_by_words[j] = NULL;
return (split_w);
}
char **split_by_words(char *str)
{
t_split *split_w;
char *tmp;
tmp = ft_strtrim(str, WHITE_SPACES);
split_w = init_split(tmp);
split_w = split_by_w1(tmp, split_w);
ft_free(tmp);
if (split_w)
free(split_w);
return (split_w->split_by_words);
}