-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpander.c
executable file
·88 lines (80 loc) · 2.3 KB
/
expander.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* expander.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mazaroua <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/04/02 00:26:22 by mazaroua #+# #+# */
/* Updated: 2023/05/16 12:55:34 by mazaroua ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void var_not_found2(t_token_list *head, t_token_list *tmp, t_token_list *var)
{
while (head && head->type != NLINE)
{
if (head->next->next && head->next->next->type == DOLLAR)
{
tmp = head;
head = head->next->next->next;
if (!ft_strcmp(head->value, var->value) && var->type == head->type)
{
tmp->next = head->next;
return ;
}
}
else
head = head->next;
}
}
void var_not_found(t_token_list **tokens, t_token_list *var)
{
t_token_list *head;
t_token_list *tmp;
head = *tokens;
tmp = NULL;
if ((*tokens)->type == DOLLAR
&& !ft_strcmp((*tokens)->next->value, var->value))
{
(*tokens) = (*tokens)->next->next;
if ((*tokens)->type == SPACE)
(*tokens) = (*tokens)->next;
return ;
}
var_not_found2(head, tmp, var);
}
void expand2(t_token_list **tokens, t_env_list **env, int flag)
{
t_env_list *envlist;
t_token_list *token;
token = *tokens;
while (token && token->type != NLINE)
{
if (token && token->type == AFDOLLAR)
{
flag = 0;
envlist = *env;
while (envlist)
{
if (!ft_strcmp(token->value, envlist->name))
{
token->value = envlist->value;
flag = 1;
}
envlist = envlist->next;
}
if (flag == 0)
var_not_found(tokens, token);
}
token = token->next;
}
}
void expand(t_token_list **tokens, t_env_list **env)
{
int flag;
flag = 0;
if (!*tokens || (*tokens)->type == NLINE)
return ;
expand2(tokens, env, flag);
}