iijung / minishell

간단한 bash 쉘 구현
0 stars 1 forks source link

히어독 입력시 SIGINT 무시하도록 수정필요 #43

Closed CodyKat closed 1 year ago

iijung commented 1 year ago
--- a/src/execute/get_heredoc.c
+++ b/src/execute/get_heredoc.c
@@ -6,7 +6,7 @@
 /*   By: minjungk <minjungk@student.42seoul.kr>     +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/05/05 00:14:21 by minjungk          #+#    #+#             */
-/*   Updated: 2023/05/06 01:02:37 by minjungk         ###   ########.fr       */
+/*   Updated: 2023/05/17 18:46:34 by minjungk         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */

@@ -60,31 +60,54 @@ static int  _expand(int fd, char *line, t_env **table)
        return (-1);
 }

-int    get_heredoc(t_env **table, char *word)
+static int     _read(t_env **table, char *word, int fd)
 {
-       int             pipes[2];
-       size_t  word_len;
-       char    *line;
-
-       if (word == NULL || pipe(pipes) == -1)
-               return (-1);
-       word_len = ft_strlen(word);
+       const size_t    word_len = ft_strlen(word);
+       char                    *line;
+
        line = get_next_line(0);
        while (line)
        {
                if (ft_strncmp(line, word, word_len) == 0
                        && (line[word_len] == '\n' || line[word_len] == '\0'))
                        break ;
-               if (_expand(pipes[PIPE_IN], line, table) < 0)
+               if (_expand(fd, line, table) < 0)
                {
-                       close(pipes[PIPE_OUT]);
-                       pipes[PIPE_OUT] = -1;
-                       break ;
+                       close(fd);
+                       free(line);
+                       return (EXIT_FAILURE);
                }
                free(line);
                line = get_next_line(0);
        }
-       free(line);
-       close(pipes[PIPE_IN]);
+       return (EXIT_SUCCESS);
+}
+
+int    get_heredoc(t_env **table, char *word)
+{
+       pid_t   pid;
+       int             status;
+       int             pipes[2];
+
+       if (word == NULL || pipe(pipes) == -1)
+               return (-1);
+       pid = fork();
+       if (pid == 0)
+       {
+               close(pipes[PIPE_OUT]);
+               exit(_read(table, word, pipes[PIPE_IN]));
+       }
+       else
+       {
+               close(pipes[PIPE_IN]);
+               signal(SIGINT, SIG_IGN);
+               waitpid(pid, &status, 0);
+               signal(SIGINT, SIG_DFL);
+               if (WIFSIGNALED(status) || WEXITSTATUS(status) == EXIT_FAILURE)
+               {
+                       close(pipes[PIPE_OUT]);
+                       return (-1);
+               }
+       }
        return (pipes[PIPE_OUT]);
 }
diff --git a/src/prompt/prompt.c b/src/prompt/prompt.c
index 22dbaa8..f94e9a1 100644
--- a/src/prompt/prompt.c
+++ b/src/prompt/prompt.c
@@ -6,7 +6,7 @@
 /*   By: jaemjeon <jaemjeon@student.42.fr>          +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/02/06 02:10:21 by minjungk          #+#    #+#             */
-/*   Updated: 2023/02/21 11:35:59 by minjungk         ###   ########.fr       */
+/*   Updated: 2023/05/17 18:24:47 by minjungk         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */

@@ -53,6 +53,8 @@ static char   *get_readline(void)
        set_terminal();
        line = readline("minishell$ ");
        tcsetattr(STDIN_FILENO, TCSANOW, &term);
+       signal(SIGINT, SIG_DFL);
+       signal(SIGQUIT, SIG_DFL);
        return (line);
 }