redirection parsing: add utility functions for t_redir_list

This commit is contained in:
Khaïs COLIN 2025-03-07 13:34:47 +01:00
parent b0c34f36ef
commit 541bad80c0
Signed by: logistic-bot
SSH key fingerprint: SHA256:RlpiqKeXpcPFZZ4y9Ou4xi2M8OhRJovIwDlbCaMsuAo
3 changed files with 71 additions and 1 deletions

View file

@ -51,6 +51,7 @@ srcs = \
src/parser/wordsplit/wordsplit.c \ src/parser/wordsplit/wordsplit.c \
src/parser/wordsplit/wordsplit_utils.c \ src/parser/wordsplit/wordsplit_utils.c \
src/postprocess/redirections/redirection.c \ src/postprocess/redirections/redirection.c \
src/postprocess/redirections/redirection_list.c \
src/postprocess/redirections/redirection_parsing.c \ src/postprocess/redirections/redirection_parsing.c \
objs = $(srcs:.c=.o) objs = $(srcs:.c=.o)

View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirection_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}

View file

@ -6,7 +6,7 @@
/* By: khais <marvin@42.fr> +#+ +:+ +#+ */ /* By: khais <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2025/03/07 14:29:32 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; struct s_redirection_list *next;
} t_redir_list; } t_redir_list;
t_redir_list *redir_list_push(t_redir_list *lst, t_redirection *item);
#endif // REDIRECTION_LIST_H #endif // REDIRECTION_LIST_H