-
Notifications
You must be signed in to change notification settings - Fork 1
/
cmd_cmd_invocation.c
81 lines (73 loc) · 1.88 KB
/
cmd_cmd_invocation.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
#include <stdio.h>
#include <readline/readline.h>
#include <readline/history.h>
#include "execution.h"
#include "minishell.h"
#include "utils.h"
/*
** Malloc and initialize t_command_invocation
*/
t_command_invocation *cmd_init_cmdinvo(const char **exec_and_args)
{
t_command_invocation *cmdinvo;
cmdinvo = malloc(sizeof(t_command_invocation));
if (!cmdinvo)
return (NULL);
cmdinvo->exec_and_args = exec_and_args;
cmdinvo->input_redirections = NULL;
cmdinvo->output_redirections = NULL;
cmdinvo->piped_command = NULL;
cmdinvo->pid = 0;
return (cmdinvo);
}
int cmd_add_inredirect(t_command_invocation *command,
const char *filepath, int fd)
{
t_cmd_redirection *red;
red = ft_calloc(1, sizeof(t_cmd_redirection));
check_malloc_has_succeeded("add_inredirect", red);
red->filepath = ft_strdup(filepath);
check_malloc_has_succeeded("add_inredirect", (void *)red->filepath);
red->fd = fd;
if (!cmd_redirection_add_back(&command->input_redirections, red))
{
free(red);
return (ERROR);
}
return (0);
}
int cmd_add_outredirect(t_command_invocation *command,
const char *filepath, int fd, bool is_append)
{
t_cmd_redirection *red;
red = ft_calloc(1, sizeof(t_cmd_redirection));
check_malloc_has_succeeded("add_outredirect", red);
red->filepath = ft_strdup(filepath);
check_malloc_has_succeeded("add_outredirect", (void *)red->filepath);
red->fd = fd;
red->is_append = is_append;
if (!cmd_redirection_add_back(&command->output_redirections, red))
{
free(red);
return (ERROR);
}
return (0);
}
/*
** add command to cmd->pipe_command
*/
t_command_invocation *cmd_cmdinvo_add_pipcmd(t_command_invocation **cmds,
t_command_invocation *pipcmd)
{
t_command_invocation *current_cmd;
if (!*cmds)
*cmds = pipcmd;
else
{
current_cmd = *cmds;
while (current_cmd->piped_command)
current_cmd = current_cmd->piped_command;
current_cmd->piped_command = pipcmd;
}
return (pipcmd);
}