| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the set builtin | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file set.c | ||
| 9 | * @brief The file containing the set builtin | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Print the list of variable if only one argument | ||
| 16 | * @param mysh The shell structure | ||
| 17 | * @return <b>int</b> <u>1</u> if we print the variables, <u>0</u> otherwise | ||
| 18 | */ | ||
| 19 | 86 | int display_variable(mysh_t *mysh) | |
| 20 | { | ||
| 21 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 44 times.
|
86 | if (mysh->args[1] != NULL) |
| 22 | 42 | return 0; | |
| 23 |
2/2✓ Branch 0 taken 56 times.
✓ Branch 1 taken 44 times.
|
100 | for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) { |
| 24 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 56 times.
|
56 | if (my_strcmp(((variable_t *)tmp->data)->name, "?") == 0) |
| 25 | ✗ | continue; | |
| 26 | 56 | my_printf("%s\t", ((variable_t *)tmp->data)->name); | |
| 27 |
2/2✓ Branch 0 taken 55 times.
✓ Branch 1 taken 1 times.
|
56 | if (((variable_t *)tmp->data)->value != NULL) |
| 28 | 55 | my_putstr(((variable_t *)tmp->data)->value); | |
| 29 | 56 | my_putchar('\n'); | |
| 30 | } | ||
| 31 | 44 | return 1; | |
| 32 | } | ||
| 33 | |||
| 34 | /** | ||
| 35 | * @brief Separate the variable name and value when there are stuck together | ||
| 36 | * with an '=' | ||
| 37 | * @param mysh The shell structure | ||
| 38 | * @param index The index of the args to separate | ||
| 39 | * @return <b>void</b> | ||
| 40 | */ | ||
| 41 | 35 | static void change_args(mysh_t *mysh, int index) | |
| 42 | { | ||
| 43 | 35 | char **new_args = MALLOC(sizeof(char *) * | |
| 44 | (my_array_len((void *)mysh->args) + 3)); | ||
| 45 | |||
| 46 |
2/2✓ Branch 0 taken 35 times.
✓ Branch 1 taken 35 times.
|
70 | for (int i = 0; i < index; i++) |
| 47 | 35 | new_args[i] = mysh->args[i]; | |
| 48 | 35 | new_args[index] = my_malloc_strndup(mysh->args[index], | |
| 49 | 35 | my_get_char_index(mysh->args[index], '=', 1)); | |
| 50 | 35 | new_args[index + 1] = my_malloc_strdup("="); | |
| 51 | 105 | new_args[index + 2] = my_malloc_strdup(mysh->args[index] + | |
| 52 | 35 | my_get_char_index(mysh->args[index], '=', 1) + 1); | |
| 53 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 35 times.
|
35 | for (int i = index + 1; mysh->args[i] != NULL; i++) |
| 54 | ✗ | new_args[i + 2] = mysh->args[i]; | |
| 55 | 35 | new_args[my_array_len((void *)mysh->args) + 2] = NULL; | |
| 56 | 35 | mysh->args = new_args; | |
| 57 | 35 | } | |
| 58 | |||
| 59 | /** | ||
| 60 | * @brief Check if the variable is in the right order | ||
| 61 | * @param list The list of variable | ||
| 62 | * @param cmp The function to compare | ||
| 63 | * @return <b>void</b> | ||
| 64 | */ | ||
| 65 | 134 | static void check_swap(node_t **list) | |
| 66 | { | ||
| 67 | 134 | void *tmp = NULL; | |
| 68 | |||
| 69 |
1/2✓ Branch 0 taken 134 times.
✗ Branch 1 not taken.
|
134 | if ((*list)->prev != NULL |
| 70 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 128 times.
|
134 | && my_strcmp(((variable_t *)(*list)->prev->data)->name, |
| 71 | 134 | ((variable_t *)(*list)->data)->name) > 0) { | |
| 72 | 6 | tmp = (*list)->prev->data; | |
| 73 | 6 | (*list)->prev->data = (*list)->data; | |
| 74 | 6 | (*list)->data = tmp; | |
| 75 | } | ||
| 76 | 134 | } | |
| 77 | |||
| 78 | /** | ||
| 79 | * @brief Sort the variable list | ||
| 80 | * @param begin The list of variable | ||
| 81 | * @param cmp The function to compare | ||
| 82 | * @return <b>void</b> | ||
| 83 | */ | ||
| 84 | 51 | static void my_sort_variable(node_t **begin) | |
| 85 | { | ||
| 86 |
2/2✓ Branch 0 taken 95 times.
✓ Branch 1 taken 51 times.
|
146 | for (node_t *list1 = *begin; list1 != NULL; list1 = list1->next) { |
| 87 | 95 | for (node_t *list2 = (*begin)->next; | |
| 88 |
2/2✓ Branch 0 taken 134 times.
✓ Branch 1 taken 95 times.
|
229 | list2 != NULL; list2 = list2->next) |
| 89 | 134 | check_swap(&list2); | |
| 90 | } | ||
| 91 | 51 | } | |
| 92 | |||
| 93 | /** | ||
| 94 | * @brief Add a variable to the list | ||
| 95 | * @param mysh The shell structure | ||
| 96 | * @param name The name of the variable | ||
| 97 | * @param value The value of the variable | ||
| 98 | * @return <b>void</b> | ||
| 99 | */ | ||
| 100 | 64 | void add_variable(mysh_t *mysh, char *name, char *value) | |
| 101 | { | ||
| 102 | 64 | variable_t *variable = NULL; | |
| 103 | |||
| 104 |
2/2✓ Branch 0 taken 77 times.
✓ Branch 1 taken 64 times.
|
141 | for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) { |
| 105 |
2/2✓ Branch 1 taken 13 times.
✓ Branch 2 taken 64 times.
|
77 | if (my_strcmp(((variable_t *)tmp->data)->name, name) == 0) |
| 106 | 13 | variable = (variable_t *)tmp->data; | |
| 107 | } | ||
| 108 |
2/2✓ Branch 0 taken 51 times.
✓ Branch 1 taken 13 times.
|
64 | if (variable == NULL) { |
| 109 | 51 | variable = MALLOC(sizeof(variable_t)); | |
| 110 | 51 | variable->name = name; | |
| 111 | 51 | variable->value = value; | |
| 112 | 51 | my_push_back(&mysh->variable_list, variable, UNKNOWN); | |
| 113 | 51 | my_sort_variable(&mysh->variable_list); | |
| 114 | } else { | ||
| 115 | 13 | variable->value = value; | |
| 116 | } | ||
| 117 | 64 | } | |
| 118 | |||
| 119 | /** | ||
| 120 | * @brief Add all the variables to the list | ||
| 121 | * @param mysh The shell structure | ||
| 122 | * @return <b>void</b> | ||
| 123 | */ | ||
| 124 | 42 | static void add_all_variables(mysh_t *mysh) | |
| 125 | { | ||
| 126 | 42 | char *value = NULL; | |
| 127 | |||
| 128 |
2/2✓ Branch 0 taken 42 times.
✓ Branch 1 taken 42 times.
|
84 | for (int index = 1; mysh->args[index] != NULL; index++) { |
| 129 |
2/2✓ Branch 1 taken 41 times.
✓ Branch 2 taken 1 times.
|
42 | if (my_strcmp(mysh->args[index + 1], "=") == 0) |
| 130 | 41 | value = mysh->args[index + 2]; | |
| 131 | 42 | add_variable(mysh, mysh->args[index], value); | |
| 132 |
2/2✓ Branch 1 taken 41 times.
✓ Branch 2 taken 1 times.
|
42 | if (my_strcmp(mysh->args[index + 1], "=") == 0) |
| 133 | 41 | index = index + 2; | |
| 134 | 42 | value = NULL; | |
| 135 | } | ||
| 136 | 42 | } | |
| 137 | |||
| 138 | /** | ||
| 139 | * @brief The set builtin | ||
| 140 | * @param mysh The shell structure | ||
| 141 | * @return <b>int</b> <u>0</u> if the command succeed, <u>1</u> otherwise | ||
| 142 | */ | ||
| 143 | 86 | int exec_set(mysh_t *mysh) | |
| 144 | { | ||
| 145 |
2/2✓ Branch 1 taken 44 times.
✓ Branch 2 taken 42 times.
|
86 | if (display_variable(mysh)) |
| 146 | 44 | return 0; | |
| 147 |
2/2✓ Branch 0 taken 54 times.
✓ Branch 1 taken 42 times.
|
96 | for (int index = 1; mysh->args[index] != NULL; index++) { |
| 148 |
2/2✓ Branch 1 taken 41 times.
✓ Branch 2 taken 13 times.
|
54 | if (my_str_contains(mysh->args[index], "=") == 1 |
| 149 |
2/2✓ Branch 1 taken 35 times.
✓ Branch 2 taken 6 times.
|
41 | && my_strlen(mysh->args[index]) != 1) { |
| 150 | 35 | change_args(mysh, index); | |
| 151 | 35 | index = index + 2; | |
| 152 | } | ||
| 153 | } | ||
| 154 |
2/2✓ Branch 0 taken 83 times.
✓ Branch 1 taken 42 times.
|
125 | for (int index = 1; mysh->args[index] != NULL; index++) { |
| 155 |
2/2✓ Branch 1 taken 41 times.
✓ Branch 2 taken 42 times.
|
83 | if (my_strcmp(mysh->args[index], "=") == 0) { |
| 156 | 41 | index++; | |
| 157 | 41 | continue; | |
| 158 | } | ||
| 159 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 42 times.
|
42 | if (is_valid_variable(mysh->args[index], "set") == 0) |
| 160 | ✗ | return 1; | |
| 161 | } | ||
| 162 | 42 | add_all_variables(mysh); | |
| 163 | 42 | return 0; | |
| 164 | } | ||
| 165 | |||
| 166 | /** | ||
| 167 | * @brief Get the value of a variable | ||
| 168 | * @param mysh The shell structure | ||
| 169 | * @param name The name of the variable | ||
| 170 | * @return <b>char *</b> The value of the variable | ||
| 171 | */ | ||
| 172 | ✗ | char *get_variable_value(mysh_t *mysh, char *name) | |
| 173 | { | ||
| 174 | ✗ | for (node_t *tmp = mysh->variable_list; tmp != NULL; tmp = tmp->next) { | |
| 175 | ✗ | if (my_strcmp(((variable_t *)tmp->data)->name, name) == 0) | |
| 176 | ✗ | return ((variable_t *)tmp->data)->value; | |
| 177 | } | ||
| 178 | ✗ | return NULL; | |
| 179 | } | ||
| 180 |