maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
loc_env_var_handler_utils.c File Reference
#include "tokens.h"
Include dependency graph for loc_env_var_handler_utils.c:

Go to the source code of this file.

Functions

char * ev_loop (char **start, t_ms_data *data, char *str_start, char *expanded_str)
 
char * expand_variable (char **start, t_ms_data *data)
 
void handle_local_vars (t_ms_data *data, char *arg)
 
char ** ft_add_segment (char **result, char *start, size_t length, int *count)
 
char ** ft_split_preserve_quotes (char *str, char delimiter)
 

Function Documentation

◆ ev_loop()

char * ev_loop ( char **  start,
t_ms_data data,
char *  str_start,
char *  expanded_str 
)

Definition at line 15 of file loc_env_var_handler_utils.c.

17{
18 char *var_name;
19 char *var_start;
20 char *expanded_var;
21 char *tmp;
22 int len_before_var;
23
24 var_start = *start;
25 (*start)++;
26 while (**start && (ft_isalnum(**start) || **start == '_'))
27 (*start)++;
28 var_name = ft_substr(var_start, 0, *start - var_start);
29 expanded_var = expand_env_and_loc_var(var_name, data);
30 free(var_name);
31 len_before_var = var_start - str_start;
32 tmp = ft_substr(str_start, 0, len_before_var);
33 expanded_str = ft_strjoin_free(expanded_str, tmp);
34 free(tmp);
35 expanded_str = ft_strjoin_free(expanded_str, expanded_var);
36 free(expanded_var);
37 return (expanded_str);
38}
char * ft_substr(char const *s, unsigned int start, size_t len)
Definition ft_substr.c:27
int ft_isalnum(int c)
Definition ft_isalnum.c:15
char * ft_strjoin_free(char *s1, char *s2)
char * expand_env_and_loc_var(char *arg, t_ms_data *data)

References expand_env_and_loc_var(), ft_isalnum(), ft_strjoin_free(), and ft_substr().

Referenced by expand_variable().

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

◆ expand_variable()

char * expand_variable ( char **  start,
t_ms_data data 
)

Definition at line 40 of file loc_env_var_handler_utils.c.

41{
42 char *expanded_str;
43 char *str_start;
44 char *result;
45
46 result = NULL;
47 str_start = *start;
48 expanded_str = ft_strdup("");
49 while (**start && **start != '\0')
50 {
51 if (**start == '$')
52 {
53 expanded_str = ev_loop(start, data, str_start, expanded_str);
54 str_start = *start;
55 str_start = str_start_adj(str_start);
56 }
57 else
58 (*start)++;
59 }
60 if (str_start != *start)
61 {
62 result = ft_substr(str_start, 0, *start - str_start);
63 expanded_str = ft_strjoin_free(expanded_str, result);
64 free(result);
65 }
66 return (expanded_str);
67}
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
char * ev_loop(char **start, t_ms_data *data, char *str_start, char *expanded_str)
char * str_start_adj(char *arg)

References ev_loop(), ft_strdup(), ft_strjoin_free(), ft_substr(), and str_start_adj().

Referenced by process_argument(), and process_single_token().

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

◆ ft_add_segment()

char ** ft_add_segment ( char **  result,
char *  start,
size_t  length,
int *  count 
)

Definition at line 91 of file loc_env_var_handler_utils.c.

92{
93 result = realloc(result, (*count + 1) * sizeof(char *));
94 result[*count] = ft_strndup(start, length);
95 (*count)++;
96 return (result);
97}
char * ft_strndup(const char *s, size_t n)
Definition ft_strndup.c:23

References ft_strndup().

Referenced by add_segment_on_delimiter(), and ft_split_preserve_quotes().

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

◆ ft_split_preserve_quotes()

char ** ft_split_preserve_quotes ( char *  str,
char  delimiter 
)

Definition at line 99 of file loc_env_var_handler_utils.c.

100{
101 int count;
102 int in_quotes;
103 char *start;
104 char **result;
105
106 in_quotes = 0;
107 start = str;
108 result = initialize_result_array(&count);
109 if (!result)
110 return (NULL);
111 while (*str)
112 {
113 in_quotes = toggle_quotes(in_quotes, *str);
114 if (*str == delimiter && !in_quotes)
115 result = add_segment_on_delimiter(result, &start, str, &count);
116 str++;
117 }
118 if (str != start)
119 result = ft_add_segment(result, start, str - start, &count);
120 return (finalize_result_array(result, count));
121}
char ** ft_add_segment(char **result, char *start, size_t length, int *count)
int toggle_quotes(int in_quotes, char current_char)
char ** finalize_result_array(char **result, int count)
char ** initialize_result_array(int *count)
char ** add_segment_on_delimiter(char **result, char **start, char *str, int *count)

References add_segment_on_delimiter(), finalize_result_array(), ft_add_segment(), initialize_result_array(), and toggle_quotes().

Referenced by split_loc_vars().

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

◆ handle_local_vars()

void handle_local_vars ( t_ms_data data,
char *  arg 
)

Definition at line 69 of file loc_env_var_handler_utils.c.

70{
71 char *p;
72 int valid_var;
73
74 p = arg;
75 valid_var = 1;
76 if (arg[0] != '=' && ft_strchr(arg, '='))
77 {
78 while (p < ft_strchr(arg, '='))
79 {
80 if (!ft_isalnum(*p++))
81 {
82 valid_var = 0;
83 break ;
84 }
85 }
86 if (valid_var)
88 }
89}
int handle_add_set_shell_variable(t_env **shell_var, char *line)
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25
t_env * shell_variables
Definition shell.h:25

References ft_isalnum(), ft_strchr(), handle_add_set_shell_variable(), and s_ms_data::shell_variables.

Referenced by post_process_command_args().

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