-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplace_dollar.c
102 lines (92 loc) · 2.73 KB
/
replace_dollar.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* replace_dollar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rdanyell <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/06/01 16:18:54 by rdanyell #+# #+# */
/* Updated: 2022/06/16 16:49:53 by rdanyell ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *subst_dollar(char *com, t_envp *envp_list)
{
while (envp_list)
{
if (!ft_strncmp(com, envp_list->key, 1000))
return (envp_list->value);
envp_list = envp_list->next;
}
return ("");
}
void change_dollar1(t_dollar *doll, char *str)
{
char *tmp_str;
doll->rez = ft_strjoin(doll->head, doll->com);
tmp_str = doll->rez;
doll->rez = ft_strjoin(doll->rez, doll->tail);
str = doll->rez;
ft_free(tmp_str);
ft_free(doll->head);
ft_free(doll->tail);
}
char *chng_doll(t_dollar *doll, char *str, int *num, t_envp *envp_list)
{
int n;
int i;
t_envp *tmp;
char *tmp_str;
n = *num;
doll->rez = ft_substr(str, 0, ft_strlen(str));
tmp = envp_list;
tmp_str = doll->rez;
doll->head = ft_substr(doll->rez, 0, n);
ft_free(tmp_str);
i = n;
while (str[i] && ft_isalnum(str[i + 2]))
i++;
doll->com = ft_substr(str, n + 1, i - n + 1);
doll->tail = ft_substr(str, i + 2, ft_strlen(str));
tmp_str = doll->com;
doll->com = subst_dollar(doll->com, envp_list);
ft_free(tmp_str);
envp_list = tmp;
change_dollar1(doll, str);
*num = 0;
return (doll->rez);
}
void repl_doll(t_dollar *doll, int *ind, char **str, t_envp *envp_list)
{
int i;
i = *ind;
if ((*str)[i + 1] && (*str)[i + 1] != '?')
{
doll->tmp = *str;
*str = chng_doll(doll, *str, &i, envp_list);
ft_free(doll->tmp);
doll->count_one = 0;
doll->count_double = 0;
}
}
int replace_dollar(char **str, t_envp *envp_list)
{
t_dollar *doll;
int i;
i = 0;
doll = init_doll();
while ((*str)[i])
{
if ((*str)[i] == '<' && ((*str)[i + 1] && (*str)[i + 1] == '<'))
dollar_redirect(*str, &i);
if ((*str)[i] == '$' && doll->count_one % 2 == 0)
repl_doll(doll, &i, str, envp_list);
if ((*str)[i] == '\'' && doll->count_double % 2 == 0)
doll->count_one++;
if ((*str)[i] == '\"' && doll->count_one % 2 == 0)
doll->count_double++;
i++;
}
free(doll);
return (0);
}