33/* ::: :::::::: */
44/* redirect.c :+: :+: :+: */
55/* +:+ +:+ +:+ */
6- /* By: dayano <dayano @student.42.fr> +#+ +:+ +#+ */
6+ /* By: ttsubo <ttsubo @student.42.fr> +#+ +:+ +#+ */
77/* +#+#+#+#+#+ +#+ */
88/* Created: 2025/04/28 15:19:19 by dayano #+# #+# */
9- /* Updated: 2025/05/15 13:05:46 by dayano ### ########.fr */
9+ /* Updated: 2025/05/18 15:33:05 by ttsubo ### ########.fr */
1010/* */
1111/* ************************************************************************** */
1212
@@ -41,7 +41,7 @@ bool redirect_stdout(t_cmd *cmd)
4141 return (true);
4242}
4343
44- bool is_limiter (int fd , char * limiter )
44+ bool is_limiter (t_cmd * cmd , int fd , char * limiter , t_minish * minish )
4545{
4646 char * line ;
4747
@@ -57,13 +57,17 @@ bool is_limiter(int fd, char *limiter)
5757 free (line );
5858 return (true);
5959 }
60+ if (cmd -> is_expand_heredoc )
61+ line = expand_vars (line , minish );
62+ if (!line )
63+ return (free_str (& line ), false);
6064 write (fd , line , ft_strlen (line ));
6165 write (fd , "\n" , 1 );
6266 free (line );
6367 return (false);
6468}
6569
66- void here_doc (char * limiter )
70+ void here_doc (t_cmd * cmd , char * limiter , t_minish * minish )
6771{
6872 int pipefd [2 ];
6973
@@ -73,7 +77,7 @@ void here_doc(char *limiter)
7377 perror ("pipe" );
7478 return ;
7579 }
76- while (!is_limiter (pipefd [1 ], limiter ))
80+ while (!is_limiter (cmd , pipefd [1 ], limiter , minish ))
7781 ;
7882 close (pipefd [1 ]);
7983 if (dup2 (pipefd [0 ], STDIN_FILENO ) < 0 )
@@ -84,14 +88,14 @@ void here_doc(char *limiter)
8488 close (pipefd [0 ]);
8589}
8690
87- bool redirect_stdin (t_cmd * cmd )
91+ bool redirect_stdin (t_cmd * cmd , t_minish * minish )
8892{
8993 int fd ;
9094 char * path ;
9195
9296 path = cmd -> argv [0 ];
9397 if (cmd -> type == REDIR_HEREDOC )
94- return (here_doc (path ), true);
98+ return (here_doc (cmd , path , minish ), true);
9599 if (cmd -> type != REDIR_IN )
96100 return (true);
97101 fd = open (path , O_RDONLY );
@@ -116,10 +120,10 @@ bool redirect_stdin(t_cmd *cmd)
116120 * @note This function is only called if the caller's is_redirect is true
117121 * @note false if the internal function is not executed.
118122 */
119- bool redirect (t_cmd * cmd )
123+ bool redirect (t_cmd * cmd , t_minish * minish )
120124{
121125 if (cmd -> type == REDIR_IN )
122- return (redirect_stdin (cmd ));
126+ return (redirect_stdin (cmd , minish ));
123127 if (cmd -> type == REDIR_HEREDOC )
124128 return (true);
125129 if (cmd -> type == REDIR_OUT || cmd -> type == REDIR_APPEND )
0 commit comments