| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* | ||
| 2 | ** EPITECH PROJECT, 2024 | ||
| 3 | ** 42sh | ||
| 4 | ** File description: | ||
| 5 | ** The file containing the scripting functions | ||
| 6 | */ | ||
| 7 | /** | ||
| 8 | * @file scripting.c | ||
| 9 | * @brief The file containing the scripting functions | ||
| 10 | */ | ||
| 11 | |||
| 12 | #include "../include/myshell.h" | ||
| 13 | |||
| 14 | /** | ||
| 15 | * @brief Remove the comments from a line | ||
| 16 | * @param line The line | ||
| 17 | * @return <b>void</b> | ||
| 18 | */ | ||
| 19 | 3730 | void remove_comments(char *line) | |
| 20 | { | ||
| 21 |
1/2✓ Branch 0 taken 3730 times.
✗ Branch 1 not taken.
|
3730 | for (int i = 0; line[i] != '\0'; i++) { |
| 22 |
1/2✓ Branch 0 taken 3730 times.
✗ Branch 1 not taken.
|
3730 | if (line[i] == '#') { |
| 23 | 3730 | line[i] = '\0'; | |
| 24 | 3730 | return; | |
| 25 | } | ||
| 26 | } | ||
| 27 | } | ||
| 28 | |||
| 29 | /** | ||
| 30 | * @brief Get the content of a file | ||
| 31 | * @param file The file path | ||
| 32 | * @param fd The file descriptor | ||
| 33 | * @return <b>char **</b> The content of the file line by line, | ||
| 34 | * <u>NULL</u> if an error occured | ||
| 35 | */ | ||
| 36 | ✗ | static char **get_file_content(char *file, int *fd) | |
| 37 | { | ||
| 38 | struct stat file_info; | ||
| 39 | ✗ | char *line = NULL; | |
| 40 | ✗ | char **file_content = NULL; | |
| 41 | |||
| 42 | ✗ | *fd = open(file, O_RDONLY); | |
| 43 | ✗ | if (*fd == -1 || stat(file, &file_info) == -1) { | |
| 44 | ✗ | my_fprintf(2, "%s: %s\n", file, strerror(errno)); | |
| 45 | ✗ | return NULL; | |
| 46 | } | ||
| 47 | ✗ | line = calloc(file_info.st_size + 1, sizeof(char)); | |
| 48 | ✗ | if (read(*fd, line, file_info.st_size) == -1) { | |
| 49 | ✗ | my_fprintf(2, "%s: %s\n", file, strerror(errno)); | |
| 50 | ✗ | return NULL; | |
| 51 | } | ||
| 52 | ✗ | file_content = STR2ARRAY_SEP(line, "\n"); | |
| 53 | ✗ | FREE(line); | |
| 54 | ✗ | return file_content; | |
| 55 | } | ||
| 56 | |||
| 57 | /** | ||
| 58 | * @brief The instructions for the child process | ||
| 59 | * (write right command line in pipe) | ||
| 60 | * @param pipe_fd The pipe file descriptor | ||
| 61 | * @param file_content The content of the file | ||
| 62 | * @return <b>void</b> | ||
| 63 | */ | ||
| 64 | ✗ | static void child_process(int pipe_fd[2], char **file_content) | |
| 65 | { | ||
| 66 | ✗ | close(pipe_fd[0]); | |
| 67 | ✗ | for (int index = 0; file_content[index] != NULL; index++) { | |
| 68 | ✗ | remove_comments(file_content[index]); | |
| 69 | ✗ | if (file_content[index][0] != '\0') | |
| 70 | ✗ | my_fprintf(pipe_fd[1], "%s\n", file_content[index]); | |
| 71 | } | ||
| 72 | ✗ | close(pipe_fd[1]); | |
| 73 | ✗ | exit(0); | |
| 74 | } | ||
| 75 | |||
| 76 | /** | ||
| 77 | * @brief Read a file and execute the commands in it | ||
| 78 | * @param file The file path | ||
| 79 | * @return <b>int</b> <u>0</u> if success, <u>1</u> if an error occurred | ||
| 80 | */ | ||
| 81 | ✗ | int read_file_in_stdin(char *file) | |
| 82 | { | ||
| 83 | ✗ | int fd = 0; | |
| 84 | ✗ | char **file_content = get_file_content(file, &fd); | |
| 85 | ✗ | int pipe_fd[2] = {0}; | |
| 86 | ✗ | pid_t pid = 0; | |
| 87 | |||
| 88 | ✗ | if (file_content == NULL) | |
| 89 | ✗ | return 1; | |
| 90 | ✗ | pipe(pipe_fd); | |
| 91 | ✗ | pid = fork(); | |
| 92 | ✗ | if (pid == 0) { | |
| 93 | ✗ | child_process(pipe_fd, file_content); | |
| 94 | } else { | ||
| 95 | ✗ | close(pipe_fd[1]); | |
| 96 | ✗ | dup2(pipe_fd[0], 0); | |
| 97 | ✗ | waitpid(pid, NULL, 0); | |
| 98 | } | ||
| 99 | ✗ | FREE_WORD_ARRAY(file_content); | |
| 100 | ✗ | close(fd); | |
| 101 | ✗ | return 0; | |
| 102 | } | ||
| 103 |