fixed redirection issues
This commit is contained in:
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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)
|
||||
{
|
||||
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)
|
||||
|
63
srcs/exec.c
63
srcs/exec.c
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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;
|
||||
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,13 +294,20 @@ 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)
|
||||
{
|
||||
if (cur_cmd->type == ARG)
|
||||
{
|
||||
cmd_args[i] = cur_cmd->token;
|
||||
cur_cmd = cur_cmd->next;
|
||||
//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;
|
||||
}
|
||||
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);
|
||||
|
@ -6,7 +6,7 @@
|
||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* 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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user