-
Notifications
You must be signed in to change notification settings - Fork 0
/
ft_export.c
158 lines (147 loc) · 4.19 KB
/
ft_export.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fvieira <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/17 13:20:49 by fvieira #+# #+# */
/* Updated: 2023/02/17 13:20:50 by fvieira ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/*char *get_default_key(t_prompt *every, int *i, int c)
{
char *def_key;
char *new_key;
new_key = get_key(every->st_arg[c]);
while (every->new_env[*i])
{
def_key = get_key(every->new_env[*i]);
if (!ft_strcmp(def_key, new_key))
break ;
free(def_key);
def_key = NULL;
(*i)++;
}
free(new_key);
return (def_key);
}*/
// && validate_variables(var[count], 0))
char **create_new_var(t_prompt *every, int i, char *var)
{
if (!ft_strchr(var, '='))
every->new_env[i++] = ft_strdup(var);
else
{
free(every->new_env[i]);
every->new_env[i++] = formated_word(var);
}
return (every->new_env);
}
/*char **create_new_var(t_prompt *every, int i, int c)
{
char **var;
int count;
count = count_words(every->st_arg[c], " \t\n");
var = ft_alt_split(every->st_arg[c], " \t\n");
every->new_env = set_new_env2(every->new_env, every, i + count + 2);
count = 0;
while (var[count])
{
// def_key = get_default_key(every, &i, c);
if (!ft_strchr(var[count], '=')) // && validate_variables(var[count], 0))
{
printf("WORD == %s\n", var[count]);
printf("new_env[i] == %s\n", every->new_env[i]);
every->new_env[i++] = ft_strdup(var[count++]);
}
else
{
printf("WORD: %s\n", var[count]);
free(every->new_env[i]);
every->new_env[i++] = formated_word(var[count++]);
}
}
freesplit(var);
return (every->new_env);
}*/
char **manage_env_variables(t_prompt *every, int c, int i)
{
char *new_value;
char *def_key;
char **var;
int count;
while (every->new_env[i])
i++;
count = count_words(every->st_arg[c], " \t\n");
var = ft_alt_split(every->st_arg[c], " \t\n");
every->new_env = set_new_env2(every->new_env, every, i + count + 2);
count = -1;
while (var[++count])
{
i = 0;
def_key = get_default_key(every, &i, var[count]);
new_value = get_value(every->st_arg[c]);
if ((!def_key && !new_value) || new_value)
every->new_env = create_new_var(every, i, var[count]);
if (def_key)
free(def_key);
free(new_value);
}
freesplit(var);
every->exit_stat = 0;
return (every->new_env);
}
/*char **manage_env_variables(t_prompt *every, int c, int i)
{
char *def_key;
char *new_value;
def_key = get_default_key(every, &i, c);
new_value = get_value(every->st_arg[c]);
if ((!def_key && !new_value) && validate_variables(every->st_arg[c], 0))
{
every->new_env = set_new_env2(every->new_env, every, i + 1);
every->new_env[i] = ft_strdup(every->st_arg[c]);
}
else if (new_value && validate_variables(get_key(every->st_arg[c]), 1))
{
if (!every->new_env[i])
every->new_env = set_new_env2(every->new_env, every, i + 1);
else
free(every->new_env[i]);
every->new_env[i] = formated_word(every->st_arg[c]);
}
if (def_key)
free(def_key);
free(new_value);
every->exit_stat = 0;
return (every->new_env);
}*/
char **ft_export(t_prompt *e, int c)
{
int i;
i = 0;
if (e->st_arg[c][0] == '\0')
{
while (e->new_env[i])
{
c = -1;
ft_printf(e->fd, "declare -x ");
while (e->new_env[i][++c])
{
ft_putchar_fd(e->new_env[i][c], e->fd);
if ((e->new_env[i][c] == '=' && e->new_env[i][c + 1] != '"')
|| (ft_strchr(e->new_env[i], '=') && (e->new_env[i][c + 1] == 0
&& e->new_env[i][c] != '"')))
ft_putchar_fd('"', e->fd);
}
ft_putchar_fd('\n', e->fd);
i++;
}
e->exit_stat = 0;
}
else
e->new_env = manage_env_variables(e, c, 0);
return (e->new_env);
}