-
Notifications
You must be signed in to change notification settings - Fork 0
/
heredoc_redir.c
124 lines (114 loc) · 3.16 KB
/
heredoc_redir.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* heredoc_redir.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dkham <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/06/09 23:31:20 by dkham #+# #+# */
/* Updated: 2023/06/10 00:49:26 by dkham ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void handle_heredocs(t_shell *my_shell)
{
t_pipes *head;
int i;
char *tmpfile;
head = my_shell->head;
while (head)
{
i = 0;
while (head->simple_cmd->redirection && \
head->simple_cmd->redirection[i])
{
if (ft_strcmp(head->simple_cmd->redirection[i], "<<") == 0)
{
tmpfile = ft_strjoin("/tmp/", head->simple_cmd->redir_value[i]);
make_hd(my_shell, tmpfile, head->simple_cmd->redir_value[i]);
free(head->simple_cmd->redirection[i]);
head->simple_cmd->redirection[i] = ft_strdup("<");
free(head->simple_cmd->redir_value[i]);
head->simple_cmd->redir_value[i] = ft_strdup(tmpfile);
free(tmpfile);
}
i++;
}
head = head->next;
}
}
int save_stdin_and_set_signal(t_shell *my_shell)
{
set_heredoc_signal(my_shell);
return (dup(0));
}
void make_hd(t_shell *my_shell, char *filename, char *end_str)
{
int fd;
int save_stdin;
char *line;
my_shell->heredoc_used = 1;
fd = open(filename, O_CREAT | O_RDWR | O_TRUNC, 0644);
save_stdin = save_stdin_and_set_signal(my_shell);
while (1)
{
line = readline("> ");
if (ft_strcmp(line, end_str) == 0)
break ;
else if (line == NULL)
{
dup2(save_stdin, 0);
g_exit_status = 130;
free(line);
break ;
}
ft_putendl_fd(line, fd);
free(line);
}
free(line);
init_signal(my_shell);
close(fd);
}
void cleanup_heredocs(t_shell *my_shell)
{
int i;
t_pipes *head;
i = 0;
head = my_shell->head;
while (head)
{
if (my_shell->head->simple_cmd->redir_value[i])
{
if (ft_strncmp(my_shell->head->simple_cmd->redir_value[i], \
"/tmp/", 5) == 0)
unlink(my_shell->head->simple_cmd->redir_value[i]);
}
head = head->next;
}
}
void handle_redirections(t_shell *my_shell, t_pipes *head)
{
int i;
int fd;
i = 0;
while (head->simple_cmd->redirection[i])
{
if (ft_strcmp(head->simple_cmd->redirection[i], "<") == 0)
{
fd = open(head->simple_cmd->redir_value[i], O_RDONLY);
if (fd < 0)
print_error_message(head->simple_cmd->redir_value[i]);
else
my_shell->fd_in = fd;
}
else if (ft_strcmp(head->simple_cmd->redirection[i], ">") == 0 && \
fd >= 0)
output_redir(my_shell, head, i, 0);
else if (ft_strcmp(head->simple_cmd->redirection[i], ">>") == 0 && \
fd >= 0)
output_redir(my_shell, head, i, 1);
i++;
}
head = head->next;
return ;
}