| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the shell functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file shell.c | ||
| 9 | * @brief The file containing the shell functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Get the shell structure | ||
| 16 | * @return <b>mysh_t *</b> The shell structure | ||
| 17 | */ | ||
| 18 | 430 | mysh_t *get_mysh(void) | |
| 19 | { | ||
| 20 | static mysh_t mysh = {0}; | ||
| 21 | |||
| 22 | 430 | return &mysh; | |
| 23 | } | ||
| 24 | |||
| 25 | /** | ||
| 26 | * @brief Initialize the shell structure | ||
| 27 | * @param environ The environment variables | ||
| 28 | * @return <b>mysh_t *</b> The shell structure | ||
| 29 | */ | ||
| 30 | 366 | mysh_t *init_shell(char **env) | |
| 31 | { | ||
| 32 | 366 | mysh_t *mysh = get_mysh(); | |
| 33 | |||
| 34 | 366 | mysh->exit_status = 0; | |
| 35 | 366 | mysh->config_file = -1; | |
| 36 | 366 | mysh->env = my_malloc_strdup_word_array(env); | |
| 37 | 366 | update_path_list(mysh); | |
| 38 | 366 | return mysh; | |
| 39 | } | ||
| 40 | |||
| 41 | /** | ||
| 42 | * @brief Check if the shell is a tty and display the prompt if it is | ||
| 43 | * @return <b>void</b> | ||
| 44 | */ | ||
| 45 | 1062 | void check_tty(void) | |
| 46 | { | ||
| 47 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1062 times.
|
1062 | if (isatty(0) == 1) { |
| 48 | ✗ | set_title(); | |
| 49 | ✗ | display_prompt(); | |
| 50 | } | ||
| 51 | 1062 | } | |
| 52 | |||
| 53 | /** | ||
| 54 | * @brief The shell loop | ||
| 55 | * @param environ The environment variables | ||
| 56 | * @return <b>void</b> | ||
| 57 | */ | ||
| 58 | 366 | void shell(char **env) | |
| 59 | { | ||
| 60 | 366 | mysh_t *mysh = init_shell(env); | |
| 61 | 366 | int getline_status = 0; | |
| 62 | |||
| 63 | 366 | check_42shrc(mysh); | |
| 64 | 366 | check_path(mysh); | |
| 65 | 366 | create_history(mysh); | |
| 66 | while (1) { | ||
| 67 | 1062 | check_tty(); | |
| 68 | 1062 | getline_status = my_getline(&mysh->line, stdin); | |
| 69 |
3/4✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1052 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
|
1062 | if (getline_status == -1 && isatty(0) == 1) |
| 70 | ✗ | my_putstr("exit\n"); | |
| 71 |
2/2✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1052 times.
|
1062 | if (getline_status == -1) |
| 72 | 10 | my_exit(mysh, mysh->exit_status, NULL); | |
| 73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1052 times.
|
1052 | if (mysh->line == NULL) |
| 74 | ✗ | continue; | |
| 75 | 1052 | set_command_in_history(mysh, mysh->line); | |
| 76 | 1052 | analyse_backticks(mysh, mysh->line); | |
| 77 | 696 | FREE(mysh->line); | |
| 78 | } | ||
| 79 | } | ||
| 80 |