-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathherdoc.c
105 lines (97 loc) · 2.34 KB
/
herdoc.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
105
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* herdoc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: hchairi <hchairi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/22 12:41:27 by hchairi #+# #+# */
/* Updated: 2023/07/22 12:53:34 by hchairi ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void wr_expand(char *l, t_nodes *node, t_env *env, int fd)
{
char *to_expand;
char *exp;
int i;
int j;
i = -1;
while (l[++i])
{
if (l[i] == '$' && node->check == 0)
{
j = ++i;
if (l[i] == '_' || ft_isalpha(l[i]))
{
while (l[++i] && (l[i] == '_' || ft_isalnum(l[i])))
;
to_expand = ft_substr(l, j, i - j);
exp = get_node_value(env, to_expand);
ft_putstr_fd(exp, fd);
}
}
else
write(fd, &l[i], 1);
}
}
void expand_herdoc(void)
{
t_nodes *node;
t_nodes *herdoc;
node = g_all.head;
while (node && node->next)
{
if (node && node->next && node->type == HERDOC
&& node->next->type == SPACES)
{
herdoc = node;
node = node->next->next;
while (node && node->next && node->type != PIPES
&& node->type != SPACES)
{
if (node && (node->type == D_Q || node->type == S_Q))
{
herdoc->check = 1;
return ;
}
node = node->next;
}
}
if (node && node->next)
node = node->next;
}
}
void fd_dup(int *d, t_cmd *cmd, int *fd)
{
*d = dup(0);
dup2(g_all.dup_z, 0);
g_all.k = 1;
cmd->in = fd[1];
}
void herdoc(t_nodes *node, t_cmd *cmd, char *del, t_env *env)
{
char *l;
int d;
int pipefd[2];
if (pipe(pipefd) == -1)
{
perror("pipe");
return ;
}
fd_dup(&d, cmd, pipefd);
while (1)
{
l = readline(">");
if (!ft_strcmp(l, del) || !l)
{
free(l);
break ;
}
wr_expand(l, node, env, pipefd[1]);
free(l);
}
g_all.k = 0;
close(pipefd[1]);
dup2(d, 0);
}