-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoken_list.c
90 lines (82 loc) · 2.5 KB
/
token_list.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token_list.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: almelo <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/02/19 13:40:12 by almelo #+# #+# */
/* Updated: 2023/03/22 19:28:05 by almelo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static int is_operator(int c)
{
return (c == '|' || c == '<' || c == '>');
}
static int is_metachar(int c)
{
return (ft_isspace(c) || is_operator(c));
}
static enum e_label get_label(char *input, size_t i)
{
if (input[i] == '|')
return (PIPE);
else if (input[i] == '<')
{
if (input[i + 1] == '<')
return (HEREDOC);
else
return (IN);
}
else if (input[i] == '>')
{
if (input[i + 1] == '>')
return (APPEND);
else
return (OUT);
}
else
return (WORD);
}
static enum e_bool get_quote_state(char *input, int i, enum e_bool is_quoted)
{
static char quote;
if ((input[i] == '\"' || input[i] == '\'') && !is_quoted)
{
quote = input[i];
is_quoted = TRUE;
}
else if (input[i] == quote
&& (is_metachar(input[i + 1]) || input[i + 1] == '\0')
&& is_quoted)
is_quoted = FALSE;
return (is_quoted);
}
void tokenize_input(t_tokenl *token_lst, char *input, t_lexer_state *state)
{
size_t i;
token_lst->head = NULL;
i = 0;
while (i <= ft_strlen(input))
{
state->is_quoted = get_quote_state(input, i, state->is_quoted);
if ((is_metachar(input[i]) || input[i] == '\0')
&& (state->is_word && !state->is_quoted))
{
state->input[i] = '\0';
queue_token(token_lst, new_token(state->input + state->curr, WORD));
if (is_operator(input[i]))
queue_token(token_lst, new_token(NULL, get_label(input, i)));
state->is_word = FALSE;
}
else if (is_operator(input[i]) && !state->is_word)
queue_token(token_lst, new_token(NULL, get_label(input, i)));
if (!(is_metachar(input[i])) && !state->is_word && input[i] != '\0')
{
state->is_word = TRUE;
state->curr = i;
}
i++;
}
}