Browse Source

[norm] heredoc: split on two functions. Update comments.

Moul 2 years ago
parent
commit
2bfa8d8026
5 changed files with 31 additions and 16 deletions
  1. 26 13
      src/heredoc.c
  2. 1 1
      src/pipe_exec.c
  3. 1 1
      src/redirect.c
  4. 2 1
      src/sh.h
  5. 1 0
      src/struct.h

+ 26 - 13
src/heredoc.c

@@ -5,33 +5,46 @@
 ** Unset term caps modes
 ** retrieve text through a prompt
 ** Set term caps modes
-** backup STDIN, pipe the text in STDIN
-** wait the fork, launch the cmd with the upper text
-** restore STDIN
+** pipe the text in STDIN
+** wait the fork
 */
 
-void	prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2)
+void	prompt_redirection_init(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2)
 {
 	char	*txt;
-	int		pipefd[2];
-	int		stdfd;
 	int		i;
 
 	set_term_modes(0);
 	txt = prompt_heredoc(e, txt, t->otxt, i);
 	set_term_modes(1);
-	pipe(pipefd);
+	pipe(e->pipefd);
 	if (fork() == 0)
 	{
-		close(pipefd[0]);
-		ft_putstr_fd(txt, pipefd[1]);
+		close(e->pipefd[0]);
+		ft_putstr_fd(txt, e->pipefd[1]);
 		ft_strdel(&txt);
 		exit(0);
 	}
-	close(pipefd[1]);
-	stdfd = dup(0);
-	dup2(pipefd[0], 0);
 	wait(NULL);
+	close(e->pipefd[1]);
+	prompt_redirection_exec(e, p, t, t2);
+}
+
+/*
+** backup STDIN, dup pipe on STDIN
+** if double redirections: open_right_redirections
+** exec the cmd which read on pipe
+** wait fork
+** if double redirections: close file fd, restore STDOUT
+** restore STDIN
+*/
+
+void	prompt_redirection_exec(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2)
+{
+	int		stdfd;
+
+	stdfd = dup(0);
+	dup2(e->pipefd[0], 0);
 	if (t2 != NULL)
 		open_right_redirections(e, p, t2);
 	if (fork() == 0)
@@ -46,7 +59,7 @@ void	prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2)
 		close(1);
 		dup(e->fdout);
 	}
-	close(pipefd[0]);
+	close(e->pipefd[0]);
 	close(0);
 	dup(stdfd);
 }

+ 1 - 1
src/pipe_exec.c

@@ -94,7 +94,7 @@ void	launch_double_redirections(t_e *e, t_pipe *p, t_pipe_redir **t)
 
 	if (ft_strcmp(t[0]->operator, "<<") == 0)
 	{
-		prompt_redirection(e, p, t[0], t[1]);
+		prompt_redirection_init(e, p, t[0], t[1]);
 		return ;
 	}
 	else if (fork() == 0)

+ 1 - 1
src/redirect.c

@@ -19,7 +19,7 @@ void	open_left_redirections(t_e *e, t_pipe *p, t_pipe_redir *t)
 		dup(p->fd[p->i][0]);
 	}
 	else if (ft_strcmp(t->operator, "<<") == 0)
-		prompt_redirection(e, p, t, NULL);
+		prompt_redirection_init(e, p, t, NULL);
 }
 
 /*

+ 2 - 1
src/sh.h

@@ -65,7 +65,8 @@ void	close_redirections(t_e *e, t_pipe *p, t_pipe_redir *t);
 /*
 ** heredoc.c
 */
-void	prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2);
+void	prompt_redirection_init(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2);
+void	prompt_redirection_exec(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2);
 char	*prompt_heredoc(t_e *e, char *txt, char *eof, int i);
 int		here_edition(t_e *h, int heredoc);
 /*

+ 1 - 0
src/struct.h

@@ -66,6 +66,7 @@ typedef struct			s_e {
 	t_block				**b;
 	int					fd_file;
 	int					fdout;
+	int					pipefd[2];
 }						t_e;
 
 /*