-
Notifications
You must be signed in to change notification settings - Fork 0
/
min_argv.c
executable file
·146 lines (135 loc) · 3.02 KB
/
min_argv.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* min_argv.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: pmoreno- <pmoreno-@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/14 11:10:35 by potero-d #+# #+# */
/* Updated: 2022/08/11 15:08:26 by potero-d ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int change_quote(int q, char c)
{
int r;
r = 0;
if ((c == 39) && (q == 0))
r = 1;
else if ((c == 39) && (q == 1))
r = 0;
else if ((c == 39) && (q == 2))
r = 2;
else if ((c == 34) && (q == 0))
r = 2;
else if ((c == 34) && (q == 1))
r = 1;
else if ((c == 34) && (q == 2))
r = 0;
return (r);
}
void arguments(t_argv **argv, char *str)
{
int i;
int len;
int quote;
i = 0;
quote = 0;
while (str[i] != 0)
{
len = 0;
if (str[i] == 39 || str[i] == 34)
quote = change_quote(quote, str[i]);
if (str[i] == 124 && quote == 0)
{
len++;
while (str[i + len] != 124 && str[i + len])
len++;
lst_add_back(argv, lstnew(ft_substr(str, i + 1, len - 1)));
i = i + len - 1 ;
}
else
{
while ((str[i + len] != 124 && str[i + len])
|| (str[i + len] == 124 && quote != 0))
len++;
lst_add_back(argv, lstnew(ft_substr(str, i, len)));
i = i + len - 1;
}
i++;
}
}
static int min_words(char *s)
{
int w;
int i;
int q;
q = 0;
i = 0;
while (s[i] == ' ')
i++;
w = 1;
while (s[i + 1])
{
if (s[i] == ' ' && s[i + 1] != ' ' && q == 0)
w++;
else if (s[i] == 39 || s[i] == 34)
q = change_quote(q, s[i]);
i++;
}
return (w);
}
void static aux(char ***str, char *s, int q, int w)
{
int i;
int j;
i = 0;
j = 0;
while (s[i] == ' ')
i++;
while (s[i])
{
if ((s[i] == ' ') && (q == 0))
{
(*str)[w++] = ft_substr(s, i - j, j);
while (s[i + 1] == ' ')
i++;
j = 0;
}
else if ((s[i] != ' ') || (s[i] == ' ' && q != 0))
j++;
if (s[i] == 34 || s[i] == 39)
q = change_quote(q, s[i]);
i++;
}
if (s[i - 1] != ' ')
(*str)[w++] = ft_substr(s, i - j, j);
(*str)[w] = 0;
}
void min_split(t_data *data)
{
int words;
int w;
int q;
t_argv *argv;
w = 0;
q = 0;
argv = *data->argv;
while (argv)
{
if (!argv->arg)
argv->split = 0;
if (ft_strlen(argv->arg) == 0)
{
argv->split = malloc(sizeof(char *) * 1);
argv->split[0] = 0;
}
words = min_words(argv->arg);
argv->num_split = words;
argv->split = malloc(sizeof(char *) * (words + 1));
if (!argv->split)
argv->split = 0;
aux(&argv->split, argv->arg, q, w);
argv = argv->next;
}
}