-
Notifications
You must be signed in to change notification settings - Fork 0
/
token_simplification_utils_3.c
140 lines (131 loc) · 3.77 KB
/
token_simplification_utils_3.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_simplification_utils_3.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abuzdin <abuzdin@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/08/08 15:55:27 by mthiry #+# #+# */
/* Updated: 2022/08/12 15:40:40 by abuzdin ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void out_arg_management(t_node *elem)
{
if (elem->next && (elem->next && elem->next->type == WSPACE))
{
elem = elem->next;
if (elem->next && (elem->next->type != PIPE
&& elem->next->type != REDIR_IN
&& elem->next->type != REDIR_OUT
&& elem->next->type != REDIR_HD
&& elem->next->type != REDIR_AP))
elem->next->type = OUT_ARG;
}
else if (elem->next && (elem->next->type != PIPE
&& elem->next->type != REDIR_IN
&& elem->next->type != REDIR_OUT
&& elem->next->type != REDIR_HD
&& elem->next->type != REDIR_AP))
elem->next->type = OUT_ARG;
}
void in_arg_management(t_node *elem)
{
if (elem->next && (elem->next && elem->next->type == WSPACE))
{
elem = elem->next;
if (elem->next && (elem->next->type != PIPE
&& elem->next->type != REDIR_IN
&& elem->next->type != REDIR_OUT
&& elem->next->type != REDIR_HD
&& elem->next->type != REDIR_AP))
elem->next->type = IN_ARG;
}
else if (elem->next && (elem->next->type != PIPE
&& elem->next->type != REDIR_IN
&& elem->next->type != REDIR_OUT
&& elem->next->type != REDIR_HD
&& elem->next->type != REDIR_AP))
elem->next->type = IN_ARG;
}
// for cases like: cmd <> f1
static int dredir_cmd(t_node *elem, t_input *data)
{
int fd;
if (elem->next && elem->next->type == REDIR_OUT)
{
elem = delete_node(elem);
elem = delete_node(elem);
}
while (elem && elem->type == WSPACE)
elem = elem->next;
if (elem && elem->type == WORD && elem->value && data->j > 1)
{
fd = open(elem->value, O_WRONLY | O_CREAT | O_APPEND, 00644);
if (error_check_noexit(fd, elem->value, data))
return (1);
close(fd);
elem = ms_token_del(elem);
}
else if (elem && elem->type == WORD && elem->value && data->j == 1)
{
fd = open(elem->value, O_WRONLY | O_CREAT | O_APPEND, 00644);
if (error_check_noexit(fd, elem->value, data))
return (1);
close(fd);
}
return (0);
}
// for cases like: <> f1
static int dredir_nocmd(t_node *elem, t_input *data)
{
int fd;
t_node *in;
t_node *out;
in = elem;
if (elem->next && elem->next->type == REDIR_OUT)
{
out = elem->next;
elem = elem->next->next;
}
while (elem && elem->type == WSPACE)
elem = elem->next;
if (elem && elem->type == WORD && elem->value)
{
fd = open(elem->value, O_WRONLY | O_CREAT | O_APPEND, 00644);
if (error_check_noexit(fd, elem->value, data))
return (1);
close(fd);
elem->type = 999;
in->type = 999;
out->type = 999;
}
return (0);
}
// check double redirection <>
int check_dredir(t_node *elem, t_input *data)
{
int ret;
while (elem)
{
data->j = 0;
while (elem && elem->type != REDIR_IN)
{
if (elem->type == WORD)
data->j++;
else if (elem->type == PIPE)
data->j = 0;
elem = elem->next;
}
if (!elem)
return (0);
if (data->j > 0)
ret = dredir_cmd(elem, data);
else if (data->j == 0)
ret = dredir_nocmd(elem, data);
if (ret == 1)
return (1);
elem = elem->next;
}
return (0);
}