-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.c
109 lines (100 loc) · 2.86 KB
/
token.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhalid <akhalid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/06 01:10:55 by akhalid #+# #+# */
/* Updated: 2022/03/03 00:35:53 by akhalid ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_token *init_token(t_type type, char *val)
{
t_token *token;
token = (t_token *)malloc(sizeof(t_token));
token->val = val;
token->type = type;
return (token);
}
t_token *token_lf(t_lexer *lexer, t_token *token)
{
lexer_forward(lexer);
return (token);
}
t_token *operator_token(t_lexer *lexer)
{
if (lexer->c == '|')
return (token_lf(lexer, init_token(PIPE, lexer_to_string(lexer))));
if (lexer->c == '>')
{
if (lexer->cmd[lexer->i + 1] == '>')
{
lexer_forward(lexer);
return (token_lf(lexer, init_token(APND, lexer_to_string(lexer))));
}
return (token_lf(lexer, init_token(OUT, lexer_to_string(lexer))));
}
if (lexer->c == '<')
{
if (lexer->cmd[lexer->i + 1] == '<')
{
lexer_forward(lexer);
return (token_lf(lexer, init_token(HRDOC, lexer_to_string(lexer))));
}
return (token_lf(lexer, init_token(INP, lexer_to_string(lexer))));
}
return (NULL);
}
t_token *quoted_wrd_token(t_lexer *lexer, char c)
{
char *val;
char *s;
val = ft_strdup("");
lexer_forward(lexer);
while (lexer->c != c && lexer->c)
{
if (lexer->c == '$' && c == '\"')
{
s = quoted_expansion(lexer);
lexer_backward(lexer);
}
else
s = lexer_to_string(lexer);
val = ft_strjoin(val, s);
lexer_forward(lexer);
}
if (lexer->c != c)
g_all.lexer_err = 1;
lexer_forward(lexer);
if (!is_operator(lexer->c) && !ft_isspace(lexer->c) && lexer->c)
val = more_wrd_token(lexer, val);
return (init_token(WRD, val));
}
t_token *unquoted_wrd_token(t_lexer *lexer)
{
char *val;
char *s;
val = ft_strdup("");
while (!is_operator(lexer->c) && !ft_isspace(lexer->c) && lexer->c)
{
if (lexer->c == '\'' || lexer->c == '\"')
{
val = ft_strjoin(val, char_quoted_wrd_token(lexer, lexer->c));
break ;
}
else if (lexer->c == '$')
{
s = unquoted_expansion(lexer);
lexer_backward(lexer);
}
else
s = lexer_to_string(lexer);
val = ft_strjoin(val, s);
lexer_forward(lexer);
}
if (!val[0])
return (free_val(val));
return (init_token(WRD, val));
}