-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
97 lines (89 loc) · 2.33 KB
/
parser.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: akhalid <akhalid@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/02/06 01:10:57 by akhalid #+# #+# */
/* Updated: 2022/03/03 00:36:05 by akhalid ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
t_token **realloc_tokens(t_token **tokens, t_token *tmp)
{
int i;
t_token **new;
i = 0;
if (tokens)
while (tokens[i])
i++;
new = (t_token **)malloc(sizeof(t_token *) * (i + 2));
i = 0;
if (tokens)
{
while (tokens[i])
{
new[i] = (t_token *)malloc(sizeof(t_token));
new[i]->val = ft_strdup(tokens[i]->val);
new[i]->type = tokens[i]->type;
i++;
}
}
new[i++] = tmp;
new[i] = NULL;
free_tokens(tokens);
return (new);
}
t_token *get_token(t_lexer *lexer)
{
if (lexer->c && lexer->i < lexer->length)
{
if (ft_isspace(lexer->c))
skip_spaces(lexer);
if (!is_operator(lexer->c) && lexer->c != '\'' && lexer->c != '\"')
return (unquoted_wrd_token(lexer));
if (lexer->c == '\"')
return (quoted_wrd_token(lexer, lexer->c));
if (lexer->c == '\'')
return (quoted_wrd_token(lexer, lexer->c));
return (operator_token(lexer));
}
return (0);
}
void free_tokens(t_token **tokens)
{
int i;
i = 0;
if (tokens)
{
while (tokens[i])
{
free(tokens[i]->val);
free(tokens[i]);
i++;
}
free(tokens);
}
}
void parse_commands(t_token **tokens)
{
t_command *cmd;
int i;
g_all.cmd = init_command();
cmd = g_all.cmd;
i = 0;
while (tokens[i])
{
token_to_cmd(tokens, cmd, i);
if (tokens[i]->type == INP || tokens[i]->type == OUT
|| tokens[i]->type == APND || tokens[i]->type == HRDOC)
i++;
if (tokens[i]->type == PIPE)
{
cmd->next = init_command();
cmd = cmd->next;
}
i++;
}
}