From 66297e73a7ef7cc0b7d2e26c530d2c2597faf863 Mon Sep 17 00:00:00 2001 From: mdev9 Date: Mon, 4 Mar 2024 10:22:17 +0100 Subject: [PATCH] fixed redirection issues --- srcs/echo.c | 15 ++++++++---- srcs/exec.c | 67 +++++++++++++++++++++++++++++++++-------------------- srcs/main.c | 4 ++-- 3 files changed, 54 insertions(+), 32 deletions(-) diff --git a/srcs/echo.c b/srcs/echo.c index 9b5fc55..b3526de 100755 --- a/srcs/echo.c +++ b/srcs/echo.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */ -/* Updated: 2024/02/14 12:36:02 by tomoron ### ########.fr */ +/* Updated: 2024/03/04 10:10:04 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -22,11 +22,16 @@ int echo(t_cmd *args) put_nl = 0; args = args->next; } - while (args && args->type == ARG) + while (args && args->type !=PIPE) { - ft_putstr_fd(args->token, STDOUT_FILENO); - if (args->next) - ft_putchar_fd(' ', STDOUT_FILENO); + if (args->type != ARG) + args = args->next; + else + { + ft_putstr_fd(args->token, STDOUT_FILENO); + if (args->next) + ft_putchar_fd(' ', STDOUT_FILENO); + } args = args->next; } if (put_nl) diff --git a/srcs/exec.c b/srcs/exec.c index 92dbcc4..6bf7f45 100755 --- a/srcs/exec.c +++ b/srcs/exec.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */ -/* Updated: 2024/03/02 16:48:46 by marde-vr ### ########.fr */ +/* Updated: 2024/03/04 10:21:15 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -124,10 +124,15 @@ int get_args_count(t_cmd *cmds) count = 0; cur_cmd = cmds; - while (cur_cmd->next != 0 && cur_cmd->type == ARG) + while (cur_cmd->next) { + if (cur_cmd->type == PIPE) + break; cur_cmd = cur_cmd->next; - count++; + if (cur_cmd->type == ARG) + count++; + else + cur_cmd = cur_cmd->next; } if (cur_cmd->type == ARG) count++; @@ -155,7 +160,7 @@ void redirect_input(t_msh *msh, int i) } else if (i > 0) { - ft_printf_fd(2, "input_fd: %d\n", msh->fds[i - 1][0]); + //ft_printf_fd(2, "input_fd: %d\n", msh->fds[i - 1][0]); if (dup2(msh->fds[i - 1][0], 0) < 0) ft_exit(msh, 1); } @@ -165,6 +170,7 @@ void redirect_output(t_msh *msh, int i) { if (msh->out_type != PIPE) { + //ft_printf_fd(2, "output_fd: %d\n", msh->out_fd); if (dup2(msh->out_fd, 1) < 0) ft_exit(msh, 1); } @@ -179,12 +185,12 @@ void pipe_child(t_msh *msh, char **cmd_args, int i) { if (msh->in_type != ARG) { - ft_printf_fd(2, "redirecting input of %s of type %d with fd %d\n", msh->cmds->token, msh->in_type, msh->in_fd); + //ft_printf_fd(2, "redirecting input of %s of type %d with fd %d\n", msh->cmds->token, msh->in_type, msh->in_fd); redirect_input(msh, i); } - if (msh->out_type != ARG) + if (msh->out_type == PIPE || msh->out_type == RED_O || msh->out_type == RED_O_APP) { - ft_printf_fd(2, "redirecting output of %s of type %d with fd %d\n", msh->cmds->token, msh->out_type, msh->out_fd); + //ft_printf_fd(2, "redirecting output of %s of type %d with fd %d\n", msh->cmds->token, msh->out_type, msh->out_fd); redirect_output(msh, i); } if (i != 0) @@ -288,12 +294,19 @@ char **get_cmd_args(t_msh *msh) if (!cmd_args || !msh->fds) ft_exit(msh, 1); cur_cmd = msh->cmds; + //ft_printf_fd(2, "cmd: %s: args_count: %d\n", cur_cmd->token, args_count); i = 0; while (i < args_count) { - cmd_args[i] = cur_cmd->token; + if (cur_cmd->type == ARG) + { + cmd_args[i] = cur_cmd->token; + //ft_printf_fd(2, "%s[%d] = %s\n", msh->cmds->token, i, cur_cmd->token); + i++; + } + else + cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next; - i++; } return (cmd_args); } @@ -334,13 +347,13 @@ void remove_command_from_msh(t_msh *msh) msh->in_type = ARG; } -void get_in_type(t_msh *msh) +void get_in_type(t_msh *msh, t_cmd *cmds) { t_cmd *cur_cmd; //msh->in_type = ARG; - cur_cmd = msh->cmds; + cur_cmd = cmds; while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG) cur_cmd = cur_cmd->next; /* @@ -352,24 +365,27 @@ void get_in_type(t_msh *msh) if (cur_cmd->type) { msh->in_type = cur_cmd->type; - if (msh->in_type == HERE_DOC || msh->in_type == RED_I) + if (cur_cmd->type == HERE_DOC || cur_cmd->type == RED_I) { - cur_cmd = cur_cmd->next; - if (msh->in_type == HERE_DOC) - handle_here_doc(msh, cur_cmd->token); - if (msh->in_type == RED_I) + if (cur_cmd->type == HERE_DOC) + handle_here_doc(msh, cur_cmd->next->token); + if (cur_cmd->type == RED_I) { - msh->in_fd = open(cur_cmd->token, O_RDONLY); - ft_printf_fd(2, "opened %s: %d\n", cur_cmd->token, msh->in_fd); + if (msh->in_fd != 0) + close(msh->in_fd); + msh->in_fd = open(cur_cmd->next->token, O_RDONLY); + //ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->in_fd); if (msh->in_fd == -1) { - ft_printf_fd(2, "minishell: %s: ", cur_cmd->token); + ft_printf_fd(2, "minishell: %s: ", cur_cmd->next->token); perror(""); + g_return_code = 1; // todo: cancel execution of all commands } } - //cur_cmd = cur_cmd->next; - //msh->cmds = cur_cmd; + cur_cmd = cur_cmd->next; + if (cur_cmd->next && (cur_cmd->next->type == HERE_DOC || cur_cmd->next->type == RED_I)) + get_in_type(msh, cur_cmd->next); //ft_printf_fd(2, "cmd: %s\n", msh->cmds->token); } } @@ -389,6 +405,7 @@ void get_out_type(t_msh *msh) msh->out_type = ARG; else { + //ft_printf_fd(2, "%s of type %d", cur_cmd->token, cur_cmd->type); msh->out_type = cur_cmd->type; if (msh->out_type == RED_O) msh->out_fd = open(cur_cmd->next->token, O_CREAT | O_RDWR | O_TRUNC, @@ -396,8 +413,8 @@ void get_out_type(t_msh *msh) if (msh->out_type == RED_O_APP) msh->out_fd = open(cur_cmd->next->token, O_CREAT | O_RDWR | O_APPEND, 0644); - if (msh->out_fd) // debug - ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->out_fd); + //if (msh->out_fd) // debug + // ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->out_fd); if (msh->out_fd == -1) { perror("open"); @@ -426,9 +443,9 @@ void exec_command(t_msh *msh) msh->fds[i] = ft_calloc(2, sizeof(int *)); if (!msh->fds[i]) ft_exit(msh, 1); - get_in_type(msh); + get_in_type(msh, msh->cmds); get_out_type(msh); - ft_printf_fd(2, "%d\n", msh->out_fd); + //ft_printf_fd(2, "%d\n", msh->out_fd); //ft_printf_fd(2, "cmd: %s\n", msh->cmds->token); if (!cmd_is_builtin(msh, msh->cmds->token)) get_cmd_path(msh); diff --git a/srcs/main.c b/srcs/main.c index bdae764..d9547d3 100755 --- a/srcs/main.c +++ b/srcs/main.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */ -/* Updated: 2024/03/02 16:13:58 by marde-vr ### ########.fr */ +/* Updated: 2024/03/04 10:21:29 by marde-vr ### ########.fr */ /* */ /* ************************************************************************** */ @@ -100,7 +100,7 @@ int main(int argc, char **argv, char **envp) msh->cmds = parse_command(command, msh->env); free(command); msh->cmds = handle_alias(msh); - //print_parsed_cmd(msh->cmds); + //print_parsed_cmd(msh->cmds); // debug exec_command(msh); free_cmd(msh->cmds); }