fixed ambiguous commands

This commit is contained in:
mdev9
2024-04-24 21:14:54 +02:00
parent 7cb969caba
commit 6b4a3dd857
4 changed files with 24 additions and 13 deletions

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */
/* Updated: 2024/04/24 19:18:07 by tomoron ### ########.fr */ /* Updated: 2024/04/24 21:13:53 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -119,6 +119,11 @@ void exec_commands(t_msh *msh)
if (!msh->tokens && !is_parenthesis(msh->cmds)) if (!msh->tokens && !is_parenthesis(msh->cmds))
{ {
g_return_code = 0; g_return_code = 0;
get_redirections(msh, msh->cmds);
if(msh->in_fd > 2)
close(msh->in_fd);
if(msh->out_fd > 2)
close(msh->out_fd);
return ; return ;
} }
cmd_count = get_cmd_count(msh->cmds); cmd_count = get_cmd_count(msh->cmds);
@ -126,12 +131,11 @@ void exec_commands(t_msh *msh)
msh->pids = ft_calloc(cmd_count, sizeof(int *)); msh->pids = ft_calloc(cmd_count, sizeof(int *));
if (!msh->pids || !msh->fds) if (!msh->pids || !msh->fds)
ft_exit(msh, 1); ft_exit(msh, 1);
i = 0; i = -1;
while (i < cmd_count) while (++i < cmd_count)
{ {
get_redirections(msh, msh->cmds); get_redirections(msh, msh->cmds);
exec_command(msh, i, cmd_count); exec_command(msh, i, cmd_count);
i++;
} }
end_execution(msh, cmd_count); end_execution(msh, cmd_count);
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 10:51:13 by marde-vr #+# #+# */ /* Created: 2024/04/24 10:51:13 by marde-vr #+# #+# */
/* Updated: 2024/04/24 10:51:25 by marde-vr ### ########.fr */ /* Updated: 2024/04/24 21:11:53 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */ /* Created: 2024/03/05 18:15:27 by marde-vr #+# #+# */
/* Updated: 2024/04/24 14:41:40 by tomoron ### ########.fr */ /* Updated: 2024/04/24 21:06:43 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,6 +33,13 @@ void redirect_input(t_msh *msh, int i, char **cmd_args)
} }
} }
void ambiguous_redirect(char *str, t_msh *msh)
{
ft_printf_fd(2, "minishell: %s: ambiguous redirect\n", str);
msh->in_fd = -2;
g_return_code = 1;
}
int open_input_file(t_msh *msh, t_cmd **cur_token) int open_input_file(t_msh *msh, t_cmd **cur_token)
{ {
t_token *filename; t_token *filename;
@ -46,20 +53,20 @@ int open_input_file(t_msh *msh, t_cmd **cur_token)
filename = parse_tokens((*cur_token)->value, msh->env); filename = parse_tokens((*cur_token)->value, msh->env);
if (!filename) if (!filename)
ft_exit(msh, 1); ft_exit(msh, 1);
msh->in_fd = open(filename->value, O_RDONLY); if(filename->next)
free_token(filename); ambiguous_redirect((*cur_token)->value, msh);
if(!filename->next)
msh->in_fd = open(filename->value, O_RDONLY);
if (msh->in_fd == -1) if (msh->in_fd == -1)
{ {
filename = parse_tokens((*cur_token)->value, msh->env);
if (!filename)
ft_exit(msh, 1);
ft_printf_fd(2, "minishell: %s: ", filename->value); ft_printf_fd(2, "minishell: %s: ", filename->value);
perror(""); perror("");
free_token(filename); free_token(filename);
return (1); return (1);
} }
free_token(filename);
} }
return (0); return (msh->in_fd == -2);
} }
int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc) int get_in_type(t_msh *msh, t_cmd *t_strt, t_cmd *tokens, int here_doc)

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */ /* Created: 2024/04/19 14:09:44 by tomoron #+# #+# */
/* Updated: 2024/04/24 13:25:20 by marde-vr ### ########.fr */ /* Updated: 2024/04/24 21:14:15 by marde-vr ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */