Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2024 | ||
3 | ** 42sh | ||
4 | ** File description: | ||
5 | ** The file containing the if builtin | ||
6 | */ | ||
7 | /** | ||
8 | * @file if.c | ||
9 | * @brief The file containing the if builtin | ||
10 | */ | ||
11 | |||
12 | #include "../../include/myshell.h" | ||
13 | |||
14 | /** | ||
15 | * @brief Execute the commands specified when the condition is true | ||
16 | * @param mysh The shell structure | ||
17 | * @param args The command arguments | ||
18 | * @param start The index of the start of the command arguments | ||
19 | */ | ||
20 | 1 | static int exec_true_condition(mysh_t *mysh, char **args, int start) | |
21 | { | ||
22 | 1 | pid_t pid = 0; | |
23 | 1 | int size = 0; | |
24 | 1 | char *command = NULL; | |
25 | |||
26 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (int index = start; args[index] != NULL; index++) |
27 | 1 | size += my_strlen(args[index]) + 1; | |
28 | 1 | command = calloc(size, sizeof(char)); | |
29 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | for (int index = start; args[index] != NULL; index++) { |
30 | 1 | my_strcat(command, args[index]); | |
31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (args[index + 1] != NULL) |
32 | ✗ | my_strcat(command, " "); | |
33 | } | ||
34 | 1 | pid = fork(); | |
35 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (pid == 0) { |
36 | 1 | analyse_backticks(mysh, command); | |
37 | 1 | my_exit(mysh, mysh->exit_status, NULL); | |
38 | } | ||
39 | 1 | waitpid(pid, &mysh->exit_status, 0); | |
40 | 1 | FREE(command); | |
41 | 1 | return mysh->exit_status; | |
42 | } | ||
43 | |||
44 | /** | ||
45 | * @brief Read the input of the user when the condition is false | ||
46 | * @param mysh The shell structure | ||
47 | * @return <b>char **</b> The command arguments | ||
48 | */ | ||
49 | 8 | static char **read_input(mysh_t *mysh) | |
50 | { | ||
51 | 8 | int size = 0; | |
52 | 8 | char *line = NULL; | |
53 | 8 | char **content = NULL; | |
54 | |||
55 |
5/6✓ Branch 0 taken 24 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 8 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 16 times.
✗ Branch 5 not taken.
|
46 | while (size != EOF && (content == NULL || |
56 |
2/2✓ Branch 2 taken 14 times.
✓ Branch 3 taken 2 times.
|
32 | (my_strcmp(content[0], "endif") && my_strcmp(content[0], "else")))) { |
57 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 22 times.
|
22 | IS_ATTY_PRINT("if? "); |
58 | 22 | free_str_and_tab(line, NULL); | |
59 | 22 | size = my_getline(&line, stdin); | |
60 | 22 | set_command_in_history(mysh, line); | |
61 | 22 | free_str_and_tab(NULL, content); | |
62 | 22 | content = str_to_array_inhibitors(line); | |
63 | } | ||
64 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 2 times.
✗ Branch 6 not taken.
|
8 | if (size == EOF || (content != NULL && my_strcmp(content[0], "endif") |
65 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | && my_strcmp(content[0], "else"))) { |
66 | 6 | my_putstr_error("then: then/endif not found.\n"); | |
67 | 6 | return free_str_and_tab(line, content); | |
68 | } | ||
69 | 2 | free_str_and_tab(line, NULL); | |
70 | 2 | return content; | |
71 | } | ||
72 | |||
73 | /** | ||
74 | * @brief Execute the false condition who wait for an else or an endif | ||
75 | * @param mysh The shell structure | ||
76 | * @return <b>int</b> The exit status of the command executed | ||
77 | */ | ||
78 | 8 | static int exec_false_condition(mysh_t *mysh) | |
79 | { | ||
80 | 8 | char **line_content = read_input(mysh); | |
81 | 8 | char **new_line_content = NULL; | |
82 | |||
83 |
3/4✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
|
8 | if (line_content == NULL || line_content[0] == NULL) { |
84 | 6 | FREE_WORD_ARRAY(line_content); | |
85 | 6 | return 1; | |
86 | } | ||
87 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (my_strcmp(line_content[0], "endif") == 0) { |
88 | ✗ | FREE_WORD_ARRAY(line_content); | |
89 | ✗ | return 0; | |
90 | } | ||
91 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (my_strncmp(line_content[1], "if", 2) != 0) { |
92 | ✗ | new_line_content = my_malloc_strdup_word_array(line_content); | |
93 | ✗ | FREE_WORD_ARRAY(line_content); | |
94 | ✗ | return exec_true_condition(mysh, new_line_content, 1); | |
95 | } | ||
96 | 2 | mysh->exit_status = exec_else_if(mysh, line_content); | |
97 | 2 | FREE_WORD_ARRAY(line_content); | |
98 | 2 | return mysh->exit_status; | |
99 | } | ||
100 | |||
101 | /** | ||
102 | * @brief The else if builtin | ||
103 | * @param mysh The shell structure | ||
104 | * @param line_content The command arguments | ||
105 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
106 | */ | ||
107 | 2 | int exec_else_if(mysh_t *mysh, char **line_content) | |
108 | { | ||
109 | 2 | int condition = 0; | |
110 | |||
111 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (line_content[2] == NULL) { |
112 | ✗ | my_putstr_error("if: Too few arguments.\n"); | |
113 | ✗ | return 1; | |
114 | } | ||
115 | 2 | condition = get_condition(line_content[2]); | |
116 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (condition == -1) |
117 | ✗ | return 1; | |
118 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (line_content[3] == NULL) { |
119 | ✗ | my_putstr_error("if: Empty if.\n"); | |
120 | ✗ | return 1; | |
121 | } | ||
122 |
2/4✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
|
2 | if (condition && my_strcmp(line_content[3], "then") == 0) |
123 | 2 | return 0; | |
124 | ✗ | if (condition) | |
125 | ✗ | return exec_true_condition(mysh, line_content, 3); | |
126 | ✗ | return exec_false_condition(mysh); | |
127 | } | ||
128 | |||
129 | /** | ||
130 | * @brief The if builtin | ||
131 | * @param mysh The shell structure | ||
132 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
133 | */ | ||
134 | 15 | int exec_if(mysh_t *mysh) | |
135 | { | ||
136 | 15 | int condition = 0; | |
137 | |||
138 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 12 times.
|
15 | if (mysh->args[1] == NULL) { |
139 | 3 | my_putstr_error("if: Too few arguments.\n"); | |
140 | 3 | return 1; | |
141 | } | ||
142 | 12 | condition = get_condition(mysh->args[1]); | |
143 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
|
12 | if (condition == -1) |
144 | ✗ | return 1; | |
145 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
|
12 | if (mysh->args[2] == NULL) { |
146 | 2 | my_putstr_error("if: Empty if.\n"); | |
147 | 2 | return 1; | |
148 | } | ||
149 |
6/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 8 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 8 times.
✓ Branch 6 taken 1 times.
|
10 | if (condition && my_strcmp(mysh->args[2], "then") == 0 || |
150 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 8 times.
|
8 | condition == 0 && my_strcmp(mysh->args[2], "then") != 0) |
151 | 1 | return 0; | |
152 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 8 times.
|
9 | if (condition) |
153 | 1 | return exec_true_condition(mysh, mysh->args, 2); | |
154 | 8 | return exec_false_condition(mysh); | |
155 | } | ||
156 |