| Line | Branch | Exec | Source | 
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2023 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the alias functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file alias.c | ||
| 9 | * @brief The file containing the alias functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Print the concated syntax | ||
| 16 | * @param is_concated The alias is concated | ||
| 17 | * @param start The alias is at the start of the printing | ||
| 18 | * @return <b>void</b> | ||
| 19 | */ | ||
| 20 | ✗ | static void print_concated(int is_concated, int start) | |
| 21 | { | ||
| 22 | ✗ | if (is_concated && start) | |
| 23 | ✗ | my_putchar('('); | |
| 24 | ✗ | if (is_concated && !start) | |
| 25 | ✗ | my_putstr(")\n"); | |
| 26 | ✗ | if (!is_concated && !start) | |
| 27 | ✗ | my_putchar('\n'); | |
| 28 | ✗ | } | |
| 29 | |||
| 30 | /** | ||
| 31 | * @brief Search the alias in the list and print the value | ||
| 32 | * @param mysh The shell structure | ||
| 33 | * @param alias The alias name to search | ||
| 34 | * @return <b>void</b> | ||
| 35 | */ | ||
| 36 | ✗ | void print_alias(mysh_t *mysh, char *alias) | |
| 37 | { | ||
| 38 | ✗ | for (node_t *tmp = mysh->alias_list; tmp != NULL; tmp = tmp->next) { | |
| 39 | ✗ | if (my_strcmp(((alias_t *)tmp->data)->name, alias) == 0) { | |
| 40 | ✗ | my_putstr(((alias_t *)tmp->data)->value); | |
| 41 | ✗ | break; | |
| 42 | } | ||
| 43 | } | ||
| 44 | ✗ | } | |
| 45 | |||
| 46 | /** | ||
| 47 | * @brief Check if the alias has an error | ||
| 48 | * @param mysh The shell structure | ||
| 49 | * @param alias The alias structure | ||
| 50 | * @return <b>int</b> <u>1</u> if the command argument are less than 3, | ||
| 51 | * <u>0</u> otherwise | ||
| 52 | */ | ||
| 53 | 11 | int display_alias(mysh_t *mysh) | |
| 54 | { | ||
| 55 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 11 times. | 11 | if (mysh->args[1] == NULL) { | 
| 56 | ✗ | for (node_t *tmp = mysh->alias_list; tmp != NULL; tmp = tmp->next) { | |
| 57 | ✗ | my_printf("%s\t", ((alias_t *)tmp->data)->name); | |
| 58 | ✗ | print_concated(((alias_t *)tmp->data)->is_concated, 1); | |
| 59 | ✗ | my_putstr(((alias_t *)tmp->data)->value); | |
| 60 | ✗ | print_concated(((alias_t *)tmp->data)->is_concated, 0); | |
| 61 | } | ||
| 62 | ✗ | return 1; | |
| 63 | } | ||
| 64 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 11 times. | 11 | if (mysh->args[2] == NULL) { | 
| 65 | ✗ | print_alias(mysh, mysh->args[1]); | |
| 66 | ✗ | return 1; | |
| 67 | } | ||
| 68 | 11 | return 0; | |
| 69 | } | ||
| 70 | |||
| 71 | /** | ||
| 72 | * @brief Concatenate the arguments | ||
| 73 | * @param mysh The shell structure | ||
| 74 | * @param alias The alias structure | ||
| 75 | * @return <b>void</b> | ||
| 76 | */ | ||
| 77 | 9 | static void concat_args(mysh_t *mysh, alias_t *alias) | |
| 78 | { | ||
| 79 | 2/2✓ Branch 0 taken 9 times. ✓ Branch 1 taken 9 times. | 18 | for (int j = 3; mysh->args[j] != NULL; j++) { | 
| 80 | 9 | alias->value = my_strcat(alias->value, " "); | |
| 81 | 9 | alias->value = my_strcat(alias->value, mysh->args[j]); | |
| 82 | } | ||
| 83 | 9 | } | |
| 84 | |||
| 85 | /** | ||
| 86 | * @brief Replace the alias if it already exists | ||
| 87 | * @param mysh The shell structure | ||
| 88 | * @param alias The alias structure | ||
| 89 | * @return <b>int</b> <u>1</u> if the alias is replaced, <u>0</u> otherwise | ||
| 90 | */ | ||
| 91 | 11 | int replace_alias(mysh_t *mysh) | |
| 92 | { | ||
| 93 | alias_t *alias; | ||
| 94 | |||
| 95 | 1/2✗ Branch 0 not taken. ✓ Branch 1 taken 11 times. | 11 | for (node_t *tmp = mysh->alias_list; tmp != NULL; tmp = tmp->next) { | 
| 96 | ✗ | alias = (alias_t *)tmp->data; | |
| 97 | ✗ | if (my_strcmp(alias->name, mysh->args[1]) == 0 && | |
| 98 | ✗ | mysh->args[3] == NULL) { | |
| 99 | ✗ | alias->is_concated = 0; | |
| 100 | ✗ | alias->value = my_strcpy(alias->value, mysh->args[2]); | |
| 101 | ✗ | return 1; | |
| 102 | } | ||
| 103 | ✗ | if (my_strcmp(alias->name, mysh->args[1]) == 0 && | |
| 104 | ✗ | mysh->args[3] != NULL) { | |
| 105 | ✗ | alias->is_concated = 1; | |
| 106 | ✗ | alias->value = my_strcpy(alias->value, mysh->args[2]); | |
| 107 | ✗ | concat_args(mysh, alias); | |
| 108 | ✗ | return 1; | |
| 109 | } | ||
| 110 | } | ||
| 111 | 11 | return 0; | |
| 112 | } | ||
| 113 | |||
| 114 | /** | ||
| 115 | * @brief Add an alias to the list | ||
| 116 | * @param mysh The shell structure | ||
| 117 | * @return <b>void</b> | ||
| 118 | */ | ||
| 119 | 11 | void add_alias(mysh_t *mysh) | |
| 120 | { | ||
| 121 | 11 | alias_t *alias = MALLOC(sizeof(alias_t)); | |
| 122 | 11 | int len = 0; | |
| 123 | |||
| 124 | 11 | alias->is_concated = 0; | |
| 125 | 11 | alias->name = my_malloc_strdup(mysh->args[1]); | |
| 126 | 2/2✓ Branch 0 taken 20 times. ✓ Branch 1 taken 11 times. | 31 | for (int i = 2; mysh->args[i] != NULL; i++) | 
| 127 | 20 | len += my_strlen(mysh->args[i]) + 1; | |
| 128 | 11 | alias->value = CALLOC(my_strlen(mysh->args[2]) + len + 1, sizeof(char)); | |
| 129 | 11 | alias->value = my_strcpy(alias->value, mysh->args[2]); | |
| 130 | 2/2✓ Branch 0 taken 2 times. ✓ Branch 1 taken 9 times. | 11 | if (mysh->args[3] == NULL) | 
| 131 | 2 | return my_push_back(&mysh->alias_list, alias, UNKNOWN); | |
| 132 | 9 | alias->is_concated = 1; | |
| 133 | 9 | concat_args(mysh, alias); | |
| 134 | 9 | my_push_back(&mysh->alias_list, alias, UNKNOWN); | |
| 135 | } | ||
| 136 | |||
| 137 | /** | ||
| 138 | * @brief The alias builtin | ||
| 139 | * @param mysh The shell structure | ||
| 140 | * @return <b>int</b> Always <u>0</u> | ||
| 141 | */ | ||
| 142 | 11 | int exec_alias(mysh_t *mysh) | |
| 143 | { | ||
| 144 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 11 times. | 11 | if (display_alias(mysh)) | 
| 145 | ✗ | return 0; | |
| 146 | 1/2✗ Branch 1 not taken. ✓ Branch 2 taken 11 times. | 11 | if (replace_alias(mysh)) | 
| 147 | ✗ | return 0; | |
| 148 | 11 | add_alias(mysh); | |
| 149 | 11 | my_sort_alias(&mysh->alias_list, &my_strcmp); | |
| 150 | 11 | return 0; | |
| 151 | } | ||
| 152 |