-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtokens.c
101 lines (90 loc) · 2.14 KB
/
tokens.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* tokens.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: burkaya <[email protected] +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/03/01 14:28:31 by egumus #+# #+# */
/* Updated: 2024/04/05 00:26:25 by burkaya ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
void ft_add_token(t_state *s, char *token, int type, int index)
{
t_token *new;
if (!s->tokens)
return ;
new = ft_create_token(token, type);
if (!new)
return ;
if (!s->tokens[index])
s->tokens[index] = new;
else
{
new->prev = ft_get_last_token(s->tokens[index]);
ft_get_last_token(s->tokens[index])->next = new;
}
}
t_token *ft_create_token(char *value, int type)
{
t_token *new;
new = (t_token *)malloc(sizeof(t_token));
if (!new)
return (NULL);
new->value = ft_strdup(value, NULL);
new->type = type;
new->remove = 1;
new->next = NULL;
new->prev = NULL;
return (new);
}
t_token *ft_get_last_token(t_token *token)
{
t_token *tmp;
tmp = token;
while (tmp->next)
tmp = tmp->next;
return (tmp);
}
void ft_free_tokens(t_token **token)
{
t_token **tmp;
t_token *next;
t_token *tmp2;
int i;
if (!token || !*token)
return ;
tmp = token;
i = 0;
while (tmp[i])
{
next = tmp[i];
while (next)
{
tmp2 = next;
next = next->next;
free(tmp2->value);
free(tmp2);
}
i++;
}
}
void ft_init_prev_tokens(t_token **tokens)
{
t_token **tmp;
t_token *next;
int i;
tmp = tokens;
i = 0;
while (tmp[i])
{
next = tmp[i];
while (next->next)
{
next->next->prev = next;
next = next->next;
}
i++;
}
}