Skip to content

Commit

Permalink
FIXED PIPES AND REDIRECTIONS WORKING I THINK??
Browse files Browse the repository at this point in the history
  • Loading branch information
jteissie committed Jul 14, 2024
1 parent 4a99a09 commit ecbc989
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 35 deletions.
8 changes: 4 additions & 4 deletions include/execution.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execution.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 16:53:36 by jteissie #+# #+# */
/* Updated: 2024/07/14 17:34:09 by jteissie ### ########.fr */
/* Updated: 2024/07/14 23:35:52 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -31,8 +31,8 @@ void get_redirections(t_lex_parser *table, char *redirection[]);
int redirect_file(int file_fd[], int mode);

int execute_data(t_parser *parsed_data, char **env);
int execute_commands(t_parser *data, char **envp);
int process_command(t_lex_parser *p, char **envp, t_parser *d);
int execute_commands(t_parser *data, char **envp, int std_fds[]);
int process_command(t_lex_parser *p, char **envp, t_parser *d, int std_fds[]);
int redirect_child(t_lex_parser *p, int p_fd[], int has_pipe[]);

#endif
24 changes: 19 additions & 5 deletions src/execution/execute_commands.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* execute_commands.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 14:19:59 by jteissie #+# #+# */
/* Updated: 2024/07/14 13:51:29 by jteissie ### ########.fr */
/* Updated: 2024/07/15 00:02:08 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -55,7 +55,7 @@ int count_commands(t_parser *data)
return (cmd_count);
}

int execute_commands(t_parser *data, char **envp)
int execute_commands(t_parser *data, char **envp, int std_fds[])
{
int cmd_count;
t_lex_parser *roaming;
Expand All @@ -66,7 +66,7 @@ int execute_commands(t_parser *data, char **envp)
{
if (roaming->type == TK_PARS_CMD)
{
if (process_command(roaming, envp, data) == PANIC)
if (process_command(roaming, envp, data, std_fds) == PANIC)
return (PANIC);
cmd_count--;
}
Expand All @@ -79,11 +79,25 @@ int execute_commands(t_parser *data, char **envp)
int execute_data(t_parser *parsed_data, char **env)
{
int status;
int std_fd[2];
int dup_status;

status = SUCCESS;
close(4); //CLOSE /dev/ptmx WHERE IS THIS SHITTY FD OPENED???
std_fd[0] = dup(STDIN_FILENO);
std_fd[1] = dup(STDOUT_FILENO);
dup_status = 0;
if (std_fd[0] < 0 || std_fd[1] < 0)
return (PANIC);
if (parsed_data->node)
status = execute_commands(parsed_data, env);
status = execute_commands(parsed_data, env, std_fd);
if (parsed_data->node)
free_parsed_mem(parsed_data);
dup_status += dup2(std_fd[0], STDIN_FILENO);
dup_status += dup2(std_fd[1], STDOUT_FILENO);
if (dup_status < 0)
return (PANIC);
close(std_fd[0]);
close(std_fd[1]);
return (status);
}
8 changes: 5 additions & 3 deletions src/execution/execution_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: marvin <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/07 18:14:25 by jteissie #+# #+# */
/* Updated: 2024/07/14 23:15:47 by marvin ### ########.fr */
/* Updated: 2024/07/15 00:37:21 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -63,11 +63,13 @@ int redirect_files(int file_fd[])
{
dup_status += dup2(file_fd[0], STDIN_FILENO);
close(file_fd[0]);
file_fd[0] = 0;
}
if (file_fd[1])
{
dup_status += dup2(file_fd[1], STDOUT_FILENO);
close(file_fd[1]);
file_fd[1] = 0;
}
return (dup_status);
}
Expand All @@ -94,10 +96,10 @@ int open_files(t_lex_parser *table)
if (redir[1])
file_fd[1] = open(redir[1], O_CREAT | O_TRUNC | O_WRONLY, 0644);
if (file_fd[0] < 0 || file_fd[1] < 0)
return (PANIC );
return (-1);
roaming = roaming->next;
if (redirect_files(file_fd) < 0)
return (PANIC);
return (-1);
redir[0] = NULL;
redir[1] = NULL;
}
Expand Down
8 changes: 5 additions & 3 deletions src/execution/process_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* process_command.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/10 13:07:11 by jteissie #+# #+# */
/* Updated: 2024/07/14 17:31:52 by jteissie ### ########.fr */
/* Updated: 2024/07/15 00:25:59 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -68,7 +68,7 @@ int redirect_parent(int p_fd[])
return (dup_status);
}

int process_command(t_lex_parser *p, char **envp, t_parser *d)
int process_command(t_lex_parser *p, char **envp, t_parser *d, int std_fds[])
{
int pipe_fd[2];
int has_pipe[2];
Expand All @@ -85,6 +85,8 @@ int process_command(t_lex_parser *p, char **envp, t_parser *d)
return (PANIC);
if (pid_child == 0)
{
close(std_fds[0]);
close(std_fds[1]);
if (redirect_child(p, pipe_fd, has_pipe) == PANIC)
handle_error("syscall error in exec child.\n", errno);
execute_cmd(cmd_table->cmd, envp, d);
Expand Down
8 changes: 5 additions & 3 deletions src/execution/redirect_child.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* redirect_child.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/12 14:00:07 by jteissie #+# #+# */
/* Updated: 2024/07/14 17:32:02 by jteissie ### ########.fr */
/* Updated: 2024/07/15 00:33:32 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand All @@ -18,9 +18,11 @@ int redirect_pipe(int p_fd[], int has_pipe[])

dup_status = 0;
close(p_fd[0]);
p_fd[0] = -1;
if (has_pipe[1] == TRUE)
dup_status += dup2(p_fd[1], STDOUT_FILENO);
close(p_fd[1]);
p_fd[1] = -1;
return (dup_status);
}

Expand Down Expand Up @@ -48,7 +50,7 @@ int redirect_child(t_lex_parser *parsed, int p_fd[], int has_pipe[])
{
if (redirect_pipe(p_fd, has_pipe) < 0)
return (PANIC);
if (open_files(parsed) == PANIC)
if (open_files(parsed) < 0)
return (PANIC);
return (SUCCESS);
}
21 changes: 5 additions & 16 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
/* ::: :::::::: */
/* main.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jteissie <jteissie@student.42.fr> +#+ +:+ +#+ */
/* By: marvin <marvin@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 13:43:42 by bthomas #+# #+# */
/* Updated: 2024/07/14 16:09:16 by jteissie ### ########.fr */
/* Updated: 2024/07/15 00:06:53 by marvin ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -76,22 +76,17 @@ int main(int argc, char **argv, char **env)
{
t_data data;
t_parser parsed_data;
int std_fd[2];
// char *prompt;

(void)argv;
(void)argc;
init(&data, env);
// prompt = get_prompt(NULL);
parsed_data.node = NULL;
while (1)
{
// while (1)
// {
// prompt = get_prompt(prompt);
data.input = readline("minishell>");
std_fd[0] = dup(STDIN_FILENO);
std_fd[1] = dup(STDOUT_FILENO);
if (std_fd[0] < 0 || std_fd[1] < 0)
return (PANIC);
if (data.input)
add_history(data.input);
if (valid_input(data.input))
Expand All @@ -100,13 +95,7 @@ int main(int argc, char **argv, char **env)
ft_printf("Error: Invalid token found\n");
parse_data(&data, &parsed_data);
execute_data(&parsed_data, env);
dup2(std_fd[0], STDIN_FILENO);
dup2(std_fd[1], STDOUT_FILENO);
close(std_fd[0]);
close(std_fd[1]);
if (std_fd[0] < 0 || std_fd[1] < 0)
return (PANIC);
}
// }
// if (prompt)
// free(prompt);
return (lex_clean_exit(&data, 0));
Expand Down
2 changes: 1 addition & 1 deletion val_run.sh
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#!/bin/bash

valgrind --leak-check=full --track-origins=yes ./minishell
valgrind --leak-check=full --track-origins=yes --trace-children=yes --track-fds=yes ./minishell

0 comments on commit ecbc989

Please sign in to comment.