Browse Source

[enh] handle double redirections with heredoc.

Moul 2 years ago
parent
commit
c1da64f57e
5 changed files with 20 additions and 9 deletions
  1. 9 1
      src/heredoc.c
  2. 1 1
      src/pipe_exec.c
  3. 5 6
      src/redirect.c
  4. 1 1
      src/sh.h
  5. 4 0
      src/struct.h

+ 9 - 1
src/heredoc.c

@@ -10,7 +10,7 @@
 ** restore STDIN
 */
 
-void	prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t)
+void	prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t, t_pipe_redir *t2)
 {
 	char	*txt;
 	int		pipefd[2];
@@ -32,12 +32,20 @@ void	prompt_redirection(t_e *e, t_pipe *p, t_pipe_redir *t)
 	stdfd = dup(0);
 	dup2(pipefd[0], 0);
 	wait(NULL);
+	if (t2 != NULL)
+		open_right_redirections(e, p, t2);
 	if (fork() == 0)
 	{
 		manage_cmd_2(e, cmd_splitting(e, t->itxt, ' '));
 		exit(0);
 	}
 	wait(NULL);
+	if (t2 != NULL)
+	{
+		close(e->fd_file);
+		close(1);
+		dup(e->fdout);
+	}
 	close(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]);
+		prompt_redirection(e, p, t[0], t[1]);
 		return ;
 	}
 	else if (fork() == 0)

+ 5 - 6
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);
+		prompt_redirection(e, p, t, NULL);
 }
 
 /*
@@ -31,15 +31,14 @@ void	open_left_redirections(t_e *e, t_pipe *p, t_pipe_redir *t)
 
 void	open_right_redirections(t_e *e, t_pipe *p, t_pipe_redir *t)
 {
-	int		fd_file;
-
 	if (t->ofd == -100)
 	{
 		if (ft_strstr(t->operator, ">>") != NULL)
-			fd_file = open(t->otxt, O_APPEND | O_WRONLY | O_CREAT, 0644);
+			e->fd_file = open(t->otxt, O_APPEND | O_WRONLY | O_CREAT, 0644);
 		else
-			fd_file = open(t->otxt, O_TRUNC | O_WRONLY | O_CREAT, 0644);
-		t->ifd == 2 ? dup2(fd_file, STDERR) : dup2(fd_file, STDOUT);
+			e->fd_file = open(t->otxt, O_TRUNC | O_WRONLY | O_CREAT, 0644);
+		e->fdout = t->ifd == 2 ? dup(STDERR) : dup(STDOUT);
+		t->ifd == 2 ? dup2(e->fd_file, STDERR) : dup2(e->fd_file, STDOUT);
 	}
 	else
 	{

+ 1 - 1
src/sh.h

@@ -65,7 +65,7 @@ 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);
+void	prompt_redirection(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);
 /*

+ 4 - 0
src/struct.h

@@ -41,6 +41,8 @@ typedef struct			s_block
 ** number columns in current window
 ** boolean value for ctrl + d in prompt
 ** slen: len from semicolon split: used for non-splitting inside coted blocks
+** fd_file: file descriptor of file for redirection on the right
+** fdout: fd backup of stdout so restore it later
 */
 
 typedef struct			s_e {
@@ -62,6 +64,8 @@ typedef struct			s_e {
 	int					d;
 	int					slen;
 	t_block				**b;
+	int					fd_file;
+	int					fdout;
 }						t_e;
 
 /*