GCC Code Coverage Report


Directory: ./
File: src/separators/pipes.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 84 84 100.0%
Functions: 6 6 100.0%
Branches: 44 46 95.7%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the pipes functions
6 */
7 /**
8 * @file pipes.c
9 * @brief The file containing the pipes functions
10 */
11
12 #include "../../include/myshell.h"
13 #include <sys/mman.h>
14
15 /**
16 * @brief Check if the command contains an ambiguous command
17 * @param mysh The shell structure
18 * @return <b>int</b> <u>1</u> if an error occurred, <u>0</u> otherwise
19 */
20 104 static int check_ambiguous_command(mysh_t *mysh)
21 {
22
2/2
✓ Branch 0 taken 207 times.
✓ Branch 1 taken 99 times.
306 for (int index = 0; mysh->input_list[index] != NULL; index++) {
23
4/4
✓ Branch 0 taken 103 times.
✓ Branch 1 taken 104 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 100 times.
207 if (index != 0 && mysh->input_list[index]->left_type != 0) {
24 3 mysh->exit_status = 1;
25 3 my_putstr_error("Ambiguous input redirect.\n");
26 3 free_input_list(mysh);
27 3 FREE_WORD_ARRAY(mysh->pipe_cmds);
28 3 return 1;
29 }
30
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 99 times.
204 if (mysh->pipe_cmds[index + 1] != NULL &&
31
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 103 times.
105 mysh->input_list[index]->right_type != 0) {
32 2 mysh->exit_status = 1;
33 2 my_putstr_error("Ambiguous output redirect.\n");
34 2 free_input_list(mysh);
35 2 FREE_WORD_ARRAY(mysh->pipe_cmds);
36 2 return 1;
37 }
38 }
39 99 return 0;
40 }
41
42 /**
43 * @brief Check if the command is a null command
44 * @param mysh The shell structure
45 * @param line The command line
46 * @return <b>int</b> <u>1</u> if the command is a null command,
47 * <u>0</u> otherwise
48 */
49 1027 static int check_null_command(mysh_t *mysh, char *line)
50 {
51
2/2
✓ Branch 0 taken 176815 times.
✓ Branch 1 taken 916 times.
177731 for (int index = 0; line[index] != '\0' &&
52
2/2
✓ Branch 0 taken 176705 times.
✓ Branch 1 taken 110 times.
353519 mysh->pipe_cmds[1] == NULL; index++) {
53
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 176683 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 16 times.
176705 if (line[index] == '|' && char_is_inhibited(line, index) == 0
54
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 5 times.
6 && char_is_paranthesed(line, index) == 0) {
55 1 mysh->exit_status = 1;
56 1 my_putstr_error("Invalid null command.\n");
57 1 free_input_list(mysh);
58 1 FREE_WORD_ARRAY(mysh->pipe_cmds);
59 1 return 1;
60 }
61 }
62 1026 return 0;
63 }
64
65 /**
66 * @brief Check if the command contains a pipe or not
67 * @param mysh The shell structure
68 * @param line The command line
69 * @return <b>int</b> <u>1</u> if the command doesn't contain a pipe
70 * or only pipes, <u>0</u> if the command contains a pipe
71 */
72 1027 static int check_pipe(mysh_t *mysh, char *line)
73 {
74
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1026 times.
1027 if (check_null_command(mysh, line))
75 1 return 1;
76
2/2
✓ Branch 0 taken 916 times.
✓ Branch 1 taken 110 times.
1026 if (mysh->pipe_cmds[1] == NULL) {
77 916 select_redirections(mysh, mysh->input_list[0]);
78 554 free_input_list(mysh);
79 554 FREE_WORD_ARRAY(mysh->pipe_cmds);
80 554 return 1;
81 }
82
2/2
✓ Branch 0 taken 219 times.
✓ Branch 1 taken 104 times.
323 for (int index = 0; mysh->pipe_cmds[index] != NULL; index++) {
83
3/4
✓ Branch 1 taken 213 times.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 213 times.
219 if (my_str_is(mysh->pipe_cmds[index], " \t\n") || line[0] == '|') {
84 6 mysh->exit_status = 1;
85 6 my_putstr_error("Invalid null command.\n");
86 6 free_input_list(mysh);
87 6 FREE_WORD_ARRAY(mysh->pipe_cmds);
88 6 return 1;
89 }
90 }
91 104 return check_ambiguous_command(mysh);
92 }
93
94 /**
95 * @brief Set the pipe list (input_list and pipe_cmds)
96 * @param mysh The shell structure
97 * @param line The command line
98 * @return <b>void</b>
99 */
100 1046 int set_pipe_list(mysh_t *mysh, char *line)
101 {
102 1046 int index = 0;
103
104
3/4
✓ Branch 0 taken 1046 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 4 times.
✓ Branch 4 taken 1042 times.
1046 if (line == NULL || my_strcmp(line, "") == 0)
105 4 return 1;
106 1042 mysh->pipe_cmds = array_separators(line, "|");
107 1042 mysh->input_list = malloc(sizeof(input_command_t *) *
108 1042 (my_array_len((void **) mysh->pipe_cmds) + 1));
109
2/2
✓ Branch 0 taken 1153 times.
✓ Branch 1 taken 1027 times.
2180 for (; mysh->pipe_cmds[index] != NULL; index++) {
110 1153 mysh->input_list[index] = get_input_command(mysh->pipe_cmds[index]);
111
2/2
✓ Branch 0 taken 15 times.
✓ Branch 1 taken 1138 times.
1153 if (mysh->input_list[index] == NULL) {
112 15 mysh->exit_status = 1;
113 15 free_input_list(mysh);
114 15 FREE_WORD_ARRAY(mysh->pipe_cmds);
115 15 return 1;
116 }
117 }
118 1027 mysh->input_list[index] = NULL;
119 1027 return 0;
120 }
121
122 /**
123 * @brief Execute the pipe
124 * @param mysh The shell structure
125 * @param line The command line
126 * @param index The index of the command
127 * @return <b>void</b>
128 */
129 100 static void execute_pipe(mysh_t *mysh, char *line, int index)
130 {
131 int fd[2];
132 100 pid_t pid = 0;
133
134 100 pipe(fd);
135 100 pid = fork();
136
2/2
✓ Branch 0 taken 113 times.
✓ Branch 1 taken 100 times.
213 if (pid == 0) {
137 113 close(fd[0]);
138 113 dup2(fd[1], 1);
139 113 select_redirections(mysh, mysh->input_list[index]);
140 108 my_exit(mysh, mysh->exit_status, NULL);
141 } else {
142 100 close(fd[1]);
143 100 dup2(fd[0], 0);
144 }
145 100 }
146
147 /**
148 * @brief Analyse and execute the pipes
149 * @param mysh The shell structure
150 * @param line The command line
151 * @return <b>void</b>
152 */
153 1046 void analyse_pipes(mysh_t *mysh, char *line)
154 {
155
4/4
✓ Branch 1 taken 1027 times.
✓ Branch 2 taken 19 times.
✓ Branch 4 taken 566 times.
✓ Branch 5 taken 99 times.
1046 if (set_pipe_list(mysh, line) || check_pipe(mysh, line))
156 585 return;
157 99 mysh->saved_stdin = dup(0);
158 99 mysh->saved_stdout = dup(1);
159
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 99 times.
199 for (int index = 0; mysh->pipe_cmds[index + 1] != NULL; index++)
160 100 execute_pipe(mysh, line, index);
161 198 select_redirections(mysh, mysh->input_list
162 99 [my_array_len((void **)mysh->pipe_cmds) - 1]);
163 99 dup2(mysh->saved_stdin, 0);
164 99 dup2(mysh->saved_stdout, 1);
165 99 free_input_list(mysh);
166 99 FREE_WORD_ARRAY(mysh->pipe_cmds);
167 }
168