MiniDevil As beautiful as a shell
parser_redir.c File Reference

Redirection parsing: parses <, >, >>, << tokens & files. More...

#include "parser.h"
#include "ast.h"
#include "libft.h"
+ Include dependency graph for parser_redir.c:

Detailed Description

Redirection parsing: parses <, >, >>, << tokens & files.

Definition in file parser_redir.c.

Functions

static t_node_type get_redir_node_type (t_token_type type)
 Convert a token type to the corresponding AST redirection node type. More...
 
static t_astparse_one_redirection (t_token **tokens, t_ast *cmd)
 Parse a single redirection token and the following WORD (filename) More...
 
static void collect_and_merge_remaining_args (t_ast *cmd, t_token **tokens)
 Collect trailing WORD tokens and merge them into a command's args. More...
 
static t_astget_command_node (t_ast *node)
 Extract the innermost command from a redirection chain. More...
 
t_astparse_command (t_token **tokens)
 Parse a command with all its redirections. More...
 

Function Documentation

◆ get_redir_node_type()

static t_node_type get_redir_node_type ( t_token_type  type)
static

Convert a token type to the corresponding AST redirection node type.

Parameters
typeToken type
Returns
NODE_REDIR_... type or NODE_COMMAND (fallback)

Definition at line 23 of file parser_redir.c.

References NODE_COMMAND, NODE_REDIR_APPEND, NODE_REDIR_HEREDOC, NODE_REDIR_IN, NODE_REDIR_OUT, TOKEN_APPEND, TOKEN_HEREDOC, TOKEN_REDIR_IN, and TOKEN_REDIR_OUT.

Referenced by parse_one_redirection().

+ Here is the caller graph for this function:

◆ parse_one_redirection()

static t_ast* parse_one_redirection ( t_token **  tokens,
t_ast cmd 
)
static

Parse a single redirection token and the following WORD (filename)

Parameters
tokensPointer to current token pointer (advanced)
cmdCommand to wrap
Returns
Redirection node or NULL on error
Warning
cmd is freed on failure (do not use after NULL return)

Definition at line 44 of file parser_redir.c.

References create_redir_node(), free_ast(), get_redir_node_type(), join_connected_delim(), t_token::next, TOKEN_WORD, and t_token::type.

Referenced by parse_command().

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

◆ collect_and_merge_remaining_args()

static void collect_and_merge_remaining_args ( t_ast cmd,
t_token **  tokens 
)
static

Collect trailing WORD tokens and merge them into a command's args.

Handles cases such as "cmd arg1 > out arg2" as it collects new_args, allocates a merged array and copies old + new args, then updates cmd->data.cmd.args and argc

Parameters
cmdCommand node to extend
tokensPointer to current token pointer

Definition at line 80 of file parser_redir.c.

References t_cmd_node::argc, t_cmd_node::args, t_ast_data::cmd, collect_args(), and t_ast::data.

Referenced by parse_command().

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

◆ get_command_node()

static t_ast* get_command_node ( t_ast node)
static

Extract the innermost command from a redirection chain.

Parameters
nodeStarting node of the chain
Returns
Innermost non redirection node or NULL

Definition at line 115 of file parser_redir.c.

References t_redir_node::cmd, t_ast::data, NODE_REDIR_HEREDOC, NODE_REDIR_IN, t_ast_data::redir, and t_ast::type.

Referenced by parse_command().

+ Here is the caller graph for this function:

◆ parse_command()

t_ast* parse_command ( t_token **  tokens)

Parse a command with all its redirections.

Parses a simple command and wraps it in redirection nodes as long as redirection tokens follow. After each redirection it collects any trailing word tokens and adds them to the command's args

Parameters
tokensPointer to current token pointer (advanced)
Returns
Command AST or NULL on error
Note
It reverses the redirection chain for left to right execution

Definition at line 134 of file parser_redir.c.

References collect_and_merge_remaining_args(), get_command_node(), is_redirection(), parse_one_redirection(), parse_simple_command(), and reverse_redir_chain().

Referenced by parse_pipeline().

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

Go to the source code of this file.