maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
redirection.h File Reference
#include "shell.h"
#include "tokens.h"
Include dependency graph for redirection.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

int redirect_in (t_ast *node, t_ms_data *data)
 
int redirect_out (t_ast *node, t_ms_data *data)
 
int redirect_append (t_ast *node, 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_file (t_ast *node, char *direction)
 open file in the context of redirection
 
int open_tmp_file (const char *type)
 

Function Documentation

◆ open_file()

int open_file ( t_ast node,
char *  direction 
)

open file in the context of redirection

-- "<" - read

  • ">" - write
  • ">>" - append
  • "tty" - open /dev/tty
  • Parameters
    nodecurrent node in the AST
  • Parameters
    directiontype of redirection
  • Returns
    int file descriptor

Definition at line 34 of file utils.c.

35{
36 int fd;
37
38 if ((ft_strcmp(direction, "<") == 0) || (ft_strcmp(direction, "read") == 0))
39 fd = open(node->args[0], O_RDONLY);
40 else if (ft_strcmp(direction, ">") == 0)
41 fd = open(node->args[0], O_WRONLY | O_CREAT | O_TRUNC, 0644);
42 else if ((ft_strcmp(direction, ">>") == 0) \
43 || (ft_strcmp(direction, "temp") == 0))
44 fd = open(node->args[0], O_WRONLY | O_CREAT | O_APPEND, 0644);
45 else if (ft_strcmp(direction, "tty") == 0)
46 fd = open("/dev/tty", O_RDWR);
47 else
48 fd = -1;
49 return (fd);
50}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char ** args
Definition tokens.h:52

References s_ast::args, and ft_strcmp().

Referenced by open_and_redirect(), open_and_redirect(), and setup_redirection().

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

◆ open_tmp_file()

int open_tmp_file ( const char *  type)

Definition at line 52 of file utils.c.

53{
54 int file_fd;
55
56 file_fd = -1;
57 if (ft_strcmp(type, "w") == 0)
58 file_fd = open("/tmp/heredoc", O_WRONLY | O_CREAT | O_TRUNC, 0644);
59 else if (ft_strcmp(type, "r") == 0)
60 file_fd = open("/tmp/heredoc", O_RDONLY);
61 if (file_fd < 0)
62 ft_perror("open");
63 return (file_fd);
64}
int ft_perror(char *str)

References ft_perror(), and ft_strcmp().

Referenced by redirect_here_doc().

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

◆ process_and_reassemble()

char * process_and_reassemble ( char *  line,
t_ms_data data 
)

Definition at line 27 of file loc_env_var_handler_heredoc.c.

28{
29 char **tokens;
30 size_t result_len;
31 char *result;
32
33 tokens = ft_split(line, ' ');
34 free(line);
35 if (!tokens)
36 return (NULL);
37 result_len = process_tokens(tokens, data);
38 result = assemble_result(tokens, result_len);
39 ft_free_2d_arr(tokens);
40 return (result);
41}
void ft_free_2d_arr(char **arr)
char ** ft_split(char const *s, char c)
Definition ft_split.c:95
size_t process_tokens(char **tokens, t_ms_data *data)
static char * assemble_result(char **tokens, size_t result_len)

References assemble_result(), ft_free_2d_arr(), ft_split(), and process_tokens().

Referenced by redirect_here_doc().

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

◆ redirect_append()

int redirect_append ( t_ast node,
t_ms_data data 
)

Definition at line 41 of file redirect_append.c.

42{
43 pid_t pid;
44 int exec_status;
45 int status;
46
47 pid = fork();
48 if (pid == -1)
49 return (EXIT_FAILURE);
50 if (pid == 0)
51 {
52 if (open_and_redirect(node, data) != 0)
53 exit(EXIT_FAILURE);
54 exec_status = execute_ast(node->left, data);
55 exit(exec_status);
56 }
57 if (waitpid(pid, &status, 0) == -1)
58 return (EXIT_FAILURE);
59 if (WIFSIGNALED(status))
60 return (128 + WTERMSIG(status));
61 return (WEXITSTATUS(status));
62}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
#define EXIT_FAILURE
Definition exit_status.h:17
static int open_and_redirect(t_ast *node, t_ms_data *data)
struct s_ast * left
Definition tokens.h:53

References execute_ast(), EXIT_FAILURE, s_ast::left, and open_and_redirect().

Referenced by execute_ast().

Here is the call graph for this function:
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 (EXIT_SUCCESS);
114}
#define EXIT_SUCCESS
Definition exit_status.h:16
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)
char * process_and_reassemble(char *line, t_ms_data *data)
int open_tmp_file(const char *type)
Definition utils.c:52
struct s_ast * right
Definition tokens.h:54

References s_ast::args, execute_child(), EXIT_SUCCESS, 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:

◆ redirect_in()

int redirect_in ( t_ast node,
t_ms_data data 
)

Definition at line 45 of file redirect_in.c.

46{
47 int status;
48 int original_stdin;
49
50 if (setup_redirection(node, &original_stdin) == -1)
51 return (EXIT_FAILURE);
52 if (!node->left->args[0])
53 status = EXIT_SUCCESS;
54 else
55 status = execute_ast(node->left, data);
56 if (dup2(original_stdin, STDIN_FILENO) == -1)
57 status = EXIT_FAILURE;
58 close(original_stdin);
59 return (status);
60}
static int setup_redirection(t_ast *node, int *original_stdin)
Definition redirect_in.c:22

References s_ast::args, execute_ast(), EXIT_FAILURE, EXIT_SUCCESS, s_ast::left, and setup_redirection().

Referenced by execute_ast().

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

◆ redirect_out()

int redirect_out ( t_ast node,
t_ms_data data 
)

Definition at line 54 of file redirect_out.c.

55{
56 pid_t pid;
57 int exec_status;
58 int status;
59
60 pid = fork();
61 if (pid == -1)
62 return (EXIT_FAILURE);
63 if (pid == 0)
64 {
65 if (open_and_redirect(node, data) != 0)
66 exit(EXIT_FAILURE);
67 exec_status = execute_ast(node->left, data);
68 exit(exec_status);
69 }
70 if (waitpid(pid, &status, 0) == -1)
71 return (EXIT_FAILURE);
72 if (WIFSIGNALED(status))
73 return (128 + WTERMSIG(status));
74 return (WEXITSTATUS(status));
75}
static int open_and_redirect(t_ast *node, t_ms_data *data)
redirect out ">" to the file output

References execute_ast(), EXIT_FAILURE, s_ast::left, and open_and_redirect().

Referenced by execute_ast().

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