-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse_lst_to_arr.c
More file actions
104 lines (94 loc) · 2.73 KB
/
parse_lst_to_arr.c
File metadata and controls
104 lines (94 loc) · 2.73 KB
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parse_lst_to_arr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jwoo <jwoo@student.42seoul.kr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/08/10 10:15:26 by jwoo #+# #+# */
/* Updated: 2021/08/10 19:28:13 by jwoo ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int is_separate(char *command)
{
if (ft_strncmp(command, "|", 2) == 0)
return (PIPE);
if (ft_strncmp(command, "<", 2) == 0)
return (REDIRECT1);
if (ft_strncmp(command, ">", 2) == 0)
return (REDIRECT2);
if (ft_strncmp(command, "<<", 3) == 0)
return (REDIRECT3);
if (ft_strncmp(command, ">>", 3) == 0)
return (REDIRECT4);
return (NONE);
}
int get_unit_size(t_list *arg_list)
{
int size;
t_list *first;
size = 0;
first = arg_list;
while (arg_list)
{
if (is_separate((char *)arg_list->content) != NONE)
break ;
size++;
arg_list = arg_list->next;
}
arg_list = first;
return (size);
}
void add_one_unit_cmd(t_list **arg_list, t_list **cmd_list, char **arg_arr)
{
t_list *unit;
unit = ft_lstnew(arg_arr);
if ((*arg_list) != NULL)
{
unit->next_flag = is_separate((char *)(*arg_list)->content);
(*arg_list) = (*arg_list)->next;
}
else
{
unit->next_flag = NONE;
}
if (unit != 0 && ft_lstlast(*cmd_list) != 0)
unit->pre_flag = ft_lstlast(*cmd_list)->next_flag;
else if (unit != 0)
unit->pre_flag = NONE;
ft_lstadd_back(cmd_list, unit);
}
void add_one_line_of_cmd(t_list **arg_list, t_envp *envp, t_list **cmd_list)
{
int sperate_num;
char **arg_arr;
int idx;
sperate_num = get_unit_size(*arg_list);
idx = -1;
arg_arr = (char **)malloc(sizeof(char *) * (sperate_num + 1));
if (!arg_arr)
exit(1);
while (++idx < sperate_num)
{
arg_arr[idx] = remove_quote((char *)(*arg_list)->content, envp);
(*arg_list) = (*arg_list)->next;
}
arg_arr[sperate_num] = (char *)0;
add_one_unit_cmd(arg_list, cmd_list, arg_arr);
}
t_list *list_to_char_arr(t_list *arg_list, t_envp *envp)
{
t_list *first;
t_list *cmd_list;
int lst_size;
cmd_list = 0;
lst_size = ft_lstsize(arg_list);
first = arg_list;
while (arg_list)
{
add_one_line_of_cmd(&arg_list, envp, &cmd_list);
}
arg_list = first;
return (cmd_list);
}