-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer_utils.c
112 lines (102 loc) · 2.87 KB
/
lexer_utils.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
106
107
108
109
110
111
112
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* lexer_utils.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: ulyildiz <ulyildiz@student.42kocaeli.com.t +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 11:33:23 by ulyildiz #+# #+# */
/* Updated: 2024/05/23 11:33:23 by ulyildiz ### ########.fr */
/* */
/* ************************************************************************** */
#include "functions.h"
#include "42-libft/libft.h"
#include <stdlib.h>
static t_token_types identify_t(const char *arr)
{
if (ft_strlen(arr) == 1 && *arr == '|')
return (PIPE);
else if (ft_strlen(arr) == 1 && *arr == '<' )
return (RDR_OUT);
else if (ft_strlen(arr) == 2 && ft_strnstr(arr, "<<", 2))
return (HEREDOC);
else if (ft_strlen(arr) == 2 && ft_strnstr(arr, ">>", 2))
return (RDR_D_IN);
else if (ft_strlen(arr) == 1 && *arr == '>')
return (RDR_IN);
else if (*arr == '\'')
return (QUOTE);
else if (*arr == '\"')
return (D_QUOTE);
return (CMD);
}
static int token_add_back(t_tokens **list, t_tokens *new)
{
t_tokens *tmp;
tmp = *list;
if (!new)
return (0);
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new;
return (1);
}
void is_expendable(t_tokens *lst)
{
t_tokens *tmp;
tmp = lst;
while (tmp)
{
while (tmp && (tmp->type != D_QUOTE && tmp->type != QUOTE))
tmp = tmp->next;
if (tmp && tmp->type == D_QUOTE && tmp->is_expend == NONE)
{
lst = tmp->next;
tmp = tmp->next;
while (tmp && tmp->type != D_QUOTE)
tmp = tmp->next;
while (tmp && lst && lst != tmp)
{
lst->is_expend = WITHIN_D_Q;
lst = lst->next;
}
}
else if (tmp && tmp->type == QUOTE && tmp->is_expend == NONE)
{
lst = tmp->next;
tmp = tmp->next;
while (tmp && tmp->type != QUOTE)
tmp = tmp->next;
while (tmp && lst && lst != tmp)
{
lst->is_expend = WITHIN_Q;
lst = lst->next;
}
}
if (tmp)
tmp = tmp->next;
}
}
static t_tokens *create_token(char *arr)
{
t_tokens *new;
new = (t_tokens *)ft_calloc(1, sizeof(t_tokens));
if (!new)
return (NULL);
new->value = ft_strdup(arr);
new->is_expend = NONE;
new->type = identify_t(arr);
return (new);
}
int tlist(t_tokens **head, char *arr)
{
t_tokens *new;
new = create_token(arr);
if (!new)
return (0);
if (!*head)
*head = new;
else
token_add_back(head, new);
return (1);
}