-
Notifications
You must be signed in to change notification settings - Fork 1
/
env_variable_pa.c
123 lines (112 loc) · 3.12 KB
/
env_variable_pa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* env_variable_pa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asfaihi <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/28 11:29:21 by asfaihi #+# #+# */
/* Updated: 2021/09/28 12:15:48 by asfaihi ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
char *variable_expander(char *key, t_list *env_lst)
{
t_env *temp;
char *expanded;
expanded = NULL;
while (env_lst != NULL)
{
temp = env_lst->content;
if (!(ft_strcmp(key, temp->key)))
expanded = ft_strdup(temp->value);
env_lst = env_lst->next;
}
return (expanded);
}
static void get_variable(char *s, t_list *env_lst, t_prs *prs)
{
int j;
char *temp;
char *tmp;
prs->var.i++;
j = prs->var.i;
if (ft_isdigit(s[prs->var.i]))
prs->var.i++;
else
while (ft_isalnum(s[prs->var.i]) || s[prs->var.i] == '_')
prs->var.i++;
tmp = ft_substr(s, j, prs->var.i - j);
temp = variable_expander(tmp, env_lst);
if (prs->outside_quote && ft_strchr(temp, ' '))
{
temp = remove_spaces(temp);
prs->extra_args = ft_split(temp, ' ');
free(temp);
temp = prs->extra_args[0];
}
free(tmp);
tmp = prs->var.result;
prs->var.result = ft_strjoin(prs->var.result, temp);
free(temp);
free(tmp);
}
static void var_edge_cases(char *s, t_prs *prs)
{
char *temp;
char *tmp;
if (s[prs->var.i] == '$' && s[prs->var.i + 1] == '?')
{
tmp = prs->var.result;
prs->var.i += 2;
prs->var.result = ft_strjoin(prs->var.result,
temp = ft_itoa(prs->ret_value));
free(temp);
free(tmp);
}
if (s[prs->var.i] == '$' && s[prs->var.i + 1] == '$')
{
tmp = prs->var.result;
prs->var.i += 2;
prs->var.result = ft_strjoin(prs->var.result, temp = ft_itoa(48532));
free(temp);
free(tmp);
}
}
static void check_var_edge_cases(char *s, t_list *env_lst, t_prs *prs)
{
char *tmp;
var_edge_cases(s, prs);
if (s[prs->var.i] == '$' && (s[prs->var.i + 1] == ' '
|| s[prs->var.i + 1] == '\0'))
{
tmp = prs->var.result;
prs->var.i++;
prs->var.result = ft_strjoin(prs->var.result, "$");
free(tmp);
}
else if (s[prs->var.i] == '$')
get_variable(s, env_lst, prs);
}
char *env_var_checker(char *s, t_list *env_lst, t_prs *prs)
{
int j;
char *temp;
char *tmp;
prs->var.result = NULL;
temp = NULL;
prs->var.i = 0;
while (s[prs->var.i])
{
j = prs->var.i;
while (s[prs->var.i] && s[prs->var.i] != '$')
prs->var.i++;
tmp = prs->var.result;
temp = ft_substr(s, j, prs->var.i - j);
prs->var.result = ft_strjoin(prs->var.result, temp);
free(temp);
free(tmp);
check_var_edge_cases(s, env_lst, prs);
}
return (prs->var.result);
}