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)
 redirection input in context of executing AST
 
int redirect_out (t_ast *node, t_ms_data *data)
 
int redirect_append (t_ast *node, t_ms_data *data)
 redirect append ">>" to the end of the file output
 
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(), redirect_append(), and redirect_in().

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 
)

redirect append ">>" to the end of the file output

--

  • Parameters
    nodecurrent node in the AST
  • Parameters
    dataminishell data structure
  • Returns
    status:
  • 0: success
  • 1: error

Definition at line 31 of file redirect_append.c.

32{
33 pid_t pid;
34 int status;
35
36 pid = fork();
37 if (pid == -1)
38 return (1);
39 if (pid == 0)
40 {
41 data->std_out = open_file(node->right, ">>");
42 if (data->std_out == -1)
43 return (1);
44 execute_ast(node->left, data);
45 exit(0);
46 }
47 waitpid(pid, &status, 0);
48 return (WEXITSTATUS(status));
49}
int execute_ast(t_ast *node, t_ms_data *data)
execute Abstract Syntax Tree
Definition execute.c:38
int open_file(t_ast *node, char *direction)
open file in the context of redirection
Definition utils.c:34
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53
int std_out
Definition shell.h:27

References execute_ast(), s_ast::left, open_file(), s_ast::right, and s_ms_data::std_out.

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 (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)
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

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:

◆ redirect_in()

int redirect_in ( t_ast node,
t_ms_data data 
)

redirection input in context of executing AST

--

  • Parameters
    nodecurrent node in the AST
  • Parameters
    dataminishell structure data
  • Returns
    status:
  • 0: success
  • 1: error

Definition at line 38 of file redirect_in.c.

39{
40 pid_t pid;
41
42 pid = fork();
43 if (pid == -1)
44 return (1);
45 if (pid == 0)
46 {
47 data->std_in = open_file(node->right, "<");
48 if (data->std_in == -1)
49 return (1);
50 execute_ast(node->left, data);
51 exit(0);
52 }
53 waitpid(pid, &data->exit_status, 0);
54 return (0);
55}
int exit_status
Definition shell.h:30
int std_in
Definition shell.h:26

References execute_ast(), s_ms_data::exit_status, s_ast::left, open_file(), s_ast::right, and s_ms_data::std_in.

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 88 of file redirect_out.c.

89{
90 pid_t pid;
91 int status;
92
93 pid = fork();
94 if (pid == -1)
95 return (1);
96 if (pid == 0)
97 {
98 if (open_and_redirect(node, data) != 0)
99 exit(1);
100 execute_ast(node->left, data);
101 exit(0);
102 }
103 waitpid(pid, &status, 0);
104 return (WEXITSTATUS(status));
105}
static int open_and_redirect(t_ast *node, t_ms_data *data)
redirect out ">" to the file output

References execute_ast(), 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: