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
|
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* collect_redirs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: chuhlig <chuhlig@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/08/02 13:49:31 by dkaiser #+# #+# */
/* Updated: 2025/01/22 16:08:25 by chuhlig ### ########.fr */
/* */
/* ************************************************************************** */
#include "minishell.h"
#include <stdlib.h>
static void collect_and_check_redir(t_redirection *result,
t_token **cur, t_minidata *data, t_token **tokens);
static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
t_env *env);
static int set_heredoc_data(t_token *cur, t_redirection *result,
t_env *env);
t_redirection *collect_redirs(t_token **tokens, t_env *env,
t_list **create_files)
{
t_redirection *result;
t_token *cur;
t_minidata data;
cur = *tokens;
result = malloc(sizeof(t_redirection) * 2);
if (result == NULL)
return (free_tokens(*tokens), NULL);
free(set_redir(&result[0], 0, NULL, env));
free(set_redir(&result[1], 0, NULL, env));
data.create_files = create_files;
data.env = env;
while (cur != NULL)
{
if (cur->type == REDIR_TOKEN && cur->next->type == STRING_TOKEN)
collect_and_check_redir(result, &cur, &data, tokens);
else if (cur->type == REDIR_TOKEN)
return (free(result), NULL);
else
cur = cur->next;
}
return (result);
}
static void collect_and_check_redir(t_redirection *result, t_token **cur,
t_minidata *data, t_token **tokens)
{
char *str;
if ((*cur)->content.redir_type != INPUT_LIMITER)
str = ft_strdup((*cur)->next->content.string);
if ((*cur)->content.redir_type == INPUT_LIMITER)
{
if (!set_heredoc_data(*cur, result, data->env))
return ;
}
else if ((*cur)->content.redir_type == INPUT_FILE)
q4fc(data->create_files, set_redir(&result[0], INPUT_FILE,
format_string(str, data->env, 0), data->env));
else if ((*cur)->content.redir_type == OUTPUT_OVERRIDE)
q4fc(data->create_files, set_redir(&result[1], OUTPUT_OVERRIDE,
format_string(str, data->env, 0), data->env));
else if ((*cur)->content.redir_type == OUTPUT_APPEND)
q4fc(data->create_files, set_redir(&result[1], OUTPUT_APPEND,
format_string(str, data->env, 0), data->env));
i_love_the_norme(cur, tokens);
}
static t_redirection *set_redir(t_redirection *redir, int type, char *spec,
t_env *env)
{
t_redirection *result;
redir->type = type;
// if (redir->specifier != NULL)
// free(redir->specifier);
if (spec != NULL)
redir->specifier = format_string(spec, env, ft_atoi("0"));
else
redir->specifier = spec;
if (redir->type != INPUT_LIMITER)
{
result = malloc(sizeof(t_redirection));
if (!result)
return (NULL);
result->type = type;
result->specifier = spec;
return (result);
}
return (NULL);
}
static int set_heredoc_data(t_token *cur, t_redirection *result, t_env *env)
{
char *heredoc_data;
heredoc_data = NULL;
if (cur->content.redir_type == INPUT_LIMITER)
{
heredoc_data = read_heredoc(cur->next->content.string);
if (!heredoc_data)
{
perror("Heredoc allocation failed");
return (0);
}
set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
}
set_redir(&result[0], INPUT_LIMITER, heredoc_data, env);
return (1);
}
|