summaryrefslogtreecommitdiffstats
path: root/src/ft_s_lcom.c
blob: ab6341482be2917eb580c2fdba343c7c8e90c976 (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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* ************************************************************************** */
/*                                                                            */
/*                                                        :::      ::::::::   */
/*   ft_s_lcom.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 "ft_p_lcom.h"
#include "ft_p_lcom_next.h"
#include "ft_s_struct.h"

static int8_t
	ft_fill_lcom(char *words[],
				t_lcom **lcom)
{
	/* TODO: norme */
	uint64_t	i;
	uint64_t	j;

	i = 0;
	if (words[0])
	{
		if (!((*lcom)->com = (char*)malloc((ft_strlen(words[0]) + 1) *
			sizeof(char))))
			return (-1);
		ft_strlcpy((*lcom)->com, words[0], ft_strlen(words[0]) + 1);
	}
	else
		return (0);
	while(words[i])
	{
		/* TODO: cut fd number "msh ~> echo a 2>file" */
		/*                                    ^       */
		if (ft_ischarset("<>", words[i][0]))
			break ;
		i++;
	}
	if (!((*lcom)->argv = (char**)malloc((i + 1) * sizeof(char*))))
			return (-1);
	j = 0;
	while (i > 0 && j < i)
	{
		if (!((*lcom)->argv[j] =
			  (char*)malloc((ft_strlen(words[j]) + 1) * sizeof(char))))
			return (-1);
		ft_strlcpy((*lcom)->argv[j], words[j],
				   ft_strlen(words[j]) + 1);
		j++;
	}
	(*lcom)->argv[j] = 0;
	return (0);
}

t_lcom
	*ft_lcom_last(t_lcom *lcom)
{
	while (lcom->next != NULL)
		lcom = lcom->next;
	return (lcom);
}

void
	ft_lcom_add_back(t_lcom **alcom,
					t_lcom *new)
{
	t_lcom	*tmp;

	if (!*alcom)
		*alcom = new;
	else
	{
		tmp = ft_lcom_last(*alcom);
		tmp->next = new;
	}
}

void
	ft_lcom_clear(t_lcom **lcom)
{
	t_lcom	*tmp;
	t_lcom	*renext;

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

/* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
/* TODO: remove this after tests */
/* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
static void
ft_print_words(char *words[])
{
	while (*words)
	{
		ft_printf("[%s]\n", *words);
		words++;
	}
}

t_lcom
	*ft_lcom_new(const char word[],
				t_msh *msh)
{
	t_lcom	*link;
	char	**words;

	(void)msh;
	if (!(link = (t_lcom*)malloc(sizeof(t_lcom))))
		return (NULL);
	link->redir = 0;
	link->com = NULL;
	link->argv = NULL;
	link->rdrfd = 0;
	link->rdrpath = NULL;
	if (ft_get_redir(word, &link) != 0)
		return (NULL);
	if (!(words = ft_subst_args(word, link->redir)))
		return (NULL);
	if (!(words = ft_subst_vars(words, msh)))
		return (NULL);
	/* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
	/* TODO: remove this after tests */
	ft_print_words(words);
	/* TODO: remove this after tests */
	/* !!!!!!!!!! ATTENTION !!!!!!!!!!! */
	if (ft_fill_lcom(words, &link) < 0)
	{
		ft_delwords(words);
		return (NULL);
	}
	link->next = NULL;
	ft_delwords(words);
	return (link);
}