-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathtoken.c
76 lines (68 loc) · 1.95 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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* token.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bmachado <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/10/22 11:24:49 by bmachado #+# #+# */
/* Updated: 2022/02/23 11:28:42 by bmachado ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static void free_old_input(char *input)
{
if (!ft_isfilled(input))
return ;
free(input);
}
static void clear_token_error(t_token **token_lst)
{
if (!token_lst)
return ;
clear_token_lst(token_lst);
}
static int define_iter(char *input, int i)
{
int iter;
iter = 0;
if (is_redir(input, i) == D_GREAT || is_redir(input, i) == D_LESS)
iter += 1;
if (is_quote(input[i]))
iter += quoted_len(input, i);
iter++;
return (iter);
}
static void recursion_token(char *input, char *tmp, t_token **token_lst, int i)
{
i += define_iter(input, i);
tmp = ft_substr(input, i, ft_strlen(input));
input = tmp;
tokenizer(token_lst, input);
free(tmp);
}
void tokenizer(t_token **token_lst, char *input)
{
int next;
char *tmp;
int ret;
int i;
i = -1;
tmp = NULL;
while (++i < (int)ft_strlen(input))
{
next = peek_next_one(input, i);
ret = split_type(next, input, i, token_lst);
if (ret > 0)
{
recursion_token(input, tmp, token_lst, i);
break ;
}
else if (ret < 0)
{
clear_token_error(token_lst);
break ;
}
}
free_old_input(tmp);
}