MiniDevil As beautiful as a shell
parser_cmd.c File Reference

Command parsing: builds command nodes from argument tokens. More...

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

Detailed Description

Command parsing: builds command nodes from argument tokens.

Definition in file parser_cmd.c.

Functions

int count_word_tokens (t_token *tokens)
 Count consecutive word token groups. More...
 
static int fill_args_array (char **args, t_token **tokens, int count)
 Fill argument array from consecutive word tokens. More...
 
char ** collect_args (t_token **tokens, int *argc)
 Collect consecutive word tokens into an arguments array. More...
 
t_astparse_simple_command (t_token **tokens)
 Parse a simple command from consecutive word tokens. More...
 

Function Documentation

◆ count_word_tokens()

int count_word_tokens ( t_token tokens)

Count consecutive word token groups.

Connected tokens count as 1 group

Parameters
tokensToken list starting position
Returns
Number of word groups

Definition at line 25 of file parser_cmd.c.

References t_token::connected, t_token::next, TOKEN_WORD, and t_token::type.

Referenced by collect_args().

+ Here is the caller graph for this function:

◆ fill_args_array()

static int fill_args_array ( char **  args,
t_token **  tokens,
int  count 
)
static

Fill argument array from consecutive word tokens.

Concatenates the connected tokens via ft_strjoin and advances the token pointer past all the consumed tokens

Parameters
argsPre allocated array of size count + 1
tokensPointer to current token pointer
countNumber of word groups to collect
Returns
1 on success and 0 if failed midway (so that we can free what was partially allocated)

Definition at line 53 of file parser_cmd.c.

References t_token::next.

Referenced by collect_args().

+ Here is the caller graph for this function:

◆ collect_args()

char** collect_args ( t_token **  tokens,
int *  argc 
)

Collect consecutive word tokens into an arguments array.

Parameters
tokensPointer to current token pointer
argcNumber of arguments collected
Returns
NULL terminated arguments array or NULL if none

Definition at line 88 of file parser_cmd.c.

References count_word_tokens(), and fill_args_array().

Referenced by collect_and_merge_remaining_args(), and parse_simple_command().

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

◆ parse_simple_command()

t_ast* parse_simple_command ( t_token **  tokens)

Parse a simple command from consecutive word tokens.

Collects the arguments via collect_args() and wraps them in a NODE_COMMAND AST node

Parameters
tokensPointer to current token pointer
Returns
NODE_COMMAND AST node or NULL if no words were found

Definition at line 117 of file parser_cmd.c.

References collect_args(), and create_cmd_node().

Referenced by parse_command().

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

Go to the source code of this file.