Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file removed .heredoc_tmp
Empty file.
40 changes: 21 additions & 19 deletions srcs/exec/exec_pipeline.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: clados-s <clados-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/25 19:04:49 by claudio #+# #+# */
/* Updated: 2026/02/02 13:52:26 by clados-s ### ########.fr */
/* Updated: 2026/02/03 10:10:57 by clados-s ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,13 +32,7 @@ static void wait_children(t_info *info)
info->exit_code = WEXITSTATUS(info->exit_code);
}

/* mudança na lógica para lista linkada
verifico se exite um pipe anterior, se sim, ele redireciona
para o stdin e fecha o prev, se houver um próximo, ele fecha
pra leitura e redireciona o fd de escrito para o stdout, chama
o exec_cmd que vai tratar os redirects e executar o comando, tbm
guarda o status de saída na variavel exit_code
*/
/*preparo os heredocs antes de iniciar o pipeline*/
static void child_process(t_token *token, t_info *info, int fd[2], int prev_fd)
{
if (prev_fd != -1)
Expand All @@ -56,29 +50,21 @@ static void child_process(t_token *token, t_info *info, int fd[2], int prev_fd)
exit(info->exit_code);
}

/*executa o pipeline de comandos, verifica se há um próximo
token e cria um pipe se necessário*/
void exec_pipeline(t_token *token, t_info *info)

static void pipeline_loop(t_token *token, t_info *info)
{
int fd[2];
int prev_fd;
pid_t pid;

prev_fd = -1;
if (error(token))
return ;
if (token && !token->next && !token->prev && is_parent_builtin(token))
{
exec_cmd(token, info);
return ;
}
while (token)
{
if (token->next && pipe(fd) == -1)
return (perror("pipe"));
pid = fork();
if (pid == -1)
return (perror ("fork"));
return (perror("fork"));
if (pid == 0)
child_process(token, info, fd, prev_fd);
if (token->next)
Expand All @@ -89,3 +75,19 @@ void exec_pipeline(t_token *token, t_info *info)
}
wait_children(info);
}

/**/
void exec_pipeline(t_token *token, t_info *info)
{
if (prepare_heredocs(token) == -1)
{
info->exit_code = 130;
return ;
}
if (error(token))
return ;
if (token && !token->next && !token->prev && is_parent_builtin(token))
exec_cmd(token, info);
else
pipeline_loop(token, info);
}
9 changes: 5 additions & 4 deletions srcs/exec/heredoc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@
/* By: clados-s <clados-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2026/02/02 11:15:46 by clados-s #+# #+# */
/* Updated: 2026/02/02 14:29:34 by clados-s ### ########.fr */
/* Updated: 2026/02/02 14:47:15 by clados-s ### ########.fr */
/* */
/* ************************************************************************** */

#include "../../includes/minishell.h"

extern volatile sig_atomic_t g_sig;

/*crio um arquivo temporario onde vai ser escrito o heredoc, inicia a linha com
"> ", caso dê uma ctrl +d ele printa um warning e sai, dps d tudo garanto que
od fd seja fechado e retorne o fd do arquivo temporario*/
/*loop de here só pra refatorar*/
static void loop_heredoc(int fd, char *delimiter)
{
char *line;
Expand All @@ -42,6 +40,9 @@ static void loop_heredoc(int fd, char *delimiter)
}
}

/*crio um arquivo temporario onde vai ser escrito o heredoc, inicia a linha com
"> ", caso dê uma ctrl +d ele printa um warning e sai, dps d tudo garanto que
od fd seja fechado e retorne o fd do arquivo temporario*/
int process_heredoc(char *delimiter)
{
int fd;
Expand Down
48 changes: 33 additions & 15 deletions srcs/exec/redirect.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: clados-s <clados-s@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/12/18 16:40:45 by clados-s #+# #+# */
/* Updated: 2026/02/02 13:52:06 by clados-s ### ########.fr */
/* Updated: 2026/02/02 14:50:55 by clados-s ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -32,35 +32,53 @@ static int open_file(char *file, char *mode)
return (fd);
}

static int process_redir(t_token *token, int i, int original_stdin)
{
int fd;

if (!ft_strncmp(token->rdc[i], "<<", 3))
dup2(original_stdin, STDIN_FILENO);
fd = open_file(token->rdc[i + 1], token->rdc[i]);
if (fd == -1)
{
perror(token->rdc[i + 1]);
close(original_stdin);
return (-1);
}
if (!ft_strncmp(token->rdc[i], "<", 2))
{
dup2(fd, STDIN_FILENO);
if (!ft_strncmp(token->rdc[i], "<<", 3))
unlink(".heredoc_tmp");
}
else
dup2(fd, STDOUT_FILENO);
close(fd);
return (0);
}

/*aqui eu trato os redirects, percorro a matriz de redirecionamentos
e trato cada um, os de input eu faço um dup pra stdin e os de output
eu faço um dup pra stdout e vou incrementando de 2 em 2 pra ir pegando
só os redirects*/
int handle_redirections(t_token *token)
{
int original_stdin;
int i;
int fd;

if (!token->rdc)
return (0);
original_stdin = dup(STDIN_FILENO);
i = 0;
while (token->rdc[i])
{
if (!token->rdc[i + 1])
break ;
fd = open_file(token->rdc[i + 1], token->rdc[i]);
if (fd == -1)
return (-1);
if (!ft_strncmp(token->rdc[i], "<", 2))
if (process_redir(token, i, original_stdin) == -1)
{
dup2(fd, STDIN_FILENO);
if (!ft_strncmp(token->rdc[i], "<<", 3))
unlink(".heredoc_tmp");
close(original_stdin);
return (-1);
}
else
dup2(fd, STDOUT_FILENO);
close(fd);
i += 2; //ex: matriz [0| >]->[1| out]->[2| <]->.[3| in]
i += 2;
}
close(original_stdin);
return (0);
}