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 | 427 | mysh_t *get_mysh(void) | |
19 | { | ||
20 | static mysh_t mysh = {0}; | ||
21 | |||
22 | 427 | 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 | 371 | mysh_t *init_shell(char **env) | |
31 | { | ||
32 | 371 | mysh_t *mysh = get_mysh(); | |
33 | |||
34 | 371 | mysh->exit_status = 0; | |
35 | 371 | mysh->config_file = -1; | |
36 | 371 | mysh->env = my_malloc_strdup_word_array(env); | |
37 | 371 | update_path_list(mysh); | |
38 | 371 | 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 | 980 | void check_tty(void) | |
46 | { | ||
47 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 980 times.
|
980 | if (isatty(0) == 1) { |
48 | ✗ | set_title(); | |
49 | ✗ | display_prompt(); | |
50 | } | ||
51 | 980 | } | |
52 | |||
53 | /** | ||
54 | * @brief The shell loop | ||
55 | * @param environ The environment variables | ||
56 | * @return <b>void</b> | ||
57 | */ | ||
58 | 371 | void shell(char **env) | |
59 | { | ||
60 | 371 | mysh_t *mysh = init_shell(env); | |
61 | 371 | int getline_status = 0; | |
62 | |||
63 | 371 | check_42shrc(mysh); | |
64 | 371 | check_path(mysh); | |
65 | 371 | create_history(mysh); | |
66 | while (1) { | ||
67 | 980 | check_tty(); | |
68 | 980 | getline_status = my_getline(&mysh->line, stdin); | |
69 |
3/4✓ Branch 0 taken 9 times.
✓ Branch 1 taken 971 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 9 times.
|
980 | if (getline_status == -1 && isatty(0) == 1) |
70 | ✗ | my_putstr("exit\n"); | |
71 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 971 times.
|
980 | if (getline_status == -1) |
72 | 9 | my_exit(mysh, mysh->exit_status, NULL); | |
73 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 971 times.
|
971 | if (mysh->line == NULL) |
74 | ✗ | continue; | |
75 | 971 | set_command_in_history(mysh, mysh->line); | |
76 | 971 | analyse_backticks(mysh, mysh->line); | |
77 | 609 | FREE(mysh->line); | |
78 | } | ||
79 | } | ||
80 |