From e0e77f4012776b25ddb20a4751e96bd99bbe28b2 Mon Sep 17 00:00:00 2001 From: Quinten Mennen Date: Tue, 25 Feb 2025 20:55:30 +0100 Subject: [PATCH] processing heredocs --- .vscode/settings.json | 10 ++++++- sources.mk | 43 +++++++++++++++--------------- src/builtin/builtin_pwd.c | 16 +++++------ src/parser/parser_get_arguments.c | 16 +++++------ src/redirect/redirect_get_inputs.c | 29 +++++++++++++++++++- 5 files changed, 74 insertions(+), 40 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 6be99c7..98bea1c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,6 +1,14 @@ { "files.associations": { "minishell.h": "c", - "signal.h": "c" + "signal.h": "c", + "optional": "c", + "ostream": "c", + "system_error": "c", + "array": "c", + "functional": "c", + "tuple": "c", + "type_traits": "c", + "utility": "c" } } diff --git a/sources.mk b/sources.mk index e2269f4..3fa9ed9 100644 --- a/sources.mk +++ b/sources.mk @@ -1,22 +1,23 @@ -VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/signal:src/debug:src/expander:src/builtin:src/redirect: -SOURCES = history_load.c history_write.c prompt.c ft_lstclear_safe.c \ -ft_lstnew_safe.c ft_strdup_safe.c ft_strjoin_safe.c init_minishell.c \ -malloc_safe.c print_banner.c error_msg.c check_malloc.c \ -free_command_list.c free_freelist.c free_lexer.c free_minishell.c \ -free_minishell_line.c free_safe.c free_token.c free_token_list.c \ -lexer_read_char.c lexer_new.c lexer_parse_input.c lexer_read_word.c \ -lexer_token_next.c token_new.c token_parse.c environment_add.c \ -environment_del.c environment_free.c environment_free_list.c \ -environment_get.c environment_get_arr.c environment_parse.c \ -environment_print.c executor_close_fds.c executor_absolute_path.c \ -executor_child.c executor_count_fds.c executor_create_pipes.c \ -executor_create_redirects.c executor_execute_pipeline.c \ -executor_fork.c executor_open_fds.c parser_get_arguments.c \ -parser_get_commands.c parser_new_command.c signal.c print_commands.c \ -print_freelist.c expander_expand_dollar.c expander_is_character.c \ -expander_allocate_memory.c expander_get_variable.c \ -expander_parse_string.c expander_parse_variables.c builtin_echo.c \ -builtin_router.c simple_builtins.c is_builtin.c builtin_cd.c \ -builtin_env.c builtin_exit.c builtin_export.c builtin_pwd.c \ -builtin_unset.c redirect_valid_type.c redirect_get_inputs.c \ +VPATH = src:src/prompt:src/utils:src/lexer:src/token:src/environment:src/executor:src/parser:src/debug:src/expander:src/builtin:src/signal:src/redirect: +SOURCES = history_load.c history_write.c prompt.c error_msg.c \ +ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \ +ft_strjoin_safe.c init_minishell.c malloc_safe.c print_banner.c \ +check_malloc.c free_command_list.c free_freelist.c free_lexer.c \ +free_minishell.c free_minishell_line.c free_safe.c free_token.c \ +free_token_list.c lexer_read_char.c lexer_new.c lexer_parse_input.c \ +lexer_read_word.c lexer_token_next.c token_new.c token_parse.c \ +environment_add.c environment_del.c environment_free.c \ +environment_free_list.c environment_get.c environment_get_arr.c \ +environment_parse.c environment_print.c executor_close_fds.c \ +executor_absolute_path.c executor_child.c executor_count_fds.c \ +executor_create_pipes.c executor_create_redirects.c \ +executor_execute_pipeline.c executor_fork.c executor_open_fds.c \ +parser_get_arguments.c parser_get_commands.c parser_new_command.c \ +print_commands.c print_freelist.c expander_is_character.c \ +expander_expand_dollar.c expander_allocate_memory.c \ +expander_get_variable.c expander_parse_string.c \ +expander_parse_variables.c builtin_echo.c builtin_router.c \ +simple_builtins.c is_builtin.c builtin_cd.c builtin_env.c \ +builtin_exit.c builtin_export.c builtin_pwd.c builtin_unset.c \ +signal.c redirect_valid_type.c redirect_get_inputs.c \ redirect_get_outputs.c redirect_new.c main.c \ diff --git a/src/builtin/builtin_pwd.c b/src/builtin/builtin_pwd.c index f380fa3..bbf57d1 100644 --- a/src/builtin/builtin_pwd.c +++ b/src/builtin/builtin_pwd.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* builtin_pwd.c :+: :+: */ -/* +:+ */ -/* By: whaffman +#+ */ -/* +#+ */ -/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */ -/* Updated: 2025/02/25 15:17:51 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* builtin_pwd.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */ +/* Updated: 2025/02/25 20:07:22 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -14,7 +14,7 @@ int builtin_pwd(t_minishell *minishell, t_command *cmd) { - char *cwd; + char *cwd; (void)minishell; (void)cmd; diff --git a/src/parser/parser_get_arguments.c b/src/parser/parser_get_arguments.c index 4bd8db2..afbb729 100644 --- a/src/parser/parser_get_arguments.c +++ b/src/parser/parser_get_arguments.c @@ -1,12 +1,12 @@ /* ************************************************************************** */ /* */ -/* :::::::: */ -/* parser_get_arguments.c :+: :+: */ -/* +:+ */ -/* By: qmennen +#+ */ -/* +#+ */ -/* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */ -/* Updated: 2025/02/25 16:40:26 by whaffman ######## odam.nl */ +/* ::: :::::::: */ +/* parser_get_arguments.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: qmennen +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */ +/* Updated: 2025/02/25 20:14:18 by qmennen ### ########.fr */ /* */ /* ************************************************************************** */ @@ -56,9 +56,7 @@ char **parser_get_arguments(t_list *list, t_minishell *minishell) while ((++i) < cmds && current) { 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_safe(minishell, ((t_token *)current->content)->value); diff --git a/src/redirect/redirect_get_inputs.c b/src/redirect/redirect_get_inputs.c index 7addf44..705ac5e 100644 --- a/src/redirect/redirect_get_inputs.c +++ b/src/redirect/redirect_get_inputs.c @@ -12,6 +12,31 @@ #include "redirect.h" +static void process_heredoc(t_minishell *ms, t_token *heredoc, t_token *delim) +{ + char *line; + int fd; + + fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644); + if (fd < 0) + return ; + + + while (TRUE) + { + line = readline("heredoc>"); + if ((*line && ft_strcmp(line, delim->value) == 0) || !line) + break ; + if (!*line) + ft_putendl_fd("", fd); + else + ft_putendl_fd(line, fd); + } + close(fd); + heredoc->type = T_REDIRECT_IN; + delim->value = ft_strdup_safe(ms, ".ms_heredoc"); +} + t_list *redirect_get_inputs(t_minishell *minishell, t_list *list) { t_list *current; @@ -30,9 +55,11 @@ t_list *redirect_get_inputs(t_minishell *minishell, t_list *list) current = current->next; continue ; } + if (token->type == T_HEREDOC && redirect_is_valid(current, token)) + process_heredoc(minishell, token, current->next->content); if (redirect_is_valid(current, token)) { - ft_lstadd_front(&redirects, ft_lstnew_safe(minishell, + ft_lstadd_back(&redirects, ft_lstnew_safe(minishell, redirect_new(minishell, token->type, ft_strdup_safe(minishell, ((t_token *)current->next->content)->value))));