From d9dfac106d6781ef3ecae37481b2e59a0f4bebc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Mon, 10 Mar 2025 16:42:53 +0100 Subject: [PATCH] redirection parsing refactor: put redirection found actions in subroutine --- .../redirections/redirection_parsing.c | 55 ++++++++++++------- 1 file changed, 35 insertions(+), 20 deletions(-) diff --git a/src/postprocess/redirections/redirection_parsing.c b/src/postprocess/redirections/redirection_parsing.c index 21dae57..3523c10 100644 --- a/src/postprocess/redirections/redirection_parsing.c +++ b/src/postprocess/redirections/redirection_parsing.c @@ -6,7 +6,7 @@ /* 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 +/* +** +** 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 ** from words, parsing them, and storing them in redirection. @@ -32,17 +64,10 @@ ** ** iterate though the words of the given cmd ** -** 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 maker, and save it. +** if a redirection is detected, call redirection_found */ struct s_simple_cmd *parse_redirections(struct s_simple_cmd *cmd) { - t_worddesc *marker; - t_redirection *redirection; t_redir_type type; 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)); if (type == REDIR_OUTPUT) - { - 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) + if (redirection_found(cmd, i, type) == NULL) return (NULL); - cmd->redirections = redir_list_push(cmd->redirections, redirection); - if (cmd->redirections == NULL) - return (redirection_destroy(redirection), NULL); - } i++; } return (cmd);