From 25a7368c4cee51b5bf73ef68d85cd7375588a2f0 Mon Sep 17 00:00:00 2001 From: tomoron Date: Fri, 29 Mar 2024 16:06:12 +0100 Subject: [PATCH] parsing bonus (pas complet) --- Makefile | 6 ++- srcs/exec_bonus.c | 11 +++-- srcs/lst_cmd.c | 4 +- srcs/main.c | 11 +++-- srcs/minishell.h | 4 +- srcs/parsing.c | 4 +- srcs/parsing_bonus.c | 108 +++++++++++++++++++++++++++++++++++++++++-- 7 files changed, 129 insertions(+), 19 deletions(-) diff --git a/Makefile b/Makefile index eec6698..7d1c2ef 100644 --- a/Makefile +++ b/Makefile @@ -6,7 +6,7 @@ # By: marde-vr +#+ +:+ +#+ # # +#+#+#+#+#+ +#+ # # Created: 2023/07/28 00:35:01 by tomoron #+# #+# # -# Updated: 2024/03/27 18:29:50 by tomoron ### ########.fr # +# Updated: 2024/03/29 14:34:57 by tomoron ### ########.fr # # # # **************************************************************************** # @@ -15,6 +15,7 @@ SRCS_RAW = main.c\ lst_cmd.c\ cd.c\ lst_env.c\ + lst_token.c\ exec.c\ exit.c\ echo.c\ @@ -35,7 +36,8 @@ SRCS_RAW = main.c\ utils.c\ utils2.c\ signal_handler.c\ - parsing_bonus.c + parsing_bonus.c\ + exec_bonus.c OBJS_DIR = objs/ diff --git a/srcs/exec_bonus.c b/srcs/exec_bonus.c index 6090988..390792c 100644 --- a/srcs/exec_bonus.c +++ b/srcs/exec_bonus.c @@ -6,16 +6,19 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/28 13:50:14 by tomoron #+# #+# */ -/* Updated: 2024/03/28 14:34:55 by tomoron ### ########.fr */ +/* Updated: 2024/03/29 14:47:40 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void exec_command_bonus(char *cmd_str) +void exec_command_bonus(t_msh *msh, char *cmd_str) { t_cmd *cmd; - cmd = parsing_bonus(commands); - print_parsed_cmd(t_cmd *cmd); + (void)msh; + printf("cmd : %s\n",cmd_str); + cmd = parsing_bonus(cmd_str); + printf("%p\n", cmd); + print_parsed_cmd(cmd); } diff --git a/srcs/lst_cmd.c b/srcs/lst_cmd.c index 82ec88c..ed9309d 100755 --- a/srcs/lst_cmd.c +++ b/srcs/lst_cmd.c @@ -6,7 +6,7 @@ /* By: marde-vr +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/02/06 20:46:19 by tomoron #+# #+# */ -/* Updated: 2024/03/28 14:30:24 by tomoron ### ########.fr */ +/* Updated: 2024/03/29 14:30:00 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ t_cmd *cmd_add_back(t_cmd *cmd, char *value, t_cmd_type type) if (!res) return (cmd); res->value = value; - res->type = type; + res->cmd_type = type; if (!cmd) return (res); current = cmd; diff --git a/srcs/main.c b/srcs/main.c index 9af79b6..ceee7bd 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/03/28 14:44:28 by tomoron ### ########.fr */ +/* Updated: 2024/03/29 14:45:43 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -83,7 +83,7 @@ int init_minishell(t_msh **msh, int argc, char **argv, char **envp) (*msh)->env = get_env(envp); tcgetattr(1, &t_p); (*msh)->echoctl = t_p.c_lflag & ECHOCTL; - signal(SIGINT, signal_handler_interactive); + //signal(SIGINT, signal_handler_interactive); signal(SIGQUIT, signal_handler_interactive); if (set_echoctl(0)) ft_exit(*msh, 1); @@ -121,11 +121,11 @@ int main(int argc, char **argv, char **envp) return (g_return_code); }*/ -int main(void int argc, char **argv, char **envp) +int main(int argc, char **argv, char **envp) { char *commands; char *prompt; - t_cmd *cmd + t_msh *msh; commands = (char *)1; init_minishell(&msh, argc, argv, envp); @@ -137,6 +137,7 @@ int main(void int argc, char **argv, char **envp) commands = readline(prompt); free(prompt); add_history(commands); - exec_command_bonus(commands); + exec_command_bonus(msh, commands); + free(commands); } } diff --git a/srcs/minishell.h b/srcs/minishell.h index 31636ea..285cf6c 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/03/28 14:31:17 by tomoron ### ########.fr */ +/* Updated: 2024/03/29 14:36:18 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ @@ -87,6 +87,7 @@ void *here_doc_variables(int write, int index, void *data); int add_var_to_str(char *res, char **command, t_env *env); void find_cmd_path(t_msh *msh, char **paths, int *found); t_env *env_add_back(t_env *env, char *name, char *value); +void exec_command_bonus(t_msh *msh, char *cmd_str); int cmd_is_builtin(t_msh *msh, char *cmd_token); void child(t_msh *msh, char **cmd_args, int i); t_token *parse_command(char *command, t_env *env); @@ -99,6 +100,7 @@ void signal_handler_interactive(int signum); int get_token_len(char *cmd, t_env *env); void signal_handler_here_doc(int signum); t_token *parsing_syntax_error(t_token *res); +t_cmd *parsing_bonus(char *cmd); int file_access(t_msh *msh, int *found); void remove_command_from_msh(t_msh *msh); void ft_exit(t_msh *msh, int error_code); diff --git a/srcs/parsing.c b/srcs/parsing.c index 68a0acb..ccc5e0a 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/03/28 14:10:02 by tomoron ### ########.fr */ +/* Updated: 2024/03/29 14:31:04 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" @@ -101,7 +101,7 @@ t_token *parse_command(char *command, t_env *env) value = 0; if (type == ARG && value == 0) return (free_token(res)); - res = cmd_add_back(res, value, type); + res = token_add_back(res, value, type); while (ft_isspace(*command)) command++; } diff --git a/srcs/parsing_bonus.c b/srcs/parsing_bonus.c index bdec630..f30f0ab 100644 --- a/srcs/parsing_bonus.c +++ b/srcs/parsing_bonus.c @@ -6,13 +6,115 @@ /* By: tomoron +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/03/27 14:40:44 by tomoron #+# #+# */ -/* Updated: 2024/03/28 14:43:59 by tomoron ### ########.fr */ +/* Updated: 2024/03/29 15:57:36 by tomoron ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_cmd *parsing_bonus(char *cmd) +int check_syntax(char *cmd) { - t_cmd *res; + 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 * (-(*cmd == ')')); + cmd++; + } + if (in_quote || in_dquote || parenthesis) + ft_putstr_fd("minishell: syntax error", 2); + return (!(in_quote || in_dquote || parenthesis)); +} + +t_cmd_type get_cmd_type_bonus(char **cmd) +{ + t_cmd_type res; + + while (ft_isspace(**cmd)) + (*cmd)++; + if (**cmd == '|' && (*cmd)[1] != '|') + res = PIPE; + else if (**cmd == '|' && (*cmd)[1] == '|') + res = OR; + else if (**cmd == '&' && (*cmd)[1] == '&') + res = AND; + else if (**cmd == '(') + res = PAREN; + else + res = CMD; + if(res != CMD) + (*cmd)++; + if (res == OR || res == AND) + (*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); +} + +int *get_normal_cmd_len(char *cmd) +{ +} + +char *get_cmd_value(char **cmd , t_cmd_type type) +{ + int len; + + if (type == PAREN) + len = get_parenthesis_cmd_len(*cmd); + else + len = get_normal_cmd_len(*cmd); + return(0); +} + +t_cmd *parsing_bonus(char *cmd) +{ + t_cmd *res; + t_cmd_type type; + char *value; + + res = 0; + if (!check_syntax(cmd)) + return (0); + while (*cmd) + { + type = get_cmd_type_bonus(&cmd); + if (type == CMD || type == PAREN) + value = get_cmd_value(&cmd); + else + value = 0; + res = cmd_add_back(res, value, type); + } + return (res); }