-
Notifications
You must be signed in to change notification settings - Fork 0
/
subshell.c
114 lines (105 loc) · 2.7 KB
/
subshell.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* subshell.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mleclair <mleclair@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/28 11:51:57 by mleclair #+# #+# */
/* Updated: 2017/05/12 14:37:24 by mleclair ### ########.fr */
/* */
/* ************************************************************************** */
#include "chell.h"
int verif_par2(char **str, char *tmp)
{
free(*str);
*str = tmp;
free(env()->input);
env()->input = NULL;
env()->bool2 = 1;
return (-2);
}
int verif_par3(char **str, int *i, int *u, int *t)
{
if (bs_str(*str, *i, ')') && *u == 0)
return (-1);
if (bs_str(*str, *i, '('))
++*u;
else if (bs_str(*str, *i, ')'))
++*t;
return (0);
}
int verif_par(char **str, int u, int t, int i)
{
char *tmp;
while ((*str)[++i])
if (verif_par3(str, &i, &u, &t) == -1)
return (-1);
if (t > u)
return (-1);
else if (t == u)
return (0);
else
{
tmp = termcaps(ft_sprintf("subshell>"), 9, 0);
if (env()->bool1 == 1)
return (verif_par2(str, tmp));
*str = ft_strjoinfree(*str, " ", 1);
*str = ft_strjoinfree(*str, tmp, 3);
if (verif_par(str, 0, 0, -1) == -2)
return (-2);
}
ft_remstr(env()->input, ft_strfind(env()->input, '('),
ft_strcfind(env()->input, ')') == -1 ? ft_strlen(env()->input) :
ft_strcfind(env()->input, ')'));
return (0);
}
int verif_subshell(char *str)
{
int i;
int u;
int k;
int w;
i = -1;
u = 0;
w = 0;
k = 0;
while (str[++i])
{
if (bs_str(str, i, '(') && bs_str(str, i + 1, ')'))
return (-1);
if (bs_str(str, i, '`'))
u = u == 0 ? 1 : 0;
if (bs_str(str, i, '"'))
k = k == 0 ? 1 : 0;
if (bs_str(str, i, '\''))
w = w == 0 ? 1 : 0;
}
if (u != 0 || k != 0 || w != 0)
return (-2);
return (0);
}
int subshell(t_env *env, char **input)
{
int i;
i = verif_par(input, 0, 0, -1);
if (i == -1 || verif_subshell(*input) == -1)
{
error(-19, NULL, NULL);
return (-1);
}
if (i == -2)
return (0);
if (verif_subshell(*input) == -2)
{
error(-15, NULL, NULL);
return (-1);
}
subshell2(env, 0, 0, *input);
if (verif_par(input, 0, 0, -1) == -1)
return (-1);
i = 0;
while (ft_strfind(*input, '(') != -1 && i != -1)
i = subshell(env, input);
return (i);
}