| 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 | 1248 | static int check_no_parentheses(mysh_t *mysh, input_command_t *input) | |
| 22 | { | ||
| 23 |
2/2✓ Branch 1 taken 1204 times.
✓ Branch 2 taken 44 times.
|
1248 | if (my_str_contains(input->command, "()") == 0) { |
| 24 | 1204 | input->args = str_to_array_inhibitors(input->command); | |
| 25 | 1204 | command(mysh, input); | |
| 26 | 847 | return 1; | |
| 27 | } | ||
| 28 |
1/2✓ Branch 0 taken 77 times.
✗ Branch 1 not taken.
|
77 | for (int index = 0; input->command[index] != '\0'; index++) { |
| 29 |
3/4✓ Branch 1 taken 44 times.
✓ Branch 2 taken 33 times.
✓ Branch 3 taken 44 times.
✗ Branch 4 not taken.
|
121 | if (my_char_is(input->command[index], "()") && |
| 30 | 44 | char_is_inhibited(input->command, index) == 0) | |
| 31 | 44 | 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 | 44 | static int check_if_and_foreach(mysh_t *mysh, input_command_t *input) | |
| 46 | { | ||
| 47 | 44 | int index = 0; | |
| 48 | |||
| 49 |
4/4✓ Branch 0 taken 89 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 48 times.
✓ Branch 3 taken 41 times.
|
181 | for (; input->command[index] != '\0' && |
| 50 | 137 | my_char_is_alpha(input->command[index]) == 0; index++); | |
| 51 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 41 times.
|
44 | if (input->command[index] == '\0') |
| 52 | 3 | return 0; | |
| 53 |
3/4✓ Branch 1 taken 6 times.
✓ Branch 2 taken 35 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
|
47 | if ((my_strncmp(&input->command[index], "if", 2) == 0 && |
| 54 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
|
41 | my_char_is(input->command[index + 2], "( \t")) || |
| 55 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
35 | (my_strncmp(&input->command[index], "foreach", 7) == 0 && |
| 56 | ✗ | my_char_is(input->command[index + 7], "( \t"))) { | |
| 57 | 6 | input->args = str_to_array_inhibitors(input->command); | |
| 58 | 6 | command(mysh, input); | |
| 59 | 6 | return 1; | |
| 60 | } | ||
| 61 | 35 | 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 | 38 | static int check_too_many(mysh_t *mysh, char *command) | |
| 72 | { | ||
| 73 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 37 times.
|
38 | if (my_count_letter(command, '(') > my_count_letter(command, ')')) { |
| 74 | 1 | my_fprintf(2, "Too many ('s.\n"); | |
| 75 | 1 | mysh->exit_status = 1; | |
| 76 | 1 | return 1; | |
| 77 | } | ||
| 78 |
2/2✓ Branch 2 taken 1 times.
✓ Branch 3 taken 36 times.
|
37 | if (my_count_letter(command, '(') < my_count_letter(command, ')')) { |
| 79 | 1 | my_fprintf(2, "Too many )'s.\n"); | |
| 80 | 1 | mysh->exit_status = 1; | |
| 81 | 1 | return 1; | |
| 82 | } | ||
| 83 | 36 | for (int index = my_get_char_index(command, ')', 1); | |
| 84 |
2/2✓ Branch 0 taken 67 times.
✓ Branch 1 taken 35 times.
|
102 | command[index] != '\0'; index++) { |
| 85 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 66 times.
|
67 | if (command[index] == '(') { |
| 86 | 1 | my_fprintf(2, "Too many )'s.\n"); | |
| 87 | 1 | mysh->exit_status = 1; | |
| 88 | 1 | return 1; | |
| 89 | } | ||
| 90 | } | ||
| 91 | 35 | 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 | 35 | static int check_badly_placed(mysh_t *mysh, char *command) | |
| 102 | { | ||
| 103 | 35 | int count = 0; | |
| 104 | |||
| 105 |
2/2✓ Branch 0 taken 482 times.
✓ Branch 1 taken 30 times.
|
512 | for (int index = 0; command[index] != '\0'; index++) { |
| 106 |
2/2✓ Branch 1 taken 110 times.
✓ Branch 2 taken 372 times.
|
482 | if (my_char_is(command[index], " \t\n") |
| 107 |
1/2✓ Branch 1 taken 110 times.
✗ Branch 2 not taken.
|
110 | && char_is_inhibited(command, index) == 0) |
| 108 | 110 | continue; | |
| 109 |
4/4✓ Branch 0 taken 337 times.
✓ Branch 1 taken 35 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 307 times.
|
372 | if (index != 0 && command[index - 1] == '(') |
| 110 | 30 | count++; | |
| 111 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 342 times.
|
372 | if (command[index] == ')') |
| 112 | 30 | count--; | |
| 113 |
4/4✓ Branch 0 taken 65 times.
✓ Branch 1 taken 307 times.
✓ Branch 3 taken 60 times.
✓ Branch 4 taken 5 times.
|
372 | if (count != 0 || my_char_is(command[index], "()")) |
| 114 | 367 | continue; | |
| 115 | 5 | my_fprintf(2, "Badly placed ()'s.\n"); | |
| 116 | 5 | mysh->exit_status = 1; | |
| 117 | 5 | return 1; | |
| 118 | } | ||
| 119 | 30 | 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 | 30 | static int check_null_command(mysh_t *mysh, char *command) | |
| 129 | { | ||
| 130 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | for (int index = 0; command[index] != '\0'; index++) { |
| 131 |
2/2✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
|
60 | if (my_char_is(command[index], " \t\n()") |
| 132 |
1/2✓ Branch 1 taken 30 times.
✗ Branch 2 not taken.
|
30 | && char_is_inhibited(command, index) == 0) |
| 133 | 30 | continue; | |
| 134 | 30 | 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 | 30 | static char *get_command(char *command) | |
| 147 | { | ||
| 148 | 30 | char *new_command = NULL; | |
| 149 | 30 | int nb_letters = 0; | |
| 150 | 30 | int count = 0; | |
| 151 | |||
| 152 |
2/2✓ Branch 0 taken 477 times.
✓ Branch 1 taken 30 times.
|
507 | for (int index = 0; command[index] != '\0'; index++) { |
| 153 |
4/4✓ Branch 0 taken 447 times.
✓ Branch 1 taken 30 times.
✓ Branch 2 taken 30 times.
✓ Branch 3 taken 417 times.
|
477 | if (index != 0 && command[index - 1] == '(') |
| 154 | 30 | count++; | |
| 155 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 447 times.
|
477 | if (command[index] == ')') |
| 156 | 30 | count--; | |
| 157 |
3/4✓ Branch 1 taken 60 times.
✓ Branch 2 taken 417 times.
✓ Branch 3 taken 60 times.
✗ Branch 4 not taken.
|
477 | if (my_char_is(command[index], "()") && count == 0) |
| 158 | 60 | continue; | |
| 159 |
2/2✓ Branch 0 taken 393 times.
✓ Branch 1 taken 24 times.
|
417 | if (count != 0) |
| 160 | 393 | nb_letters++; | |
| 161 | } | ||
| 162 | 30 | new_command = my_strndup(my_strstr(command, "(") + 1, nb_letters); | |
| 163 | 30 | 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 | 1248 | void analyse_parentheses(mysh_t *mysh, input_command_t *input) | |
| 173 | { | ||
| 174 | 1248 | pid_t pid = 0; | |
| 175 | 1248 | char *command = NULL; | |
| 176 | |||
| 177 |
4/4✓ Branch 1 taken 44 times.
✓ Branch 2 taken 847 times.
✓ Branch 4 taken 38 times.
✓ Branch 5 taken 6 times.
|
1248 | if (check_no_parentheses(mysh, input) || check_if_and_foreach(mysh, input) |
| 178 |
4/4✓ Branch 1 taken 35 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 30 times.
✓ Branch 4 taken 5 times.
|
73 | || check_too_many(mysh, input->command) || |
| 179 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 30 times.
|
65 | check_badly_placed(mysh, input->command) || |
| 180 | 30 | check_null_command(mysh, input->command)) | |
| 181 | 861 | return; | |
| 182 | 30 | command = get_command(input->command); | |
| 183 | 30 | pid = fork(); | |
| 184 |
2/2✓ Branch 0 taken 30 times.
✓ Branch 1 taken 30 times.
|
60 | if (pid == 0) { |
| 185 | 30 | analyse_multi_commands(mysh, command); | |
| 186 | 30 | my_exit(mysh, mysh->exit_status, NULL); | |
| 187 | } | ||
| 188 | 30 | FREE(command); | |
| 189 | 30 | waitpid(pid, &mysh->exit_status, 0); | |
| 190 | } | ||
| 191 |