MiniDevil As beautiful as a shell
expander_utils.c File Reference

Expansion utilities: identifies variables & handles special patterns. More...

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

Detailed Description

Expansion utilities: identifies variables & handles special patterns.

Definition in file expander_utils.c.

Functions

int is_dollar_quote (t_token *token)
 Check if a token is a $ connected to a quoted token. More...
 
int is_var_char (char c)
 Check if a character is valid in a variable name. More...
 
char * extract_var_name (char *str, int *len)
 Extract a variable name from after the $ More...
 
char * append_char (char *str, char c)
 Append a single character to an allocated string. More...
 
char * append_str (char *s1, char *s2)
 Concatenates 2 strings and frees the first. More...
 

Function Documentation

◆ is_dollar_quote()

int is_dollar_quote ( t_token token)

Check if a token is a $ connected to a quoted token.

Detects the pattern: $"TOKEN_WORD" or $'TOKEN_WORD' + connected + next has quotes

Parameters
tokenToken to inspect
Returns
1 if the pattern is valid and 0 if not

Definition at line 25 of file expander_utils.c.

References t_token::connected, t_token::next, QUOTE_NONE, and t_token::value.

Referenced by expand_all_tokens().

+ Here is the caller graph for this function:

◆ is_var_char()

int is_var_char ( char  c)

Check if a character is valid in a variable name.

Parameters
cCharacter to check
Returns
1 if alphanumeric or _ and 0 otherwise

Definition at line 40 of file expander_utils.c.

Referenced by extract_var_name(), and handle_dollar_sign().

+ Here is the caller graph for this function:

◆ extract_var_name()

char* extract_var_name ( char *  str,
int *  len 
)

Extract a variable name from after the $

If the first char is a digit, it returns the only digit (matching BASH). Otherwise it scans for valid variable characters (alphanumeric and _)

Parameters
strString starting after $
lenNumber of characters consumed
Returns
Newly allocated variable name

Definition at line 55 of file expander_utils.c.

References is_var_char().

Referenced by process_dollar().

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

◆ append_char()

char* append_char ( char *  str,
char  c 
)

Append a single character to an allocated string.

Parameters
strOriginal string
cCharacter to append
Returns
New string with c appended or NULL on failure
Warning
original str is freed

Definition at line 79 of file expander_utils.c.

Referenced by expand_variables(), handle_dollar_sign(), and process_dollar().

+ Here is the caller graph for this function:

◆ append_str()

char* append_str ( char *  s1,
char *  s2 
)

Concatenates 2 strings and frees the first.

If s2 is NULL it returns s1 unchanged

Parameters
s1First string
s2Second string
Returns
Concatenated string or NULL on failure

Definition at line 113 of file expander_utils.c.

Referenced by get_append_value(), handle_special_var(), and process_dollar().

+ Here is the caller graph for this function:

Go to the source code of this file.