Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/parsing/ft_cmd_list.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* ft_cmd_list.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/07/26 18:17:01 by yabtaour #+# #+# */
9
/* Updated: 2022/09/19 16:35:02 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../minishell.h"
14
15
void ft_initialize(t_cmd *cmd, char *command, int fd_in, int fd_out)
16
{
17
char **all_cmd;
18
19
all_cmd = NULL;
20
if (command)
21
all_cmd = ft_new_split(command, ' ');
22
cmd->cmd = all_cmd;
23
cmd->fd_in = fd_in;
24
cmd->fd_out = fd_out;
25
cmd->next = NULL;
26
cmd->prev = NULL;
27
}
28
29
t_cmd *ft_create_new_command(char *command, int fd_in, int fd_out)
30
{
31
t_cmd *cmd;
32
int i;
33
int pip[2];
34
35
pipe(pip);
36
cmd = malloc(sizeof(t_cmd));
37
if (!cmd)
38
exit (1);
39
ft_initialize(cmd, command, fd_in, fd_out);
40
cmd->her_doc_num = 0;
41
cmd->her_in = 1;
42
i = 0;
43
while (cmd->cmd && cmd->cmd[i])
44
{
45
if (!ft_strcmp(cmd->cmd[i], "<<"))
46
{
47
fd_in = pip[0];
48
cmd->her_in = pip[1];
49
cmd->her_doc_num++;
50
}
51
i++;
52
}
53
cmd->fd_in = fd_in;
54
cmd->fd_out = fd_out;
55
return (cmd);
56
}
57
58
char *ft_fill_command(t_data *data)
59
{
60
char *command;
61
t_lexer *lexer_clone;
62
63
command = NULL;
64
lexer_clone = data->lst_lexer;
65
while (lexer_clone && lexer_clone->type != PIPE)
66
{
67
command = ft_strjoin(command, lexer_clone->val);
68
command = ft_strjoin(command, " ");
69
lexer_clone = lexer_clone->next;
70
}
71
return (command);
72
}
73
74
t_cmd *ft_add_back_cmd(t_data *data, int *fd, int *red, int red_num)
75
{
76
int fd_in;
77
int fd_out;
78
t_cmd *node;
79
t_cmd *cmd_clone;
80
char *command;
81
82
ft_delete_redirections(data);
83
command = ft_fill_command(data);
84
fd_in = ft_get_in(fd, red, red_num);
85
fd_out = ft_get_out(fd, red, red_num);
86
free(fd);
87
free(red);
88
node = ft_create_new_command(command, fd_in, fd_out);
89
free(command);
90
if (!data->lst_cmd)
91
return (node);
92
cmd_clone = data->lst_cmd;
93
while (cmd_clone->next)
94
cmd_clone = cmd_clone->next;
95
cmd_clone->next = node;
96
node->prev = cmd_clone;
97
return (data->lst_cmd);
98
}
99
100
void ft_add_command_pipe(t_data *data)
101
{
102
char *new;
103
int *fd;
104
int *red;
105
106
red = NULL;
107
fd = NULL;
108
while (data->lst_lexer)
109
{
110
new = NULL;
111
ft_add_normal_command(data, new, fd, red);
112
ft_delete_herdoc(data);
113
ft_delete_command(data);
114
}
115
}
116
117