/* ************************************************************************** */ /* */ /* ::: :::::::: */ /* lexer.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: narnaud +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/05/02 13:44:57 by narnaud #+# #+# */ /* Updated: 2022/05/03 11:42:26 by narnaud ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" int create_token(t_lexer *lex, char str[]) { t_token *tok; t_type type; type = lex->type; if (lex->next_type) lex->type = lex->next_type; else lex->type = WORD; lex->next_type = WORD; if (!str[0]) return (0); tok = lex->tokens; if (!lex->tokens) tok = ft_calloc(1, sizeof *tok); else { while (tok->next) tok = tok->next; tok->next = ft_calloc(1, sizeof *tok); tok = tok->next; } tok->type = type; tok->value = strdup(str); if (DEBUG) printf("token value : %s - token type : %d\n", tok->value, type); if (!lex->tokens) lex->tokens = tok; return (1); } int check_register(t_lexer *lex, char **line, char *tmp) { int spaces; if (lex->state != ROOT_ST) return (0); spaces = 0; if (ft_isspace(**line) ) { while (ft_isspace(**line)) (*line)++; spaces = 1; } if (**line == '>' && (*line)++) { if (**line == '>' && (*line)++) lex->next_type = ADD; else lex->next_type = OUT; return (create_token(lex, tmp)); } else if (**line == '<' && (*line)++) { if (**line == '<' && (*line)++) lex->next_type = HD; else lex->next_type = IN; 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)); } int replace_var(char **line, char *tmp, int tmp_i) { char **env; int i; char *var_name; // TODO : free split and var_name i = 1; env = NULL; while (ft_isalpha((*line)[i]) || ft_isdigit((*line)[i]) || (*line)[i] == '_') i++; var_name = ft_substr(*line, 1, i - 1); i = 0; while (g_env[i]) { env = ft_split(g_env[i], '='); if (!ft_strncmp(var_name, env[0], ft_strlen(var_name) + 1)) break ; i++; } i = 0; while (env[1][i]) tmp[tmp_i++] = env[1][i++]; tmp[tmp_i] = 0; *line += i; return (tmp_i); } 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 (new) { (*line)++; lex->state = new; return (1); } return (0); }