MiniDevil As beautiful as a shell
heredoc_collect.c File Reference

Heredoc collection (pre processes all heredocs in AST) More...

#include "executor.h"
#include "libft.h"
+ Include dependency graph for heredoc_collect.c:

Detailed Description

Heredoc collection (pre processes all heredocs in AST)

Definition in file heredoc_collect.c.

Functions

static int collect_one (t_ast *node, t_shell *shell)
 Collect a single heredoc content into a pipe fd. More...
 
static int walk_redir (t_ast *node, t_shell *shell)
 Process a redirection node (and collecting heredoc if applicable) More...
 
static void close_heredoc_fds (t_ast *node)
 Recursively close cached heredoc file descriptors in the AST. More...
 
int walk_heredocs (t_ast *node, t_shell *shell)
 Recursively walk the AST collecting all heredocs. More...
 
int collect_heredocs (t_ast *node, t_shell *shell)
 Pre collect all heredocs in the AST before the execution phase. More...
 

Function Documentation

◆ collect_one()

static int collect_one ( t_ast node,
t_shell shell 
)
static

Collect a single heredoc content into a pipe fd.

Calls handle_heredoc() to read the input and stores the result in the node's heredoc_fd variable for later use during execution

Parameters
nodeHeredoc redirection node (heredoc_fd is set on success)
shellShell context
Returns
0 on success and -1 on error or SIGINT

Definition at line 26 of file heredoc_collect.c.

References t_ast::data, t_redir_node::file, handle_heredoc(), t_redir_node::heredoc_fd, t_redir_node::quote, and t_ast_data::redir.

Referenced by walk_redir().

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

◆ walk_redir()

static int walk_redir ( t_ast node,
t_shell shell 
)
static

Process a redirection node (and collecting heredoc if applicable)

If the node is a heredoc, it collects its content, then recursively walks into the inner command node to find more redirections

Parameters
nodeRedirection AST node
shellShell context
Returns
0 on success and -1 if any heredoc collection fails

Definition at line 47 of file heredoc_collect.c.

References t_redir_node::cmd, collect_one(), t_ast::data, NODE_REDIR_HEREDOC, t_ast_data::redir, t_ast::type, and walk_heredocs().

Referenced by walk_heredocs().

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

◆ close_heredoc_fds()

static void close_heredoc_fds ( t_ast node)
static

Recursively close cached heredoc file descriptors in the AST.

Parameters
nodeRoot of the AST subtree

Definition at line 64 of file heredoc_collect.c.

References t_ast_data::binary, t_redir_node::cmd, t_ast::data, t_redir_node::heredoc_fd, t_binary_node::left, NODE_PIPE, NODE_REDIR_HEREDOC, NODE_REDIR_IN, t_ast_data::redir, t_binary_node::right, and t_ast::type.

Referenced by collect_heredocs().

+ Here is the caller graph for this function:

◆ walk_heredocs()

int walk_heredocs ( t_ast node,
t_shell shell 
)

Recursively walk the AST collecting all heredocs.

It traverses the pipe & redirection nodes first to ensure all heredoc prompts appear before any command runs

Parameters
nodeRoot of the AST Subtree
shellShell context
Returns
0 on success and -1 if a heredoc fails (SIGINT or error)

Definition at line 95 of file heredoc_collect.c.

References t_ast_data::binary, t_ast::data, t_binary_node::left, NODE_PIPE, NODE_REDIR_HEREDOC, NODE_REDIR_IN, t_binary_node::right, t_ast::type, and walk_redir().

Referenced by collect_heredocs(), and walk_redir().

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

◆ collect_heredocs()

int collect_heredocs ( t_ast node,
t_shell shell 
)

Pre collect all heredocs in the AST before the execution phase.

Walks the entire AST to read all heredoc inputs first. On failure it closes any previously collected heredocs fds to prevent leaks

Parameters
nodeRoot of the AST
shellShell context
Returns
0 on success and -1 on failure

Definition at line 120 of file heredoc_collect.c.

References close_heredoc_fds(), and walk_heredocs().

Referenced by process_input(), and process_ui_input().

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

Go to the source code of this file.