-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms_found_redirect.c
133 lines (123 loc) · 3.38 KB
/
ms_found_redirect.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ms_found_redirect.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: dlana <dlana@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/03/11 17:53:03 by dlana #+# #+# */
/* Updated: 2022/03/14 16:17:04 by dlana ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int ms_error_parse_redir(t_data *data, char *s, int i)
{
if (s[i] == '>' || s[i] == '<' || s[i] == '\0')
{
data->num_error = ERR_TOKEN;
if (s[i] == '\0')
return (ms_error(data->num_error, "newline"));
if (s[i] == '>')
{
if (s[i + 1] == '>')
return (ms_error(data->num_error, ">>"));
else
return (ms_error(data->num_error, ">"));
}
if (s[i] == '<')
{
if (s[i + 1] == '<')
return (ms_error(data->num_error, "<<"));
else if (s[i + 1] == '>')
return (ms_error(data->num_error, "<>"));
else
return (ms_error(data->num_error, "<"));
}
}
return (0);
}
int ms_count_redirect(t_cmd *cmd, t_data *data, int qm_o, int qm_d)
{
int i;
i = 0;
cmd->count_redir = 0;
while (cmd->str[i] != '\0')
{
ms_switch_qm(cmd->str[i], &qm_o, &qm_d);
if ((cmd->str[i] == '>' || cmd->str[i] == '<')
&& qm_d == 1 && qm_o == 1)
{
if ((cmd->str[i] == '>' && cmd->str[i + 1] == '>')
|| (cmd->str[i + 1] == '<' && cmd->str[i] == '<')
|| (cmd->str[i] == '<' && cmd->str[i + 1] == '>'))
i++;
i++;
while (cmd->str[i] == ' ' && cmd->str[i] != '\0')
i++;
if (ms_error_parse_redir(data, cmd->str, i) == -1)
return (-1);
cmd->count_redir++;
}
else
i++;
}
return (cmd->count_redir);
}
void ms_check_empty_str(t_cmd *cmd)
{
int i;
i = 0;
if (cmd->str[i] == '\0')
ms_free_str(&cmd->str);
else if (cmd->str[i] == ' ')
{
while (cmd->str[i] == ' ' && cmd->str[i] != '\0')
i++;
if (cmd->str[i] == '\0')
ms_free_str(&cmd->str);
}
}
int ms_cycle_of_record_redir(t_cmd *cmd, t_data *data, int qm_o, int qm_d)
{
int i;
int num_redir;
i = 0;
num_redir = 0;
while (cmd->str[i] != '\0')
{
ms_switch_qm(cmd->str[i], &qm_o, &qm_d);
if ((cmd->str[i] == '>' || cmd->str[i] == '<')
&& qm_d == 1 && qm_o == 1)
{
if (ms_record_redir_and_file(cmd, i, num_redir, data) == -1)
return (-1);
ms_replase_key_to_value(&cmd->str, data->tmp.size_cut, NULL, i);
if (cmd->str[i] == '\0' && i > 0)
i--;
num_redir++;
if (cmd->str[0] == '\0')
break ;
}
i++;
}
ms_check_empty_str(cmd);
return (0);
}
int ms_found_redirect(t_cmd *cmd, t_data *data)
{
int qm_d;
int qm_o;
qm_o = 1;
qm_d = 1;
if (ms_count_redirect(cmd, data, qm_o, qm_d) <= 0)
{
cmd->count_redir = 0;
return (-1);
}
ms_malloc_arr_int(&cmd->redir, cmd->count_redir);
ms_malloc_array(&cmd->file, cmd->count_redir);
ms_cycle_of_record_redir(cmd, data, qm_o, qm_d);
cmd->file[cmd->count_redir] = NULL;
cmd->redir[cmd->count_redir] = 0;
return (0);
}