diff --git a/srcs/commands.c b/srcs/commands.c index cb502ab..99eb729 100755 --- a/srcs/commands.c +++ b/srcs/commands.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:22:15 by marde-vr #+# #+# */ -/* Updated: 2024/04/22 19:32:11 by marde-vr ### ########.fr */ +/* Updated: 2024/04/23 13:46:25 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,7 +21,7 @@ int get_args_count(t_token *cmds) cur_cmd = cmds; if (cur_cmd) count++; - while (cur_cmd->next) + while (cur_cmd && cur_cmd->next) { cur_cmd = cur_cmd->next; count++; diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 5c12202..1e583fd 100755 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/04/23 13:21:32 by tomoron ### ########.fr */ +/* Updated: 2024/04/23 14:45:42 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -70,6 +70,7 @@ void exec_command_bonus(t_msh *msh, char *cmd_str) cmds = get_next_command(cmds); } free_cmd(msh->cmds_head); + msh->cmds_head = 0; } int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) @@ -109,9 +110,9 @@ void exec_command(t_msh *msh, int i, int cmd_count) msh->fds[i] = ft_calloc(2, sizeof(int *)); if (!msh->fds[i]) ft_exit(msh, 1); - if (!cmd_is_builtin(msh, msh->tokens->value)) + if (msh->tokens && !cmd_is_builtin(msh, msh->tokens->value)) get_cmd_path(msh); - if (msh->tokens->value) + if ((msh->tokens && msh->tokens->value) || is_parenthesis(msh->cmds)) exec(msh, get_cmd_args(msh), i, cmd_count); remove_command_from_msh(msh); } @@ -136,7 +137,7 @@ int get_cmd_count(t_cmd *cmds) return (nb); } -int is_parentethis(t_cmd *cmd) +int is_parenthesis(t_cmd *cmd) { if(!cmd) return(0); @@ -194,20 +195,12 @@ void end_execution(t_msh *msh, int cmd_count) set_echoctl(0); } -int handle_parenthesis(t_msh *msh) -{ - if (!(msh->cmds->cmd_type == PAREN || (msh->cmds->cmd_type == PIPE - && msh->cmds->next->cmd_type == PAREN))) - return (0); - return (1); -} - void exec_commands(t_msh *msh) { int cmd_count; int i; - if (!msh->tokens) + if (!msh->tokens && !is_parenthesis(msh->cmds)) return ; cmd_count = get_cmd_count(msh->cmds); msh->fds = ft_calloc(cmd_count + 1, sizeof(int **)); diff --git a/srcs/main.c b/srcs/main.c index 7f99bf4..ca455d4 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/04/22 18:41:29 by marde-vr ### ########.fr */ +/* Updated: 2024/04/23 14:33:16 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -144,5 +144,5 @@ int main(int argc, char **argv, char **envp) free(commands); } printf("exit\n"); - return(g_return_code); + ft_exit(msh, g_return_code); } diff --git a/srcs/minishell.h b/srcs/minishell.h index d9a4c87..b3183cf 100755 --- a/srcs/minishell.h +++ b/srcs/minishell.h @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ -/* Updated: 2024/04/23 12:37:16 by tomoron ### ########.fr */ +/* Updated: 2024/04/23 13:44:49 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -142,6 +142,7 @@ int is_output_type(t_cmd *cmd); int print_env(t_env *env); t_cmd *free_cmd(t_cmd *cmd); int ft_export(t_msh *msh, t_token *cmd, t_env *env); +int is_parenthesis(t_cmd *cmd); int is_input_type(t_cmd *cmd); void free_env(t_env *env); int ft_unset(t_msh *msh); diff --git a/srcs/parsing.c b/srcs/parsing.c index 3008966..9d5f43b 100755 --- a/srcs/parsing.c +++ b/srcs/parsing.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ -/* Updated: 2024/04/22 19:37:26 by marde-vr ### ########.fr */ +/* Updated: 2024/04/23 13:32:17 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -88,10 +88,10 @@ t_token *parse_cmds_to_token(t_cmd *command, t_env *env) t_token *new; res = 0; - while (command && (is_cmd_type(command) || is_output_type(command) + while (command && (command->cmd_type == CMD || is_output_type(command) || is_input_type(command))) { - if (is_cmd_type(command)) + if (command->cmd_type == CMD) { new = parse_tokens(command->value, env); res = add_token_back(res, new); diff --git a/srcs/parsing_bonus.c b/srcs/parsing_bonus.c index 0fc7cf9..50fa8c7 100755 --- a/srcs/parsing_bonus.c +++ b/srcs/parsing_bonus.c @@ -6,7 +6,7 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */ -/* Updated: 2024/04/22 19:38:45 by marde-vr ### ########.fr */ +/* Updated: 2024/04/23 16:00:00 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -141,11 +141,35 @@ void print_syntax_error_bonus(t_cmd *cmd) fprintf(stderr, "'\n"); } + +int check_parens_syntax(t_cmd *cmd, t_cmd *last) +{ + t_cmd *parsed_cmd; + t_cmd *tmp; + + if(last && is_cmd_type(last)) + { + ft_putstr_fd("minishell: syntax error\n", 2); + return(0); + } + parsed_cmd = parsing_bonus(cmd->value); + if(!parsed_cmd) + { + ft_putstr_fd("minishell: syntax error\n", 2); + return(0); + } + tmp = check_cmds_syntax(parsed_cmd); + if(tmp) + print_syntax_error_bonus(tmp); + free_cmd(parsed_cmd); + return(tmp == 0); +} + int check_tokens_syntax(t_cmd *cmd, t_cmd *last) { t_token *token; - if (is_cmd_type(last)) + if (last && is_cmd_type(last)) { ft_putstr_fd("minishell : syntax error\n", 2); return (0); @@ -157,6 +181,21 @@ int check_tokens_syntax(t_cmd *cmd, t_cmd *last) return (1); } +int check_cmd_type_syntax(t_cmd *cmds, t_cmd *last) +{ + if (cmds->cmd_type == CMD) + { + if (!check_tokens_syntax(cmds, last)) + return (0); + } + else if(cmds->cmd_type == PAREN) + { + if(!check_parens_syntax(cmds, last)) + return(0); + } + return(1); +} + t_cmd *check_cmds_syntax(t_cmd *cmds) { t_cmd *last; @@ -166,6 +205,8 @@ t_cmd *check_cmds_syntax(t_cmd *cmds) if (!is_operand_type(cmds) && cmds->cmd_type != PIPE && cmds->value == 0) return (cmds); last = cmds; + if(is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, 0)) + return(cmds); cmds = cmds->next; while (cmds) { @@ -175,9 +216,8 @@ t_cmd *check_cmds_syntax(t_cmd *cmds) if (is_operand_type(cmds) || cmds->cmd_type == PIPE) if ((!is_cmd_type(last) && !is_output_type(last) && !is_input_type(last)) || !cmds->next || (!is_cmd_type(cmds->next) && !is_output_type(cmds->next) && !is_input_type(cmds->next))) return (cmds); - if (is_cmd_type(cmds)) - if (!check_tokens_syntax(cmds, last)) - return (cmds); + if(is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last)) + return(cmds); last = cmds; cmds = cmds->next; } diff --git a/srcs/pipe.c b/srcs/pipe.c index f94aa86..c3d8e16 100755 --- a/srcs/pipe.c +++ b/srcs/pipe.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/05 18:17:25 by marde-vr #+# #+# */ -/* Updated: 2024/04/22 19:49:36 by marde-vr ### ########.fr */ +/* Updated: 2024/04/23 14:47:24 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -27,10 +27,42 @@ void close_pipe_fds(t_msh *msh, int i) close(msh->fds[i][1]); } +void handle_parenthesis(t_msh *msh) +{ + char *command; + + command = 0; + if(msh->cmds->cmd_type == PAREN) + command = ft_strdup(msh->cmds->value); + else if(msh->cmds->cmd_type == PIPE) + command = ft_strdup(msh->cmds->next->value); + if(!command) + { + ft_printf_fd(2, "an error occured"); + ft_exit(msh, 1); + } + free(msh->pids); + free_cmd(msh->cmds_head); + free_fds(msh); + msh->in_type = 0; + msh->out_type = 0; + msh->in_fd = 0; + msh->out_fd = 0; + msh->locked_return_code = 0; + exec_command_bonus(msh, command); + free(command); + ft_exit(msh, g_return_code); +} + void execute_command(t_msh *msh, char **cmd_args) { char **env; - + + if (is_parenthesis(msh->cmds)) + { + free(cmd_args); + handle_parenthesis(msh); + } if (exec_builtin(msh)) { free(cmd_args);