-
Notifications
You must be signed in to change notification settings - Fork 1
/
find_argument.c
112 lines (104 loc) · 2.63 KB
/
find_argument.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
#include "minishell.h"
void find_argument_five(char *s, t_source *src, int ***i, int option)
{
int o;
o = (***i);
if (option == 1)
{
src->tmp = finding_aslash(s, o, src);
if (src->aslash == 0 && src->tmp != 1)
{
src->re[src->re_b++] = s[(***i)];
src->tmp = 0;
}
}
else if (option == 2)
{
src->re[src->re_b++] = s[(***i)];
src->aslash = 0;
}
}
int find_argument_four(char *s, t_source *src, int **i, int option)
{
if (option == 1)
find_argument_five(s, src, &i, 1);
else if (option == 2)
find_argument_five(s, src, &i, 2);
else if (option == 3)
{
if (s[(**i)] == ' ' && (src->dquotes == 1 || src->squotes == 1))
{
arg_if_space(s, &i, src);
return (0);
}
else if (s[(**i)] == ' ' && (src->dquotes == 0 || src->squotes == 0)
&& (s[(**i) + 1] != '>' && s[(**i) + 1] != '<'))
return (2);
}
else if (option == 4)
{
if (src->squotes == 1)
{
src->re[src->re_b++] = s[(**i)];
return (0);
}
return (2);
}
return (0);
}
int find_argument_three(char *s, t_source *src, int *i)
{
if ((s[(*i)] == '>' || s[(*i)] == '<')
&& src->aslash == 0 && src->dquotes == 0 && src->squotes == 0)
return (find_file_name(&i, s, src));
else if (s[(*i)] == ' ')
return (find_argument_four(s, src, &i, 3));
else if ((s[(*i)] == '|' || s[(*i)] == ';')
&& (src->dquotes == 0 && src->aslash == 0))
return (find_argument_four(s, src, &i, 4));
else if ((s[(*i)] == '\"') && src->squotes == 0 && src->aslash == 0)
finding_quotes(s, (*i), src);
else if ((s[(*i)] == '\'') && src->dquotes == 0 && src->aslash == 0)
finding_quotes(s, (*i), src);
else if (s[(*i)] == '$' && src->aslash == 0 && (ft_isalpha(s[(*i) + 1]) == 1
|| s[(*i) + 1] == '?') && src->squotes == 0)
(*i) = get_env_value_arg(s, src, (*i)) - 1;
else if (s[(*i)] == '\\' && src->aslash == 0)
find_argument_four(s, src, &i, 1);
else if ((s[(*i)] == '\"' || s[(*i)] == '\'' || s[(*i)] == '\\' || s[(*i)]
== ';' || s[(*i)] == '|' || s[(*i)] == '$' || s[(*i)] == '>'
|| s[(*i)] == '<') && src->aslash == 1)
find_argument_four(s, src, &i, 2);
else
src->re[src->re_b++] = s[(*i)];
return (0);
}
int find_argument_two(char *s, t_source *src, int i)
{
int a;
while (s[i] != '\n' && s[i] != '\0')
{
a = find_argument_three(s, src, &i);
if (a == 1)
continue ;
else if (a == 2)
break ;
i++;
}
return (i);
}
char *find_argument(char *s, t_source *src)
{
int i;
i = src->offset;
while (s[i] == ' ')
i++;
src->re = malloc((4096) * sizeof(char));
src->re_b = 0;
i = find_argument_two(s, src, i);
if (src->dquotes == 1 || src->squotes == 1 || src->aslash == 1)
;
src->offset = i;
src->re[src->re_b] = '\0';
return ((src->re));
}