-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhandle_line.c
More file actions
68 lines (63 loc) · 2.21 KB
/
handle_line.c
File metadata and controls
68 lines (63 loc) · 2.21 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* handle_line.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jwoo <jwoo@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/12 17:12:50 by jwoo #+# #+# */
/* Updated: 2021/08/16 10:57:04 by jwoo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void handle_simple(t_fd *fd, t_cmd *cmd, t_envp *envp)
{
int backup[2];
pipe(backup);
dup2(0, backup[0]);
dup2(1, backup[1]);
if (fd->fd[0] != -1)
dup2(fd->fd[0], 0);
if (fd->fd[1] != -1)
dup2(fd->fd[1], 1);
g_status = interpret_without_pipe(cmd->append_cmd, envp);
if (cmd->append_cmd != 0)
free_two_dimension(cmd->append_cmd);
cmd->append_cmd = 0;
dup2(backup[0], 0);
dup2(backup[1], 1);
close(backup[0]);
close(backup[1]);
close(fd->fd[0]);
close(fd->fd[1]);
if (fd->current_idx > 0)
close(fd->pipe_fds[fd->current_idx - 1][0]);
close(fd->pipe_fds[fd->idx][1]);
}
void handle_line(char **line_prompt, t_cmd *cmd, t_envp *envp, t_fd *fd)
{
t_quote quote;
set_signal();
add_history(line_prompt[0]);
check_quote(line_prompt[0], "e);
init_cmd(line_prompt[0], cmd, envp, quote);
init_fd(fd, cmd->command);
while (cmd->command)
{
set_fd_cmd(fd, cmd);
if (is_redirection(cmd->current_cmd->pre_flag) == 0 \
&& is_redirection(cmd->command->next_flag) == 1)
if (handle_redirect(cmd, fd) > 0)
return (free_cmd_fd(cmd, fd));
if (cmd->current_cmd->pre_flag == PIPE \
|| (cmd->command->next_flag == PIPE))
{
if (handle_pipe(cmd, fd, envp) > 0)
return (free_cmd_fd(cmd, fd));
}
else
handle_simple(fd, cmd, envp);
cmd->command = cmd->command->next;
}
return (free_cmd_fd(cmd, fd));
}