MiniDevil As beautiful as a shell
expander.c File Reference

Variable expansion: replaces $VARIABLES and $? More...

#include "token.h"
#include "env.h"
#include "libft.h"
+ Include dependency graph for expander.c:

Detailed Description

Variable expansion: replaces $VARIABLES and $?

Definition in file expander.c.

Functions

static int handle_special_var (char *str, char **result, int exit_status)
 Handle $? expansion (last exit status) More...
 
static int process_dollar (char *str, int i, char **result, t_env *env_list)
 Process a normal $VARIABLE expansion. More...
 
static int handle_dollar_sign (char *str, char **result, t_env *env_list, int exit_status)
 Dispatch $ expansion (special vars then normal) More...
 
char * expand_variables (char *str, t_env *env_list, t_quote_type quote_type, int exit_status)
 Expand all $VARIABLE in a string. More...
 
int expand_all_tokens (t_token *tokens, t_shell *shell)
 Expand all token values before parsing. More...
 

Function Documentation

◆ handle_special_var()

static int handle_special_var ( char *  str,
char **  result,
int  exit_status 
)
static

Handle $? expansion (last exit status)

Parameters
strInput at $
resultPointer to the result string being built
exit_statusThe last exit status
Returns
2 if $? was handled (len of "$?") and 0 if not a special variable

Definition at line 25 of file expander.c.

References append_str().

Referenced by handle_dollar_sign().

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

◆ process_dollar()

static int process_dollar ( char *  str,
int  i,
char **  result,
t_env env_list 
)
static

Process a normal $VARIABLE expansion.

Extracts the variable name after $ and looks it up in the env list, then appends its value to result. A single $ appends a literal $

Parameters
strInput string
iPosition of the $ character in str
resultPointer to the result string being built
env_listEnvironment list
Returns
Number of chars consumed (1 for only $ var_name_len + 1 for $VAR)

Definition at line 51 of file expander.c.

References append_char(), append_str(), extract_var_name(), and get_env_value().

Referenced by handle_dollar_sign().

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

◆ handle_dollar_sign()

static int handle_dollar_sign ( char *  str,
char **  result,
t_env env_list,
int  exit_status 
)
static

Dispatch $ expansion (special vars then normal)

Parameters
strInput at the $ char
resultPointer to the result string being built
env_listEnvironment list
exit_statusExit status for $?
Returns
Number of characters consumed from str

Definition at line 88 of file expander.c.

References append_char(), handle_special_var(), is_var_char(), and process_dollar().

Referenced by expand_variables().

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

◆ expand_variables()

char* expand_variables ( char *  str,
t_env env_list,
t_quote_type  quote_type,
int  exit_status 
)

Expand all $VARIABLE in a string.

Parameters
strInput string
env_listEnvironment list for the variables lookups
quote_typeQuote context (QUOTE_SINGLE = no expansion)
exit_statusLast exit status for $? expansion
Returns
Newly allocated expanded string or NULL

Definition at line 111 of file expander.c.

References append_char(), handle_dollar_sign(), and QUOTE_SINGLE.

Referenced by expand_full(), and write_heredoc_line().

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

◆ expand_all_tokens()

int expand_all_tokens ( t_token tokens,
t_shell shell 
)

Expand all token values before parsing.

  • Walks the token list and expands each TOKEN_WORD
  • Single quoted tokens and heredoc delimiters (token after <<) are not exp
  • A bare $ connected to a quoted token is replaced with an empty string
Parameters
tokensToken list head
shellShell state
Returns
0 on success and -1 on allocation failure
Warning
On -1 the token list could be partially expanded

Definition at line 150 of file expander.c.

References t_shell::env, t_shell::exit_status, expand_full(), is_dollar_quote(), t_token::next, QUOTE_SINGLE, t_token::quote_type, TOKEN_HEREDOC, TOKEN_WORD, t_token::type, and t_token::value.

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.