| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the parsing functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file parsing.c | ||
| 9 | * @brief The file containing the parsing functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Check if a character is inhibited | ||
| 16 | * @param str The string to check | ||
| 17 | * @param index The index of the character | ||
| 18 | * @return <b>char</b> The inhibitor if the character is inhibited, | ||
| 19 | * <u>0</u> otherwise | ||
| 20 | */ | ||
| 21 | 1730 | char char_is_inhibited(char *str, int index) | |
| 22 | { | ||
| 23 | 1730 | char inhibitor = 0; | |
| 24 | |||
| 25 |
1/2✓ Branch 0 taken 18306 times.
✗ Branch 1 not taken.
|
18306 | for (int i = 0; i < index + 1; i++) { |
| 26 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18306 times.
|
18306 | if (str[i] == '\0') |
| 27 | ✗ | break; | |
| 28 |
4/6✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18305 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1 times.
|
18306 | if (inhibitor == str[i] && inhibitor != '\\' && i == index) |
| 29 | ✗ | return 0; | |
| 30 |
3/4✓ Branch 0 taken 1 times.
✓ Branch 1 taken 18305 times.
✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
|
18306 | if (inhibitor == str[i] && inhibitor != '\\') { |
| 31 | 1 | inhibitor = 0; | |
| 32 | 1 | continue; | |
| 33 | } | ||
| 34 |
2/2✓ Branch 0 taken 1730 times.
✓ Branch 1 taken 16575 times.
|
18305 | if (i == index) |
| 35 | 1730 | return inhibitor; | |
| 36 |
2/2✓ Branch 0 taken 20 times.
✓ Branch 1 taken 16555 times.
|
16575 | if (inhibitor == '\\') |
| 37 | 20 | inhibitor = 0; | |
| 38 |
3/4✓ Branch 1 taken 146 times.
✓ Branch 2 taken 16429 times.
✓ Branch 3 taken 146 times.
✗ Branch 4 not taken.
|
16575 | if (my_char_is(str[i], "\'\"\\") && inhibitor == 0) |
| 39 | 146 | inhibitor = str[i]; | |
| 40 | } | ||
| 41 | ✗ | return inhibitor; | |
| 42 | } | ||
| 43 | |||
| 44 | /** | ||
| 45 | * @brief Check if a char is paranthesed | ||
| 46 | * @param str The string | ||
| 47 | * @param index The index | ||
| 48 | * @return <b>int</b> <u>1</u> if the char is paranthesed, <u>0</u> otherwise | ||
| 49 | */ | ||
| 50 | 1421 | int char_is_paranthesed(char *str, int index) | |
| 51 | { | ||
| 52 | 1421 | int count = 0; | |
| 53 | |||
| 54 |
3/4✓ Branch 0 taken 17286 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 15865 times.
✓ Branch 3 taken 1421 times.
|
17286 | for (int i = 0; str[i] != '\0' && i <= index; i++) { |
| 55 |
2/2✓ Branch 0 taken 1421 times.
✓ Branch 1 taken 14444 times.
|
15865 | if (i == 0) |
| 56 | 1421 | continue; | |
| 57 |
2/2✓ Branch 0 taken 400 times.
✓ Branch 1 taken 14044 times.
|
14444 | if (str[i - 1] == '(') |
| 58 | 400 | count++; | |
| 59 |
2/2✓ Branch 0 taken 116 times.
✓ Branch 1 taken 14328 times.
|
14444 | if (str[i] == ')') |
| 60 | 116 | count--; | |
| 61 | } | ||
| 62 |
2/2✓ Branch 0 taken 284 times.
✓ Branch 1 taken 1137 times.
|
1421 | if (count > 0) |
| 63 | 284 | return 1; | |
| 64 | 1137 | return 0; | |
| 65 | } | ||
| 66 | |||
| 67 | /** | ||
| 68 | * @brief Check if a char is protected | ||
| 69 | * @param str The string | ||
| 70 | * @param index The index | ||
| 71 | * @return <b>int</b> <u>1</u> if the char is protected, <u>0</u> otherwise | ||
| 72 | */ | ||
| 73 | 1497 | int char_is_protected(char *str, int index) | |
| 74 | { | ||
| 75 |
4/4✓ Branch 1 taken 1384 times.
✓ Branch 2 taken 113 times.
✓ Branch 4 taken 247 times.
✓ Branch 5 taken 1137 times.
|
1497 | if (char_is_inhibited(str, index) || char_is_paranthesed(str, index)) |
| 76 | 360 | return 1; | |
| 77 | 1137 | return 0; | |
| 78 | } | ||
| 79 | |||
| 80 | /** | ||
| 81 | * @brief Find a valid string in a string | ||
| 82 | * depending on inhibitors and parentheses | ||
| 83 | * @note It's an improved version of my_strstr | ||
| 84 | * @param str The string to check | ||
| 85 | * @param to_find The string to find | ||
| 86 | * @return <b>char *</b> The address of the string if it's valid, | ||
| 87 | * <u>NULL</u> otherwise | ||
| 88 | */ | ||
| 89 | 2594 | char *find_valid_str(char *str, char const *to_find) | |
| 90 | { | ||
| 91 | 2594 | char *new_str = str; | |
| 92 | |||
| 93 |
2/2✓ Branch 1 taken 236 times.
✓ Branch 2 taken 2408 times.
|
2644 | while (my_strstr(new_str, to_find) != NULL) { |
| 94 |
2/2✓ Branch 2 taken 186 times.
✓ Branch 3 taken 50 times.
|
236 | if (char_is_protected(str, my_strstr(new_str, to_find) - str) == 0) |
| 95 | 186 | return my_strstr(new_str, to_find); | |
| 96 | 50 | new_str = my_strstr(new_str, to_find) + 1; | |
| 97 | } | ||
| 98 | 2408 | return NULL; | |
| 99 | } | ||
| 100 | |||
| 101 | /** | ||
| 102 | * @brief Check if the variable is valid | ||
| 103 | * @param variable The variable | ||
| 104 | * @param builtin The builtin who called the function | ||
| 105 | * @return <b>int</b> <u>1</u> if the variable is valid, <u>0</u> otherwise | ||
| 106 | */ | ||
| 107 | 18 | int is_valid_variable(char *variable, char *builtin) | |
| 108 | { | ||
| 109 |
3/6✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 18 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 18 times.
|
36 | if (variable == NULL || variable[0] == '\0' || |
| 110 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
18 | (my_char_is_alpha(variable[0]) == 0 && variable[0] != '_')) { |
| 111 | ✗ | my_fprintf(2, "%s: Variable name must begin with a letter.\n", | |
| 112 | builtin); | ||
| 113 | ✗ | return 0; | |
| 114 | } | ||
| 115 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 18 times.
|
108 | for (int index = 0; variable[index] != '\0'; index++) { |
| 116 |
3/4✓ Branch 1 taken 18 times.
✓ Branch 2 taken 72 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 18 times.
|
108 | if (my_char_is_alpha(variable[index]) == 0 && |
| 117 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
18 | my_char_is_num(variable[index]) == 0 && variable[index] != '_') { |
| 118 | ✗ | my_fprintf(2, "%s: " | |
| 119 | "Variable name must contain alphanumeric characters.\n", | ||
| 120 | builtin); | ||
| 121 | ✗ | return 0; | |
| 122 | } | ||
| 123 | } | ||
| 124 | 18 | return 1; | |
| 125 | } | ||
| 126 |