maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
shell_variables.c File Reference
#include "shell.h"
#include "env.h"
#include "libft.h"
#include "tokens.h"
#include <stddef.h>
Include dependency graph for shell_variables.c:

Go to the source code of this file.

Functions

int handle_add_set_shell_variable (t_env **shell_var, char *line)
 
int handle_get_shell_variable (t_ms_data *data, const char *key)
 
int handle_shell_variable (t_ast *node, t_ms_data *data)
 
void shell_variable_update (t_ms_data *data, int status)
 

Function Documentation

◆ handle_add_set_shell_variable()

int handle_add_set_shell_variable ( t_env **  shell_var,
char *  line 
)

Definition at line 24 of file shell_variables.c.

25{
26 char *key;
27 char *value;
28 char *nq_value;
29
30 key = NULL;
31 value = NULL;
32 nq_value = NULL;
33 if (*shell_var == NULL)
34 add_shell_var_node(shell_var, line);
35 else
36 {
37 key = ft_strcdup(line, '=');
38 value = ft_strchr(line, '=') + 1;
39 nq_value = ft_remove_quotes(value, '\"');
40 set_shell_var(shell_var, key, nq_value);
41 free(nq_value);
42 free(key);
43 }
44 return (0);
45}
void set_shell_var(t_env **shell_var, const char *key, const char *value)
char * ft_strcdup(const char *s, int c)
Definition utils.c:52
void add_shell_var_node(t_env **shell_var, const char *line)
char * ft_remove_quotes(char *str, char quote_type)
char * ft_strchr(const char *s, int c)
Definition ft_strchr.c:25

References add_shell_var_node(), ft_remove_quotes(), ft_strcdup(), ft_strchr(), and set_shell_var().

Referenced by handle_local_vars(), and init_ms_data().

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

◆ handle_get_shell_variable()

int handle_get_shell_variable ( t_ms_data data,
const char *  key 
)

Definition at line 47 of file shell_variables.c.

48{
49 char *value;
50 t_env *shell_var;
51
52 shell_var = data->shell_variables;
53 value = get_env(shell_var, key);
54 if (value)
55 {
56 ft_putstr_fd(value, data->std_out);
57 ft_putstr_fd("\n", data->std_out);
58 free(value);
59 return (0);
60 }
61 return (-1);
62}
char * get_env(t_env *envp, const char *key)
Definition env.c:59
void ft_putstr_fd(char *s, int fd)
Definition env.h:17
int std_out
Definition shell.h:27
t_env * shell_variables
Definition shell.h:25

References ft_putstr_fd(), get_env(), s_ms_data::shell_variables, and s_ms_data::std_out.

Referenced by handle_shell_variable().

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

◆ handle_shell_variable()

int handle_shell_variable ( t_ast node,
t_ms_data data 
)

Definition at line 64 of file shell_variables.c.

65{
66 if (node->left && node->right == NULL)
67 return (handle_get_shell_variable(data, node->args[0]));
68 return (-1);
69}
int handle_get_shell_variable(t_ms_data *data, const char *key)
char ** args
Definition tokens.h:52
struct s_ast * right
Definition tokens.h:54
struct s_ast * left
Definition tokens.h:53

References s_ast::args, handle_get_shell_variable(), s_ast::left, and s_ast::right.

Here is the call graph for this function:

◆ shell_variable_update()

void shell_variable_update ( t_ms_data data,
int  status 
)

Definition at line 71 of file shell_variables.c.

72{
73 char *status_str;
74
75 data->exit_status = status;
76 status_str = ft_itoa(status);
77 set_shell_var(&data->shell_variables, "?", status_str);
78 free(status_str);
79}
char * ft_itoa(int n)
Definition ft_itoa.c:40
int exit_status
Definition shell.h:30

References s_ms_data::exit_status, ft_itoa(), set_shell_var(), and s_ms_data::shell_variables.

Here is the call graph for this function: