From 4c678f767d58cf5d14454682a5dc8eb69010108a Mon Sep 17 00:00:00 2001 From: gazhonsepaskwa Date: Fri, 7 Feb 2025 12:35:13 +0100 Subject: [PATCH] syntax --- includes/parser/tokenizer.h | 1 + lib/libft/libft.h | 1 + lib/libft/srcs/format/ft_atoi.c | 11 +++++++++ srcs/main.c | 29 ++++++++++++++--------- srcs/parsing/parser.c | 6 +++-- srcs/parsing/syntax.c | 41 +++++++++++++++++++++++++++++++++ 6 files changed, 76 insertions(+), 13 deletions(-) create mode 100644 srcs/parsing/syntax.c diff --git a/includes/parser/tokenizer.h b/includes/parser/tokenizer.h index db68daa..67a4ff1 100644 --- a/includes/parser/tokenizer.h +++ b/includes/parser/tokenizer.h @@ -62,5 +62,6 @@ int is_sticked(char *val); int trim_nodes(t_node *head); void debug_linked_list(t_node *head, char *msg); int find_quote_node(t_node *head, char q); +int syntax_error(t_node *head); #endif diff --git a/lib/libft/libft.h b/lib/libft/libft.h index 52af24d..43d0af1 100644 --- a/lib/libft/libft.h +++ b/lib/libft/libft.h @@ -45,6 +45,7 @@ char *ft_strjoin(const char *s1, const char *s2); char *ft_strtrim(const char *s1, const char *set); char *ft_strmapi(char const *s, char (*f)(unsigned int, char)); void ft_striteri(char *s, void (*f)(unsigned int, char *)); +int is_only_space(char *str); void *ft_memset(void *b, int c, size_t len); void *ft_memcpy(void *dst, const void *src, size_t n); diff --git a/lib/libft/srcs/format/ft_atoi.c b/lib/libft/srcs/format/ft_atoi.c index 168f9cf..ab236ed 100644 --- a/lib/libft/srcs/format/ft_atoi.c +++ b/lib/libft/srcs/format/ft_atoi.c @@ -21,6 +21,17 @@ static int ft_isspace(char c) return (0); } +int is_only_space(char *str) +{ + int i; + + i = -1; + while (str[++i]) + if (!ft_isspace(str[i])) + return (0); + return (1); +} + static int ft_signer(char c, int *i) { int sign; diff --git a/srcs/main.c b/srcs/main.c index 3058bdc..778e388 100644 --- a/srcs/main.c +++ b/srcs/main.c @@ -18,36 +18,41 @@ static char *powerline(void) char *tilt; char *input; char *prompt; + char *line; pwd = getcwd(NULL, 0); + tilt = " "; if (ft_strncmp(pwd, "/home/", 6) == 0) { - pwd = pwd + 6; + pwd += 6; while (*pwd && (*pwd) != '/') - pwd = pwd + 1; + pwd ++; tilt = " ~"; } - else - tilt = " "; -// printf("%s---------------------------------------------- -// ----------------------------------%s", POW5, RESET); - prompt = ft_sprintf("\n%s  MMOAT %s%s%s%s%s %s%s%s ", - POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET); + line = ft_sprintf("%s----------------------------------------------\ +----------------------------------%s", POW5, RESET); + prompt = ft_sprintf("%s\n%s  MMOAT %s%s%s%s%s %s%s%s ", + line, POW1, POW2, POW3, POW4, tilt, pwd, RESET, POW5, RESET); input = readline(prompt); if (ft_strlen(input) > 0) add_history(input); free(prompt); + free(line); return (input); } static void interpret_cmd(char **input, t_msh *msh) { msh->head = parser(*input, msh); + if (!msh->head) + { + ft_free(input); + return ; + } msh->ex_code = execute_command(msh->head); free_ast(msh->head); msh->head = NULL; - free(*input); - *input = NULL; + ft_free(input); } int main(int ac, char **av, char **envp) @@ -60,12 +65,14 @@ int main(int ac, char **av, char **envp) if (!msh) return (1); if (ac == 1) + { while (1) { - while (!msh->input || !msh->input[0]) + while (!msh->input || !msh->input[0] || is_only_space(msh->input)) msh->input = powerline(); interpret_cmd(&msh->input, msh); } + } else { msh->input = ft_strdup(av[1]); diff --git a/srcs/parsing/parser.c b/srcs/parsing/parser.c index 62f9a51..1c0d856 100644 --- a/srcs/parsing/parser.c +++ b/srcs/parsing/parser.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* test.c :+: :+: :+: */ +/* parser.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: nalebrun +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/01/15 08:23:41 by nalebrun #+# #+# */ -/* Updated: 2025/02/03 11:49:21 by nalebrun ### ########.fr */ +/* Updated: 2025/02/07 12:08:40 by nalebrun ### ########.fr */ /* */ /* ************************************************************************** */ @@ -46,6 +46,8 @@ t_ast_n *parser(char *input, t_msh *msh) lst = tokenize(input); if (!lst) return (NULL); + if (syntax_error(lst)) + return (NULL); if (DEBUG) { dio = drawio_init("ast.xml"); diff --git a/srcs/parsing/syntax.c b/srcs/parsing/syntax.c new file mode 100644 index 0000000..88b9099 --- /dev/null +++ b/srcs/parsing/syntax.c @@ -0,0 +1,41 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* syntax.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: nalebrun +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/07 12:08:53 by nalebrun #+# #+# */ +/* Updated: 2025/02/07 12:08:53 by nalebrun ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../../includes/parser/parsing.h" + +int only_operator(t_node *head) +{ + if (!head->next && head->token == OPERATOR) + return (1); + return (0); +} + +int syntax_err_mess(char *token) +{ + ft_fprintf(2, "minishell : syntax error near unexpected token `%s'\n", token); + return (1); +} + +int syntax_error(t_node *head) +{ + // t_node *cpy; + + if (only_operator(head)) + return(syntax_err_mess(head->val)); + // cpy = node; + // while (cpy) + // { + // cpy = cpy->next; + // } + + return (0); +}