patate douce

This commit is contained in:
2024-04-24 15:07:12 +02:00
parent 3a55a9f6ec
commit 2f3132a2c3
6 changed files with 251 additions and 198 deletions

View File

@ -6,7 +6,7 @@
# By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2023/07/28 00:35:01 by tomoron #+# #+# #
# Updated: 2024/04/24 11:00:09 by marde-vr ### ########.fr #
# Updated: 2024/04/24 14:58:52 by tomoron ### ########.fr #
# #
# **************************************************************************** #
@ -42,6 +42,9 @@ SRCS_RAW = main.c\
parsing_bonus.c\
exec_bonus.c\
exec_utils.c\
get_len_bonus.c\
check_syntax.c\
check_syntax_utils.c\
free.c
OBJS_DIR = objs/

115
srcs/check_syntax.c Normal file
View File

@ -0,0 +1,115 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_syntax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:50:15 by tomoron #+# #+# */
/* Updated: 2024/04/24 15:00:00 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_parens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
{
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, env);
if (tmp)
print_syntax_error_bonus(tmp);
free_cmd(parsed_cmd);
return (tmp == 0);
}
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)
{
if (!check_parens_syntax(cmds, last, env))
return (0);
}
return (1);
}
int check_cmd_syntax(t_cmd *cmds, t_cmd *last, t_env *env)
{
if (cmds->cmd_type == ERR)
return (1);
if (!is_operand_type(cmds) && cmds->cmd_type != PIPE
&& cmds->value == 0)
return (1);
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 (1);
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last, env))
return (1);
return (0);
}
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
{
t_cmd *last;
if (!cmds || is_operand_type(cmds) || cmds->cmd_type == PIPE)
return (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, env))
return (cmds);
cmds = cmds->next;
while (cmds)
{
if (check_cmd_syntax(cmds, last, env))
return (cmds);
last = cmds;
cmds = cmds->next;
}
return (0);
}
int check_str_syntax(char *cmd)
{
int in_quote;
int in_dquote;
int parenthesis;
in_quote = 0;
in_dquote = 0;
parenthesis = 0;
while (*cmd)
{
if (*cmd == '\'' && !in_dquote)
in_quote = !in_quote;
if (*cmd == '"' && !in_quote)
in_dquote = !in_dquote;
if ((*cmd == '(' || *cmd == ')') && !in_quote && !in_dquote)
parenthesis += 1 - (2 * (*cmd == ')'));
cmd++;
}
if (in_quote || in_dquote || parenthesis)
ft_putstr_fd("minishell: syntax error\n", 2);
return (!(in_quote || in_dquote || parenthesis));
}

45
srcs/check_syntax_utils.c Normal file
View File

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* check_syntax_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:54:53 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:55:56 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void print_syntax_error_bonus(t_cmd *cmd)
{
if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN)
return ;
ft_printf_fd(2, "minishell : syntax error near unexpected token `");
if (cmd->cmd_type == AND)
ft_printf_fd(2, "&&");
if (cmd->cmd_type == OR)
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_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env)
{
t_token *token;
if (last && is_cmd_type(last))
{
ft_putstr_fd("minishell : syntax error\n", 2);
return (0);
}
token = parse_cmds_to_token(cmd, env);
if (!token)
return (0);
free_token(token);
return (1);
}

80
srcs/get_len_bonus.c Normal file
View File

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_len_bonus.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/24 14:51:00 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:59:47 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_normal_cmd_len(char *cmd)
{
int len;
int in_quote;
int in_dquote;
len = 0;
in_quote = 0;
in_dquote = 0;
while (cmd[len] && (in_quote || in_dquote || (cmd[len] != '|'
&& cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')'
&& cmd[len] != '<' && cmd[len] != '>')))
{
if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote;
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
len++;
}
return (len);
}
int get_next_arg_len(char *cmd)
{
int len;
int in_quote;
int in_dquote;
len = 0;
in_quote = 0;
in_dquote = 0;
while (cmd[len] && ((!ft_isspace(cmd[len]) && cmd[len] != '&'
&& cmd[len] != '|' && cmd[len] != '<' && cmd[len] != '>')
|| in_quote || in_dquote))
{
if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote;
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
len++;
}
return (len);
}
int get_parenthesis_cmd_len(char *cmd)
{
int len;
int parenthesis;
int in_quote;
int in_dquote;
len = 0;
parenthesis = 1;
in_quote = 0;
in_dquote = 0;
while (cmd[len] && parenthesis)
{
if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote;
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
if ((cmd[len] == '(' || cmd[len] == ')') && !in_quote && !in_dquote)
parenthesis += 1 * (-(cmd[len] == ')'));
len++;
}
return (len - 1);
}

View File

@ -6,7 +6,7 @@
/* By: marde-vr <marde-vr@42angouleme.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/02/04 17:31:38 by tomoron #+# #+# */
/* Updated: 2024/04/24 14:03:39 by tomoron ### ########.fr */
/* Updated: 2024/04/24 15:05:21 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
@ -154,6 +154,11 @@ 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 get_parenthesis_cmd_len(char *cmd);
int get_normal_cmd_len(char *cmd);
int get_next_arg_len(char *cmd);
int check_str_syntax(char *cmd);
int check_tokens_syntax(t_cmd *cmd, t_cmd *last, t_env *env);
int is_parenthesis(t_cmd *cmd);
int is_input_type(t_cmd *cmd);
void free_env(t_env *env);

View File

@ -6,36 +6,12 @@
/* By: tomoron <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */
/* Updated: 2024/04/23 18:41:13 by tomoron ### ########.fr */
/* Updated: 2024/04/24 14:53:19 by tomoron ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_str_syntax(char *cmd)
{
int in_quote;
int in_dquote;
int parenthesis;
in_quote = 0;
in_dquote = 0;
parenthesis = 0;
while (*cmd)
{
if (*cmd == '\'' && !in_dquote)
in_quote = !in_quote;
if (*cmd == '"' && !in_quote)
in_dquote = !in_dquote;
if ((*cmd == '(' || *cmd == ')') && !in_quote && !in_dquote)
parenthesis += 1 - (2 * (*cmd == ')'));
cmd++;
}
if (in_quote || in_dquote || parenthesis)
ft_putstr_fd("minishell: syntax error\n", 2);
return (!(in_quote || in_dquote || parenthesis));
}
t_cmd_type str_to_cmd_type(char *cmd)
{
if (*cmd == '|' && cmd[1] != '|')
@ -71,52 +47,6 @@ t_cmd_type get_cmd_type_bonus(char **cmd)
return (res);
}
int get_parenthesis_cmd_len(char *cmd)
{
int len;
int parenthesis;
int in_quote;
int in_dquote;
len = 0;
parenthesis = 1;
in_quote = 0;
in_dquote = 0;
while (cmd[len] && parenthesis)
{
if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote;
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
if ((cmd[len] == '(' || cmd[len] == ')') && !in_quote && !in_dquote)
parenthesis += 1 * (-(cmd[len] == ')'));
len++;
}
return (len - 1);
}
int get_normal_cmd_len(char *cmd)
{
int len;
int in_quote;
int in_dquote;
len = 0;
in_quote = 0;
in_dquote = 0;
while (cmd[len] && (in_quote || in_dquote || (cmd[len] != '|'
&& cmd[len] != '&' && cmd[len] != '(' && cmd[len] != ')'
&& cmd[len] != '<' && cmd[len] != '>')))
{
if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote;
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
len++;
}
return (len);
}
char *get_cmd_value(char **cmd, t_cmd_type type)
{
int len;
@ -133,131 +63,6 @@ char *get_cmd_value(char **cmd, t_cmd_type type)
return (res);
}
void print_syntax_error_bonus(t_cmd *cmd)
{
if (cmd->cmd_type == CMD || cmd->cmd_type == PAREN)
return ;
ft_printf_fd(2, "minishell : syntax error near unexpected token `");
if (cmd->cmd_type == AND)
ft_printf_fd(2, "&&");
if (cmd->cmd_type == OR)
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)
{
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, env);
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_env *env)
{
t_token *token;
if (last && is_cmd_type(last))
{
ft_putstr_fd("minishell : syntax error\n", 2);
return (0);
}
token = parse_cmds_to_token(cmd, env);
if (!token)
return (0);
free_token(token);
return (1);
}
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)
{
if (!check_parens_syntax(cmds, last, env))
return (0);
}
return (1);
}
t_cmd *check_cmds_syntax(t_cmd *cmds, t_env *env)
{
t_cmd *last;
if (!cmds || is_operand_type(cmds) || cmds->cmd_type == PIPE)
return (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, 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)))
return (cmds);
if (is_cmd_type(cmds) && !check_cmd_type_syntax(cmds, last, env))
return (cmds);
last = cmds;
cmds = cmds->next;
}
return (0);
}
int get_next_arg_len(char *cmd)
{
int len;
int in_quote;
int in_dquote;
len = 0;
in_quote = 0;
in_dquote = 0;
while (cmd[len] && ((!ft_isspace(cmd[len]) && cmd[len] != '&'
&& cmd[len] != '|' && cmd[len] != '<' && cmd[len] != '>')
|| in_quote || in_dquote))
{
if (cmd[len] == '\'' && !in_dquote)
in_quote = !in_quote;
if (cmd[len] == '"' && !in_quote)
in_dquote = !in_dquote;
len++;
}
return (len);
}
char *get_next_arg(char **cmd)
{
int len;