process heredoc new structure
This commit is contained in:
parent
910a4dcf7f
commit
c8806e22c6
@ -20,5 +20,6 @@ t_list *redirect_get_outputs(t_minishell *msh, t_list *list);
|
|||||||
int redirect_is_valid(t_list *lst, t_token *token, int mode);
|
int redirect_is_valid(t_list *lst, t_token *token, int mode);
|
||||||
int redirect_token_type(t_token *token);
|
int redirect_token_type(t_token *token);
|
||||||
int redirect_is_delimiter(t_token *token);
|
int redirect_is_delimiter(t_token *token);
|
||||||
|
int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@ -20,4 +20,5 @@ expander_get_variable.c builtin_cd.c builtin_echo.c builtin_env.c \
|
|||||||
builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \
|
builtin_exit.c builtin_export.c builtin_pwd.c builtin_router.c \
|
||||||
builtin_unset.c is_builtin.c simple_builtins.c signal.c \
|
builtin_unset.c is_builtin.c simple_builtins.c signal.c \
|
||||||
signal_init.c.c redirect_get_outputs.c redirect_new.c \
|
signal_init.c.c redirect_get_outputs.c redirect_new.c \
|
||||||
redirect_valid_type.c redirect_get_inputs.c main.c \
|
redirect_get_inputs.c redirect_valid_type.c \
|
||||||
|
redirect_process_heredoc.c main.c \
|
||||||
|
|||||||
@ -1,52 +1,17 @@
|
|||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
/* */
|
/* */
|
||||||
/* :::::::: */
|
/* ::: :::::::: */
|
||||||
/* redirect_get_inputs.c :+: :+: */
|
/* redirect_get_inputs.c :+: :+: :+: */
|
||||||
/* +:+ */
|
/* +:+ +:+ +:+ */
|
||||||
/* By: Quinten <qmennen@student.codam.nl> +#+ */
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
/* +#+ */
|
/* +#+#+#+#+#+ +#+ */
|
||||||
/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */
|
/* Created: 2025/02/23 12:29:05 by Quinten #+# #+# */
|
||||||
/* Updated: 2025/02/23 12:29:05 by Quinten ######## odam.nl */
|
/* Updated: 2025/02/26 16:46:44 by qmennen ### ########.fr */
|
||||||
/* */
|
/* */
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
|
|
||||||
#include "redirect.h"
|
#include "redirect.h"
|
||||||
|
|
||||||
static int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim)
|
|
||||||
{
|
|
||||||
char *line;
|
|
||||||
char *expand;
|
|
||||||
int fd;
|
|
||||||
|
|
||||||
fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
|
||||||
if (fd < 0)
|
|
||||||
{
|
|
||||||
error_msg("heredoc", "unable to write to temp file");
|
|
||||||
return (fd);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (TRUE && fd > 0)
|
|
||||||
{
|
|
||||||
line = readline(">");
|
|
||||||
if ((*line && ft_strcmp(line, delim->value) == 0) || !line) // TODO: What if not line?
|
|
||||||
break;
|
|
||||||
if (!*line)
|
|
||||||
ft_putendl_fd("", fd);
|
|
||||||
else
|
|
||||||
{
|
|
||||||
expand = expander_parse_string(line, msh);
|
|
||||||
ft_putendl_fd(expand, fd);
|
|
||||||
free_safe(msh, (void **)&expand);
|
|
||||||
}
|
|
||||||
free(line);
|
|
||||||
}
|
|
||||||
close(fd);
|
|
||||||
free(line);
|
|
||||||
heredoc->type = T_REDIRECT_IN;
|
|
||||||
delim->value = ft_strdup_safe(msh, ".ms_heredoc");
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
t_list *redirect_get_inputs(t_minishell *msh, t_list *list)
|
t_list *redirect_get_inputs(t_minishell *msh, t_list *list)
|
||||||
{
|
{
|
||||||
t_list *current;
|
t_list *current;
|
||||||
|
|||||||
56
src/redirect/redirect_process_heredoc.c
Normal file
56
src/redirect/redirect_process_heredoc.c
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* redirect_process_heredoc.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: qmennen <qmennen@student.codam.nl> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* Created: 2025/02/26 16:46:32 by qmennen #+# #+# */
|
||||||
|
/* Updated: 2025/02/26 16:54:09 by qmennen ### ########.fr */
|
||||||
|
/* */
|
||||||
|
/* ************************************************************************** */
|
||||||
|
|
||||||
|
#include "minishell.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* process_heredoc - Handles the processing of a heredoc in a minishell.
|
||||||
|
* @msh: Pointer to the minishell structure.
|
||||||
|
* @heredoc: Pointer to the token representing the heredoc.
|
||||||
|
* @delim: Pointer to the token representing the delimiter.
|
||||||
|
*
|
||||||
|
* This function reads lines from the standard input until a line matching
|
||||||
|
* the delimiter is encountered or an end-of-file (EOF) is reached. Each
|
||||||
|
* line is expanded and written to a temporary file named ".ms_heredoc".
|
||||||
|
* The function sets the type of the heredoc token to T_REDIRECT_IN and
|
||||||
|
* updates the delimiter token's value to the temporary file's name.
|
||||||
|
*
|
||||||
|
* Return: 1 on success, or a negative value on failure.
|
||||||
|
*/
|
||||||
|
int process_heredoc(t_minishell *msh, t_token *heredoc, t_token *delim)
|
||||||
|
{
|
||||||
|
char *line;
|
||||||
|
char *expand;
|
||||||
|
const int fd = open(".ms_heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
|
||||||
|
|
||||||
|
if (fd < 0)
|
||||||
|
{
|
||||||
|
error_msg("heredoc", "unable to write to temp file");
|
||||||
|
return (fd);
|
||||||
|
}
|
||||||
|
while (TRUE)
|
||||||
|
{
|
||||||
|
line = readline(">");
|
||||||
|
if ((*line && ft_strcmp(line, delim->value) == 0) || !line)
|
||||||
|
break ;
|
||||||
|
if (!*line)
|
||||||
|
ft_strlcat(line, "\n", 1);
|
||||||
|
expand = expander_parse_string(line, msh);
|
||||||
|
ft_putendl_fd(expand, fd);
|
||||||
|
free_safe(msh, (void **)&expand);
|
||||||
|
free(line);
|
||||||
|
}
|
||||||
|
close(fd);
|
||||||
|
heredoc->type = T_REDIRECT_IN;
|
||||||
|
delim->value = ft_strdup_safe(msh, ".ms_heredoc");
|
||||||
|
return (free(line), 1);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user