redirection parsing refactor: put redirection found actions in subroutine

This commit is contained in:
Khaïs COLIN 2025-03-10 16:42:53 +01:00
parent 5df876bba3
commit d9dfac106d
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 12:30:04 by khais #+# #+# */ /* Created: 2025/03/07 12:30:04 by khais #+# #+# */
/* Updated: 2025/03/10 16:40:17 by khais ### ########.fr */ /* Updated: 2025/03/11 14:57:56 by khais ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -18,6 +18,38 @@
#include "../../ft_errno.h" #include "../../ft_errno.h"
#include <unistd.h> #include <unistd.h>
/*
**
** if a redirection token is detected, the current word will be the operator
** token. Destroy it.
**
** the current word is now the marker. save it and destroy it
**
** create a new redirection with the operator and the marker, and save it.
**
** in case of error, return NULL, and set ft_errno
*/
static struct s_simple_cmd *redirection_found(
struct s_simple_cmd *cmd,
size_t i,
t_redir_type type)
{
t_worddesc *marker;
t_redirection *redirection;
wordlist_destroy_idx(&cmd->words, i);
marker = wordlist_pop_idx(&cmd->words, i);
if (marker == NULL)
return (ft_errno(FT_EMALFORMED_REDIRECTION), NULL);
redirection = redirection_create(type, marker);
if (redirection == NULL)
return (NULL);
cmd->redirections = redir_list_push(cmd->redirections, redirection);
if (cmd->redirections == NULL)
return (redirection_destroy(redirection), NULL);
return (cmd);
}
/* /*
** Modify the given simple_cmd in-place, removing all redirection directives ** Modify the given simple_cmd in-place, removing all redirection directives
** from words, parsing them, and storing them in redirection. ** from words, parsing them, and storing them in redirection.
@ -32,17 +64,10 @@
** **
** iterate though the words of the given cmd ** iterate though the words of the given cmd
** **
** if a redirection token is detected, the current word will be the operator ** if a redirection is detected, call redirection_found
** token. Destroy it.
**
** the current word is now the marker. save it and destroy it
**
** create a new redirection with the operator and the maker, and save it.
*/ */
struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd) struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
{ {
t_worddesc *marker;
t_redirection *redirection;
t_redir_type type; t_redir_type type;
size_t i; size_t i;
@ -51,18 +76,8 @@ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd)
{ {
type = redir_type_from_worddesc(wordlist_get(cmd->words, i)); type = redir_type_from_worddesc(wordlist_get(cmd->words, i));
if (type == REDIR_OUTPUT) if (type == REDIR_OUTPUT)
{ if (redirection_found(cmd, i, type) == NULL)
wordlist_destroy_idx(&cmd->words, i);
marker = wordlist_pop_idx(&cmd->words, i);
if (marker == NULL)
return (ft_errno(FT_EMALFORMED_REDIRECTION), NULL);
redirection = redirection_create(type, marker);
if (redirection == NULL)
return (NULL); return (NULL);
cmd->redirections = redir_list_push(cmd->redirections, redirection);
if (cmd->redirections == NULL)
return (redirection_destroy(redirection), NULL);
}
i++; i++;
} }
return (cmd); return (cmd);