diff --git a/Makefile b/Makefile index 2060049..18963b6 100644 --- a/Makefile +++ b/Makefile @@ -22,7 +22,7 @@ LIBFT = $(LIBFT_PATH)/libft.a OBJ_PATH = obj -VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor:src/parser:src/signal +VPATH = src:src/environment:src/prompt:src/lexer:src/token:src/utils:src/executor:src/parser:src/expander:src/debug:src/signal SOURCES = $(shell basename -a $(shell find $(SRC_PATH) -type f -name "*.c")) OBJECTS = $(addprefix $(OBJ_PATH)/, $(SOURCES:.c=.o)) diff --git a/inc/debug.h b/inc/debug.h new file mode 100644 index 0000000..934b75a --- /dev/null +++ b/inc/debug.h @@ -0,0 +1,20 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* debug.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/18 20:06:53 by qmennen #+# #+# */ +/* Updated: 2025/02/18 20:10:49 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef DEBUG_H +# define DEBUG_H + +# include "minishell.h" + +void print_commands(void *param); + +#endif diff --git a/inc/expander.h b/inc/expander.h new file mode 100644 index 0000000..3e61de9 --- /dev/null +++ b/inc/expander.h @@ -0,0 +1,25 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/18 19:00:51 by qmennen #+# #+# */ +/* Updated: 2025/02/19 15:19:50 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef EXPANDER_H +# define EXPANDER_H + +#include "minishell.h" + +t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell); +t_list *expander_parse_variables(const char *s, t_minishell *minishell); +char *expander_allocate_memory(const char *s, t_list *variables); +char *expander_parse_string(char *s, t_minishell *minishell); +int expander_character_valid(const char c); +int expander_expand_dollar(char *src, char *dest, int *j, t_list *variables); + +#endif diff --git a/inc/minishell.h b/inc/minishell.h index ce63aa9..a10b955 100644 --- a/inc/minishell.h +++ b/inc/minishell.h @@ -22,6 +22,8 @@ # include "tokenizer.h" # include "executor.h" # include "parser.h" +# include "expander.h" +# include "debug.h" # include "utils.h" # define TRUE 1 diff --git a/inc/parser.h b/inc/parser.h index 8caaa0a..f757caa 100644 --- a/inc/parser.h +++ b/inc/parser.h @@ -16,7 +16,7 @@ # include "minishell.h" t_command *parser_command_new(char *cmd); -char **parser_get_arguments(t_list *list); -t_list *parser_get_commands(t_list *list); +char **parser_get_arguments(t_list *list, t_minishell *minishell); +t_list *parser_get_commands(t_minishell *minishell); #endif diff --git a/inc/tokenizer.h b/inc/tokenizer.h index 1e8d893..306bc03 100644 --- a/inc/tokenizer.h +++ b/inc/tokenizer.h @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* tokenizer.h :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */ -/* Updated: 2025/02/05 12:36:01 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* tokenizer.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/05 12:36:00 by whaffman #+# #+# */ +/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -18,16 +18,16 @@ */ t_lexer *ft_lexer_new(const char *input); void ft_lexer_free(t_lexer *lexer); -void ft_lexer_readchar(t_lexer *lexer); -char *ft_lexer_readword(t_lexer *lexer); +void lexer_readchar(t_lexer *lexer); +char *lexer_readword(t_lexer *lexer); t_list *ft_parse_input(t_lexer *lexer); /** * Token */ t_token *ft_token_next(t_lexer *lexer); -t_token *ft_token_new(t_token_type type, char *c, int pos); +t_token *token_new(t_token_type type, char *c, int pos); void ft_token_free(t_token *token); void ft_clear_tokenlist(void *content); -t_token *ft_parse_token(t_lexer *lexer); +t_token *token_parse(t_lexer *lexer); #endif // TOKENIZER_H diff --git a/inc/typedef.h b/inc/typedef.h index 817f5e0..c3b4b8a 100644 --- a/inc/typedef.h +++ b/inc/typedef.h @@ -16,9 +16,13 @@ typedef enum e_token_type { T_WORD, + T_DQWORD, + T_SQWORD, T_PIPE, T_REDIRECT_IN, T_REDIRECT_OUT, + T_AND, + T_OR, T_APPEND_OUT, T_HEREDOC, T_EOF, diff --git a/src/debug/print_commands.c b/src/debug/print_commands.c new file mode 100644 index 0000000..88ed829 --- /dev/null +++ b/src/debug/print_commands.c @@ -0,0 +1,29 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* print_commands.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/18 20:06:37 by qmennen #+# #+# */ +/* Updated: 2025/02/18 20:11:45 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +void print_commands(void *param) +{ + t_command *command; + int i; + + command = (t_command *)param; + if (!command) + return ; + printf("command: %s\n", command->command); + printf("args: "); + i = 0; + while (command->args[i++]) + printf("%s ", command->args[i]); + printf("\n"); +} diff --git a/src/expander/expander_allocate_memory.c b/src/expander/expander_allocate_memory.c new file mode 100644 index 0000000..286ed37 --- /dev/null +++ b/src/expander/expander_allocate_memory.c @@ -0,0 +1,40 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander_allocate_memory.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 13:57:19 by qmennen #+# #+# */ +/* Updated: 2025/02/19 13:57:36 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +char *expander_allocate_memory(const char *s, t_list *variables) +{ + int size; + t_list *current; + t_environment *env; + char *string; + + size = 0; + current = variables; + while (current) + { + if (current->content == NULL) + { + current = current->next; + continue; + } + env = (t_environment *)current->content; + size += (ft_strlen(env->value) - ft_strlen(env->name)); + current = current->next; + } + size += ft_strlen(s); + string = malloc(size); + if (!string) + perror("expander malloc"); + return (string); +} diff --git a/src/expander/expander_expand_dollar.c b/src/expander/expander_expand_dollar.c new file mode 100644 index 0000000..414e19c --- /dev/null +++ b/src/expander/expander_expand_dollar.c @@ -0,0 +1,45 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander_expand_dollar.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 15:05:58 by qmennen #+# #+# */ +/* Updated: 2025/02/19 15:22:40 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static int get_var_len(const char *source, int idx) +{ + int i; + + i = 0; + while (expander_character_valid(source[idx + i])) + i++; + return (i); +} + +int expander_expand_dollar(char *src, char *dest, int *j, t_list *variables) +{ + int i; + int v_len; + t_environment *env; + + i = 0; + v_len = 0; + if (variables && variables->content) + { + env = (t_environment *)variables->content; + v_len = ft_strlen(env->name); + while (env->value[i]) + { + dest[(*j)++] = env->value[i++]; + } + } + else + v_len = get_var_len(src, (*j) + 1); + return (v_len); +} diff --git a/src/expander/expander_get_variable.c b/src/expander/expander_get_variable.c new file mode 100644 index 0000000..924798a --- /dev/null +++ b/src/expander/expander_get_variable.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander_get_variable.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 13:59:03 by qmennen #+# #+# */ +/* Updated: 2025/02/19 13:59:14 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_environment *expander_get_var(const char *s, int idx, t_minishell *minishell) +{ + int i; + t_environment *env; + char *name; + + i = 0; + while (expander_character_valid(s[idx + i])) + i++; + name = ft_substr(s, idx, i); + if (!name || !*name) + return (NULL); + env = environment_get(minishell->environment, name); + free(name); + return (env); +} diff --git a/src/expander/expander_is_character.c b/src/expander/expander_is_character.c new file mode 100644 index 0000000..161a0c8 --- /dev/null +++ b/src/expander/expander_is_character.c @@ -0,0 +1,18 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander_is_character.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 13:15:11 by qmennen #+# #+# */ +/* Updated: 2025/02/19 13:15:55 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +int expander_character_valid(const char c) +{ + return (ft_isalnum(c) || c == '_'); +} diff --git a/src/expander/expander_parse_string.c b/src/expander/expander_parse_string.c new file mode 100644 index 0000000..f5e1406 --- /dev/null +++ b/src/expander/expander_parse_string.c @@ -0,0 +1,58 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander_parse_string.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/18 19:00:35 by qmennen #+# #+# */ +/* Updated: 2025/02/19 15:20:04 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +static void free_variables(t_list *variables) +{ + t_list *current; + t_list *last; + + current = variables; + while (current) + { + last = current; + current = current->next; + free(last); + } +} + +char *expander_parse_string(char *s, t_minishell *minishell) +{ + t_list *variables; + t_list *current; + char *string; + int i; + int j; + + variables = expander_parse_variables(s, minishell); + string = expander_allocate_memory(s, variables); + i = 0; + j = 0; + current = variables; + while (s[i]) + { + if (s[i] == '$' && s[i + 1]) + { + i++; + i += expander_expand_dollar(s, string, &j, current); + if (current) + current = current->next; + } + else + string[j++] = s[i++]; + } + + string[j] = 0; + free_variables(variables); + return (string); +} diff --git a/src/expander/expander_parse_variables.c b/src/expander/expander_parse_variables.c new file mode 100644 index 0000000..b3e9023 --- /dev/null +++ b/src/expander/expander_parse_variables.c @@ -0,0 +1,37 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* expander_parse_variables.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/19 13:58:13 by qmennen #+# #+# */ +/* Updated: 2025/02/19 15:07:23 by qmennen ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "minishell.h" + +t_list *expander_parse_variables(const char *s, t_minishell *minishell) +{ + + int i; + t_list *var_list; + t_environment *env; + + i = 0; + var_list = NULL; + while (s[i]) + { + if (s[i] == '$') + { + env = expander_get_var(s, i + 1, minishell); + if (env) + ft_lstadd_back(&var_list, ft_lstnew(env)); + else + ft_lstadd_back(&var_list, ft_lstnew(NULL)); + } + i++; + } + return (var_list); +} diff --git a/src/lexer/lexer_parse_input.c b/src/lexer/lexer_parse_input.c index 861e4eb..466d668 100644 --- a/src/lexer/lexer_parse_input.c +++ b/src/lexer/lexer_parse_input.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 19:09:20 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:09:26 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:18:10 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ diff --git a/src/lexer/lexer_read_char.c b/src/lexer/lexer_read_char.c index 6f62b34..4b67351 100644 --- a/src/lexer/lexer_read_char.c +++ b/src/lexer/lexer_read_char.c @@ -1,18 +1,18 @@ /* ************************************************************************** */ /* */ /* ::: :::::::: */ -/* lexer_readchar.c :+: :+: :+: */ +/* lexer_read_char.c :+: :+: :+: */ /* +:+ +:+ +:+ */ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 19:04:53 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:05:06 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -void ft_lexer_readchar(t_lexer *lexer) +void lexer_readchar(t_lexer *lexer) { if ((size_t)lexer->n_pos > ft_strlen(lexer->input)) { diff --git a/src/lexer/lexer_read_word.c b/src/lexer/lexer_read_word.c index 2738be8..1bbd225 100644 --- a/src/lexer/lexer_read_word.c +++ b/src/lexer/lexer_read_word.c @@ -6,7 +6,7 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 19:03:47 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:28:17 by qmennen ### ########.fr */ +/* Updated: 2025/02/19 14:12:19 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,15 +21,15 @@ static char *parse_quotes(t_lexer *lexer) qc = lexer->current_char; word = NULL; - ft_lexer_readchar(lexer); + lexer_readchar(lexer); start = lexer->pos; while (lexer->current_char != '\0' && lexer->current_char != qc) - ft_lexer_readchar(lexer); + lexer_readchar(lexer); len = lexer->pos - start; word = malloc(sizeof(char) * len + 1); ft_strlcpy(word, lexer->input + start, len + 1); if (lexer->current_char == qc) - ft_lexer_readchar(lexer); + lexer_readchar(lexer); else { free(word); @@ -38,7 +38,7 @@ static char *parse_quotes(t_lexer *lexer) return (word); } -char *ft_lexer_readword(t_lexer *lexer) +char *lexer_readword(t_lexer *lexer) { int start; int len; @@ -54,7 +54,7 @@ char *ft_lexer_readword(t_lexer *lexer) && lexer->current_char != '\0' && !ft_isspace(lexer->current_char) && lexer->current_char != '"' && lexer->current_char != '\'') { - ft_lexer_readchar(lexer); + lexer_readchar(lexer); } len = lexer->pos - start; word = malloc(sizeof(char) * len + 1); diff --git a/src/lexer/lexer_token_next.c b/src/lexer/lexer_token_next.c index aa57eae..0aab9db 100644 --- a/src/lexer/lexer_token_next.c +++ b/src/lexer/lexer_token_next.c @@ -6,11 +6,22 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/04 16:07:58 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:25:35 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" +#include "typedef.h" + +static t_token_type get_word_type(char c) +{ + if (c == '\'') + return (T_SQWORD); + else if (c == '"') + return (T_DQWORD); + else + return (T_WORD); +} /** * @brief Retrieves the next token from the lexer. @@ -33,28 +44,30 @@ */ t_token *ft_token_next(t_lexer *lexer) { - t_token *token; - char *word; - int current_pos; + t_token *token; + t_token_type word_type; + char *word; + int current_pos; token = NULL; while (ft_isspace(lexer->current_char)) - ft_lexer_readchar(lexer); + lexer_readchar(lexer); current_pos = lexer->pos; if (lexer->current_char == '\0') - token = ft_token_new(T_EOF, NULL, current_pos); + token = token_new(T_EOF, NULL, current_pos); else if (lexer->current_char == '<' || lexer->current_char == '>' || lexer->current_char == '|') - token = ft_parse_token(lexer); + token = token_parse(lexer); else if (ft_isprint(lexer->current_char)) { - word = ft_lexer_readword(lexer); + word_type = get_word_type(lexer->current_char); + word = lexer_readword(lexer); if (!word) - return (ft_token_new(T_ERROR, &lexer->current_char, current_pos)); - token = ft_token_new(T_WORD, word, current_pos); + return (token_new(T_ERROR, &lexer->current_char, current_pos)); + token = token_new(word_type, word, current_pos); free(word); } else - token = ft_token_new(T_ERROR, NULL, current_pos); + token = token_new(T_ERROR, NULL, current_pos); return (token); } diff --git a/src/main.c b/src/main.c index 4b03161..1cabd60 100644 --- a/src/main.c +++ b/src/main.c @@ -41,7 +41,7 @@ int main(int argc, char **argv, char **envp) minishell->lexer = ft_lexer_new(minishell->line); minishell->tokens = ft_parse_input(minishell->lexer); ft_lstiter(minishell->tokens, token_print); - minishell->commands = parser_get_commands(minishell->tokens); + minishell->commands = parser_get_commands(minishell); simple_builtins(minishell); free_minishell_line(minishell); ft_lstclear(&minishell->commands, free_command_list); diff --git a/src/parser/parser_get_arguments.c b/src/parser/parser_get_arguments.c index 2b428ce..0cdb42b 100644 --- a/src/parser/parser_get_arguments.c +++ b/src/parser/parser_get_arguments.c @@ -16,12 +16,14 @@ static int count_cmds(t_list *list) { int cmds; t_list *current; + t_token *token; cmds = 0; current = list; while (current) { - if (((t_token *)current->content)->type != T_WORD) + token = ((t_token *)current->content); + if (token->type >= 3) break ; cmds++; current = current->next; @@ -29,7 +31,18 @@ static int count_cmds(t_list *list) return (cmds); } -char **parser_get_arguments(t_list *list) +static int parser_should_expand(t_list *value) +{ + t_token *token; + + token = (t_token *)value->content; + if (!token) + return (0); + return (token->type == T_DQWORD || (token->type == T_WORD && + token->value[0] == '$' && token->value[1])); +} + +char **parser_get_arguments(t_list *list, t_minishell *minishell) { t_list *current; char **args; @@ -47,7 +60,12 @@ char **parser_get_arguments(t_list *list) i = -1; while ((++i) < cmds && current) { - if (((t_token *)current->content)->type == T_WORD) + if (parser_should_expand(current)) + { + args[i] = expander_parse_string(((t_token *)current->content)->value, minishell); + } + else if (((t_token *)current->content)->type == T_WORD || + ((t_token *)current->content)->type == T_SQWORD) args[i] = ft_strdup(((t_token *)current->content)->value); current = current->next; } diff --git a/src/parser/parser_get_commands.c b/src/parser/parser_get_commands.c index 786e143..74c21ff 100644 --- a/src/parser/parser_get_commands.c +++ b/src/parser/parser_get_commands.c @@ -1,35 +1,18 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* parser_get_commands.c :+: :+: */ -/* +:+ */ -/* By: qmennen +#+ */ -/* +#+ */ -/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ -/* Updated: 2025/02/12 20:49:00 by willem ######## odam.nl */ +/* ::: :::::::: */ +/* parser_get_commands.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/11 14:06:02 by qmennen #+# #+# */ +/* Updated: 2025/02/18 20:36:01 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -// static void print_cmds(void *content) -// { -// t_command *cmd; -// int i; - -// cmd = (t_command *)content; -// printf("cmd: %s\n", cmd->command); -// printf("args: "); -// i = 0; -// while (cmd->args[i]) -// { -// printf("%s ", cmd->args[i]); -// i++; -// } -// printf("\n"); -// } - -t_list *parser_get_commands(t_list *list) +t_list *parser_get_commands(t_minishell *minishell) { t_list *command_list; t_list *current; @@ -37,20 +20,20 @@ t_list *parser_get_commands(t_list *list) t_token *token; command_list = NULL; - if (!list) + if (!minishell->tokens) return (NULL); - current = list; + current = minishell->tokens; while (current) { token = (t_token *) current->content; command = parser_command_new(ft_strdup(token->value)); - command->args = parser_get_arguments(current); + command->args = parser_get_arguments(current, minishell); ft_lstadd_back(&command_list, ft_lstnew(command)); - while (current && ((t_token *)current->content)->type == T_WORD) + while (current && ((t_token *)current->content)->type < 3) current = current->next; - if (current && ((t_token *)current->content)->type != T_WORD) + if (current && ((t_token *)current->content)->type >= 3) current = current->next; } - //ft_lstiter(command_list, print_cmds); +// ft_lstiter(command_list, print_commands); return (command_list); } diff --git a/src/token/token_new.c b/src/token/token_new.c index 6529b1b..9626f44 100644 --- a/src/token/token_new.c +++ b/src/token/token_new.c @@ -6,13 +6,13 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 19:10:49 by qmennen #+# #+# */ -/* Updated: 2025/02/05 19:11:32 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:01:52 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_token *ft_token_new(t_token_type type, char *c, int pos) +t_token *token_new(t_token_type type, char *c, int pos) { t_token *token; diff --git a/src/token/token_parse.c b/src/token/token_parse.c index 5ce93aa..7a9d561 100644 --- a/src/token/token_parse.c +++ b/src/token/token_parse.c @@ -6,39 +6,69 @@ /* By: qmennen +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/02/05 19:10:17 by qmennen #+# #+# */ -/* Updated: 2025/02/11 17:38:51 by qmennen ### ########.fr */ +/* Updated: 2025/02/18 17:02:17 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ #include "minishell.h" -t_token *ft_parse_token(t_lexer *lexer) +static t_token_type token_from_char(char c, int is_double) { - t_token *token; + if (c == '<') + { + if (is_double) + return (T_HEREDOC); + return (T_REDIRECT_IN); + } + else if (c == '>') + { + if (is_double) + return (T_APPEND_OUT); + return (T_REDIRECT_OUT); + } + else if (c == '&' && is_double) + return (T_AND); + else if (c == '|') + { + if (is_double) + return (T_OR); + return (T_PIPE); + } + return (T_ERROR); +} - token = NULL; - if (lexer->current_char == '|') - { - token = ft_token_new(T_PIPE, "|", lexer->pos); - } - else if (lexer->current_char == '<' && lexer->input[lexer->pos + 1] == '<') - { - token = ft_token_new(T_HEREDOC, "<<", lexer->pos); - ft_lexer_readchar(lexer); - } - else if (lexer->current_char == '<') - { - token = ft_token_new(T_REDIRECT_IN, "<", lexer->pos); - } - else if (lexer->current_char == '>' && lexer->input[lexer->pos + 1] == '>') - { - token = ft_token_new(T_APPEND_OUT, ">>", lexer->pos); - ft_lexer_readchar(lexer); - } - else if (lexer->current_char == '>') - { - token = ft_token_new(T_REDIRECT_OUT, ">", lexer->pos); - } - ft_lexer_readchar(lexer); +static char *char_from_type(t_token_type type) +{ + if (type == T_HEREDOC) + return ("<<"); + else if (type == T_REDIRECT_IN) + return ("<"); + else if (type == T_APPEND_OUT) + return (">>"); + else if (type == T_REDIRECT_OUT) + return (">"); + else if (type == T_AND) + return ("&&"); + else if (type == T_OR) + return ("||"); + else if (type == T_PIPE) + return ("|"); + return (NULL); +} + +t_token *token_parse(t_lexer *lexer) +{ + int is_double; + char c; + t_token *token; + t_token_type type; + + c = lexer->current_char; + is_double = lexer->input[lexer->pos + 1] == c; + type = token_from_char(c, is_double); + token = token_new(type, char_from_type(type), lexer->pos); + if (is_double) + lexer_readchar(lexer); + lexer_readchar(lexer); return (token); }