-
Notifications
You must be signed in to change notification settings - Fork 0
/
dollar.c
105 lines (96 loc) · 2.9 KB
/
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
103
104
105
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dollar.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ebalgruu <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/11/27 01:39:23 by ebalgruu #+# #+# */
/* Updated: 2021/11/27 01:39:25 by ebalgruu ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
char *ft_dollar(char *str, int *i, t_env *envp, t_arg *arg)
{
int j;
char *tmp;
t_env *env;
j = *i;
(*i)++;
while (str[*i] != '\0' && str[*i] != ' ' && str[*i] != '|'
&& str[*i] != '$' && str[*i] != '<' && str[*i] != '>'
&& str[*i] != '\'' && str[*i] != '\"')
(*i)++;
tmp = ft_substr_pars(str, j + 1, *i - j - 1, arg);
env = envp;
while (env)
{
if (!ft_strcmp(tmp, env->key))
{
free(tmp);
tmp = ft_strdup_pars(env->value, arg);
return (tmp);
}
env = env->next;
}
return (ft_strdup_pars("", arg));
}
static char *if_question(char *str, int *i, t_env *env, t_arg *arg)
{
char *dollar;
char *tmp;
char *tmp2;
int j;
(void)env;
j = *i + 1;
tmp2 = ft_substr_pars(str, 0, *i, arg);
dollar = ft_itoa_pars(arg->errnum, arg);
j++;
tmp = ft_strjoin_pars(tmp2, dollar, arg);
*i = (int)ft_strlen(tmp);
free(tmp2);
tmp2 = ft_substr_pars(str, j, ft_strlen(str) - j, arg);
free(str);
str = ft_strjoin_pars(tmp, tmp2, arg);
ft_free(&tmp, &tmp2, &dollar);
return (str);
}
static char *if_not_question(char *str, int *i, t_env *env, t_arg *arg)
{
char *dollar;
char *tmp;
char *tmp2;
int j;
j = *i + 1;
tmp2 = ft_substr_pars(str, 0, *i, arg);
while (str[j] && str[j] != ' ' && str[j] != '<' && str[j] != '>'
&& str[j] != '|' && str[j] != '\''
&& str[j] != '\"' && str[j] != '$')
j++;
dollar = ft_dollar(str, i, env, arg);
tmp = ft_strjoin_pars(tmp2, dollar, arg);
*i = (int)ft_strlen(tmp);
free(tmp2);
tmp2 = ft_substr_pars(str, j, ft_strlen(str) - j, arg);
free(str);
str = ft_strjoin_pars(tmp, tmp2, arg);
ft_free(&tmp, &tmp2, &dollar);
return (str);
}
char *ft_check_dollar(char *tmp, int *i, t_env *env, t_arg *arg)
{
int j;
char *str;
str = ft_strdup_pars(tmp, arg);
free(tmp);
j = *i + 1;
if (str[j] == '?')
str = if_question(str, i, env, arg);
else if (str[j] && str[j] != ' ' && str[j] != '<'
&& str[j] != '>' && str[j] != '$'
&& str[j] != '|' && str[j] != '\'' && str[j] != '\"')
str = if_not_question(str, i, env, arg);
else
(*i)++;
return (str);
}