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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* p_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdint.h>
#include <stdlib.h>
#include "d_define.h"
#include "p_args.h"
#include "p_args_next.h"
#include "p_args_escape.h"
#include "u_utils.h"
/* ================= */
/* TODO: DELETE THIS */
/* ================= */
static void
p_print(char *words[])
{
char **ptr;
ptr = words;
while (*ptr != NULL)
{
ft_printf("[%s]\n", *ptr);
ptr++;
}
}
/* ================== */
/* TODO: DELETE ABOVE */
/* ================== */
static void
p_meet_bs(char *ptr, t_quote_mode mode)
{
if (mode != Q_SINGLE)
{
if (*(ptr + 1) == C_BACKS)
{
*ptr = C_SUB;
*(ptr + 1) = C_SUB;
}
}
}
static t_quote_mode
p_meet_dquote(char *head, char *ptr, t_quote_mode mode)
{
if (mode == Q_NONE)
{
if (u_is_not_escaped(head, ptr) == TRUE)
{
return (Q_DOUBLE);
}
else
{
return (Q_NONE);
}
}
else if (mode == Q_DOUBLE && u_is_not_escaped(head, ptr) == TRUE)
{
return (Q_NONE);
}
return (mode);
}
static t_quote_mode
p_meet_squote(char *head, char *ptr, t_quote_mode mode)
{
if (mode == Q_NONE)
{
if (u_is_not_escaped(head, ptr) == TRUE)
{
return (Q_SINGLE);
}
else
{
return (Q_NONE);
}
}
else if (mode == Q_SINGLE)
{
return (Q_NONE);
}
return (mode);
}
static t_bool
p_meet_whitespace(char *head, char *ptr, t_quote_mode mode)
{
if (mode == Q_NONE && u_is_not_escaped(head, ptr) == TRUE)
{
return (TRUE);
}
return (FALSE);
}
static char
*p_skip_whitespace(char *ptr)
{
while (*ptr != C_NUL && ft_iswhitespace(*ptr))
ptr++;
return (ptr);
}
static uint16_t
p_count_args(const char word[], size_t start[])
{
char *ptr;
t_quote_mode mode;
uint16_t count;
ptr = (char *)word;
mode = Q_NONE;
count = 1;
ptr = p_skip_whitespace(ptr);
start[0] = (ptr - word);
while (*ptr != C_NUL)
{
if (*ptr == C_BACKS)
p_meet_bs(ptr, mode);
if (*ptr == C_DQUOTE)
mode = p_meet_dquote((char*)word, ptr, mode);
else if (*ptr == C_SQUOTE)
mode = p_meet_squote((char*)word, ptr, mode);
if (ft_iswhitespace(*ptr) &&
p_meet_whitespace((char*)word, ptr, mode) == TRUE)
{
count += 1;
ptr = p_skip_whitespace(ptr);
start[count - 1] = (ptr - word);
ptr -= 1;
}
ptr++;
}
return (count);
/* TODO: quotes parse error */
}
static char
**p_split_words_no_rdr(const char word[])
{
char **words;
size_t start[512];
uint16_t argc;
uint16_t to_del;
argc = p_count_args(word, start);
if ((words = (char**)malloc((argc + 1) * sizeof(char*))) == NULL)
return (NULL);
words[argc] = NULL;
if ((to_del = p_dup_words(words, word, argc, start)) != argc)
{
p_del_alloced_words(words, to_del);
return (NULL);
}
p_args_escape_chars_and_quotes(words);
p_print(words);
exit(0);
return (words);
}
char
**p_split_args(const char word[], int8_t redir)
{
char **words;
words = NULL;
if (redir == 0)
{
if ((words = p_split_words_no_rdr(word)) == NULL)
return (NULL);
return (words);
}
return (words);
/* char **words; */
/* char *subst; */
/* size_t i; */
/* if (redir == 0) */
/* { */
/* if (!(words = ft_split(word, ' '))) */
/* return (NULL); */
/* return (words); */
/* } */
/* i = 0; */
/* while (word[i] && ft_ischarset("<>", word[i]) == FALSE) */
/* i++; */
/* while (redir > 0 && ft_isdigit(word[i]) == TRUE) */
/* i--; */
/* if (!(subst = ft_substr(word, 0, i))) */
/* return (NULL); */
/* if (!(words = ft_split(subst, ' '))) */
/* { */
/* ft_memdel((void*)&subst); */
/* return (NULL); */
/* } */
/* ft_memdel((void*)&subst); */
/* return (words); */
}
|