-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirection_pa.c
97 lines (87 loc) · 2.45 KB
/
redirection_pa.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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* redirection_pa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: asfaihi <asfaihi@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2021/06/23 14:39:41 by asfaihi #+# #+# */
/* Updated: 2021/09/28 12:19:41 by asfaihi ### ########.fr */
/* */
/* ************************************************************************** */
#include "parsing.h"
char *double_quotes(char *s, t_list *env_lst, t_prs *prs)
{
int j;
char *file;
char *temp;
prs->i++;
j = prs->i;
while (s[prs->i] && s[prs->i] != '"')
prs->i++;
temp = ft_substr(s, j, prs->i - j);
file = env_var_checker(temp, env_lst, prs);
free(temp);
prs->i++;
return (file);
}
char *single_quotes(char *s, t_prs *prs)
{
int j;
char *file;
prs->i++;
j = prs->i;
while (s[prs->i] && s[prs->i] != '\'')
prs->i++;
file = ft_substr(s, j, prs->i - j);
prs->i++;
return (file);
}
static char get_redir_type(char first, char second)
{
if (first == '>' && second != '>')
return ('O');
else if (first == '<' && second != '<')
return ('I');
else if (first == '>' && second == '>')
return ('A');
else if (first == '<' && second == '<')
return ('H');
return (0);
}
static t_redir *new_redirection_node(char *s, t_list *env_lst, t_prs *prs)
{
t_redir *new;
new = (t_redir *)malloc(sizeof(t_redir));
if (!new)
return (NULL);
initialize_redir_node(new);
while (s[prs->i] == ' ')
prs->i++;
new->type = get_redir_type(s[prs->i], s[prs->i + 1]);
if (new->type == 'O' || new->type == 'I')
{
prs->i++;
get_filepath(new, s, env_lst, prs);
}
else
{
prs->i += 2;
get_filepath(new, s, env_lst, prs);
}
return (new);
}
t_list *redirections(t_list *redir, char *s, t_list *env_lst, t_prs *prs)
{
t_list *new;
t_redir *temp;
while (s[prs->i])
{
if (s[prs->i] && s[prs->i] != '>' && s[prs->i] != '<')
break ;
temp = new_redirection_node(s, env_lst, prs);
new = ft_lstnew(temp);
ft_lstadd_back(&redir, new);
}
return (redir);
}