Line | Branch | Exec | Source |
---|---|---|---|
1 | /* | ||
2 | ** EPITECH PROJECT, 2024 | ||
3 | ** 42sh | ||
4 | ** File description: | ||
5 | ** The file containing the event functions | ||
6 | */ | ||
7 | /** | ||
8 | * @file event.c | ||
9 | * @brief The file containing the event functions | ||
10 | */ | ||
11 | |||
12 | #include "../../include/myshell.h" | ||
13 | |||
14 | ✗ | static int get_history_str(node_t **list, char *arg) | |
15 | { | ||
16 | ✗ | char **arr = NULL; | |
17 | |||
18 | ✗ | for (node_t *tmp = get_mysh()->history->prev; tmp; tmp = tmp->prev) { | |
19 | ✗ | if (tmp->prev == get_mysh()->history->prev) | |
20 | ✗ | break; | |
21 | ✗ | if (my_strncmp(tmp->data, arg, my_strlen(arg) != 0)) | |
22 | ✗ | continue; | |
23 | ✗ | arr = my_super_array(tmp->data, " \t"); | |
24 | ✗ | for (int x = 0; arr[x] != NULL; x++) | |
25 | ✗ | my_push_back(list, my_malloc_strdup(arr[x]), UNKNOWN); | |
26 | ✗ | return 0; | |
27 | } | ||
28 | ✗ | return 1; | |
29 | } | ||
30 | |||
31 | ✗ | static int get_history_nb(node_t **list, char *arg, int nb) | |
32 | { | ||
33 | ✗ | char **arr = NULL; | |
34 | |||
35 | ✗ | for (node_t *tmp = get_mysh()->history; tmp != NULL; tmp = tmp->next) { | |
36 | ✗ | if (tmp->next == get_mysh()->history) | |
37 | ✗ | break; | |
38 | ✗ | if (nb != 1) { | |
39 | ✗ | nb--; | |
40 | ✗ | continue; | |
41 | } | ||
42 | ✗ | arr = my_super_array(tmp->data, " \t"); | |
43 | ✗ | for (int i = 0; arr[i] != NULL; i++) | |
44 | ✗ | my_push_back(list, my_malloc_strdup(arr[i]), UNKNOWN); | |
45 | ✗ | FREE_WORD_ARRAY(arr); | |
46 | ✗ | return 0; | |
47 | } | ||
48 | ✗ | return 1; | |
49 | } | ||
50 | |||
51 | ✗ | int get_history(node_t **list, char *arg) | |
52 | { | ||
53 | ✗ | int nb = my_getnbr(&arg[1]); | |
54 | ✗ | int i = my_list_size_circled(get_mysh()->history) - 1; | |
55 | |||
56 | ✗ | if (my_str_isnum(&arg[1])) { | |
57 | ✗ | if (nb > i) { | |
58 | ✗ | my_fprintf(2, "%s: No such event.\n", arg); | |
59 | ✗ | return 1; | |
60 | } | ||
61 | ✗ | if (!get_history_nb(list, arg, nb)) | |
62 | ✗ | return 0; | |
63 | } else { | ||
64 | ✗ | if (!get_history_str(list, arg + 1)) | |
65 | ✗ | return 0; | |
66 | } | ||
67 | ✗ | my_fprintf(2, "%s: Event not found.\n", &arg[1]); | |
68 | ✗ | return 1; | |
69 | } | ||
70 | |||
71 | /** | ||
72 | * @brief Replace '!' by history event | ||
73 | * @param args The command arguments | ||
74 | * @return <b>char **</b> The new command arguments | ||
75 | */ | ||
76 | 1092 | char **replace_history(char **args) | |
77 | { | ||
78 | 1092 | node_t *list = NULL; | |
79 | 1092 | int r = 0; | |
80 | 1092 | char **new_args = NULL; | |
81 | |||
82 |
2/2✓ Branch 0 taken 7242 times.
✓ Branch 1 taken 1092 times.
|
8334 | for (int i = 0; args[i] != NULL; i++) { |
83 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 7242 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
7242 | if (args[i][0] == '!' && args[i][1] == '\0') { |
84 | ✗ | my_push_back(&list, my_malloc_strdup(args[i]), UNKNOWN); | |
85 | ✗ | continue; | |
86 | } | ||
87 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7242 times.
|
7242 | if (args[i][0] == '!') |
88 | ✗ | r = get_history(&list, args[i]); | |
89 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 7242 times.
|
7242 | if (r) |
90 | ✗ | return NULL; | |
91 |
1/2✓ Branch 0 taken 7242 times.
✗ Branch 1 not taken.
|
7242 | if (args[i][0] != '!') |
92 | 7242 | my_push_back(&list, my_malloc_strdup(args[i]), UNKNOWN); | |
93 | } | ||
94 | 1092 | new_args = (char **)my_list_to_array_circled(&list); | |
95 | 1092 | my_delete_circle_list(&list); | |
96 | 1092 | return new_args; | |
97 | } | ||
98 |