summaryrefslogtreecommitdiffstats
path: root/src/s_lpipes.c
blob: dc5feea1e890f01186fd1f1605d478f23945b9c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_lpipes.c                                         :+:      :+:    :+:   */
/*                                                    +:+ +:+         +:+     */
/*   By: rbousset <marvin@42.fr>                    +#+  +:+       +#+        */
/*                                                +#+#+#+#+#+   +#+           */
/*   Created: 2020/02/14 17:19:27 by rbousset          #+#    #+#             */
/*   Updated: 2020/02/14 17:19:29 by rbousset         ###   ########lyon.fr   */
/*                                                                            */
/* ************************************************************************** */

#include <libft.h>
#include <stdlib.h>
#include <sys/types.h>
#ifdef __linux__
# include <linux/limits.h>
#else
# include <limits.h>
#endif

#include "s_com.h"
#include "s_line.h"
#include "s_lpipes_split.h"
#include "s_lpipes.h"
#include "s_struct.h"

struct s_lpipes	*s_lpipes_last(struct s_lpipes *lpipes)
{
	while (lpipes->next != NULL)
		lpipes = lpipes->next;
	return (lpipes);
}

void			s_lpipes_add_back(struct s_lpipes **alpipes,
								struct s_lpipes *new)
{
	struct s_lpipes	*tmp;

	if (*alpipes == NULL)
		*alpipes = new;
	else
	{
		tmp = s_lpipes_last(*alpipes);
		tmp->next = new;
	}
}

void			s_lpipes_clear(struct s_lpipes **lpipes)
{
	struct s_lpipes	*renext;
	struct s_lpipes	*tmp;

	if (lpipes == NULL)
		return ;
	tmp = *lpipes;
	while (tmp != NULL)
	{
		renext = tmp->next;
		s_com_destroy(&tmp->com);
		ft_memdel((void*)&tmp);
		tmp = renext;
	}
	*lpipes = NULL;
}

struct s_lpipes	*s_lpipes_new(const char pipedword[], t_msh *msh)
{
	struct s_lpipes	*link;

	if ((link = (struct s_lpipes*)malloc(sizeof(struct s_lpipes))) == NULL)
		return (NULL);
	link->com = NULL;
	if ((link->com = s_com_new(pipedword, msh)) == NULL)
	{
		return (NULL);
	}
	link->next = NULL;
	return (link);
}

struct s_lpipes	*s_split_pipes(const char word[], t_msh *msh)
{
	struct s_lpipes	*lpipes;
	size_t			pos[256];
	char			tmp[ARG_MAX];
	short			i;

	ft_bzero(pos, 256 * sizeof(size_t));
	s_get_split_pos(pos, word);
	i = 0;
	while (++i < 256)
	{
		s_set_tmp(tmp, pos, i, word);
		if (((lpipes = s_lpipes_new(tmp, msh))) == NULL)
			return (NULL);
		s_lpipes_add_back(&msh->pipes, lpipes);
		if (pos[i] == 0)
			break ;
	}
	return (lpipes);
}