diff --git a/Makefile b/Makefile index 7c3b6ff..6f03bfa 100755 --- a/Makefile +++ b/Makefile @@ -14,7 +14,7 @@ ifeq ($(UNAME_S), Darwin) endif $(NAME): $(LIBFT) $(OBJS) - gcc ${OBJS} ${LIB} -o ${NAME} + gcc -g ${OBJS} ${LIB} -o ${NAME} $(LIBFT): ${MAKE} -C ./libft diff --git a/README.md b/README.md index ed0396d..173d502 100755 --- a/README.md +++ b/README.md @@ -5,7 +5,12 @@ - Protect mallocs, - verify exits codes https://wiki.bash-hackers.org/scripting/basics?s[]=variables#exit_codes +- do the `(...)` -> `./minishell ...` conversion +- finish && and || + ## Issues : +- only "" line segfault (no token created) + ## Notes : diff --git a/caller.c b/caller.c index 5560e84..5bf475d 100755 --- a/caller.c +++ b/caller.c @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/03 11:48:16 by narnaud #+# #+# */ -/* Updated: 2022/05/07 23:16:47 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/09 09:43:21 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,13 +17,16 @@ static pid_t caller(t_datas *datas, t_command *cmd); int is_builtin(char *cmd) { - if (!ft_strncmp(cmd, "echo", 5) \ - || !ft_strncmp(cmd, "pwd", 4) \ - || !ft_strncmp(cmd, "cd", 3) \ - || !ft_strncmp(cmd, "export", 7) \ - || !ft_strncmp(cmd, "unset", 6) \ - || !ft_strncmp(cmd, "env", 4) \ - || !ft_strncmp(cmd, "exit", 5)) + int cmd_len; + + cmd_len = ft_strlen(cmd) + 1; + if (!ft_strncmp(cmd, "echo", ft_min(cmd_len, 5)) \ + || !ft_strncmp(cmd, "pwd", ft_min(cmd_len, 4)) \ + || !ft_strncmp(cmd, "cd", ft_min(cmd_len, 3)) \ + || !ft_strncmp(cmd, "export", ft_min(cmd_len, 7)) \ + || !ft_strncmp(cmd, "unset", ft_min(cmd_len, 6)) \ + || !ft_strncmp(cmd, "env", ft_min(cmd_len, 4)) \ + || !ft_strncmp(cmd, "exit", ft_min(cmd_len, 5))) return (1); else return (0); diff --git a/lexer.c b/lexer.c index daf3cfd..444ec89 100755 --- a/lexer.c +++ b/lexer.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ -/* Updated: 2022/05/07 23:12:25 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/09 09:59:11 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -31,7 +31,7 @@ t_token *lexer(t_datas *datas, char *line) { if (check_state(lex, &line)) continue ; - if (lex->state != S_QUOTE_ST && *line == '$') + if (lex->state != S_QUOTE_ST && lex->deep == 0 && *line == '$') tmp_i = replace_var(datas, &line, tmp, tmp_i); else if (check_register(lex, &line, tmp)) tmp_i = (ft_bzero(tmp, STR_MAX_SIZE), 0); @@ -50,25 +50,26 @@ static int check_state(t_lexer *lex, char **line) t_state new; new = OLD_ST; - if (**line == '"') - { - if (lex->state == D_QUOTE_ST) - new = ROOT_ST; - else if (lex->state == ROOT_ST) - new = D_QUOTE_ST; - } - else if (**line == '\'') - { - if (lex->state == S_QUOTE_ST) - new = ROOT_ST; - else if (lex->state == ROOT_ST) - new = S_QUOTE_ST; - } + if (**line == '"' && lex->state == D_QUOTE_ST) + new = ROOT_ST; + else if (**line == '"' && lex->state == ROOT_ST) + new = D_QUOTE_ST; + else if (**line == '\'' && lex->state == S_QUOTE_ST) + new = ROOT_ST; + else if (**line == '\'' && lex->state == ROOT_ST) + new = S_QUOTE_ST; + else if (**line == '(' && lex->state == ROOT_ST) + lex->deep++; + else if (**line == ')' && lex->state == ROOT_ST) + lex->deep--; if (new) { - (*line)++; lex->state = new; - return (1); + if (lex->deep == 0) + { + (*line)++; + return (1); + } } return (0); } @@ -100,13 +101,39 @@ static int create_token(t_lexer *lex, char str[]) return (1); } +static int check_ope(t_lexer *lex, char **line, char *tmp) +{ + if (!ft_strncmp(*line, "&&", 2)) + { + (*line) += 2; + lex->next_type = AND; + create_token(lex, tmp); + return (create_token(lex, "&&")); + } + if (!ft_strncmp(*line, "||", 2)) + { + (*line) += 2; + lex->next_type = OR; + create_token(lex, tmp); + return (create_token(lex, "||")); + } + if (**line == '|') + { + (*line)++; + lex->next_type = PIPE; + create_token(lex, tmp); + return (create_token(lex, "|")); + } + return (0); + +} static int set_redir(t_lexer *lex, char **line, char ch); static int check_register(t_lexer *lex, char **line, char *tmp) { int spaces; - if (lex->state != ROOT_ST) + if (lex->state != ROOT_ST || lex->deep > 0) return (0); spaces = 0; if (ft_isspace(**line)) @@ -115,19 +142,13 @@ static int check_register(t_lexer *lex, char **line, char *tmp) (*line)++; spaces = 1; } - if (set_redir(lex, line, '>') \ - || set_redir(lex, line, '<')) + if (set_redir(lex, line, '>') || set_redir(lex, line, '<')) return (create_token(lex, tmp)); - else if (**line == '|') - { - (*line)++; - lex->next_type = PIPE; - create_token(lex, tmp); - return (create_token(lex, "|")); - } - if (!spaces || !*tmp) - return (0); - return (create_token(lex, tmp)); + if (check_ope(lex, line, tmp)) + return (1); + if ((**line == '(' || spaces) && *tmp) + return (create_token(lex, tmp)); + return (0); } static int set_redir(t_lexer *lex, char **line, char ch) diff --git a/libft/Makefile b/libft/Makefile index 19707b7..1713a8e 100755 --- a/libft/Makefile +++ b/libft/Makefile @@ -17,7 +17,8 @@ SRCS += mem/ft_bzero.c mem/ft_memset.c mem/ft_memchr.c mem/ft_memcpy.c \ SRCS += put/ft_putchar_fd.c put/ft_putnbr_fd.c put/ft_putendl_fd.c \ put/ft_putstr_fd.c -SRCS += nbr/ft_nbrlen.c nbr/ft_croissant.c nbr/ft_decroissant.c nbr/ft_max.c +SRCS += nbr/ft_nbrlen.c nbr/ft_croissant.c nbr/ft_decroissant.c nbr/ft_max.c \ + nbr/ft_min.c SRCS += slist/ft_size.c slist/ft_new.c slist/ft_last.c \ slist/ft_add_back.c slist/ft_add_front.c slist/ft_delone.c \ diff --git a/libft/libft.h b/libft/libft.h index 65efa2d..721fdf7 100755 --- a/libft/libft.h +++ b/libft/libft.h @@ -6,7 +6,7 @@ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2021/10/19 14:42:45 by narnaud #+# #+# */ -/* Updated: 2022/05/03 09:48:13 by narnaud ### ########.fr */ +/* Updated: 2022/05/09 08:59:36 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -57,6 +57,7 @@ size_t ft_longbaselen(long nbr, size_t base); int ft_croissant(int a, int b); int ft_decroissant(int a, int b); int ft_max(int a, int b); +int ft_min(int a, int b); void *ft_memset(void *b, int c, size_t len); char *ft_strchr(const char *s, int c); diff --git a/libft/nbr/ft_min.c b/libft/nbr/ft_min.c new file mode 100755 index 0000000..45a9ab6 --- /dev/null +++ b/libft/nbr/ft_min.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_min.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: narnaud@student.42nice.fr +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2022/05/07 22:56:56 by narnaud@stude #+# #+# */ +/* Updated: 2022/05/09 08:58:50 by narnaud ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "../libft.h" + +int ft_min(int a, int b) +{ + if (a < b) + return (a); + else + return (b); +} diff --git a/minishell.h b/minishell.h index 9cbe68c..6068ec0 100755 --- a/minishell.h +++ b/minishell.h @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:50:44 by narnaud #+# #+# */ -/* Updated: 2022/05/06 13:30:59 by narnaud ### ########.fr */ +/* Updated: 2022/05/09 09:02:20 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -77,11 +77,13 @@ int piper(t_datas *datas, t_command *cmd); typedef enum e_type { WORD, - PIPE, OUT, ADD, IN, HD, + PIPE, + AND, + OR, } t_type; typedef struct s_token @@ -108,6 +110,7 @@ typedef struct s_lex t_type type; t_type next_type; t_token *tokens; + int deep; int n_elem; } t_lexer; diff --git a/parser.c b/parser.c index 089f150..05f7583 100755 --- a/parser.c +++ b/parser.c @@ -6,7 +6,7 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 16:09:25 by narnaud #+# #+# */ -/* Updated: 2022/05/07 23:07:11 by narnaud@stude ### ########.fr */ +/* Updated: 2022/05/09 09:56:29 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ @@ -26,7 +26,7 @@ t_command *parser(t_datas *datas, t_token *tok, t_command *prev) cmd->prev = prev; cmd->argv = ft_calloc(count_arguments(tok) + 1, sizeof(char *)); cmd->argc = 0; - while (tok && tok->type != PIPE) + while (tok && tok->type < PIPE) tok = parse_cmd(datas, tok, cmd); if (tok) {