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
|
#include "minishell.h"
/* static int st_frame_init(t_command_frame *frame) */
/* { */
/* #<{(| if (pipe(frame->pipe_in) == -1) |)}># */
/* #<{(| return (-1); |)}># */
/* #<{(| if (pipe(frame->pipe_out) == -1) |)}># */
/* #<{(| return (-1); |)}># */
/* frame->pipe_in[MS_PIPE_READ] = -1; */
/* frame->pipe_in[MS_PIPE_WRITE] = -1; */
/* frame->pipe_out[MS_PIPE_READ] = -1; */
/* frame->pipe_out[MS_PIPE_WRITE] = -1; */
/* return (0); */
/* } */
/* */
/* #<{(| */
/* ** Run child process with executable located by `path`, his arguments in `argv` */
/* ** and the current environment variables in `envp`. */
/* ** Returns the child process pid or -1 if an error occured when forking. */
/* |)}># */
/* */
/* static pid_t st_run_process(char *path, char **argv, char **envp, t_command_frame *frame) */
/* { */
/* int i; */
/* pid_t child_pid; */
/* */
/* if (path == NULL || argv == NULL || envp == NULL || frame == NULL) */
/* return (-1); */
/* if ((child_pid = fork()) == -1) */
/* return (-1); */
/* if (child_pid == 0) */
/* { */
/* if (frame->pipe_in[MS_PIPE_READ] != -1) */
/* dup2(STDIN_FILENO, frame->pipe_in[MS_PIPE_WRITE]); */
/* if (frame->pipe_out[MS_PIPE_WRITE] != -1) */
/* dup2(STDOUT_FILENO, frame->pipe_out[MS_PIPE_READ]); */
/* if ((execve(path, argv, envp)) == -1) */
/* exit(EXIT_FAILURE); */
/* } */
/* else */
/* return (child_pid); */
/* } */
/* */
/* #<{(| */
/* ** Run a command. */
/* |)}># */
/* */
/* static int st_command_run(t_command *command, t_command_frame *frame) */
/* { */
/* t_redirection *tmp; */
/* */
/* if ((tmp = st_has_redirection_in(command)) != NULL) */
/* { */
/* if (pipe(frame->pipe_in) == -1) */
/* return (-1); */
/* if ((frame->pipe_in[MS_PIPE_READ] = open(tmp->filename, O_RDONLY)) < 0) */
/* return (-1); */
/* } */
/* if ((tmp = st_has_redirection_out(command)) != NULL) */
/* { */
/* if (pipe(frame->pipe_out) == -1) */
/* return (-1); */
/* if ((frame->pipe_out[MS_PIPE_WRITE] = open(tmp->filename, O_WRONLY | O_CREAT)) < 0) */
/* return (-1); */
/* } */
/* if ((child_pid = st_run_process(path, argv, envp, &frame)) == -1) */
/* return (-1); */
/* wait(&child_pid); */
/* return (0); */
/* } */
/* */
/* #<{(| */
/* ** Run a standalone command. */
/* |)}># */
/* */
/* static int st_command_standalone_run(t_command *command) */
/* { */
/* t_command_frame frame; */
/* */
/* if (st_frame_init(&frame) == -1) */
/* return (-1); */
/* return (st_command_run(command, &frame)); */
/* } */
/* */
/* #<{(| */
/* ** Run a piped command */
/* |)}># */
/* */
/* // in main loop, if previous was piped, init frame with previous pipe */
/* static int st_command_piped_run(t_command *command, int pipe_out[2]) */
/* { */
/* t_command_frame frame; */
/* */
/* if (st_frame_init(&frame) == -1) */
/* return (-1); */
/* frame.pipe_out[MS_PIPE_WRITE] = */
/* */
/* } */
/* */
/* */
/* #<{(| */
/* ** Evaluate the commands in parsing according by what they are separated by. */
/* |)}># */
/* */
/* int ms_eval(t_parsing *parsing) */
/* { */
/* pid_t child_pid; */
/* t_command_frame frame; */
/* */
/* if (parsing == NULL) */
/* return (-1); */
/* if (ft_lstsize(parsing->commands) != ft_lstsize(parsing->separators) - 1) */
/* return (-1); */
/* while (parsing->separators != NULL) */
/* { */
/* if ((t_separator)parsing->separators->content == SEPARATOR_SEMICOLON) */
/* { */
/* } */
/* else if ((t_separator)parsing->separators->content == SEPARATOR_PIPE) */
/* { */
/* */
/* // if has out redirection */
/* // */
/* */
/* // create pipe */
/* // fork fst */
/* // redirect stdout to pipe in */
/* // execve */
/* */
/* // fork snd */
/* // redirect std to pipe out */
/* // execve */
/* */
/* } */
/* ft_lstpop_front(&parsing->commands, free); */
/* ft_lstpop_front(&parsing->separators, NULL); */
/* } */
/* return (0); */
/* } */
|