-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcommand_process.c
More file actions
98 lines (91 loc) · 3.26 KB
/
command_process.c
File metadata and controls
98 lines (91 loc) · 3.26 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* command_process.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tchobert <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/04 14:23:58 by tchobert #+# #+# */
/* Updated: 2025/02/04 14:24:11 by tchobert ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int setup_command_redirections(t_command *command)
{
if (command->command_redirections.in_stream != STDIN_FILENO
&& command->command_redirections.in_stream != -1)
{
dup2(command->command_redirections.in_stream, STDIN_FILENO);
close(command->command_redirections.in_stream);
}
if (command->command_redirections.out_stream != STDOUT_FILENO
&& command->command_redirections.out_stream != -1)
{
dup2(command->command_redirections.out_stream, STDOUT_FILENO);
close(command->command_redirections.out_stream);
}
if (command->command_redirections.opening_status == OPENING_ERROR)
{
ft_dprintf(STDERR_FILENO, "%s\n", command->opening_failure_msg);
return (EXIT_FAILURE);
}
return (EXIT_SUCCESS);
}
void close_command_process_unused_fds(t_minishell_context *minishell_context,
t_command *command)
{
t_command_pipeline cmd_pipeline;
cmd_pipeline = minishell_context->command_session.command_pipeline;
while (cmd_pipeline)
{
if (cmd_pipeline->content != command)
{
if (((t_command *)cmd_pipeline->content)
->command_redirections.in_stream != -1)
{
close(((t_command *)cmd_pipeline->content)
->command_redirections.in_stream);
}
if (((t_command *)cmd_pipeline->content)
->command_redirections.out_stream != -1)
{
close(((t_command *)cmd_pipeline->content)
->command_redirections.out_stream);
}
}
cmd_pipeline = cmd_pipeline->next;
}
}
static void undefined_command_process(t_minishell_context *minishell_context)
{
const int exit_value = get_exit_status_value();
clean_command_process(minishell_context);
exit(exit_value);
}
void command_process(t_minishell_context *minishell_context,
t_command *command)
{
close_command_process_unused_fds(minishell_context, command);
if (setup_command_redirections(command) == EXIT_FAILURE)
{
clean_command_process(minishell_context);
exit(EXIT_FAILURE);
}
if (command->command_nature == UNDEFINED)
{
undefined_command_process(minishell_context);
}
if (command->command_nature == BUILTIN)
execute_builtin(minishell_context, command, BUILTIN_IN_PIPELINE);
if (command->command_nature == ONLY_ASSIGNATION)
{
add_command_env_to_shell_env(command->command_environment);
clean_command_process(minishell_context);
exit(SUCCESS);
}
if (execute_command(command) == INVALID_COMMAND)
{
clean_command_process(minishell_context);
exit(COMMAND_NOT_FOUND_EXIT_STATUS);
}
}