parsing: parse redirections

This commit is contained in:
Khaïs COLIN 2025-04-14 12:48:15 +02:00
parent 1866da6ea6
commit 1c9653a5a5

View file

@ -6,11 +6,16 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/31 10:28:28 by jguelen #+# #+# */
/* Updated: 2025/04/11 16:56:59 by khais ### ########.fr */
/* Updated: 2025/04/14 12:48:26 by khais ### ########.fr */
/* */
/* ************************************************************************** */
#include "cmd_parsing.h"
#include <fcntl.h>
#include "worddesc/worddesc.h"
#include "wordlist/wordlist.h"
#include <unistd.h>
#include "../executing/here_doc/here_doc.h"
/*
** NOTE: This file will temporarily include way more functions than allowed.
@ -52,20 +57,83 @@ static bool is_redir(t_worddesc *token)
|| token->token_type == HERE_DOC_REDIR_TOKEN);
}
/*
** TODO Deal with heredocs properly and fill type, source, open_flags, cflags
** and redirectee (Don't know about here_doc_eof).
*/
static t_redir_type redir_type_from_token_type(t_token_type type)
{
if (type == OUT_APPEND_REDIR_TOKEN)
return (FT_OUTPUT_APPEND_REDIR);
if (type == OUT_TRUNC_REDIR_TOKEN)
return (FT_OUTPUT_TRUNC_REDIR);
if (type == IN_REDIR_TOKEN)
return (FT_INPUT_REDIR);
if (type == HERE_DOC_REDIR_TOKEN)
return (FT_HEREDOC);
return (FT_INVALID_REDIR);
}
static int redir_source_from_type(t_redir_type type)
{
if (type == FT_OUTPUT_APPEND_REDIR || type == FT_OUTPUT_TRUNC_REDIR)
return (STDOUT_FILENO);
if (type == FT_INPUT_REDIR)
return (STDIN_FILENO);
return (-1);
}
static int redir_open_flags_from_type(t_redir_type type)
{
if (type == FT_OUTPUT_APPEND_REDIR)
return (O_CREAT | O_APPEND | O_WRONLY);
if (type == FT_OUTPUT_TRUNC_REDIR)
return (O_CREAT | O_TRUNC | O_WRONLY);
return (0);
}
static t_redirect *redir_from_words(t_worddesc *operator, t_worddesc *specifier, t_minishell *app)
{
t_redirect *redir;
if (operator == NULL || specifier == NULL)
return (NULL);
redir = ft_calloc(1, sizeof(t_redirect));
if (redir == NULL)
return (ft_errno(FT_EERRNO), NULL);
redir->type = redir_type_from_token_type(operator->token_type);
redir->source = redir_source_from_type(redir->type);
redir->open_flags = redir_open_flags_from_type(redir->type);
redir->c_flags = 0644;
redir->redirectee.dest = -1;
if (redir->type == FT_HEREDOC)
{
redir->here_doc_eof = ft_strdup(specifier->word);
redir->redirectee.dest = here_doc(specifier, STDIN_FILENO, app);
worddesc_destroy(specifier);
}
else
redir->redirectee.filename = specifier;
worddesc_destroy(operator);
return (redir);
}
t_redirect *minishell_redir_parse(t_minishell *app, t_cmd_builder *builder)
{
ft_printf("%s() %s\n", __FUNCTION__, builder->tokens->word->word);
t_redirect *redir;
t_redirect *redir_list;
t_worddesc *redir_operator;
t_worddesc *redir_specifier;
t_redirect *new_redir;
redir = NULL;
redir_list = NULL;
while (is_redir(builder->tokens->word))
{
if (builder->tokens->next == NULL)
return (redirect_destroy(redir_list), NULL);
redir_operator = wordlist_pop(&builder->tokens);
redir_specifier = wordlist_pop(&builder->tokens);
new_redir = redir_from_words(redir_operator, redir_specifier, app);
t_redirect_add_back(&redir_list, new_redir);
}
return (redir);
return (redir_list);
}
/*