processing heredocs

This commit is contained in:
Quinten Mennen 2025-02-25 20:55:30 +01:00
parent 873d6b48fc
commit e0e77f4012
5 changed files with 74 additions and 40 deletions

10
.vscode/settings.json vendored
View File

@ -1,6 +1,14 @@
{ {
"files.associations": { "files.associations": {
"minishell.h": "c", "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"
} }
} }

View File

@ -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: 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 ft_lstclear_safe.c \ SOURCES = history_load.c history_write.c prompt.c error_msg.c \
ft_lstnew_safe.c ft_strdup_safe.c ft_strjoin_safe.c init_minishell.c \ ft_lstclear_safe.c ft_lstnew_safe.c ft_strdup_safe.c \
malloc_safe.c print_banner.c error_msg.c check_malloc.c \ ft_strjoin_safe.c init_minishell.c malloc_safe.c print_banner.c \
free_command_list.c free_freelist.c free_lexer.c free_minishell.c \ check_malloc.c free_command_list.c free_freelist.c free_lexer.c \
free_minishell_line.c free_safe.c free_token.c free_token_list.c \ free_minishell.c free_minishell_line.c free_safe.c free_token.c \
lexer_read_char.c lexer_new.c lexer_parse_input.c lexer_read_word.c \ free_token_list.c lexer_read_char.c lexer_new.c lexer_parse_input.c \
lexer_token_next.c token_new.c token_parse.c environment_add.c \ lexer_read_word.c lexer_token_next.c token_new.c token_parse.c \
environment_del.c environment_free.c environment_free_list.c \ environment_add.c environment_del.c environment_free.c \
environment_get.c environment_get_arr.c environment_parse.c \ environment_free_list.c environment_get.c environment_get_arr.c \
environment_print.c executor_close_fds.c executor_absolute_path.c \ environment_parse.c environment_print.c executor_close_fds.c \
executor_child.c executor_count_fds.c executor_create_pipes.c \ executor_absolute_path.c executor_child.c executor_count_fds.c \
executor_create_redirects.c executor_execute_pipeline.c \ executor_create_pipes.c executor_create_redirects.c \
executor_fork.c executor_open_fds.c parser_get_arguments.c \ executor_execute_pipeline.c executor_fork.c executor_open_fds.c \
parser_get_commands.c parser_new_command.c signal.c print_commands.c \ parser_get_arguments.c parser_get_commands.c parser_new_command.c \
print_freelist.c expander_expand_dollar.c expander_is_character.c \ print_commands.c print_freelist.c expander_is_character.c \
expander_allocate_memory.c expander_get_variable.c \ expander_expand_dollar.c expander_allocate_memory.c \
expander_parse_string.c expander_parse_variables.c builtin_echo.c \ expander_get_variable.c expander_parse_string.c \
builtin_router.c simple_builtins.c is_builtin.c builtin_cd.c \ expander_parse_variables.c builtin_echo.c builtin_router.c \
builtin_env.c builtin_exit.c builtin_export.c builtin_pwd.c \ simple_builtins.c is_builtin.c builtin_cd.c builtin_env.c \
builtin_unset.c redirect_valid_type.c redirect_get_inputs.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 \ redirect_get_outputs.c redirect_new.c main.c \

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* builtin_pwd.c :+: :+: */ /* builtin_pwd.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: whaffman <whaffman@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */ /* Created: 2025/02/20 11:32:28 by whaffman #+# #+# */
/* Updated: 2025/02/25 15:17:51 by whaffman ######## odam.nl */ /* Updated: 2025/02/25 20:07:22 by qmennen ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -14,7 +14,7 @@
int builtin_pwd(t_minishell *minishell, t_command *cmd) int builtin_pwd(t_minishell *minishell, t_command *cmd)
{ {
char *cwd; char *cwd;
(void)minishell; (void)minishell;
(void)cmd; (void)cmd;

View File

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* :::::::: */ /* ::: :::::::: */
/* parser_get_arguments.c :+: :+: */ /* parser_get_arguments.c :+: :+: :+: */
/* +:+ */ /* +:+ +:+ +:+ */
/* By: qmennen <qmennen@student.codam.nl> +#+ */ /* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
/* +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */ /* Created: 2025/02/11 16:20:09 by qmennen #+# #+# */
/* Updated: 2025/02/25 16:40:26 by whaffman ######## odam.nl */ /* 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) while ((++i) < cmds && current)
{ {
if (parser_should_expand(current)) if (parser_should_expand(current))
{
args[i] = expander_parse_string(((t_token *)current->content)->value, minishell); args[i] = expander_parse_string(((t_token *)current->content)->value, minishell);
}
else if (((t_token *)current->content)->type == T_WORD || else if (((t_token *)current->content)->type == T_WORD ||
((t_token *)current->content)->type == T_SQWORD) ((t_token *)current->content)->type == T_SQWORD)
args[i] = ft_strdup_safe(minishell, ((t_token *)current->content)->value); args[i] = ft_strdup_safe(minishell, ((t_token *)current->content)->value);

View File

@ -12,6 +12,31 @@
#include "redirect.h" #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 *redirect_get_inputs(t_minishell *minishell, t_list *list)
{ {
t_list *current; t_list *current;
@ -30,9 +55,11 @@ t_list *redirect_get_inputs(t_minishell *minishell, t_list *list)
current = current->next; current = current->next;
continue ; continue ;
} }
if (token->type == T_HEREDOC && redirect_is_valid(current, token))
process_heredoc(minishell, token, current->next->content);
if (redirect_is_valid(current, token)) 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, redirect_new(minishell, token->type,
ft_strdup_safe(minishell, ft_strdup_safe(minishell,
((t_token *)current->next->content)->value)))); ((t_token *)current->next->content)->value))));