Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
yabtaour
GitHub Repository: yabtaour/Minishell-42
Path: blob/main/execution/my_mess/heredoc_exec.c
1407 views
1
/* ************************************************************************** */
2
/* */
3
/* ::: :::::::: */
4
/* heredoc_exec.c :+: :+: :+: */
5
/* +:+ +:+ +:+ */
6
/* By: ssabbaji <[email protected]> +#+ +:+ +#+ */
7
/* +#+#+#+#+#+ +#+ */
8
/* Created: 2022/09/07 13:15:29 by ssabbaji #+# #+# */
9
/* Updated: 2022/10/07 14:29:39 by ssabbaji ### ########.fr */
10
/* */
11
/* ************************************************************************** */
12
13
#include "../../minishell.h"
14
15
void hand(int num)
16
{
17
int fd[2];
18
19
(void)num;
20
rl_done = 1;
21
g_vars.g_heredoc = 0;
22
pipe(fd);
23
dup2(fd[0], 0);
24
write(fd[1], "\n", 1);
25
}
26
27
static void suppress_output(void)
28
{
29
struct termios termios_p;
30
31
if (tcgetattr(0, &termios_p) != 0)
32
perror("Minishell: tcgetattr");
33
termios_p.c_lflag &= ~ECHOCTL;
34
if (tcsetattr(0, 0, &termios_p) != 0)
35
perror("Minishell: tcsetattr");
36
}
37
38
void check_delims(t_data *data, t_cmd *cmd, int idx)
39
{
40
char *here_buff;
41
int i;
42
43
i = 0;
44
suppress_output();
45
signal(SIGINT, &hand);
46
while (g_vars.g_heredoc && i < cmd->her_doc_num)
47
{
48
here_buff = readline("> ");
49
if (here_buff == NULL)
50
{
51
free(here_buff);
52
break ;
53
}
54
if (here_buff[0] != '\0' && !ft_strcmp(data->eof[idx], here_buff))
55
{
56
i++;
57
idx++;
58
}
59
else
60
print_her_in(cmd, here_buff);
61
}
62
dup2(cmd->her_in, STDIN_FILENO);
63
}
64
65
int heredoc_exec(t_data *data, t_cmd *cmd_lst, int idx)
66
{
67
int tmp;
68
t_cmd *cmd;
69
70
cmd = cmd_lst;
71
g_vars.g_where_ami = 0;
72
tmp = dup(0);
73
g_vars.g_heredoc = 1;
74
check_delims(data, cmd, idx);
75
dup2(tmp, 0);
76
close(tmp);
77
return (1);
78
}
79
80