GCC Code Coverage Report


Directory: ./
File: src/parsing/parsing.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 42 51 82.4%
Functions: 5 5 100.0%
Branches: 41 58 70.7%

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 1208 char char_is_inhibited(char *str, int index)
22 {
23 1208 char inhibitor = 0;
24
25
1/2
✓ Branch 0 taken 15292 times.
✗ Branch 1 not taken.
15292 for (int i = 0; i < index + 1; i++) {
26
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 15292 times.
15292 if (str[i] == '\0')
27 break;
28
4/6
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15287 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 5 times.
15292 if (inhibitor == str[i] && inhibitor != '\\' && i == index)
29 return 0;
30
3/4
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 15287 times.
✓ Branch 2 taken 5 times.
✗ Branch 3 not taken.
15292 if (inhibitor == str[i] && inhibitor != '\\') {
31 5 inhibitor = 0;
32 5 continue;
33 }
34
2/2
✓ Branch 0 taken 1208 times.
✓ Branch 1 taken 14079 times.
15287 if (i == index)
35 1208 return inhibitor;
36
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14079 times.
14079 if (inhibitor == '\\')
37 inhibitor = 0;
38
3/4
✓ Branch 1 taken 149 times.
✓ Branch 2 taken 13930 times.
✓ Branch 3 taken 149 times.
✗ Branch 4 not taken.
14079 if (my_char_is(str[i], "\'\"\\") && inhibitor == 0)
39 149 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 1016 int char_is_paranthesed(char *str, int index)
51 {
52 1016 int count = 0;
53
54
3/4
✓ Branch 0 taken 14450 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 13434 times.
✓ Branch 3 taken 1016 times.
14450 for (int i = 0; str[i] != '\0' && i <= index; i++) {
55
2/2
✓ Branch 0 taken 1016 times.
✓ Branch 1 taken 12418 times.
13434 if (i == 0)
56 1016 continue;
57
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 12393 times.
12418 if (str[i - 1] == '(')
58 25 count++;
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12418 times.
12418 if (str[i] == ')')
60 count--;
61 }
62
2/2
✓ Branch 0 taken 25 times.
✓ Branch 1 taken 991 times.
1016 if (count > 0)
63 25 return 1;
64 991 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 1138 int char_is_protected(char *str, int index)
74 {
75
4/4
✓ Branch 1 taken 1010 times.
✓ Branch 2 taken 128 times.
✓ Branch 4 taken 20 times.
✓ Branch 5 taken 990 times.
1138 if (char_is_inhibited(str, index) || char_is_paranthesed(str, index))
76 148 return 1;
77 990 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 2174 char *find_valid_str(char *str, char const *to_find)
90 {
91 2174 char *new_str = str;
92
93
2/2
✓ Branch 1 taken 74 times.
✓ Branch 2 taken 2116 times.
2190 while (my_strstr(new_str, to_find) != NULL) {
94
2/2
✓ Branch 2 taken 58 times.
✓ Branch 3 taken 16 times.
74 if (char_is_protected(str, my_strstr(new_str, to_find) - str) == 0)
95 58 return my_strstr(new_str, to_find);
96 16 new_str = my_strstr(new_str, to_find) + 1;
97 }
98 2116 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 42 int is_valid_variable(char *variable, char *builtin)
108 {
109
3/6
✓ Branch 0 taken 42 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 42 times.
84 if (variable == NULL || variable[0] == '\0' ||
110
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
42 (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 229 times.
✓ Branch 1 taken 42 times.
271 for (int index = 0; variable[index] != '\0'; index++) {
116
3/4
✓ Branch 1 taken 56 times.
✓ Branch 2 taken 173 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 56 times.
285 if (my_char_is_alpha(variable[index]) == 0 &&
117
0/2
✗ Branch 1 not taken.
✗ Branch 2 not taken.
56 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 42 return 1;
125 }
126