| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the replace alias functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file replace_alias.c | ||
| 9 | * @brief The file containing the replace alias functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | ✗ | static void check_swap(node_t **list, int (*cmp)()) | |
| 15 | { | ||
| 16 | void *tmp; | ||
| 17 | |||
| 18 | ✗ | if ((*list)->prev != NULL | |
| 19 | ✗ | && cmp(((alias_t *)(*list)->prev->data)->name, | |
| 20 | ✗ | ((alias_t *)(*list)->data)->name) > 0) { | |
| 21 | ✗ | tmp = (*list)->prev->data; | |
| 22 | ✗ | (*list)->prev->data = (*list)->data; | |
| 23 | ✗ | (*list)->data = tmp; | |
| 24 | } | ||
| 25 | ✗ | } | |
| 26 | |||
| 27 | 26 | void my_sort_alias(node_t **begin, int (*cmp)()) | |
| 28 | { | ||
| 29 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 26 times.
|
52 | for (node_t *list1 = *begin; list1 != NULL; list1 = list1->next) { |
| 30 | 26 | for (node_t *list2 = (*begin)->next; | |
| 31 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
|
26 | list2 != NULL; list2 = list2->next) |
| 32 | ✗ | check_swap(&list2, cmp); | |
| 33 | } | ||
| 34 | 26 | } | |
| 35 | |||
| 36 | 1241 | static node_t *str_match_alias(char *str, node_t **alias_list) | |
| 37 | { | ||
| 38 | 1241 | alias_t *alias = NULL; | |
| 39 | |||
| 40 |
2/2✓ Branch 0 taken 90 times.
✓ Branch 1 taken 1189 times.
|
1279 | for (node_t *tmp = *alias_list; tmp != NULL; tmp = tmp->next) { |
| 41 | 90 | alias = (alias_t *)tmp->data; | |
| 42 |
2/2✓ Branch 1 taken 52 times.
✓ Branch 2 taken 38 times.
|
90 | if (my_strcmp(str, alias->name) == 0) |
| 43 | 52 | return tmp; | |
| 44 | } | ||
| 45 | 1189 | return NULL; | |
| 46 | } | ||
| 47 | |||
| 48 | 26 | static void boucle_on_alias(node_t **args_list, node_t **tmp_list, | |
| 49 | char **tmp_args) | ||
| 50 | { | ||
| 51 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 26 times.
|
88 | for (int j = 0; tmp_args[j] != NULL; j++) |
| 52 | 62 | my_push_front(tmp_list, my_malloc_strdup(tmp_args[j]), UNKNOWN); | |
| 53 |
2/2✓ Branch 0 taken 62 times.
✓ Branch 1 taken 26 times.
|
88 | for (node_t *tmp = *tmp_list; tmp != NULL; tmp = tmp->next) |
| 54 | 62 | my_push_front(args_list, my_malloc_strdup(tmp->data), UNKNOWN); | |
| 55 | 26 | } | |
| 56 | |||
| 57 | 26 | static int update_alias(node_t **already_find, char **str, | |
| 58 | node_t **tmp_list, char **tmp_args) | ||
| 59 | { | ||
| 60 | 26 | my_push_front(already_find, my_strdup(*str), STRING); | |
| 61 | 26 | my_delete_list(tmp_list); | |
| 62 | 26 | *tmp_list = NULL; | |
| 63 |
2/2✓ Branch 1 taken 14 times.
✓ Branch 2 taken 12 times.
|
26 | if (my_strcmp(*str, tmp_args[0]) == 0) { |
| 64 | 14 | my_delete_list(already_find); | |
| 65 | 14 | return 0; | |
| 66 | } | ||
| 67 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
|
12 | if (my_find_node(*already_find, tmp_args[0], my_strcmp)) { |
| 68 | ✗ | my_putstr_error("Alias loop.\n"); | |
| 69 | ✗ | my_delete_list(already_find); | |
| 70 | ✗ | return 1; | |
| 71 | } | ||
| 72 |
1/2✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
|
12 | if (tmp_args[0] != NULL) |
| 73 | 12 | *str = my_malloc_strdup(tmp_args[0]); | |
| 74 | 12 | return -1; | |
| 75 | } | ||
| 76 | |||
| 77 | 1203 | static int looping_on_first(node_t **args_list, char *str, node_t **alias_list) | |
| 78 | { | ||
| 79 | 1203 | node_t *tmp_list = NULL; | |
| 80 | 1203 | node_t *already_find = NULL; | |
| 81 | 1203 | alias_t *matching_alias = NULL; | |
| 82 | 1203 | char **tmp_args = NULL; | |
| 83 | 1203 | int result = 0; | |
| 84 | |||
| 85 |
2/2✓ Branch 1 taken 26 times.
✓ Branch 2 taken 1189 times.
|
1215 | while (str_match_alias(str, alias_list) != NULL) { |
| 86 |
1/2✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
|
26 | if (args_list != NULL) |
| 87 | 26 | my_free_ptr(my_pop_front(args_list)); | |
| 88 | 26 | matching_alias = str_match_alias(str, alias_list)->data; | |
| 89 | 26 | tmp_args = my_super_array(matching_alias->value, " \t\n"); | |
| 90 | 26 | boucle_on_alias(args_list, &tmp_list, tmp_args); | |
| 91 | 26 | result = update_alias(&already_find, &str, &tmp_list, tmp_args); | |
| 92 | 26 | FREE_WORD_ARRAY(tmp_args); | |
| 93 |
2/2✓ Branch 0 taken 14 times.
✓ Branch 1 taken 12 times.
|
26 | if (result != -1) |
| 94 | 14 | return result; | |
| 95 | } | ||
| 96 | 1189 | my_delete_list(&already_find); | |
| 97 | 1189 | return 0; | |
| 98 | } | ||
| 99 | |||
| 100 | 1203 | char **replace_alias_in_line(char **args, node_t **alias_list) | |
| 101 | { | ||
| 102 | 1203 | node_t *args_list = NULL; | |
| 103 | 1203 | char **new_args = NULL; | |
| 104 | 1203 | int index = 0; | |
| 105 | |||
| 106 |
3/6✓ Branch 0 taken 1203 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1203 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1203 times.
|
2406 | if (args == NULL || args[0] == NULL || |
| 107 | 1203 | looping_on_first(&args_list, args[0], alias_list)) | |
| 108 | ✗ | return NULL; | |
| 109 |
2/2✓ Branch 0 taken 26 times.
✓ Branch 1 taken 1177 times.
|
1203 | if (args_list != NULL) |
| 110 | 26 | index++; | |
| 111 |
2/2✓ Branch 0 taken 2800 times.
✓ Branch 1 taken 1203 times.
|
4003 | for (; args[index] != NULL; index++) |
| 112 | 2800 | my_push_back(&args_list, args[index], UNKNOWN); | |
| 113 | 1203 | new_args = (char **)my_list_to_array(args_list); | |
| 114 | 1203 | my_delete_list(&args_list); | |
| 115 | 1203 | FREE(args); | |
| 116 | 1203 | return new_args; | |
| 117 | } | ||
| 118 |