-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexpand_split.c
120 lines (112 loc) · 2.81 KB
/
expand_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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expand_split.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: cdomet-d <cdomet-d@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 18:28:27 by csweetin #+# #+# */
/* Updated: 2024/10/31 14:19:59 by cdomet-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
static size_t nb_letter_str(char *data, size_t *j, size_t letter)
{
while (data[*j] && (data[*j] == ' '
|| (data[*j] >= '\t' && data[*j] <= '\r')))
*j += 1;
while (data[*j] && data[*j] != ' ' && (data[*j] < '\t' || data[*j] > '\r'))
{
if (data[*j] == '"')
{
*j += 1;
letter += 1;
while (data[*j] && data[*j] != '"')
{
letter += 1;
*j += 1;
}
}
if (data[*j])
{
letter += 1;
*j += 1;
}
}
return (letter);
}
static char **split_tab(char **data, char **newtab)
{
size_t i;
size_t j;
size_t word;
size_t letter;
i = 0;
word = 0;
while (data[i])
{
j = 0;
while (data[i][j])
{
letter = nb_letter_str(data[i], &j, 0);
newtab[word] = ft_calloc(sizeof(char), letter + 1);
if (!newtab[word])
return (free_darr(newtab), \
print_error(errno, "minishell: parsing"));
ft_strlcpy(newtab[word], data[i] + (j - letter), letter + 1);
word += 1;
if (data[i][j])
j++;
}
i++;
}
return (newtab);
}
static void nb_word(char **tab, size_t *word)
{
size_t i;
size_t j;
i = 0;
while (tab[i])
{
j = 0;
while (tab[i][j])
{
if (tab[i][j] == '"')
{
while (tab[i][j] && tab[i][j] != '"')
j++;
}
if ((tab[i][j] != ' ' && (tab[i][j] < '\t' || tab[i][j] > '\r'))
&& (tab[i][j + 1] == ' ' || (tab[i][j + 1] >= '\t'
&& tab[i][j + 1] <= '\r') || tab[i][j + 1] == '\0'))
*word += 1;
if (tab[i][j])
j++;
}
i++;
}
}
char **expand_split(char **data, t_env **env, int status)
{
char **newtab;
char **temp;
size_t word;
newtab = NULL;
temp = NULL;
word = 0;
temp = expand(data, env, status);
if (!temp)
return (NULL);
if (!check_ws(temp))
return (temp);
nb_word(temp, &word);
newtab = ft_calloc(sizeof(char *), word + 1);
if (!newtab)
return (free_darr(temp), print_error(errno, "minishell: parsing"));
newtab = split_tab(temp, newtab);
if (!newtab)
return (free_darr(temp), NULL);
free_darr(temp);
return (newtab);
}