This repository has been archived by the owner on Sep 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipeline.c
82 lines (75 loc) · 2.47 KB
/
pipeline.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipeline.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebouvier <ebouvier@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/22 09:47:47 by mhoyer #+# #+# */
/* Updated: 2023/09/01 19:23:09 by ebouvier ### ########.fr */
/* */
/* ************************************************************************** */
#include "exec.h"
t_command *prep_cmd_pipe(t_node *node, t_minishell *minishell)
{
t_command *command;
char **env;
env = convert_env(minishell->env);
command = node_to_command(node, env, minishell);
free_mat(env);
return (command);
}
int check_middle(t_node *root, t_node *node)
{
if (node->parent && node->parent->type == PIPE
&& node->parent->right == node && node->parent != root)
return (1);
return (0);
}
int exec_cmd_pipe(t_node *root, t_node *node, t_minishell *minishell,
int pipefd[2][2])
{
t_command *cmd;
cmd = prep_cmd_pipe(node, minishell);
if (!cmd)
return (1);
if (node->parent && node->parent->type == PIPE
&& node->parent->left == node)
{
if (g_sigint || pipe(pipefd[1]) == -1 || execute_first(cmd, minishell,
pipefd) == 1)
return (free_command(cmd), 1);
}
else if (check_middle(root, node))
{
if (g_sigint || pipe(pipefd[1]) == -1 || execute_middle(cmd, minishell,
pipefd) == 1)
return (free_command(cmd), 1);
}
else if (node->parent && node->parent == root
&& node->parent->right == node)
{
if (g_sigint || execute_last(cmd, minishell, pipefd) == 1)
return (free_command(cmd), 1);
}
return (free_command(cmd), 0);
}
int execute_pipeline(t_node *root, t_node *node, t_minishell *minishell,
int pipefd[2][2])
{
if (!node)
return (0);
if (node->type == PIPE)
{
if (execute_pipeline(root, node->left, minishell, pipefd) == 1)
return (1);
if (execute_pipeline(root, node->right, minishell, pipefd) == 1)
return (1);
}
if (node->type == COMMAND)
{
if (exec_cmd_pipe(root, node, minishell, pipefd) == 1)
return (1);
}
return (0);
}