maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
redirect_heredoc.c File Reference
#include "shell.h"
#include "tokens.h"
#include "redirection.h"
#include "execute.h"
#include <fcntl.h>
#include <sys/wait.h>
#include "signals.h"
Include dependency graph for redirect_heredoc.c:

Go to the source code of this file.

Functions

static void setup_sigint_handler (struct sigaction *sa_old)
 
static int handle_heredoc_interruption (char *line, char *eof, int file_fd, 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)
 

Variables

volatile sig_atomic_t g_heredoc_interrupted = 0
 

Function Documentation

◆ execute_child()

static void execute_child ( t_ast node,
t_ms_data data,
int *  file_fd 
)
static

Definition at line 59 of file redirect_heredoc.c.

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}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
int ft_perror(char *str)
int exit_status
Definition shell.h:30
int std_in
Definition shell.h:26

References execute_ast(), s_ms_data::exit_status, ft_perror(), and s_ms_data::std_in.

Referenced by redirect_here_doc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ handle_heredoc_interruption()

static int handle_heredoc_interruption ( char *  line,
char *  eof,
int  file_fd,
struct sigaction *  sa_old 
)
static

Definition at line 47 of file redirect_heredoc.c.

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}
volatile sig_atomic_t g_heredoc_interrupted

References g_heredoc_interrupted.

Referenced by redirect_here_doc().

Here is the caller graph for this function:

◆ redirect_here_doc()

int redirect_here_doc ( t_ast node,
t_ms_data data 
)

Definition at line 89 of file redirect_heredoc.c.

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}
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)
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)
char * process_and_reassemble(char *line, t_ms_data *data)
int open_tmp_file(const char *type)
Definition utils.c:52
char ** args
Definition tokens.h:52
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53

References s_ast::args, execute_child(), ft_strdup(), g_heredoc_interrupted, handle_heredoc_interruption(), s_ast::left, open_tmp_file(), process_and_reassemble(), s_ast::right, setup_sigint_handler(), and write_heredoc_lines().

Referenced by execute_ast().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ setup_sigint_handler()

static void setup_sigint_handler ( struct sigaction *  sa_old)
static

Definition at line 37 of file redirect_heredoc.c.

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}
void handle_sigint_heredoc(int signo)

References handle_sigint_heredoc().

Referenced by redirect_here_doc().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ write_heredoc_lines()

void write_heredoc_lines ( char **  line,
int  file_fd,
char *  eof,
t_ms_data data 
)
static

Definition at line 76 of file redirect_heredoc.c.

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}
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

References ft_strcmp(), ft_strlen(), and g_heredoc_interrupted.

Referenced by redirect_here_doc().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ g_heredoc_interrupted

volatile sig_atomic_t g_heredoc_interrupted = 0