fixed redirection issues
This commit is contained in:
15
srcs/echo.c
15
srcs/echo.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 15:30:37 by tomoron #+# #+# */
|
/* 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;
|
put_nl = 0;
|
||||||
args = args->next;
|
args = args->next;
|
||||||
}
|
}
|
||||||
while (args && args->type == ARG)
|
while (args && args->type !=PIPE)
|
||||||
{
|
{
|
||||||
ft_putstr_fd(args->token, STDOUT_FILENO);
|
if (args->type != ARG)
|
||||||
if (args->next)
|
args = args->next;
|
||||||
ft_putchar_fd(' ', STDOUT_FILENO);
|
else
|
||||||
|
{
|
||||||
|
ft_putstr_fd(args->token, STDOUT_FILENO);
|
||||||
|
if (args->next)
|
||||||
|
ft_putchar_fd(' ', STDOUT_FILENO);
|
||||||
|
}
|
||||||
args = args->next;
|
args = args->next;
|
||||||
}
|
}
|
||||||
if (put_nl)
|
if (put_nl)
|
||||||
|
67
srcs/exec.c
67
srcs/exec.c
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/07 14:12:49 by tomoron #+# #+# */
|
/* 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;
|
count = 0;
|
||||||
cur_cmd = cmds;
|
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;
|
cur_cmd = cur_cmd->next;
|
||||||
count++;
|
if (cur_cmd->type == ARG)
|
||||||
|
count++;
|
||||||
|
else
|
||||||
|
cur_cmd = cur_cmd->next;
|
||||||
}
|
}
|
||||||
if (cur_cmd->type == ARG)
|
if (cur_cmd->type == ARG)
|
||||||
count++;
|
count++;
|
||||||
@ -155,7 +160,7 @@ void redirect_input(t_msh *msh, int i)
|
|||||||
}
|
}
|
||||||
else if (i > 0)
|
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)
|
if (dup2(msh->fds[i - 1][0], 0) < 0)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
@ -165,6 +170,7 @@ void redirect_output(t_msh *msh, int i)
|
|||||||
{
|
{
|
||||||
if (msh->out_type != PIPE)
|
if (msh->out_type != PIPE)
|
||||||
{
|
{
|
||||||
|
//ft_printf_fd(2, "output_fd: %d\n", msh->out_fd);
|
||||||
if (dup2(msh->out_fd, 1) < 0)
|
if (dup2(msh->out_fd, 1) < 0)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
}
|
}
|
||||||
@ -179,12 +185,12 @@ void pipe_child(t_msh *msh, char **cmd_args, int i)
|
|||||||
{
|
{
|
||||||
if (msh->in_type != ARG)
|
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);
|
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);
|
redirect_output(msh, i);
|
||||||
}
|
}
|
||||||
if (i != 0)
|
if (i != 0)
|
||||||
@ -288,12 +294,19 @@ char **get_cmd_args(t_msh *msh)
|
|||||||
if (!cmd_args || !msh->fds)
|
if (!cmd_args || !msh->fds)
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
cur_cmd = msh->cmds;
|
cur_cmd = msh->cmds;
|
||||||
|
//ft_printf_fd(2, "cmd: %s: args_count: %d\n", cur_cmd->token, args_count);
|
||||||
i = 0;
|
i = 0;
|
||||||
while (i < args_count)
|
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;
|
cur_cmd = cur_cmd->next;
|
||||||
i++;
|
|
||||||
}
|
}
|
||||||
return (cmd_args);
|
return (cmd_args);
|
||||||
}
|
}
|
||||||
@ -334,13 +347,13 @@ void remove_command_from_msh(t_msh *msh)
|
|||||||
msh->in_type = ARG;
|
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;
|
t_cmd *cur_cmd;
|
||||||
|
|
||||||
//msh->in_type = ARG;
|
//msh->in_type = ARG;
|
||||||
|
|
||||||
cur_cmd = msh->cmds;
|
cur_cmd = cmds;
|
||||||
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
while (cur_cmd && cur_cmd->next && cur_cmd->type == ARG)
|
||||||
cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
/*
|
/*
|
||||||
@ -352,24 +365,27 @@ void get_in_type(t_msh *msh)
|
|||||||
if (cur_cmd->type)
|
if (cur_cmd->type)
|
||||||
{
|
{
|
||||||
msh->in_type = 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 (cur_cmd->type == HERE_DOC)
|
||||||
if (msh->in_type == HERE_DOC)
|
handle_here_doc(msh, cur_cmd->next->token);
|
||||||
handle_here_doc(msh, cur_cmd->token);
|
if (cur_cmd->type == RED_I)
|
||||||
if (msh->in_type == RED_I)
|
|
||||||
{
|
{
|
||||||
msh->in_fd = open(cur_cmd->token, O_RDONLY);
|
if (msh->in_fd != 0)
|
||||||
ft_printf_fd(2, "opened %s: %d\n", cur_cmd->token, msh->in_fd);
|
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)
|
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("");
|
perror("");
|
||||||
|
g_return_code = 1;
|
||||||
// todo: cancel execution of all commands
|
// todo: cancel execution of all commands
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
//cur_cmd = cur_cmd->next;
|
cur_cmd = cur_cmd->next;
|
||||||
//msh->cmds = cur_cmd;
|
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);
|
//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;
|
msh->out_type = ARG;
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
//ft_printf_fd(2, "%s of type %d", cur_cmd->token, cur_cmd->type);
|
||||||
msh->out_type = cur_cmd->type;
|
msh->out_type = cur_cmd->type;
|
||||||
if (msh->out_type == RED_O)
|
if (msh->out_type == RED_O)
|
||||||
msh->out_fd = open(cur_cmd->next->token, O_CREAT | O_RDWR | O_TRUNC,
|
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)
|
if (msh->out_type == RED_O_APP)
|
||||||
msh->out_fd = open(cur_cmd->next->token,
|
msh->out_fd = open(cur_cmd->next->token,
|
||||||
O_CREAT | O_RDWR | O_APPEND, 0644);
|
O_CREAT | O_RDWR | O_APPEND, 0644);
|
||||||
if (msh->out_fd) // debug
|
//if (msh->out_fd) // debug
|
||||||
ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->out_fd);
|
// ft_printf_fd(2, "opened %s: %d\n", cur_cmd->next->token, msh->out_fd);
|
||||||
if (msh->out_fd == -1)
|
if (msh->out_fd == -1)
|
||||||
{
|
{
|
||||||
perror("open");
|
perror("open");
|
||||||
@ -426,9 +443,9 @@ void exec_command(t_msh *msh)
|
|||||||
msh->fds[i] = ft_calloc(2, sizeof(int *));
|
msh->fds[i] = ft_calloc(2, sizeof(int *));
|
||||||
if (!msh->fds[i])
|
if (!msh->fds[i])
|
||||||
ft_exit(msh, 1);
|
ft_exit(msh, 1);
|
||||||
get_in_type(msh);
|
get_in_type(msh, msh->cmds);
|
||||||
get_out_type(msh);
|
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);
|
//ft_printf_fd(2, "cmd: %s\n", msh->cmds->token);
|
||||||
if (!cmd_is_builtin(msh, msh->cmds->token))
|
if (!cmd_is_builtin(msh, msh->cmds->token))
|
||||||
get_cmd_path(msh);
|
get_cmd_path(msh);
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
|
||||||
/* +#+#+#+#+#+ +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2024/02/02 21:59:20 by tomoron #+# #+# */
|
/* 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);
|
msh->cmds = parse_command(command, msh->env);
|
||||||
free(command);
|
free(command);
|
||||||
msh->cmds = handle_alias(msh);
|
msh->cmds = handle_alias(msh);
|
||||||
//print_parsed_cmd(msh->cmds);
|
//print_parsed_cmd(msh->cmds); // debug
|
||||||
exec_command(msh);
|
exec_command(msh);
|
||||||
free_cmd(msh->cmds);
|
free_cmd(msh->cmds);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user