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 | 1146 | static int check_invalid_command(mysh_t *mysh, char *line) | |
21 | { | ||
22 | 1146 | int ampersand = 0; | |
23 | 1146 | int pipe = 0; | |
24 | |||
25 |
2/2✓ Branch 0 taken 43611 times.
✓ Branch 1 taken 1143 times.
|
44754 | for (int index = 0; line[index] != '\0'; index++) { |
26 |
5/6✓ Branch 0 taken 43611 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 43608 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 88 times.
✓ Branch 5 taken 43520 times.
|
43611 | if (ampersand > 2 || pipe > 2 || line[index] == '&' && |
27 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
|
88 | line[index + 1] == '|' && char_is_protected(line, index) == 0) { |
28 | 3 | return 1; | |
29 | } | ||
30 |
4/4✓ Branch 0 taken 88 times.
✓ Branch 1 taken 43520 times.
✓ Branch 3 taken 52 times.
✓ Branch 4 taken 36 times.
|
43608 | if (line[index] == '&' && char_is_protected(line, index) == 0) |
31 | 52 | ampersand++; | |
32 |
4/4✓ Branch 0 taken 212 times.
✓ Branch 1 taken 43396 times.
✓ Branch 3 taken 163 times.
✓ Branch 4 taken 49 times.
|
43608 | if (line[index] == '|' && char_is_protected(line, index) == 0) |
33 | 163 | pipe++; | |
34 |
6/6✓ Branch 0 taken 43520 times.
✓ Branch 1 taken 88 times.
✓ Branch 2 taken 43308 times.
✓ Branch 3 taken 212 times.
✓ Branch 4 taken 40644 times.
✓ Branch 5 taken 2664 times.
|
43608 | if (line[index] != '&' && line[index] != '|' && line[index] != ' ' |
35 |
4/4✓ Branch 0 taken 40629 times.
✓ Branch 1 taken 15 times.
✓ Branch 2 taken 39590 times.
✓ Branch 3 taken 1039 times.
|
40644 | && line[index] != '\t' && line[index] != '\n') { |
36 | 39590 | ampersand = 0; | |
37 | 39590 | pipe = 0; | |
38 | } | ||
39 | } | ||
40 | 1143 | return 0; | |
41 | } | ||
42 | |||
43 | /** | ||
44 | * @brief Execute the operators | ||
45 | * @param mysh The shell structure | ||
46 | * @return <b>void</b> | ||
47 | */ | ||
48 | 1143 | static void execute_operators(mysh_t *mysh) | |
49 | { | ||
50 | 1143 | node_t *operator = NULL; | |
51 | 1143 | int skip = 0; | |
52 | |||
53 |
2/2✓ Branch 0 taken 1168 times.
✓ Branch 1 taken 787 times.
|
1955 | for (; mysh->operators_cmds != NULL;) { |
54 | 1168 | analyse_pipes(mysh, mysh->operators_cmds->data); | |
55 | 812 | FREE(mysh->operators_cmds->data); | |
56 | 812 | my_free_ptr(my_pop_front(&mysh->operators_cmds)); | |
57 | 812 | operator = my_pop_front(&mysh->operators_list); | |
58 |
4/4✓ Branch 0 taken 45 times.
✓ Branch 1 taken 787 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 19 times.
|
832 | while (operator != NULL && ((my_strcmp(operator->data, "&&") == 0 && |
59 |
6/6✓ Branch 0 taken 6 times.
✓ Branch 1 taken 20 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 19 times.
✓ Branch 4 taken 19 times.
✓ Branch 5 taken 19 times.
|
64 | (mysh->exit_status != 0 || skip)) || |
60 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 6 times.
|
57 | (my_strcmp(operator->data, "||") == 0 && mysh->exit_status == 0))) { |
61 | 20 | FREE(operator); | |
62 | 20 | my_free_ptr(my_pop_front(&mysh->operators_cmds)); | |
63 | 20 | operator = my_pop_front(&mysh->operators_list); | |
64 | 20 | skip = 1; | |
65 | } | ||
66 | 812 | skip = 0; | |
67 | } | ||
68 | 787 | } | |
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 | 45 | static int push_line(mysh_t *mysh, char **line, char *backup) | |
78 | { | ||
79 |
4/4✓ Branch 1 taken 27 times.
✓ Branch 2 taken 18 times.
✓ Branch 3 taken 26 times.
✓ Branch 4 taken 1 times.
|
72 | if (find_valid_str(*line, "&&") != NULL && |
80 | 27 | (find_valid_str(*line, "&&") < find_valid_str(*line, "||") | |
81 |
2/2✓ Branch 1 taken 25 times.
✓ Branch 2 taken 1 times.
|
26 | || find_valid_str(*line, "||") == NULL)) { |
82 | 26 | my_push_back(&mysh->operators_cmds, | |
83 | 26 | my_strndup(*line, my_strstr(*line, "&&") - *line), STRING); | |
84 | 26 | my_push_back(&mysh->operators_list, "&&", STRING); | |
85 | 26 | *line = find_valid_str(*line, "&&") + 2; | |
86 | 26 | return 1; | |
87 | } | ||
88 |
3/4✓ Branch 1 taken 19 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1 times.
|
38 | if (find_valid_str(*line, "||") != NULL && |
89 | 19 | (find_valid_str(*line, "||") < find_valid_str(*line, "&&") | |
90 |
1/2✓ Branch 1 taken 18 times.
✗ Branch 2 not taken.
|
18 | || find_valid_str(*line, "&&") == NULL)) { |
91 | 19 | my_push_back(&mysh->operators_cmds, | |
92 | 19 | my_strndup(*line, my_strstr(*line, "||") - *line), STRING); | |
93 | 19 | my_push_back(&mysh->operators_list, "||", STRING); | |
94 | 19 | *line = find_valid_str(*line, "||") + 2; | |
95 | 19 | 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 | 1146 | void analyse_operators(mysh_t *mysh, char *line) | |
107 | { | ||
108 | 1146 | char *backup = line; | |
109 | |||
110 | 1146 | mysh->operators_cmds = NULL; | |
111 | 1146 | mysh->operators_list = NULL; | |
112 |
2/2✓ Branch 1 taken 3 times.
✓ Branch 2 taken 1143 times.
|
1146 | if (check_invalid_command(mysh, line)) { |
113 | 3 | mysh->exit_status = 1; | |
114 | 3 | my_putstr_error("Invalid null command.\n"); | |
115 | 3 | return; | |
116 | } | ||
117 |
4/4✓ Branch 1 taken 27 times.
✓ Branch 2 taken 1161 times.
✓ Branch 3 taken 18 times.
✓ Branch 4 taken 1143 times.
|
2349 | while (find_valid_str(line, "&&") != NULL || |
118 | 1161 | find_valid_str(line, "||") != NULL) { | |
119 |
1/2✓ Branch 1 taken 45 times.
✗ Branch 2 not taken.
|
45 | if (push_line(mysh, &line, backup)) |
120 | 45 | continue; | |
121 | } | ||
122 | 1143 | my_push_back(&mysh->operators_cmds, my_strdup(line), STRING); | |
123 | 1143 | execute_operators(mysh); | |
124 | 787 | my_delete_list(&mysh->operators_cmds); | |
125 | 787 | my_delete_list(&mysh->operators_list); | |
126 | } | ||
127 |