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

Go to the source code of this file.

Functions

char * expand_env_and_loc_var (char *arg, t_ms_data *data)
 
void final_quote_removal (int arg_count, t_ast *command_node)
 
char * get_env_variable (char *arg, t_ms_data *data)
 
void handle_trailing_quote (char *arg)
 
char * get_exit_status (t_ms_data *data)
 

Function Documentation

◆ expand_env_and_loc_var()

char * expand_env_and_loc_var ( char *  arg,
t_ms_data data 
)

Definition at line 45 of file loc_env_var_handler_utils_utils.c.

46{
47 char *env_value_dup;
48
49 arg = exit_status_adj(arg);
50 if (ft_strcmp(arg, "$?") == 0)
51 return (get_exit_status(data));
52 else if (arg[0] == '$')
53 {
55 env_value_dup = get_env_variable(arg, data);
56 return (env_value_dup);
57 }
58 return (ft_strdup(""));
59}
int ft_strcmp(const char *s1, const char *s2)
Definition ft_strcmp.c:24
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
char * get_env_variable(char *arg, t_ms_data *data)
void handle_trailing_quote(char *arg)
char * get_exit_status(t_ms_data *data)
char * exit_status_adj(char *arg)

References exit_status_adj(), ft_strcmp(), ft_strdup(), get_env_variable(), get_exit_status(), and handle_trailing_quote().

Referenced by ev_loop().

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

◆ final_quote_removal()

void final_quote_removal ( int  arg_count,
t_ast command_node 
)

Definition at line 21 of file loc_env_var_handler_utils_utils.c.

22{
23 int i;
24 size_t len;
25 char *arg;
26 char *trimmed_arg;
27
28 i = 0;
29 ft_print_2d_arr(command_node->args, "final-quote_removal");
30 while (i < arg_count)
31 {
32 arg = command_node->args[i];
33 len = ft_strlen(arg);
34 if ((arg[0] == '"' && arg[len - 1] == '"') || (arg[0] == '\''
35 && arg[len - 1] == '\''))
36 {
37 trimmed_arg = ft_strndup(arg + 1, len - 2);
38 free(command_node->args[i]);
39 command_node->args[i] = trimmed_arg;
40 }
41 i++;
42 }
43}
char * ft_strndup(const char *s, size_t n)
Definition ft_strndup.c:23
void ft_print_2d_arr(char **arr, char *name)
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15
char ** args
Definition tokens.h:52

References s_ast::args, ft_print_2d_arr(), ft_strlen(), and ft_strndup().

Here is the call graph for this function:

◆ get_env_variable()

char * get_env_variable ( char *  arg,
t_ms_data data 
)

Definition at line 77 of file loc_env_var_handler_utils_utils.c.

78{
79 char *env_value;
80
81 env_value = get_env(data->envp, arg + 1);
82 if (!env_value)
83 env_value = get_env(data->shell_variables, arg + 1);
84 if (env_value)
85 return (ft_strdup(env_value));
86 return (ft_strdup(""));
87}
char * get_env(t_env *envp, const char *key)
Definition env.c:59
t_env * envp
Definition shell.h:24
t_env * shell_variables
Definition shell.h:25

References s_ms_data::envp, ft_strdup(), get_env(), and s_ms_data::shell_variables.

Referenced by expand_env_and_loc_var().

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

◆ get_exit_status()

char * get_exit_status ( t_ms_data data)

Definition at line 61 of file loc_env_var_handler_utils_utils.c.

62{
63 char *exit_status;
64 char *exit_status_dup;
65
66 exit_status = get_shell_variable(data->shell_variables, "?");
67 exit_status_dup = ft_strdup(exit_status);
68 return (exit_status_dup);
69}
char * get_shell_variable(t_env *shell_var, const char *key)

References ft_strdup(), get_shell_variable(), and s_ms_data::shell_variables.

Referenced by expand_env_and_loc_var().

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

◆ handle_trailing_quote()

void handle_trailing_quote ( char *  arg)

Definition at line 71 of file loc_env_var_handler_utils_utils.c.

72{
73 if (arg[ft_strlen(arg) - 1] == '"')
74 arg[ft_strlen(arg) - 1] = '\0';
75}

References ft_strlen().

Referenced by expand_env_and_loc_var().

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