This commit is contained in:
2024-04-23 18:51:39 +02:00
parent e5b705ff64
commit 471a417bf9
7 changed files with 105 additions and 62 deletions

View File

@ -6,7 +6,7 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
/* Updated: 2024/04/23 16:41:42 by tomoron ### ########.fr */
/* Updated: 2024/04/23 18:41:13 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -36,28 +36,34 @@ int check_str_syntax(char *cmd)
return (!(in_quote || in_dquote || parenthesis));
}
t_cmd_type str_to_cmd_type(char *cmd)
{
if (*cmd == '|' && cmd[1] != '|')
return (PIPE);
else if (*cmd == '|' && cmd[1] == '|')
return (OR);
else if (*cmd == '&' && cmd[1] == '&')
return (AND);
else if (*cmd == '>' && cmd[1] == '>')
return (RED_O_APP);
else if (*cmd == '<' && cmd[1] == '<')
return (HERE_DOC);
else if (*cmd == '>')
return (RED_O);
else if (*cmd == '<')
return (RED_I);
else if (*cmd == '(')
return (PAREN);
else if (*cmd == '&')
return (ERR);
return (CMD);
}
t_cmd_type get_cmd_type_bonus(char **cmd)
{
t_cmd_type res;
if (**cmd == '|' && (*cmd)[1] != '|')
res = PIPE;
else if (**cmd == '|' && (*cmd)[1] == '|')
res = OR;
else if (**cmd == '&' && (*cmd)[1] == '&')
res = AND;
else if (**cmd == '>' && (*cmd)[1] == '>')
res = RED_O_APP;
else if (**cmd == '<' && (*cmd)[1] == '<')
res = HERE_DOC;
else if (**cmd == '>')
res = RED_O;
else if (**cmd == '<')
res = RED_I;
else if (**cmd == '(')
res = PAREN;
else
res = CMD;
res = str_to_cmd_type(*cmd);
if (res != CMD)
(*cmd)++;
if (res == OR || res == AND || res == RED_O_APP || res == HERE_DOC)
@ -138,31 +144,32 @@ void print_syntax_error_bonus(t_cmd *cmd)
ft_printf_fd(2, "||");
if (cmd->cmd_type == PIPE)
ft_printf_fd(2, "|");
if (cmd->cmd_type == ERR)
ft_printf_fd(2, "&");
ft_printf_fd(2, "'\n");
}
int check_parens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
int check_parens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
{
t_cmd *parsed_cmd;
t_cmd *tmp;
t_cmd *parsed_cmd;
t_cmd *tmp;
if(last && is_cmd_type(last))
if (last && is_cmd_type(last))
{
ft_putstr_fd("minishell: syntax error\n", 2);
return(0);
return (0);
}
parsed_cmd = parsing_bonus(cmd->value);
if(!parsed_cmd)
if (!parsed_cmd)
{
ft_putstr_fd("minishell: syntax error\n", 2);
return(0);
return (0);
}
tmp = check_cmds_syntax(parsed_cmd, env);
if(tmp)
if (tmp)
print_syntax_error_bonus(tmp);
free_cmd(parsed_cmd);
return(tmp == 0);
return (tmp == 0);
}
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
@ -181,19 +188,19 @@ int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
return (1);
}
int check_cmd_type_syntax(t_cmd *cmds, t_cmd *last, t_env *env)
int check_cmd_type_syntax(t_cmd *cmds, t_cmd *last, t_env *env)
{
if (cmds->cmd_type == CMD)
{
if (!check_tokens_syntax(cmds, last, env))
return (0);
}
else if(cmds->cmd_type == PAREN)
else if (cmds->cmd_type == PAREN)
{
if(!check_parens_syntax(cmds, last, env))
return(0);
if (!check_parens_syntax(cmds, last, env))
return (0);
}
return(1);
return (1);
}
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
@ -205,19 +212,24 @@ t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
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, env))
return(cmds);
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, 0, env))
return (cmds);
cmds = cmds->next;
while (cmds)
{
if (cmds->cmd_type == ERR)
return (cmds);
if (!is_operand_type(cmds) && cmds->cmd_type != PIPE
&& cmds->value == 0)
return (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)))
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) && !check_cmd_type_syntax(cmds, last, env))
return(cmds);
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last, env))
return (cmds);
last = cmds;
cmds = cmds->next;
}