-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpansions_empty_nodes_0.c
102 lines (93 loc) · 2.55 KB
/
expansions_empty_nodes_0.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansions_empty_nodes.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngasco <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/21 16:14:01 by ngasco #+# #+# */
/* Updated: 2022/03/21 16:14:02 by ngasco ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ft_remove_empty_nodes(t_cdata *t_cdata)
{
int result;
while (1)
{
result = ft_found_empty_node_to_remove(t_cdata);
if (result == 0)
break ;
else if (result == -1)
return (0);
}
return (1);
}
/* Removing a single node contaning an empty string */
int ft_found_empty_node_to_remove(t_cdata *t_cdata)
{
struct s_qnode *curr;
int i;
int found_cmd;
i = 0;
found_cmd = 0;
curr = t_cdata->t_qdata->quotes_list;
if (!*(curr->str) && curr->next == NULL)
return (ft_found_empty_node_to_remove_lonely_node(t_cdata, curr));
while (1)
{
if (*(curr->str) && !found_cmd)
found_cmd = 1;
else if (!*(curr->str) && (!curr->q_type || !curr->is_spaced)
&& curr->next)
return (ft_remove_node_with_index(i,
&t_cdata->t_qdata->quotes_list));
if (curr->next == NULL)
break ;
curr = curr->next;
i++;
}
return (0);
}
int ft_found_empty_node_to_remove_lonely_node(t_cdata *t_cdata,
struct s_qnode *curr)
{
if (!curr->q_type)
{
t_cdata->syntax_error = 1 ;
return (-1);
}
return (0);
}
int ft_remove_node_with_index(int index, struct s_qnode **root)
{
struct s_qnode *curr;
struct s_qnode *to_remove;
int i;
i = 1;
curr = *root;
if (*root == NULL)
return (1);
if (index == 0)
{
to_remove = *root;
*root = (*root)->next;
ft_remove_node_with_index_free_util(to_remove);
return (1);
}
while (i < index)
{
curr = curr->next;
i++;
}
to_remove = curr->next;
curr->next = curr->next->next;
ft_remove_node_with_index_free_util(to_remove);
return (1);
}
/* Util function to shorten function above */
void ft_remove_node_with_index_free_util(struct s_qnode *to_remove)
{
free(to_remove->str);
free(to_remove);
}