maxishell
Implementation of a shell for Linux-like systems
Loading...
Searching...
No Matches
loc_env_var_handler.c
Go to the documentation of this file.
1/* ************************************************************************** */
2/* */
3/* ::: :::::::: */
4/* loc_env_var_handler.c :+: :+: :+: */
5/* +:+ +:+ +:+ */
6/* By: rmikhayl <marvin@42.fr> +#+ +:+ +#+ */
7/* +#+#+#+#+#+ +#+ */
8/* Created: 2024/09/04 16:46:53 by rmikhayl #+# #+# */
9/* Updated: 2024/09/09 16:19:56 by dmdemirk ### ########.fr */
10/* */
11/* ************************************************************************** */
12
13#include <tokens.h>
14#include "libft.h"
15
16void handle_local_vars(t_ms_data *data, char *arg);
17char *process_argument(char *arg, t_ms_data *data);
18
19void handle_split_allocation(t_ast *command_node, int *current_size, \
20 int required_size)
21{
22 if (required_size > *current_size)
23 {
24 command_node->args = ft_realloc_array(command_node->args, \
25 *current_size, required_size);
26 *current_size = required_size;
27 }
28}
29
30void split_loc_vars(t_ast *command_node, char *processed_arg, \
31 int *current_size, int *i)
32{
33 char **split_arg;
34 int split_count;
35 int required_size;
36 int j;
37
38 split_arg = ft_split_preserve_quotes(processed_arg, ' ');
39 split_count = ft_len_2d_arr(split_arg);
40 if (split_count > 1)
41 {
42 required_size = *i + split_count;
43 handle_split_allocation(command_node, current_size, required_size);
44 j = 0;
45 while (j < split_count)
46 {
47 command_node->args[*i + j] = ft_strdup(split_arg[j]);
48 j++;
49 }
50 *i += split_count;
51 }
52 else
53 {
54 command_node->args[*i] = ft_strdup(processed_arg);
55 (*i)++;
56 }
57 ft_free_2d_arr(split_arg);
58}
59
60void post_process_command_args(t_ast *command_node, int arg_count, \
61 t_ms_data *data)
62{
63 int i;
64 char *processed_arg;
65 int current_size;
66 char *tmp_proc_arg;
67
68 i = 0;
69 tmp_proc_arg = NULL;
70 current_size = arg_count;
71 while (i < current_size)
72 {
73 if (!is_in_single_quotes(command_node->args[i]))
74 {
75 handle_local_vars(data, command_node->args[i]);
76 tmp_proc_arg = process_argument(command_node->args[i], data);
77 }
78 else
79 tmp_proc_arg = ft_substr(command_node->args[i], 1, \
80 ft_strlen(command_node->args[i]) - 2);
81 free(command_node->args[i]);
82 processed_arg = ft_remove_quotes(tmp_proc_arg, '\"');
83 free(tmp_proc_arg);
84 split_loc_vars(command_node, processed_arg, &current_size, &i);
85 free(processed_arg);
86 }
87 command_node->args[current_size] = NULL;
88}
89
90char *append_expanded_var(char *processed_arg, char *tmp_ad, \
91 char *expanded_var)
92{
93 char *tmp;
94 char *tmp_2;
95
96 tmp = ft_strjoin_free(processed_arg, expanded_var);
97 tmp_ad = tmp_adj(tmp);
98 free(expanded_var);
99 tmp_2 = ft_strdup(tmp_ad);
100 free(tmp_ad);
101 return (tmp_2);
102}
103
104char *process_argument(char *arg, t_ms_data *data)
105{
106 char *start;
107 char *processed_arg;
108 char *tmp_ad;
109 char *expanded_var;
110
111 tmp_ad = NULL;
112 processed_arg = ft_strdup("");
113 start = arg;
114 while (*start != '\0')
115 {
116 if (*start == '$')
117 {
118 expanded_var = expand_variable(&start, data);
119 if (expanded_var != NULL)
120 {
121 processed_arg = append_expanded_var(processed_arg, \
122 tmp_ad, expanded_var);
123 free(tmp_ad);
124 }
125 }
126 else
127 processed_arg = append_literal(&start, processed_arg);
128 }
129 return (processed_arg);
130}
void ft_free_2d_arr(char **arr)
char ** ft_realloc_array(char **array, int old_size, int new_size)
char * ft_substr(char const *s, unsigned int start, size_t len)
Definition ft_substr.c:27
char * ft_remove_quotes(char *str, char quote_type)
char * ft_strjoin_free(char *s1, char *s2)
size_t ft_strlen(const char *s)
Definition ft_strlen.c:15
char * ft_strdup(const char *s)
Definition ft_strdup.c:23
int ft_len_2d_arr(char **array)
void handle_split_allocation(t_ast *command_node, int *current_size, int required_size)
void handle_local_vars(t_ms_data *data, char *arg)
void split_loc_vars(t_ast *command_node, char *processed_arg, int *current_size, int *i)
char * append_expanded_var(char *processed_arg, char *tmp_ad, char *expanded_var)
void post_process_command_args(t_ast *command_node, int arg_count, t_ms_data *data)
char * process_argument(char *arg, t_ms_data *data)
Definition tokens.h:50
char ** args
Definition tokens.h:52
char ** ft_split_preserve_quotes(char *str, char delimiter)
char * append_literal(char **start, char *processed_arg)
char * expand_variable(char **start, t_ms_data *data)
char * tmp_adj(char *arg)
int is_in_single_quotes(char *arg)