-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexport.c
executable file
·114 lines (105 loc) · 2.74 KB
/
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* export.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: zkarapet <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/01/15 21:39:09 by zkarapet #+# #+# */
/* Updated: 2023/03/02 16:32:16 by aivanyan ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void doer(t_env *env_cur, t_args *a, int i, t_cmd *cmd)
{
env_cur = is_in_env_or_not(a->env_lst, cmd->no_cmd[i]);
remove_from_between(env_cur, a->env_lst);
env_lst_add_last(a->env_lst, cmd->no_cmd[i]);
}
int update_return(int f, t_args *a, t_cmd *cmd, int i)
{
if (f == 2)
return (2);
if (f == 1)
export_pars(cmd->no_cmd[i], a);
return (0);
}
int ft_export(t_cmd *cmd, t_args *a)
{
int i;
int f;
int q;
char *val;
t_env *env_cur;
i = 0;
q = 0;
env_cur = NULL;
while (cmd->no_cmd[++i])
{
val = equality_out_of_quotes(cmd->no_cmd[i]);
f = export_update(cmd->no_cmd[i], val, a);
if (update_return(f, a, cmd, i) == 2)
{
q = 1;
continue ;
}
if (val)
doer(env_cur, a, i, cmd);
}
sort(a->exp_lst);
if (!cmd->no_cmd[1])
exp_lst_print(a->exp_lst);
return (q);
}
void export_pars(char *s, t_args *a)
{
char *data;
char *unquoted;
char *quoted;
char *retval;
char *duped;
data = NULL;
unquoted = equality_out_of_quotes(s);
quoted = adding_quotes(unquoted);
a->start = ft_strlen(quoted);
a->end = 0;
a->len = ft_strlen(s) - ft_strlen(unquoted);
data = ft_strjoin(s, quoted, a);
a->start = ft_strlen(data);
a->end = 0;
a->len = 11;
duped = ft_strdup("declare -x ");
retval = ft_strjoin(duped, data, a);
env_lst_add_last(a->exp_lst, retval);
free(quoted);
free(data);
free(retval);
free(duped);
}
int export_update(char *arg, char *arg_val, t_args *a)
{
t_env *cur;
a->k = 0;
cur = a->exp_lst->head->next;
while (cur->next)
{
if (error_checks_for_var(arg, until_equal_sign(arg), 1))
return (2);
if (arg_val)
{
if (!cmpfree(before(arg), before(&cur->data[11]), 1))
{
remove_from_between(cur, a->exp_lst);
return (1);
}
a->k++;
}
else
if (cmpfree(before(arg), before(&cur->data[11]), 1))
a->k++;
cur = cur->next;
}
if (a->k == a->exp_lst->size)
return (1);
return (0);
}