From 541bad80c040d3144c4bb73562f043fb9b7815a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Fri, 7 Mar 2025 13:34:47 +0100 Subject: [PATCH] redirection parsing: add utility functions for t_redir_list --- Makefile | 1 + .../redirections/redirection_list.c | 67 +++++++++++++++++++ .../redirections/redirection_list.h | 4 +- 3 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 src/postprocess/redirections/redirection_list.c diff --git a/Makefile b/Makefile index b0ba41d..56f0bec 100644 --- a/Makefile +++ b/Makefile @@ -51,6 +51,7 @@ srcs = \ src/parser/wordsplit/wordsplit.c \ src/parser/wordsplit/wordsplit_utils.c \ src/postprocess/redirections/redirection.c \ + src/postprocess/redirections/redirection_list.c \ src/postprocess/redirections/redirection_parsing.c \ objs = $(srcs:.c=.o) diff --git a/src/postprocess/redirections/redirection_list.c b/src/postprocess/redirections/redirection_list.c new file mode 100644 index 0000000..28cf05e --- /dev/null +++ b/src/postprocess/redirections/redirection_list.c @@ -0,0 +1,67 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* redirection_list.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: khais +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2025/03/07 14:29:53 by khais #+# #+# */ +/* Updated: 2025/03/07 14:48:20 by khais ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "redirection_list.h" +#include "redirection.h" +#include "libft.h" + +/* +** create a new redir list item, with the given item +** +** may return null in case of allocation failure +*/ +static t_redir_list *redir_list_create(t_redirection *item) +{ + t_redir_list *out; + + out = ft_calloc(1, sizeof(t_redir_list)); + if (out == NULL) + return (NULL); + out->redirection = item; + return (out); +} + +/* +** free all memory associated with this redir list +*/ +static void redir_list_destroy(t_redir_list *lst) +{ + t_redir_list *prev; + + while (lst != NULL) + { + redirection_destroy(lst->redirection); + prev = lst; + lst = lst->next; + free(prev); + } +} + +/* +** Add the given item to the end of the given list. +** +** If allocation fails, return null. +*/ +t_redir_list *redir_list_push(t_redir_list *lst, t_redirection *item) +{ + t_redir_list *start; + + if (lst == NULL) + return (redir_list_create(item)); + start = lst; + while (lst->next != NULL) + lst = lst->next; + lst->next = redir_list_create(item); + if (lst->next == NULL) + return (redir_list_destroy(start), NULL); + return (start); +} diff --git a/src/postprocess/redirections/redirection_list.h b/src/postprocess/redirections/redirection_list.h index d15709a..e787915 100644 --- a/src/postprocess/redirections/redirection_list.h +++ b/src/postprocess/redirections/redirection_list.h @@ -6,7 +6,7 @@ /* By: khais +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2025/03/07 14:29:32 by khais #+# #+# */ -/* Updated: 2025/03/07 14:43:50 by khais ### ########.fr */ +/* Updated: 2025/03/07 14:48:41 by khais ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,4 +21,6 @@ typedef struct s_redirection_list struct s_redirection_list *next; } t_redir_list; +t_redir_list *redir_list_push(t_redir_list *lst, t_redirection *item); + #endif // REDIRECTION_LIST_H