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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* u_vars.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rbousset <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/02/14 17:19:27 by rbousset #+# #+# */
/* Updated: 2020/02/14 17:19:29 by rbousset ### ########lyon.fr */
/* */
/* ************************************************************************** */
#include <libft.h>
#include <stdlib.h>
#include <stdint.h>
#include "d_define.h"
#include "f_fail.h"
#include "s_destroy.h"
#include "s_lcom.h"
#include "s_struct.h"
static char
*set_rva(const char varname[],
t_msh *msh)
{
char *rvarname;
if (!(rvarname = (char*)malloc((ft_strlen(varname) + 1) * sizeof(char))))
{
lcom_clear(&msh->curr);
s_destroy(msh);
fail_alloc(msh);
}
ft_memcpy((char*)rvarname, (const char*)varname + 1,
ft_strlen(varname + 1));
*(rvarname + ft_strlen(varname + 1)) = '=';
*(rvarname + ft_strlen(varname + 1) + 1) = '\0';
return (rvarname);
}
static char
*dup_env(char *p_env,
char *rvarname,
t_msh *msh)
{
char *varval;
if (!(varval = ft_strdup(p_env)))
{
ft_memdel((void*)&rvarname);
lcom_clear(&msh->curr);
s_destroy(msh);
fail_alloc(msh);
}
ft_memdel((void*)&rvarname);
return (varval);
}
static char
*get_frm_env(char rvarname[],
t_msh *msh)
{
char **p_env;
char *pp_env;
char *varval;
p_env = msh->envp;
while (*p_env)
{
if (!ft_strncmp(rvarname, *p_env, ft_strlen(rvarname)))
{
pp_env = *p_env;
while (*pp_env != '\0' && *pp_env != '=')
pp_env += 1;
if (*pp_env == '=')
pp_env += 1;
varval = dup_env(pp_env, rvarname, msh);
return (varval);
}
p_env += 1;
}
return (NULL);
}
static char
*get_special_var(const char varname[],
t_msh *msh)
{
char *varval;
if (!ft_strncmp(varname, FT_RET_VAR, 3))
{
if (!(varval = ft_uitoa(msh->ret)))
{
lcom_clear(&msh->curr);
s_destroy(msh);
fail_alloc(msh);
}
return (varval);
}
else if (!ft_strncmp(varname, FT_ZER_VAR, 3))
{
if (!(varval = ft_strdup(msh->shname)))
{
lcom_clear(&msh->curr);
s_destroy(msh);
fail_alloc(msh);
}
return (varval);
}
return (NULL);
}
static char
*get_cstm_vr(const char varname[],
t_msh *msh)
{
t_lvars *ptr;
char *varval;
ptr = msh->vars;
while (ptr != NULL &&
ft_strncmp(varname + 1, ptr->name, ft_strlen(varname + 1)) != 0)
{
ptr = ptr->next;
}
if (ptr != NULL)
{
if (!(varval = ft_strdup(ptr->val)))
{
lcom_clear(&msh->curr);
s_destroy(msh);
fail_alloc(msh);
}
return (varval);
}
else
{
return (NULL);
}
}
/*
** char*
** u_get_var_value(const char varname[], t_msh *msh);
**
** DESCRIPTION
** The u_get_var_value() function returns
** a heap-allocated, null-terminated string
** that may later be free'd containing the
** value of the variable varname[] including
** the '$' prefix. NULL is returned if varname[]
** wasn't found.
*/
char
*u_get_var_value(const char varname[],
t_msh *msh)
{
/* TODO: check behaviour on empty vars -> "QWE=" */
/* TODO: add support for global variables -> "$hey $nigga..." */
char *varval;
char *rvarname;
varval = NULL;
if ((varval = get_special_var(varname, msh)) != NULL)
{
return (varval);
}
else if ((varval = get_cstm_vr(varname, msh)) != NULL)
{
return (varval);
}
else
{
rvarname = set_rva(varname, msh);
varval = get_frm_env(rvarname, msh);
return (varval);
}
return (NULL);
}
|