summaryrefslogtreecommitdiffstats
path: root/src/s_line.c
blob: 5fe9a966f7aa366ac78e8eca309f258c6035eb6c (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
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   s_line.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 <stdint.h>

#include "p_line.h"
#include "s_com.h"
#include "s_struct.h"

t_line
	*s_line_last(t_line *line)
{
	while (line->next != NULL)
		line = line->next;
	return (line);
}

void
	s_line_add_back(t_line **aline,
					t_line *new)
{
	t_line	*tmp;

	if (!*aline)
		*aline = new;
	else
	{
		tmp = s_line_last(*aline);
		tmp->next = new;
	}
}

void
	s_line_clear(t_line **line)
{
	t_line	*tmp;
	t_line	*renext;

	if (!line)
		return ;
	tmp = *line;
	while (tmp != NULL)
	{
		renext = tmp->next;
		if (tmp->com != NULL)
		{
			s_com_destroy(&tmp->com);
		}
		/* ft_memdel((void*)&tmp->com); */
		/* if (tmp->argv) */
		/* 	ft_delwords(tmp->argv); */
		/* if (tmp->redir != 0) */
		/* 	ft_memdel((void*)&tmp->rdrpath); */
		/* if (tmp->env_fork != NULL) */
		/* 	ft_delwords(tmp->env_fork); */
		ft_memdel((void*)&tmp);
		tmp = renext;
	}
	*line = NULL;
}

t_line
	*s_line_new(const char word[],
			t_msh *msh)
{
	t_line	*link;

	if ((link = (t_line*)malloc(sizeof(t_line))) == NULL)
		return (NULL);
	link->com = NULL;
	link->pipes = NULL;
	link->next = NULL;
	if (word == NULL)
	{
		link->next = NULL;
		return (link);
	}
	link->com = s_com_new(word, msh);
	/* TODO: check link->com = NULL */
	return (link);
}