-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbuildin_export.c
132 lines (120 loc) · 3.31 KB
/
buildin_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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* buildin_export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ggiannit <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/10 17:51:33 by ggiannit #+# #+# */
/* Updated: 2023/04/14 09:46:55 by ggiannit ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/* nuova lista per export senza value?
* forse basta fare che se value = NULL allora stampa, senno no
*
* quando dichiara invece bisogna prima cercare se non c'e',
* se c'e' la aggiunge a env_list
* */
static void ft_export_stamp(t_mish *meta)
{
int i;
int eq;
char *tmp;
t_exenv *head;
i = -1;
while (meta->env[++i])
{
eq = ft_findchar(meta->env[i], '=');
tmp = ft_substr(meta->env[i], 0, eq);
printf("declare -x %s=\"%s\"\n", tmp, &(meta->env[i][eq + 1]));
ft_free((void **) &tmp);
}
head = meta->ext_env;
while (head)
{
if (!head->value && head->key)
printf("declare -x %s\n", head->key);
head = head->next;
}
}
static int ft_key_iglliena(t_mish *meta, char *key)
{
int i;
i = 0;
while (meta->env[i])
{
if (!ft_strncmp(meta->env[i], key, ft_strlen(key))
&& meta->env[i][ft_strlen(key)] == '=')
return (1);
i++;
}
return (0);
}
static void ft_export_solokey(t_mish *meta, t_cmd *node, int k)
{
char *tmp;
t_exenv *ino;
ino = meta->ext_env;
while (ino)
{
if (!ft_strncmp(ino->key, node->pot[k], ft_findchar(node->pot[k], '='))
&& !ft_strncmp(ino->key, node->pot[k], ft_strlen(ino->key)))
{
if (ft_envlst_statusvalue(ino, ino->key) == 1)
break ;
tmp = ft_strjoin(ino->key, "=");
tmp = ft_linejoin(tmp, ino->value, ft_strlen(ino->value));
meta->env = ft_replace_add_env(meta->env, tmp);
ft_free((void **) &(ino->key));
ft_free((void **) &(ino->value));
break ;
}
ino = ino->next;
}
if (!ino)
ft_envlst_addfront(&meta->ext_env, ft_envlst_new(node->pot[k]));
}
static void ft_export_full(t_mish *meta, t_cmd *node, int k)
{
t_exenv *ino;
meta->env = ft_replace_add_env(meta->env, ft_strdup(node->pot[k]));
ino = meta->ext_env;
while (ino)
{
if (!ft_strncmp(ino->key, node->pot[k], ft_findchar(node->pot[k], '='))
&& !ft_strncmp(ino->key, node->pot[k], ft_strlen(ino->key)))
{
ft_free((void **) &(ino->key));
ft_free((void **) &(ino->value));
break ;
}
ino = ino->next;
}
}
void ft_export(t_mish *meta, t_cmd *node)
{
int i;
int k;
if (!node->pot[1])
ft_export_stamp(meta);
else
{
k = 0;
while (node->pot[++k])
{
i = 0;
while (ft_isenv(node->pot[k][i]))
i++;
if (node->pot[k][i] == '\0' ||
(node->pot[k][i] == '=' && node->pot[k][i + 1] == '\0'))
{
if (ft_key_iglliena(meta, node->pot[k]))
continue ;
ft_export_solokey(meta, node, k);
}
else
ft_export_full(meta, node, k);
}
}
}