-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.c
More file actions
116 lines (107 loc) · 2.93 KB
/
Copy pathexecutor.c
File metadata and controls
116 lines (107 loc) · 2.93 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* executor.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fracerba <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/05/03 10:41:05 by fracerba #+# #+# */
/* Updated: 2023/05/03 10:41:08 by fracerba ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_cmd_cmds(t_prompt *prompt, t_cmd **cmd, char *input)
{
char **cmd_mat;
prompt->saved_g = g_status;
send_signal(0);
cmd_mat = ft_cmdsplit(input, ' ');
free(input);
cmd_mat = cmd_split_redir_and_pipes(cmd_mat);
if (g_status)
return (get_cmd_return(cmd_mat));
cmd_mat = cmd_check_pipes(cmd_mat, 0, prompt);
if (g_status)
return (get_cmd_return(cmd_mat));
send_signal(prompt->saved_g);
cmd_mat = expander(cmd_mat, prompt->envi);
send_signal(0);
prompt->cmds = parser(prompt, cmd_mat, 0, 0);
*cmd = prompt->cmds;
if (g_status > 1)
{
ft_free_cmds(*cmd);
return (1);
}
return (0);
}
int set_infile(t_cmd *cmd, int *saved_stdin)
{
if (cmd->infile)
{
*saved_stdin = dup(STDIN_FILENO);
close(STDIN_FILENO);
if (dup2(cmd->infile, STDIN_FILENO) == -1)
return (get_error(2, NULL, NULL));
}
return (0);
}
int builtin_execve(int saved_stdin, char ***out, t_cmd *cmd, t_prompt *p)
{
if (ft_is_builtin(cmd->command, 0))
return (builtin_time(saved_stdin, out, cmd, p));
else
return (execve_time(saved_stdin, out, cmd, p));
return (0);
}
int executor(t_cmd *cmd, t_prompt *p, char ***out, int err)
{
int saved_stdin;
int saved_stdout;
saved_stdin = -1;
saved_stdout = -1;
if (!cmd->command)
err = 1;
else
{
if (set_infile(cmd, &saved_stdin))
return (-1);
saved_stdout = dup(STDOUT_FILENO);
err = builtin_execve(saved_stdin, out, cmd, p);
}
if (err && !out)
*out = 0;
if (print_output(cmd, out, err))
return (1);
if (reset_input(cmd, saved_stdin))
return (-1);
if (reset_output(cmd, saved_stdout))
return (-1);
return (err);
}
int check_loop(t_prompt *prompt, char *input)
{
t_cmd *cmd;
char **out;
int i;
int j;
i = check_input(input);
if (i < 2)
return (i);
add_history(input);
if (get_cmd_cmds(prompt, &cmd, input))
return (1);
prompt->n_cmds = count_cmds_loop(cmd);
while (cmd)
{
out = NULL;
j = executor(cmd, prompt, &out, 0);
free_matrix(out);
if (g_status == 130 || j == -1)
break ;
cmd = cmd->next;
}
ft_free_cmds(prompt->cmds);
prompt->cmds = NULL;
return (1);
}