GCC Code Coverage Report


Directory: ./
File: src/parsing/replace_alias.c
Date: 2024-06-05 00:36:48
Exec Total Coverage
Lines: 61 74 82.4%
Functions: 6 7 85.7%
Branches: 27 38 71.1%

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 11 void my_sort_alias(node_t **begin, int (*cmp)())
28 {
29
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 11 times.
22 for (node_t *list1 = *begin; list1 != NULL; list1 = list1->next) {
30 11 for (node_t *list2 = (*begin)->next;
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 list2 != NULL; list2 = list2->next)
32 check_swap(&list2, cmp);
33 }
34 11 }
35
36 1103 static node_t *str_match_alias(char *str, node_t **alias_list)
37 {
38 1103 alias_t *alias = NULL;
39
40
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 1085 times.
1118 for (node_t *tmp = *alias_list; tmp != NULL; tmp = tmp->next) {
41 33 alias = (alias_t *)tmp->data;
42
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 15 times.
33 if (my_strcmp(str, alias->name) == 0)
43 18 return tmp;
44 }
45 1085 return NULL;
46 }
47
48 9 static void boucle_on_alias(node_t **args_list, node_t **tmp_list,
49 char **tmp_args)
50 {
51
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9 times.
25 for (int j = 0; tmp_args[j] != NULL; j++)
52 16 my_push_front(tmp_list, my_malloc_strdup(tmp_args[j]), UNKNOWN);
53
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 9 times.
25 for (node_t *tmp = *tmp_list; tmp != NULL; tmp = tmp->next)
54 16 my_push_front(args_list, my_malloc_strdup(tmp->data), UNKNOWN);
55 9 }
56
57 9 static int update_alias(node_t **already_find, char **str,
58 node_t **tmp_list, char **tmp_args)
59 {
60 9 my_push_front(already_find, my_strdup(*str), STRING);
61 9 my_delete_list(tmp_list);
62 9 *tmp_list = NULL;
63
2/2
✓ Branch 1 taken 7 times.
✓ Branch 2 taken 2 times.
9 if (my_strcmp(*str, tmp_args[0]) == 0) {
64 7 my_delete_list(already_find);
65 7 return 0;
66 }
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 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 2 times.
✗ Branch 1 not taken.
2 if (tmp_args[0] != NULL)
73 2 *str = my_malloc_strdup(tmp_args[0]);
74 2 return -1;
75 }
76
77 1092 static int looping_on_first(node_t **args_list, char *str, node_t **alias_list)
78 {
79 1092 node_t *tmp_list = NULL;
80 1092 node_t *already_find = NULL;
81 1092 alias_t *matching_alias = NULL;
82 1092 char **tmp_args = NULL;
83 1092 int result = 0;
84
85
2/2
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 1085 times.
1094 while (str_match_alias(str, alias_list) != NULL) {
86
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 if (args_list != NULL)
87 9 my_free_ptr(my_pop_front(args_list));
88 9 matching_alias = str_match_alias(str, alias_list)->data;
89 9 tmp_args = my_super_array(matching_alias->value, " \t\n");
90 9 boucle_on_alias(args_list, &tmp_list, tmp_args);
91 9 result = update_alias(&already_find, &str, &tmp_list, tmp_args);
92 9 FREE_WORD_ARRAY(tmp_args);
93
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (result != -1)
94 7 return result;
95 }
96 1085 my_delete_list(&already_find);
97 1085 return 0;
98 }
99
100 1092 char **replace_alias_in_line(char **args, node_t **alias_list)
101 {
102 1092 node_t *args_list = NULL;
103 1092 char **new_args = NULL;
104 1092 int index = 0;
105
106
3/6
✓ Branch 0 taken 1092 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 1092 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 1092 times.
2184 if (args == NULL || args[0] == NULL ||
107 1092 looping_on_first(&args_list, args[0], alias_list))
108 return NULL;
109
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1083 times.
1092 if (args_list != NULL)
110 9 index++;
111
2/2
✓ Branch 0 taken 7233 times.
✓ Branch 1 taken 1092 times.
8325 for (; args[index] != NULL; index++)
112 7233 my_push_back(&args_list, args[index], UNKNOWN);
113 1092 new_args = (char **)my_list_to_array(args_list);
114 1092 my_delete_list(&args_list);
115 1092 FREE(args);
116 1092 return new_args;
117 }
118