GCC Code Coverage Report


Directory: ./
File: src/separators/operators.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 66 67 98.5%
Functions: 4 4 100.0%
Branches: 55 64 85.9%

Line Branch Exec Source
1 /*
2 ** EPITECH PROJECT, 2024
3 ** 42sh
4 ** File description:
5 ** The file containing the operators functions
6 */
7 /**
8 * @file operators.c
9 * @brief The file containing the operators functions
10 */
11
12 #include "../../include/myshell.h"
13
14 /**
15 * @brief Check if the command is invalid
16 * @param mysh The shell structure
17 * @param line The command line
18 * @return <b>int</b> <u>1</u> if the command is invalid, <u>0</u> otherwise
19 */
20 1048 static int check_invalid_command(mysh_t *mysh, char *line)
21 {
22 1048 int ampersand = 0;
23 1048 int pipe = 0;
24
25
2/2
✓ Branch 0 taken 179026 times.
✓ Branch 1 taken 1041 times.
180067 for (int index = 0; line[index] != '\0'; index++) {
26
5/6
✓ Branch 0 taken 179026 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 179019 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 24 times.
✓ Branch 5 taken 178995 times.
179026 if (ampersand > 2 || pipe > 2 || line[index] == '&' &&
27
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 24 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
24 line[index + 1] == '|' && char_is_protected(line, index) == 0) {
28 7 return 1;
29 }
30
4/4
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 178995 times.
✓ Branch 3 taken 12 times.
✓ Branch 4 taken 12 times.
179019 if (line[index] == '&' && char_is_protected(line, index) == 0)
31 12 ampersand++;
32
4/4
✓ Branch 0 taken 168 times.
✓ Branch 1 taken 178851 times.
✓ Branch 3 taken 147 times.
✓ Branch 4 taken 21 times.
179019 if (line[index] == '|' && char_is_protected(line, index) == 0)
33 147 pipe++;
34
6/6
✓ Branch 0 taken 178995 times.
✓ Branch 1 taken 24 times.
✓ Branch 2 taken 178827 times.
✓ Branch 3 taken 168 times.
✓ Branch 4 taken 171990 times.
✓ Branch 5 taken 6837 times.
179019 if (line[index] != '&' && line[index] != '|' && line[index] != ' '
35
4/4
✓ Branch 0 taken 171970 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 171021 times.
✓ Branch 3 taken 949 times.
171990 && line[index] != '\t' && line[index] != '\n') {
36 171021 ampersand = 0;
37 171021 pipe = 0;
38 }
39 }
40 1041 return 0;
41 }
42
43 /**
44 * @brief Execute the operators
45 * @param mysh The shell structure
46 * @return <b>void</b>
47 */
48 1041 static void execute_operators(mysh_t *mysh)
49 {
50 1041 node_t *operator = NULL;
51 1041 int skip = 0;
52
53
2/2
✓ Branch 0 taken 1046 times.
✓ Branch 1 taken 679 times.
1725 for (; mysh->operators_cmds != NULL;) {
54 1046 analyse_pipes(mysh, mysh->operators_cmds->data);
55 684 FREE(mysh->operators_cmds->data);
56 684 my_free_ptr(my_pop_front(&mysh->operators_cmds));
57 684 operator = my_pop_front(&mysh->operators_list);
58
4/4
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 679 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 7 times.
692 while (operator != NULL && ((my_strcmp(operator->data, "&&") == 0 &&
59
5/6
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 5 times.
✓ Branch 4 taken 7 times.
✓ Branch 5 taken 5 times.
18 (mysh->exit_status != 0 || skip)) ||
60
1/2
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
19 (my_strcmp(operator->data, "||") == 0 && mysh->exit_status == 0))) {
61 8 FREE(operator);
62 8 my_free_ptr(my_pop_front(&mysh->operators_cmds));
63 8 operator = my_pop_front(&mysh->operators_list);
64 8 skip = 1;
65 }
66 684 skip = 0;
67 }
68 679 }
69
70 /**
71 * @brief Push the line in the commands and operators lists
72 * @param mysh The shell structure
73 * @param line The line to push
74 * @param backup The backup of the line
75 * @return <b>int</b> <u>1</u> if the line has been pushed, <u>0</u> otherwise
76 */
77 13 static int push_line(mysh_t *mysh, char **line, char *backup)
78 {
79
4/4
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
20 if (find_valid_str(*line, "&&") != NULL &&
80 7 (find_valid_str(*line, "&&") < find_valid_str(*line, "||")
81
2/2
✓ Branch 1 taken 5 times.
✓ Branch 2 taken 1 times.
6 || find_valid_str(*line, "||") == NULL)) {
82 6 my_push_back(&mysh->operators_cmds,
83 6 my_strndup(*line, my_strstr(*line, "&&") - *line), STRING);
84 6 my_push_back(&mysh->operators_list, "&&", STRING);
85 6 *line = find_valid_str(*line, "&&") + 2;
86 6 return 1;
87 }
88
3/4
✓ Branch 1 taken 7 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1 times.
14 if (find_valid_str(*line, "||") != NULL &&
89 7 (find_valid_str(*line, "||") < find_valid_str(*line, "&&")
90
1/2
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
6 || find_valid_str(*line, "&&") == NULL)) {
91 7 my_push_back(&mysh->operators_cmds,
92 7 my_strndup(*line, my_strstr(*line, "||") - *line), STRING);
93 7 my_push_back(&mysh->operators_list, "||", STRING);
94 7 *line = find_valid_str(*line, "||") + 2;
95 7 return 1;
96 }
97 return 0;
98 }
99
100 /**
101 * @brief Analyse the operators and execute the commands
102 * @param mysh The shell structure
103 * @param line The command line
104 * @return <b>void</b>
105 */
106 1048 void analyse_operators(mysh_t *mysh, char *line)
107 {
108 1048 char *backup = line;
109
110 1048 mysh->operators_cmds = NULL;
111 1048 mysh->operators_list = NULL;
112
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1041 times.
1048 if (check_invalid_command(mysh, line)) {
113 7 mysh->exit_status = 1;
114 7 my_putstr_error("Invalid null command.\n");
115 7 return;
116 }
117
4/4
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 1047 times.
✓ Branch 3 taken 6 times.
✓ Branch 4 taken 1041 times.
2101 while (find_valid_str(line, "&&") != NULL ||
118 1047 find_valid_str(line, "||") != NULL) {
119
1/2
✓ Branch 1 taken 13 times.
✗ Branch 2 not taken.
13 if (push_line(mysh, &line, backup))
120 13 continue;
121 }
122 1041 my_push_back(&mysh->operators_cmds, my_strdup(line), STRING);
123 1041 execute_operators(mysh);
124 679 my_delete_list(&mysh->operators_cmds);
125 679 my_delete_list(&mysh->operators_list);
126 }
127