5 Commits e65c4bfe4f ... 715df09b95

Author SHA1 Message Date
  Moul 715df09b95 [enh] handle double redirections on a specialized f(). 2 years ago
  Moul cbc4b23842 [enh] double redirections: enhance unit tests. 2 years ago
  Moul 3af6144027 [norm] pipe: modifications which do not modify behavior. 2 years ago
  Moul d862bfe035 [mod] simplify debug printing f(). 2 years ago
  Moul e568218aaa [mod] move 'open_left_redirections()' inside multipiping. 2 years ago
5 changed files with 48 additions and 22 deletions
  1. 33 8
      src/pipe_exec.c
  2. 8 8
      src/pipe_parsing.c
  3. 1 2
      src/pipe_redir_parsing.c
  4. 2 1
      src/sh.h
  5. 4 3
      tests.sh

+ 33 - 8
src/pipe_exec.c

@@ -17,11 +17,7 @@ void	launch_pipes(t_e *e, t_pipe *p, t_pipe_redir **t)
 			p->cmd_opt[p->i] = cmd_splitting(e, t[p->i]->itxt, ' ');
 			remove_cotes(p->cmd_opt[p->i]);
 		}
-		open_left_redirections(e, p, t[p->i]);
-		if (ft_strcmp(t[p->i]->operator, "<<") != 0)
-			handle_multi_piping(e, p, t[p->i]);
-		if (ft_strstr(t[p->i]->operator, ">") == NULL)
-			close_redirections(e, p, t[p->i]);
+		handle_multi_piping(e, p, t[p->i]);
 		free(p->cmd_opt[p->i]);
 		if (p->i != 0)
 			free(p->fd[p->i - 1]);
@@ -51,10 +47,10 @@ void	handle_multi_piping(t_e *e, t_pipe *p, t_pipe_redir *t)
 {
 	int		ok;
 
-	if (t->itxt != NULL)
-		ok = manage_cmd_1(e, p->cmd_opt[p->i]);
+	ok = manage_cmd_1(e, p->cmd_opt[p->i]);
 	if (fork() == 0)
 	{
+		open_left_redirections(e, p, t);
 		if (p->i < p->nbr)
 		{
 			close(p->fd[p->i][0]);
@@ -80,13 +76,42 @@ void	inside_pipe(t_e *e, t_pipe *p, t_pipe_redir *t)
 {
 	if (ft_strstr(t->operator, ">") != NULL)
 		open_right_redirections(e, p, t);
-	if (t->itxt != NULL)
+	if (t->itxt != NULL && ft_strcmp(t->operator, "<<") != 0)
 		manage_cmd_2(e, p->cmd_opt[p->i]);
 	if (ft_strstr(t->operator, ">") != NULL)
 		close_redirections(e, p, t);
 }
 
 /*
+** Special function to handle double redirections of fd and execution fork
+** fork, open left and right redirections
+** execute cmd, close fd, father wait for end of son fork
+*/
+
+void	launch_double_redirections(t_e *e, t_pipe *p, t_pipe_redir **t)
+{
+	int		fd;
+
+	if (ft_strcmp(t[0]->operator, "<<") == 0)
+	{
+		prompt_redirection(e, p, t[0]);
+		return ;
+	}
+	else if (fork() == 0)
+	{
+		fd = open(t[0]->otxt, O_RDONLY);
+		close(0);
+		dup(fd);
+		open_right_redirections(e, p, t[1]);
+		manage_cmd_2(e, cmd_splitting(e, t[0]->itxt, ' '));
+		close(STDOUT);
+		close(fd);
+		exit(0);
+	}
+	wait(NULL);
+}
+
+/*
 ** check every cmd, every cote type
 ** if block starts and ends with same cote:
 ** cut first and last char

+ 8 - 8
src/pipe_parsing.c

@@ -42,7 +42,11 @@ void	pipe_init_2(t_e *e, t_pipe *p, char *all_cmd)
 	pipe_parsing(p, e);
 	t = (t_pipe_redir**)malloc(sizeof(t_pipe_redir*) * (p->nbr + 2));
 	fill_pipe_redir_data(e, p, t);
-	launch_pipes(e, p, t);
+	if (ft_strchr(t[0]->operator, '<') != NULL && t[1] != NULL && \
+			ft_strchr(t[1]->operator, '>') != NULL)
+		launch_double_redirections(e, p, t);
+	else
+		launch_pipes(e, p, t);
 }
 
 /*
@@ -51,14 +55,10 @@ void	pipe_init_2(t_e *e, t_pipe *p, char *all_cmd)
 ** display content on structure
 */
 
-void	display_data(t_pipe_redir **t)
+void	display_data(t_pipe_redir *t)
 {
-	int		i;
-
-	i = -1;
-	while (t[++i] != NULL)
-		ft_printf("itxt: '%s', ifd: %d, operator: '%s', ofd: %d, otxt: '%s'\n",
-			t[i]->itxt, t[i]->ifd, t[i]->operator, t[i]->ofd, t[i]->otxt);
+	ft_printf("itxt: '%s', ifd: %d, operator: '%s', ofd: %d, otxt: '%s'\n",
+		t->itxt, t->ifd, t->operator, t->ofd, t->otxt);
 }
 
 /*

+ 1 - 2
src/pipe_redir_parsing.c

@@ -65,8 +65,7 @@ int		parse_double_redir(t_e *e, char *str, t_pipe *p, t_pipe_redir **t)
 		t[p->i]->itxt = NULL;
 		t[p->i]->operator = ft_strsub(&str[snd_chev], 0, nchev);
 		t[p->i]->otxt = ft_strtrim(&str[snd_chev + nchev]);
-		t[p->i]->ofd = -100;
-		p->i++;
+		t[p->i++]->ofd = -100;
 		p->pnbr++;
 	}
 	else

+ 2 - 1
src/sh.h

@@ -39,7 +39,7 @@ void	count_pipe_nbr(t_e *e, t_pipe *p, char *str);
 */
 void	fill_pipe_redir_data(t_e *e, t_pipe *p, t_pipe_redir **t);
 void	pipe_creation(t_pipe_redir **t, t_pipe *p, char *cmd);
-void	display_data(t_pipe_redir **t);
+void	display_data(t_pipe_redir *t);
 int		parse_double_redir(t_e *e, char *str, t_pipe *p, t_pipe_redir **t);
 int		parse_redirection(t_e *e, t_pipe *p, char *str, t_pipe_redir **t);
 /*
@@ -54,6 +54,7 @@ void	parse_and(char *str, t_pipe_redir *t, int file_pos);
 void	launch_pipes(t_e *e, t_pipe *p, t_pipe_redir **t);
 void	handle_multi_piping(t_e *e, t_pipe *p, t_pipe_redir *t);
 void	inside_pipe(t_e *e, t_pipe *p, t_pipe_redir *t);
+void	launch_double_redirections(t_e *e, t_pipe *p, t_pipe_redir **t);
 void	remove_cotes(char **cmd);
 /*
 ** redirect.c

+ 4 - 3
tests.sh

@@ -82,10 +82,11 @@ tests=(
 #"cat << toto\nnrstai\nrsnati\nairnst\ntoto" # double gauche avec autre string de fin
 
 ## Double redirection
-"cat < $tmpf > /tmp/txt; cat /tmp/txt"
-"cat < $tmpf >> /tmp/txt; cat /tmp/txt"
+"cat < $tmpf > /tmp/txt; cat /tmp/txt; rm /tmp/txt"
+"cat < $tmpf >> /tmp/txt; cat /tmp/txt; rm /tmp/txt"
 ### Same file name
-"cat < $tmpf >> $tmpf"
+"cat < $tmpf > $tmpf"
+#"cat < $tmpf >> $tmpf" # This one is non stop adding many time which stopped the script
 
 ## Redirections avec fd de sortie spécifié
 ### Redirection closed