-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_until_reds.c
executable file
·104 lines (95 loc) · 2.65 KB
/
group_until_reds.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
103
104
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* group_until_reds.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zkarapet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/18 13:17:11 by zkarapet #+# #+# */
/* Updated: 2023/03/06 12:45:50 by zkarapet ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int check_and_move(int *i, char *s, int type, int *start)
{
if (type == 2 || type == 3)
(*i)++;
if (more_reds(&s[*i + 1], '<') || more_reds(&s[*i + 1], '>'))
return (1);
while (ft_is_space(s[*i + 1]))
(*i)++;
*start = *i;
while (s[*i + 1] && !ft_is_space(s[*i + 1]) && !is_red(s[*i + 1]))
(*i)++;
return (0);
}
int quote_forward(char *s, int i)
{
if (s[i] == '\'' || s[i] == '"')
i += find_last_quote(&s[i], s[i]);
return (i);
}
int find_start_end(char *s, t_cmd *cmd_node, t_red_lst *red_lst)
{
int i;
int start;
int end;
char *str;
int type;
i = -1;
start = 0;
end = -1;
str = NULL;
while (s[++i])
{
i = quote_forward(s, i);
if (is_red(s[i]))
{
str = ft_strjoin2(str, s, i, end + 1);
type = return_type(s[i], s[i + 1]);
if (check_and_move(&i, s, type, &start))
return (1);
end = i;
red_add(red_lst, file_trim(&s[start + 1], end - start, type), type);
}
}
cmd_node->args = ft_strjoin2(str, s, i, end + 1);
return (0);
}
int one_cmd_init(t_node *node, t_cmd_lst *cmd_lst, t_args *a)
{
char *s;
t_red_lst *red_lst;
s = node->data;
cmd_lst_add_last(cmd_lst);
red_lst = red_lst_construct();
if (find_start_end(s, cmd_lst->tail, red_lst))
{
cmd_lst->tail->red_lst = red_lst;
cmd_lst_destruct(&cmd_lst, cmd_lst->tail);
return (1);
}
cmd_lst->tail->red_lst = red_lst;
cmd_lst->tail->yep = last_input_work(red_lst);
if (big_loop(cmd_lst->tail, a))
{
cmd_lst_destruct(&cmd_lst, NULL);
return (1);
}
return (0);
}
t_cmd_lst *grouping_with_red(t_list *pipe_group, t_args *a)
{
t_node *cur;
t_cmd_lst *cmd_lst;
cmd_lst = cmd_lst_construct();
cur = pipe_group->head;
while (cur)
{
if (one_cmd_init(cur, cmd_lst, a))
return (NULL);
cur = cur->next;
}
redirections(cmd_lst);
return (cmd_lst);
}