-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipe_error.c
87 lines (81 loc) · 2.4 KB
/
pipe_error.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* pipe_error.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zhabri <zhabri@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/12/19 11:02:03 by zhabri #+# #+# */
/* Updated: 2022/12/22 10:46:25 by zhabri ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int pipe_error_loop(t_list *curr, t_list *prev, char *trimmed)
{
prev = curr;
trimmed = op_error_trimmed(prev);
while (curr->next)
{
curr = curr->next;
if (((t_token *)prev->content)->label == PIPE \
&& ((t_token *)curr->content)->label == PIPE \
&& !ft_strlen(trimmed))
{
ft_putstr_fd("Pipe error\n", 2);
free(trimmed);
return (-1);
}
free(trimmed);
trimmed = op_error_trimmed(curr);
if (((t_token *)curr->content)->label == PIPE \
&& curr->next == NULL && !ft_strlen(trimmed))
{
ft_putstr_fd("Pipe error\n", 2);
free(trimmed);
return (-1);
}
}
free(trimmed);
return (0);
}
int pipe_error_bis(char *trimmed, char *op_pipe, t_list *curr)
{
t_token *token;
token = NULL;
if (curr->next)
token = ((t_token *)curr->next->content);
if (!ft_strlen(trimmed)
|| (!ft_strlen(op_pipe) && !token)
|| (!ft_strlen(op_pipe) && token && token->label == PIPE))
{
free(op_pipe);
free(trimmed);
ft_putstr_fd("Pipe error\n", 2);
return (-1);
}
free(trimmed);
free(op_pipe);
return (0);
}
int pipe_error(void)
{
t_list *curr;
t_list *prev;
char *trimmed;
char *op_pipe;
curr = NULL;
prev = NULL;
trimmed = NULL;
if (g_glob->head)
curr = *g_glob->head;
if (curr && curr->content && ((t_token *)curr->content)->label == PIPE)
{
trimmed = get_first();
op_pipe = ft_strtrim(((t_token *)curr->content)->arg, " \t");
if (pipe_error_bis(trimmed, op_pipe, curr) == -1)
return (-1);
}
if (curr)
return (pipe_error_loop(curr, prev, trimmed));
return (0);
}