-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpansions_vars_0.c
125 lines (115 loc) · 3.08 KB
/
expansions_vars_0.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expansions_vars_0.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ngasco <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/14 11:30:17 by ngasco #+# #+# */
/* Updated: 2022/03/14 11:30:18 by ngasco ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
/* 1/4 Iterate list and expand as long as there are variables to expand.
Comes in handy in cases like $USER$PATH */
void ft_expand_variables(t_cdata *t_cdata)
{
while (1)
{
if (!ft_found_variable_to_expand(t_cdata))
break ;
}
}
/* 2/4 Linked list is iterated to expand variables */
int ft_found_variable_to_expand(t_cdata *t_cdata)
{
struct s_qnode *curr;
int found_var;
found_var = 0;
curr = t_cdata->t_qdata->quotes_list;
while (1)
{
if (curr->q_type != '\'')
{
if (ft_find_dollar(curr->str) && t_cdata->found_here_doc == 0)
{
curr->str = ft_add_variable_value(curr->str, t_cdata);
curr->length = ft_strlen(curr->str);
found_var = 1;
}
}
if (curr->next == NULL)
break ;
else
curr = curr->next;
}
return (found_var);
}
/* 2a Check if there's at least one $ */
int ft_find_dollar(char *str)
{
int i;
i = 0;
while (str[i + 1] != '\0')
{
if (str[i] == '$' && str[i + 1] != '\0')
return (1);
i++;
}
return (0);
}
/* 3/4 Outside quotes, variables are expanded to their value */
char *ft_add_variable_value(char *str, t_cdata *t_cdata)
{
int i;
int start;
i = 0;
start = 0;
while (str[i] != '\0')
{
if ((i == 0 && str[i] == '$') || str[i] == '$')
{
start = i;
i++;
if (str[i] == '?')
i++;
else
{
while ((str[i] >= 'A' && str[i] <= 'Z')
|| (str[i] >= 'a' && str[i] <= 'z')
|| (str[i] >= '0' && str[i] <= '9') || (str[i] == '_'))
i++;
}
break ;
}
i++;
}
return (ft_expand_var_value(t_cdata, str, start, i));
}
/* 4/4 Splice variable value in the correct spot inside of string */
char *ft_expand_var_value(t_cdata *t_cdata, char *str, int start, int end)
{
char *var_name;
char *var_value;
char *result;
if (str[start + 1] == '?')
{
var_value = ft_itoa(g_ex_status);
result = ft_splice_var_value(str, var_value, start, end);
free(str);
free(var_value);
}
else
{
var_name = ft_get_var_name(str, start + 1, end);
var_value = ft_get_env(var_name, t_cdata->envp);
if (var_value == NULL)
result = ft_remove_var_name(str, start + 1, end);
else
result = ft_splice_var_value(str, var_value, start, end);
free(str);
free(var_name);
free(var_value);
}
return (result);
}