-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser2.c
More file actions
121 lines (110 loc) · 2.89 KB
/
Copy pathparser2.c
File metadata and controls
121 lines (110 loc) · 2.89 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* parser2.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fracerba <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/07/11 12:49:14 by fracerba #+# #+# */
/* Updated: 2023/07/11 12:49:15 by fracerba ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
int get_infile(char *file, char append)
{
char *a;
int i;
if (check_file(file))
return (-2);
a = ft_strdup(file);
a = trim_file(a);
if (g_status)
return (free_file(-2, a));
if (!ft_strlen(a))
return (free_file(-3, a));
if (append)
i = get_here_doc(a);
else
i = open(a, O_RDONLY, 0666);
return (free_file(i, a));
}
int get_outfile(char *file, char append)
{
char *a;
int i;
if (check_file(file))
return (-2);
a = ft_strdup(file);
a = trim_file(a);
if (g_status)
return (free_file(-2, a));
if (!ft_strlen(a))
return (free_file(-3, a));
if (append)
i = open(a, O_WRONLY | O_CREAT | O_APPEND, 0666);
else
i = open(a, O_WRONLY | O_CREAT | O_TRUNC, 0666);
return (free_file(i, a));
}
char **get_full_cmd(char **cmd_mat)
{
int i;
char **cmd;
if (!cmd_mat)
return (NULL);
i = 0;
while (cmd_mat[i] && cmd_mat[i][0] != '|')
i++;
cmd = malloc(sizeof(char *) * (i + 1));
i = 0;
while (cmd_mat[i] && cmd_mat[i][0] != '|')
{
cmd[i] = ft_strdup(cmd_mat[i]);
i++;
}
cmd[i] = NULL;
return (cmd);
}
t_cmd *fill_cmds(t_prompt *prompt, t_cmd *cmd, char **cmd_mat)
{
int i;
i = 0;
while (cmd_mat[i] && cmd_mat[i][0] != '|')
{
if (cmd_mat[i][0] == '<')
cmd->infile = get_infile(cmd_mat[i + 1], cmd_mat[i][1]);
else if (cmd_mat[i][0] == '>')
cmd->outfile = get_outfile(cmd_mat[i + 1], cmd_mat[i][1]);
if (cmd->infile <= -1 || cmd->outfile <= -1)
{
if (cmd->infile == -3 || cmd->outfile == -3)
print_error(7, NULL, "", 1);
return (cmd);
}
i++;
}
cmd->command = get_full_cmd(cmd_mat);
cmd->command = remove_redirects(cmd->command);
cmd->command = ft_trim_cmd(cmd->command);
if (g_status)
return (cmd);
cmd->path = get_cmd_path(prompt, cmd->command[0], cmd->command);
return (cmd);
}
char **reduce_cmd(char **cmd)
{
int i;
char **epic_cmd;
i = 0;
while (cmd[i] && cmd[i][0] != '|')
i++;
if (cmd[i] && cmd[i][0] == '|')
{
i++;
epic_cmd = dup_matrix(&cmd[i]);
free_matrix(cmd);
return (epic_cmd);
}
free_matrix(cmd);
return (NULL);
}