-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpander.c
More file actions
123 lines (113 loc) · 3.34 KB
/
expander.c
File metadata and controls
123 lines (113 loc) · 3.34 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: adurusoy <adurusoy@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/20 22:11:47 by adurusoy #+# #+# */
/* Updated: 2023/12/27 21:49:45 by adurusoy ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <stdlib.h>
#include <stdio.h>
void expand_dollar_variable(t_shell *shell, t_list *lex, char **temp,
char *before)
{
char *new_value;
if (ft_isdigit((*temp)[1]))
{
new_value = ft_strdup(*temp + 2);
expand_utils3(&shell, before, new_value, lex);
*temp = ft_strchr(lex->content + ft_strlen(before), '$');
}
else if (!ft_isdigit((*temp)[1]))
{
new_value = get_env(shell->env, *temp + 1);
free(lex->content);
lex->content = ft_strjoin(before, new_value);
if (!(char *)lex->content)
{
lex->content = ft_strdup(before);
expand_utils4(&shell, before, new_value, lex);
}
free(new_value);
*temp = ft_strchr(lex->content + ft_strlen(before), '$');
}
}
void expand_question_mark(t_shell *shell, t_list *lex, char **temp,
char *before)
{
char *after;
char *back;
char *new_value;
new_value = NULL;
after = ft_strdup(*temp + 2);
free(lex->content);
new_value = ft_itoa((shell)->exec_status);
if (!new_value)
{
free(after);
free(before);
malloc_error(4, &shell);
}
back = ft_strjoin(new_value, after);
free(new_value);
expand_utils(&shell, back, after, before);
lex->content = ft_strjoin(before, back);
expand_utils2(&shell, back, lex, before);
free(back);
*temp = ft_strchr(lex->content + ft_strlen(before), '$');
}
static void expand_tilde(t_shell *shell, t_list *lex)
{
char *tmp;
char *home;
tmp = NULL;
home = get_env(shell->env, "HOME");
if (!home)
malloc_error(4, &shell);
if (((char *)lex->content)[0] == '~' && ((char *)lex->content)[1] == '/')
tilde_utils(tmp, home, lex, shell);
else if (((char *)lex->content)[0] == '~'
&& ((char *)lex->content)[1] == '\0')
{
free(lex->content);
lex->content = home;
}
else
free(home);
}
void expand_dollar(t_shell *shell, t_list *lex, char **temp, char *before)
{
if ((*temp)[1] == '?')
expand_question_mark(shell, lex, temp, before);
else if ((*temp)[1] != '$' && ft_strcmp((*temp), "$") != 0)
expand_dollar_variable(shell, lex, temp, before);
else
*temp = ft_strchr(*temp + 1, '$');
}
void expander(t_shell *shell)
{
char *temp;
t_list *lex;
char *before;
lex = shell->lex_list;
while (lex)
{
if (((char *)lex->content)[0] == '~')
expand_tilde(shell, lex);
temp = ft_strchr(lex->content, '$');
while (temp)
{
before = ft_substr(lex->content, 0, temp - (char *)lex->content);
if (check_quote(before, temp))
expand_dollar(shell, lex, &temp, before);
else
temp = ft_strchr(temp + 1, '$');
free(before);
}
lex = lex->next;
}
}