-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathft_line_parse.c
102 lines (93 loc) · 2.72 KB
/
ft_line_parse.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_line_parse.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: abenamar <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/09/05 17:58:39 by abenamar #+# #+# */
/* Updated: 2023/09/13 14:20:08 by abenamar ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
static size_t ft_add_redirection(char *str, t_list **tkns)
{
size_t i;
size_t j;
i = 0;
while (str[i] == str[0])
++i;
ft_lstadd_back(tkns, ft_lstnew(ft_substr(str, 0, i)));
while (str[i] == ' ')
++i;
j = ft_is_quoted(str + i);
if (!j)
j = 1;
while (str[i + j] && str[i + j] != ' '
&& str[i + j] != '<' && str[i + j] != '>')
++j;
return (ft_lstadd_back(tkns, ft_lstnew(ft_str_replace(\
ft_str_replace(ft_substr(str, i, j), -3, '<'), -4, '>'))), i + j);
}
static char *ft_parse_redirection(char *cmd, size_t i, t_list **tkns)
{
size_t j;
char *str;
char *tmp;
j = ft_add_redirection(cmd + i, tkns);
str = ft_substr(cmd, 0, i);
tmp = ft_substr(cmd, i + j, ft_strlen(cmd + i + j));
free(cmd);
cmd = ft_strjoin(str, tmp);
free(str);
free(tmp);
return (cmd);
}
static char *ft_search_redirections(char *cmd, t_list **tkns)
{
char *tmp;
size_t i;
if (!cmd)
return (NULL);
tmp = ft_command_setup(cmd, '<', -3, 1);
(free(cmd), cmd = ft_command_setup(tmp, '>', -4, 1), free(tmp));
if (!cmd)
return (NULL);
i = 0;
while (cmd[i] && cmd[i] != '<' && cmd[i] != '>')
++i;
while (cmd[i])
{
cmd = ft_parse_redirection(cmd, i, tkns);
if (!cmd)
return (NULL);
i = 0;
while (cmd[i] && cmd[i] != '<' && cmd[i] != '>')
++i;
}
tmp = ft_strtrim(cmd, " ");
return (free(cmd), ft_str_replace(ft_str_replace(tmp, -3, '<'), -4, '>'));
}
t_list **ft_line_parse(char *line, t_list **tkns)
{
char **strs;
size_t i;
strs = ft_command_split(line, NULL, '|', 0);
if (!strs)
return (NULL);
i = 0;
while (strs[i])
{
if (i > 0)
ft_lstadd_back(tkns, ft_lstnew(ft_strdup("|")));
strs[i] = ft_search_redirections(strs[i], tkns);
if (!(strs[i]))
return (ft_tab_free(strs + i + 1), NULL);
if (*(strs[i]))
ft_lstadd_back(tkns, ft_lstnew(strs[i]));
else
free(strs[i]);
++i;
}
return (free(strs), tkns);
}