GCC Code Coverage Report


Directory: ./
File: src/separators/parentheses.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 84 97 86.6%
Functions: 7 7 100.0%
Branches: 65 78 83.3%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the parentheses functions
6 */
7 /**
8 * @file parentheses.c
9 * @brief The file containing the parentheses functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Check if the command contains parentheses
16 * @param mysh The shell structure
17 * @param input The input command structure
18 * @return <b>int</b> <u>0</u> if the command contains parentheses,
19 * <u>1</u> otherwise
20 */
21 1116 static int check_no_parentheses(mysh_t *mysh, input_command_t *input)
22 {
23
2/2
✓ Branch 1 taken 1103 times.
✓ Branch 2 taken 13 times.
1116 if (my_str_contains(input->command, "()") == 0) {
24 1103 input->args = str_to_array_inhibitors(input->command);
25 1103 command(mysh, input);
26 736 return 1;
27 }
28
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 for (int index = 0; input->command[index] != '\0'; index++) {
29
3/4
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
32 if (my_char_is(input->command[index], "()") &&
30 13 char_is_inhibited(input->command, index) == 0)
31 13 return 0;
32 }
33 input->args = str_to_array_inhibitors(input->command);
34 command(mysh, input);
35 return 1;
36 }
37
38 /**
39 * @brief Check if the command contains if and foreach
40 * @param mysh The shell structure
41 * @param input The input command structure
42 * @return <b>int</b> <u>1</u> if the command contains if and foreach,
43 * <u>0</u> otherwise
44 */
45 13 static int check_if_and_foreach(mysh_t *mysh, input_command_t *input)
46 {
47 13 int index = 0;
48
49
4/4
✓ Branch 0 taken 28 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 17 times.
✓ Branch 3 taken 11 times.
58 for (; input->command[index] != '\0' &&
50 45 my_char_is_alpha(input->command[index]) == 0; index++);
51
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 11 times.
13 if (input->command[index] == '\0')
52 2 return 0;
53
3/4
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 9 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
13 if ((my_strncmp(&input->command[index], "if", 2) == 0 &&
54
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 9 times.
11 my_char_is(input->command[index + 2], "( \t")) ||
55
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
9 (my_strncmp(&input->command[index], "foreach", 7) == 0 &&
56 my_char_is(input->command[index + 7], "( \t"))) {
57 2 input->args = str_to_array_inhibitors(input->command);
58 2 command(mysh, input);
59 2 return 1;
60 }
61 9 return 0;
62 }
63
64 /**
65 * @brief Check if there are too many parentheses
66 * @param mysh The shell structure
67 * @param command The command to check
68 * @return <b>int</b> <u>1</u> if there are too many parentheses,
69 * <u>0</u> otherwise
70 */
71 11 static int check_too_many(mysh_t *mysh, char *command)
72 {
73
2/2
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 9 times.
11 if (my_count_letter(command, '(') > my_count_letter(command, ')')) {
74 2 my_fprintf(2, "Too many ('s.\n");
75 2 mysh->exit_status = 1;
76 2 return 1;
77 }
78
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 9 times.
9 if (my_count_letter(command, '(') < my_count_letter(command, ')')) {
79 my_fprintf(2, "Too many )'s.\n");
80 mysh->exit_status = 1;
81 return 1;
82 }
83 9 for (int index = my_get_char_index(command, ')', 1);
84
2/2
✓ Branch 0 taken 38 times.
✓ Branch 1 taken 9 times.
47 command[index] != '\0'; index++) {
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if (command[index] == '(') {
86 my_fprintf(2, "Too many )'s.\n");
87 mysh->exit_status = 1;
88 return 1;
89 }
90 }
91 9 return 0;
92 }
93
94 /**
95 * @brief Check if the parentheses are badly placed
96 * @param mysh The shell structure
97 * @param command The command to check
98 * @return <b>int</b> <u>1</u> if the parentheses are badly placed,
99 * <u>0</u> otherwise
100 */
101 9 static int check_badly_placed(mysh_t *mysh, char *command)
102 {
103 9 int count = 0;
104
105
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 5 times.
129 for (int index = 0; command[index] != '\0'; index++) {
106
2/2
✓ Branch 1 taken 29 times.
✓ Branch 2 taken 95 times.
124 if (my_char_is(command[index], " \t\n")
107
1/2
✓ Branch 1 taken 29 times.
✗ Branch 2 not taken.
29 && char_is_inhibited(command, index) == 0)
108 29 continue;
109
4/4
✓ Branch 0 taken 86 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 9 times.
✓ Branch 3 taken 77 times.
95 if (index != 0 && command[index - 1] == '(')
110 9 count++;
111
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 86 times.
95 if (command[index] == ')')
112 9 count--;
113
4/4
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 73 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 4 times.
95 if (count != 0 || my_char_is(command[index], "()"))
114 91 continue;
115 4 my_fprintf(2, "Badly placed ()'s.\n");
116 4 mysh->exit_status = 1;
117 4 return 1;
118 }
119 5 return 0;
120 }
121
122 /**
123 * @brief Check if the command is null
124 * @param mysh The shell structure
125 * @param command The command to check
126 * @return <b>int</b> <u>1</u> if the command is null, <u>0</u> otherwise
127 */
128 5 static int check_null_command(mysh_t *mysh, char *command)
129 {
130
1/2
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
10 for (int index = 0; command[index] != '\0'; index++) {
131
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
10 if (my_char_is(command[index], " \t\n()")
132
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 && char_is_inhibited(command, index) == 0)
133 5 continue;
134 5 return 0;
135 }
136 my_fprintf(2, "Invalid null command.\n");
137 mysh->exit_status = 1;
138 return 1;
139 }
140
141 /**
142 * @brief Get the command between the parentheses
143 * @param command The command to check
144 * @return <b>char *</b> The command between the parentheses
145 */
146 5 static char *get_command(char *command)
147 {
148 5 char *new_command = NULL;
149 5 int nb_letters = 0;
150 5 int count = 0;
151
152
2/2
✓ Branch 0 taken 100 times.
✓ Branch 1 taken 5 times.
105 for (int index = 0; command[index] != '\0'; index++) {
153
4/4
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 5 times.
✓ Branch 3 taken 90 times.
100 if (index != 0 && command[index - 1] == '(')
154 5 count++;
155
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 95 times.
100 if (command[index] == ')')
156 5 count--;
157
3/4
✓ Branch 1 taken 10 times.
✓ Branch 2 taken 90 times.
✓ Branch 3 taken 10 times.
✗ Branch 4 not taken.
100 if (my_char_is(command[index], "()") && count == 0)
158 10 continue;
159
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 5 times.
90 if (count != 0)
160 85 nb_letters++;
161 }
162 5 new_command = my_strndup(my_strstr(command, "(") + 1, nb_letters);
163 5 return new_command;
164 }
165
166 /**
167 * @brief Analyse the parentheses
168 * @param mysh The shell structure
169 * @param input The input command structure
170 * @return <b>void</b>
171 */
172 1116 void analyse_parentheses(mysh_t *mysh, input_command_t *input)
173 {
174 1116 pid_t pid = 0;
175 1116 char *command = NULL;
176
177
4/4
✓ Branch 1 taken 13 times.
✓ Branch 2 taken 736 times.
✓ Branch 4 taken 11 times.
✓ Branch 5 taken 2 times.
1116 if (check_no_parentheses(mysh, input) || check_if_and_foreach(mysh, input)
178
4/4
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 4 times.
20 || check_too_many(mysh, input->command) ||
179
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
14 check_badly_placed(mysh, input->command) ||
180 5 check_null_command(mysh, input->command))
181 744 return;
182 5 command = get_command(input->command);
183 5 pid = fork();
184
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 if (pid == 0) {
185 5 analyse_multi_commands(mysh, command);
186 5 my_exit(mysh, mysh->exit_status, NULL);
187 }
188 5 FREE(command);
189 5 waitpid(pid, &mysh->exit_status, 0);
190 }
191