maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
redirect_heredoc.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* redirect_heredoc.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rocky <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/09/06 13:46:24 by rocky #+# #+# */
9/* Updated: 2024/09/09 13:50:24 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include "shell.h"
14#include "tokens.h"
15#include "redirection.h"
16#include "execute.h"
17#include <fcntl.h>
18#include <sys/wait.h>
19#include "signals.h"
20
21/*
22 GLOBAL VAR
23 volatile tells compiler the var can change at compilation between accesses.
24 sig_atomic_t used for global_vars
25 */
26
27volatile sig_atomic_t g_heredoc_interrupted = 0;
28
29static void setup_sigint_handler(struct sigaction *sa_old);
30static int handle_heredoc_interruption(char *line, char *eof, int file_fd, \
31 struct sigaction *sa_old);
32static void execute_child(t_ast *node, t_ms_data *data, int *file_fd);
33static void write_heredoc_lines(char **line, int file_fd, char *eof, \
34 t_ms_data *data);
35int redirect_here_doc(t_ast *node, t_ms_data *data);
36
37static void setup_sigint_handler(struct sigaction *sa_old)
38{
39 struct sigaction sa_new;
40
41 sa_new.sa_handler = handle_sigint_heredoc;
42 sigemptyset(&sa_new.sa_mask);
43 sa_new.sa_flags = 0;
44 sigaction(SIGINT, &sa_new, sa_old);
45}
46
47static int handle_heredoc_interruption(char *line, char *eof, int file_fd, \
48 struct sigaction *sa_old)
49{
50 free(line);
51 free(eof);
52 close(file_fd);
53 unlink("/tmp/heredoc");
55 sigaction(SIGINT, sa_old, NULL);
56 return (1);
57}
58
59static void execute_child(t_ast *node, t_ms_data *data, int *file_fd)
60{
61 pid_t pid;
62
63 pid = fork();
64 if (pid == -1)
65 ft_perror("fork");
66 if (pid == 0)
67 {
68 data->std_in = dup(*file_fd);
69 execute_ast(node, data);
70 exit(0);
71 }
72 close(*file_fd);
73 waitpid(pid, &data->exit_status, 0);
74}
75
76void write_heredoc_lines(char **line, int file_fd, char *eof, \
77 t_ms_data *data)
78{
79 while (*line && (ft_strcmp(*line, eof) != 0) && !g_heredoc_interrupted)
80 {
81 write(file_fd, *line, ft_strlen(*line));
82 write(file_fd, "\n", 1);
83 free(*line);
84 (void)data;
85 *line = readline("🌞 > ");
86 }
87}
88
90{
91 char *line;
92 char *eof;
93 int file_fd;
94 struct sigaction sa_old;
95
96 line = NULL;
97 if (node->right->args[0] == NULL)
98 return (1);
99 setup_sigint_handler(&sa_old);
100 file_fd = open_tmp_file("w");
101 eof = ft_strdup(node->right->args[0]);
102 line = process_and_reassemble(readline("🌞 > "), data);
103 write_heredoc_lines(&line, file_fd, eof, data);
105 return (handle_heredoc_interruption(line, eof, file_fd, &sa_old));
106 free(line);
107 free(eof);
108 close(file_fd);
109 sigaction(SIGINT, &sa_old, NULL);
110 file_fd = open_tmp_file("r");
111 execute_child(node->left, data, &file_fd);
112 unlink("/tmp/heredoc");
113 return (0);
114}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
int ft_perror(char *str)
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
static int handle_heredoc_interruption(char *line, char *eof, int file_fd, struct sigaction *sa_old)
volatile sig_atomic_t g_heredoc_interrupted
static void setup_sigint_handler(struct sigaction *sa_old)
static void execute_child(t_ast *node, t_ms_data *data, int *file_fd)
static void write_heredoc_lines(char **line, int file_fd, char *eof, t_ms_data *data)
int redirect_here_doc(t_ast *node, t_ms_data *data)
char * process_and_reassemble(char *line, t_ms_data *data)
int open_tmp_file(const char *type)
Definition utils.c:52
void handle_sigint_heredoc(int signo)
Definition tokens.h:50
char ** args
Definition tokens.h:52
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53
int exit_status
Definition shell.h:30
int std_in
Definition shell.h:26