parentheses

This commit is contained in:
2024-04-23 16:03:01 +02:00
parent 3cf92d0495
commit 7a032b41b1
7 changed files with 94 additions and 28 deletions

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:22:15 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; cur_cmd = cmds;
if (cur_cmd) if (cur_cmd)
count++; count++;
while (cur_cmd->next) while (cur_cmd && cur_cmd->next)
{ {
cur_cmd = cur_cmd->next; cur_cmd = cur_cmd->next;
count++; count++;

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/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); cmds = get_next_command(cmds);
} }
free_cmd(msh->cmds_head); free_cmd(msh->cmds_head);
msh->cmds_head = 0;
} }
int exec(t_msh *msh, char **cmd_args, int i, int cmd_count) 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 *)); msh->fds[i] = ft_calloc(2, sizeof(int *));
if (!msh->fds[i]) if (!msh->fds[i])
ft_exit(msh, 1); 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); 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); exec(msh, get_cmd_args(msh), i, cmd_count);
remove_command_from_msh(msh); remove_command_from_msh(msh);
} }
@ -136,7 +137,7 @@ int get_cmd_count(t_cmd *cmds)
return (nb); return (nb);
} }
int is_parentethis(t_cmd *cmd) int is_parenthesis(t_cmd *cmd)
{ {
if(!cmd) if(!cmd)
return(0); return(0);
@ -194,20 +195,12 @@ void end_execution(t_msh *msh, int cmd_count)
set_echoctl(0); 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) void exec_commands(t_msh *msh)
{ {
int cmd_count; int cmd_count;
int i; int i;
if (!msh->tokens) if (!msh->tokens && !is_parenthesis(msh->cmds))
return ; return ;
cmd_count = get_cmd_count(msh->cmds); cmd_count = get_cmd_count(msh->cmds);
msh->fds = ft_calloc(cmd_count + 1, sizeof(int **)); msh->fds = ft_calloc(cmd_count + 1, sizeof(int **));

View File

@ -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/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); free(commands);
} }
printf("exit\n"); printf("exit\n");
return(g_return_code); ft_exit(msh, g_return_code);
} }

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */ /* 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); int print_env(t_env *env);
t_cmd *free_cmd(t_cmd *cmd); t_cmd *free_cmd(t_cmd *cmd);
int ft_export(t_msh *msh, t_token *cmd, t_env *env); 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); int is_input_type(t_cmd *cmd);
void free_env(t_env *env); void free_env(t_env *env);
int ft_unset(t_msh *msh); int ft_unset(t_msh *msh);

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */ /* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/09 15:26:01 by tomoron #+# #+# */ /* 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" #include "minishell.h"
@ -88,10 +88,10 @@ t_token *parse_cmds_to_token(t_cmd *command, t_env *env)
t_token *new; t_token *new;
res = 0; 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))) || is_input_type(command)))
{ {
if (is_cmd_type(command)) if (command->cmd_type == CMD)
{ {
new = parse_tokens(command->value, env); new = parse_tokens(command->value, env);
res = add_token_back(res, new); res = add_token_back(res, new);

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */ /* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 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"); 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) int check_tokens_syntax(t_cmd *cmd, t_cmd *last)
{ {
t_token *token; t_token *token;
if (is_cmd_type(last)) if (last && is_cmd_type(last))
{ {
ft_putstr_fd("minishell : syntax error\n", 2); ft_putstr_fd("minishell : syntax error\n", 2);
return (0); return (0);
@ -157,6 +181,21 @@ int check_tokens_syntax(t_cmd *cmd, t_cmd *last)
return (1); 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 *check_cmds_syntax(t_cmd *cmds)
{ {
t_cmd *last; 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) if (!is_operand_type(cmds) && cmds->cmd_type != PIPE && cmds->value == 0)
return (cmds); return (cmds);
last = cmds; last = cmds;
if(is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, 0))
return(cmds);
cmds = cmds->next; cmds = cmds->next;
while (cmds) while (cmds)
{ {
@ -175,8 +216,7 @@ t_cmd *check_cmds_syntax(t_cmd *cmds)
if (is_operand_type(cmds) || cmds->cmd_type == PIPE) 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))) 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); return (cmds);
if (is_cmd_type(cmds)) if(is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last))
if (!check_tokens_syntax(cmds, last))
return(cmds); return(cmds);
last = cmds; last = cmds;
cmds = cmds->next; cmds = cmds->next;

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:17:25 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]); 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) void execute_command(t_msh *msh, char **cmd_args)
{ {
char **env; char **env;
if (is_parenthesis(msh->cmds))
{
free(cmd_args);
handle_parenthesis(msh);
}
if (exec_builtin(msh)) if (exec_builtin(msh))
{ {
free(cmd_args); free(cmd_args);