diff --git a/lexer.c b/lexer.c index d6bc54a..edb56f7 100755 --- a/lexer.c +++ b/lexer.c @@ -6,26 +6,18 @@ /* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ -/* Updated: 2022/05/16 08:15:13 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 10:21:34 by mea ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -static int check_state(t_lexer *lex, char **line); - static int check_register(t_lexer *lex, char **line, char *tmp); -t_token *lexer(t_datas *datas, char *line) +void tmp_dealer(t_datas *datas, t_lexer *lex, char *line, char *tmp) { - t_lexer *lex; - t_token *ret; - char *tmp; - int tmp_i; + int tmp_i; - lex = ft_calloc(1, sizeof(*lex)); - lex->state = ROOT_ST; - tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp)); tmp_i = 0; while (*line) { @@ -42,6 +34,18 @@ t_token *lexer(t_datas *datas, char *line) else tmp[tmp_i++] = *(line++); } +} + +t_token *lexer(t_datas *datas, char *line) +{ + t_lexer *lex; + t_token *ret; + char *tmp; + + lex = ft_calloc(1, sizeof(*lex)); + lex->state = ROOT_ST; + tmp = ft_calloc(STR_MAX_SIZE, sizeof(*tmp)); + tmp_dealer(datas, lex, line, tmp); if (lex->type == WORD && lex->wc) create_wc(lex, tmp); else @@ -52,37 +56,6 @@ t_token *lexer(t_datas *datas, char *line) return (ret); } -static int check_state(t_lexer *lex, char **line) -{ - t_state new; - - new = OLD_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--; - else if (**line == '*' && lex->state == ROOT_ST && !lex->deep) - lex->wc = 1; - if (new) - { - lex->state = new; - if (lex->deep == 0) - { - (*line)++; - return (1); - } - } - return (0); -} - int create_token(t_lexer *lex, char str[]) { t_token *tok; @@ -146,12 +119,8 @@ static int check_register(t_lexer *lex, char **line, char *tmp) spaces = 0; if (!*tmp && (*(*line - 1) == '"' || *(*line - 1) == '\'')) lex->empty = 1; - if (ft_isspace(**line)) - { - while (ft_isspace(**line)) - (*line)++; - spaces = 1; - } + while (ft_isspace(**line) && ++spaces) + (*line)++; if (set_redir(lex, line, '>') || set_redir(lex, line, '<')) return (create_token(lex, tmp)); if (check_ope(lex, line, tmp)) diff --git a/lexer_2.c b/lexer_2.c index 170df7a..1ba8ab6 100644 --- a/lexer_2.c +++ b/lexer_2.c @@ -3,10 +3,10 @@ /* ::: :::::::: */ /* lexer_2.c :+: :+: :+: */ /* +:+ +:+ +:+ */ -/* By: narnaud +#+ +:+ +#+ */ +/* By: mea +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/16 07:53:17 by narnaud #+# #+# */ -/* Updated: 2022/05/16 09:20:23 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 10:22:46 by mea ### ########.fr */ /* */ /* ************************************************************************** */ @@ -107,3 +107,31 @@ int set_redir(t_lexer *lex, char **line, char ch) } return (0); } + +int check_state(t_lexer *lex, char **line) +{ + t_state new; + + new = OLD_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--; + else if (**line == '*' && lex->state == ROOT_ST && !lex->deep) + lex->wc = 1; + if (new) + { + lex->state = new; + if (lex->deep == 0 && (*line)++) + return (1); + } + return (0); +} diff --git a/minishell.h b/minishell.h index 877740b..09ac1b4 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/16 09:22:48 by narnaud ### ########.fr */ +/* Updated: 2022/05/16 10:23:25 by mea ### ########.fr */ /* */ /* ************************************************************************** */ @@ -65,7 +65,7 @@ char *expend_str(t_datas *datas, char *line); int set_redir(t_lexer *lex, char **line, char ch); // ----------------------------------Utils_3.c int is_valid_identifier(char *ident); -int create_wc(t_lexer *lex, char *tmp); +int create_wc(t_lexer *lex, char *tmp); // ----------------------------------Builtins.c int is_builtin(char *cmd); @@ -127,7 +127,10 @@ typedef struct s_lex } t_lexer; t_token *lexer(t_datas *datas, char *line); -int create_token(t_lexer *lex, char str[]); +int create_token(t_lexer *lex, char str[]); -int create_wc(t_lexer *lex, char *wc); -#endif +int check_state(t_lexer *lex, char **line); + +int create_wc(t_lexer *lex, char *wc); + +#endif \ No newline at end of file